import sys input = sys.stdin.readline from collections import deque W,H,N=map(int,input().split()) UP=[[0]*W for i in range(H)] RIGHT=[[0]*W for i in range(H)] for i in range(N): M=int(input()) L=list(map(int,input().split())) for j in range(len(L)-1): x=L[j] a,b=x//W,x%W y=L[j+1] c,d=y//W,y%W if a==c: if b>d: b,d=d,b RIGHT[a][b]+=1 RIGHT[a][d]-=1 else: if a>c: a,c=c,a UP[a][b]+=1 UP[c][b]-=1 for i in range(H): for j in range(W): if i>0: UP[i][j]+=UP[i-1][j] if j>0: RIGHT[i][j]+=RIGHT[i][j-1] D=[[1<<60]*W for i in range(H)] D[0][0]=0 Q=[(0,0)] Q=deque(Q) while Q: x,y=Q.popleft() if RIGHT[x][y]>0: if D[x][y+1]>D[x][y]+1: D[x][y+1]=D[x][y]+1 Q.append((x,y+1)) if y-1>=0 and RIGHT[x][y-1]>0: if D[x][y-1]>D[x][y]+1: D[x][y-1]=D[x][y]+1 Q.append((x,y-1)) if UP[x][y]>0: if D[x+1][y]>D[x][y]+1: D[x+1][y]=D[x][y]+1 Q.append((x+1,y)) if x-1>=0 and RIGHT[x-1][y]>0: if D[x-1][y]>D[x][y]+1: D[x-1][y]=D[x][y]+1 Q.append((x-1,y)) ANS=D[H-1][W-1] if ANS>10**9: print("Odekakedekinai..") else: print(ANS)