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