#include using namespace std; #define int long long // <-----!!!!!!!!!!!!!!!!!!! #define rep(i,n) for (int i=0;i<(n);i++) #define rep2(i,a,b) for (int i=(a);i<(b);i++) #define rrep(i,n) for (int i=(n)-1;i>=0;i--) #define rrep2(i,a,b) for (int i=(b)-1;i>=(a);i--) #define all(a) (a).begin(),(a).end() typedef long long ll; typedef pair P; int W, H, N; int e[1001][1001][2]; //(i, j) -> (i + 1, j), (i, j) -> (i, j + 1) inline P toNode(int n) { return P(n / W, n % W); } const int INF = 99999999; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; bool inside(int x, int y) { return 0 <= x && x < H && 0 <= y && y < W; } bool canGo(int x1, int y1, int x2, int y2) { if (!inside(x1, y1)) return false; if (!inside(x2, y2)) return false; if (x1 > x2 || y1 > y2) { swap(x1, x2); swap(y1, y2); } // assert(x1 <= x2 && y1 <= y2); return e[x1][y1][x1 == x2]; } int bfs(int sx, int sy, int gx, int gy) { queue

que; que.push(P(sx, sy)); vector> d(H, vector(W, INF)); d[sx][sy] = 0; while (!que.empty()) { int x, y; tie(x, y) = que.front(); que.pop(); if (x == gx && y == gy) break; rep(k, 4) { int nx = x + dx[k], ny = y + dy[k]; if (!canGo(x, y, nx, ny)) continue; if (d[nx][ny] == INF) { que.push(P(nx, ny)); d[nx][ny] = d[x][y] + 1; } } } return d[gx][gy]; } signed main() { std::ios::sync_with_stdio(false); std::cin.tie(0); cin >> W >> H >> N; rep(i, N) { int M; cin >> M; vector B(M + 1); rep(j, M + 1) { cin >> B[j]; } rep(j, M) { int x1, y1, x2, y2; tie(x1, y1) = toNode(B[j]); tie(x2, y2) = toNode(B[j + 1]); if (B[j] > B[j + 1]) { swap(x1, x2); swap(y1, y2); } if (y1 == y2) { e[x1][y1][0]++; e[x2][y2][0]--; } else { e[x1][y1][1]++; e[x2][y2][1]--; } } } rep(j, W) { rep(i, H) { e[i + 1][j][0] += e[i][j][0]; } } rep(i, H) { rep(j, W) { e[i][j + 1][1] += e[i][j][1]; } } int ans = bfs(0, 0, H - 1, W - 1); if (ans == INF) { cout << "Odekakedekinai.." << endl; } else { cout << ans << endl; } }