#include #include #include #include #include using namespace std; #define REP(i,s,e) for (i = s; i <= e; i++) #define rep(i,n) REP (i,0,(int)(n)-1) #define RREP(i,s,e) for (i = s; i >= e; i--) #define rrep(i,n) RREP (i,(int)(n)-1,0) #define INF (int)1e8 #define MOD (int)(1e9+7) #define RIGHT 3 #define LEFT 1 #define UP 0 #define DOWN 2 typedef long long ll; int pass[1000][1000][4]; int dist[1000][1000]; int main(void) { int i, j, w, h, n; cin >> w >> h >> n; rep (i,n) { int m, pre; cin >> m >> pre; rep (j,m) { int b; cin >> b; if (pre / w == b / w) { int y = pre / w; int from = pre % w; int to = b % w; if (from < to) { pass[y][from][RIGHT]++; pass[y][to][RIGHT]--; pass[y][to][LEFT]++; pass[y][from][LEFT]--; } else { pass[y][from][LEFT]++; pass[y][to][LEFT]--; pass[y][to][RIGHT]++; pass[y][from][RIGHT]--; } } else { int x = pre % w; int from = pre / w; int to = b / w; if (from < to) { pass[from][x][UP]++; pass[to][x][UP]--; pass[to][x][DOWN]++; pass[from][x][DOWN]--; } else { pass[from][x][DOWN]++; pass[to][x][DOWN]--; pass[to][x][UP]++; pass[from][x][UP]--; } } pre = b; } } rep (i,h) rep (j,w-1) pass[i][j+1][RIGHT] += pass[i][j][RIGHT]; rep (i,h) rrep (j,w-1) pass[i][j][LEFT] += pass[i][j+1][LEFT]; rep (j,w) rep (i,h-1) pass[i+1][j][UP] += pass[i][j][UP]; rep (j,w) rrep (i,h-1) pass[i][j][DOWN] += pass[i+1][j][DOWN]; queue> q; rep (i,h) rep (j,w) dist[i][j] = INF; dist[0][0] = 0; q.push(make_pair(0,0)); while (!q.empty()) { int y = q.front().first; int x = q.front().second; q.pop(); int dxy[4] = {1,0,-1,0}; rep (i,4) if (pass[y][x][i]) { int ny = y + dxy[i]; int nx = x + dxy[(i+1)%4]; if (ny < 0 || ny >= h || nx < 0 || nx >= w) continue; if (dist[ny][nx] > dist[y][x] + 1) { dist[ny][nx] = dist[y][x] + 1; q.push(make_pair(ny,nx)); } } } if (dist[h-1][w-1] == INF) cout << "Odekakedekinai.." << endl; else cout << dist[h-1][w-1] << endl; return 0; }