from collections import deque import sys input = sys.stdin.readline h, w, n = map(int, input().split()) hori = [[0] * (w + 1) for _ in range(h)] vert = [[0] * w for _ in range(h + 1)] for _ in range(n): m = int(input()) B = tuple(map(int, input().split())) for s, t in zip(B, B[1:]): hs, ws = divmod(s, w) ht, wt = divmod(t, w) if hs == ht: hori[hs][min(ws, wt)] += 1 hori[hs][max(ws, wt)+1] -= 1 elif ws == wt: vert[min(hs, ht)][ws] += 1 vert[max(hs, ht)+1][ws] -= 1 for i in range(h): for j in range(w): hori[i][j + 1] += hori[i][j] for j in range(w): for i in range(h): vert[i + 1][j] += vert[i][j] def toID(x, y): return x * w + y def push(x, y, d): ID = toID(x, y) if dist[ID] == -1: dist[ID] = d que.append(ID) dist = [-1] * (h * w) dist[0] = 0 que = deque([0]) while que: s = que.popleft() d = dist[s] + 1 x, y = divmod(s, w) if x and vert[x][y] and vert[x - 1][y]: push(x - 1, y, d) if x + 1 < h and vert[x][y] and vert[x + 1][y]: push(x + 1, y, d) if y and hori[x][y] and hori[x][y - 1]: push(x, y - 1, d) if y + 1 < w and hori[x][y] and hori[x][y + 1]: push(x, y + 1, d) goal = h * w - 1 if dist[goal] == -1: print("Odekakedekinai..") else: print(dist[goal])