#!/usr/bin/ python3.8 import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines W, H, Q = map(int, readline().split()) query = readlines() if H == W == 1: print(0) exit() N = H * W horizonal_max = list(range(N)) vertical_max = list(range(N)) for line in query[1::2]: B = map(int, line.split()) x = next(B) for y in B: a, b = x, y if a > b: a, b = b, a if b - a < W: if horizonal_max[a] < b: horizonal_max[a] = b else: if vertical_max[a] < b: vertical_max[a] = b x = y graph = [[] for _ in range(N)] for i in range(N): h = horizonal_max[i] if h > i: if horizonal_max[i + 1] < h: horizonal_max[i + 1] = h graph[i].append(i + 1) graph[i + 1].append(i) v = vertical_max[i] if v > i: if vertical_max[i + W] < v: vertical_max[i + W] = v graph[i].append(i + W) graph[i + W].append(i) INF = N + 10 dist = [INF] * N visited = [False] * N q = [0] goal = N - 1 d = 0 while q: qq = [] d += 1 for v in q: for w in graph[v]: if visited[w]: continue visited[w] = True qq.append(w) if visited[goal]: print(d) exit() q = qq print('Odekakedekinai..')