import sys read=sys.stdin.buffer.read readline=sys.stdin.buffer.readline readlines=sys.stdin.buffer.readlines w, h, n=map(int, readline().split()) s1=[[0]*w for i in range(h)] s2=[[0]*h for i in range(w)] for i in range(n): m=int(readline()) b=list(map(int, readline().split())) for j in range(m): x1, y1=divmod(b[j], w) x2, y2=divmod(b[j+1], w) if x1==x2: if y1>y2: y1, y2=y2, y1 s1[x1][y1]+=1 s1[x1][y2]-=1 else: if x1>x2: x1, x2=x2, x1 s2[y1][x1]+=1 s2[y1][x2]-=1 import itertools for i in range(h): s1[i]=list(itertools.accumulate(s1[i])) for i in range(w): s2[i]=list(itertools.accumulate(s2[i])) INF=10**9 d=[[INF]*w for i in range(h)] d[0][0]=0 from collections import deque que=deque() que.append(0) while que: p=que.popleft() x, y=divmod(p, w) d0=d[x][y] if x-1>=0 and s2[y][x-1]>0 and d[x-1][y]>d0+1: d[x-1][y]=d0+1 que.append((x-1)*w+y) if x+10 and d[x+1][y]>d0+1: d[x+1][y]=d0+1 que.append((x+1)*w+y) if y-1>=0 and s1[x][y-1]>0 and d[x][y-1]>d0+1: d[x][y-1]=d0+1 que.append(x*w+y-1) if y+10 and d[x][y+1]>d0+1: d[x][y+1]=d0+1 que.append(x*w+y+1) if d[h-1][w-1]==INF: print("Odekakedekinai..") else: print(d[h-1][w-1])