#include using namespace std; #define rep(i,a,b) for(int i=a;i b) swap(a, b); int ya = a / W; int xa = a % W; int yb = b / W; int xb = b % W; if (ya == yb) { // x軸での移動 hor[ya][xa]++; hor[ya][xb + 1]--; } else { // y軸での移動 vec[xa][ya]++; vec[xa][yb + 1]--; } } } rep(y, 0, H) rep(x, 1, W) hor[y][x] += hor[y][x - 1]; rep(x, 0, W) rep(y, 1, H) vec[x][y] += vec[x][y - 1]; //rep(y, 0, H) rep(x, 0, W) printf("hor[y = %d][x = %d] = %d\n", y, x, hor[y][x]); //rep(x, 0, W) rep(y, 0, H) printf("vec[x = %d][y = %d] = %d\n", x, y, vec[x][y]); } //----------------------------------------------------------------- bool done[1010][1010]; int dist[1010][1010]; int dx[4] = { 0, 1, 0, -1 }; int dy[4] = { -1, 0, 1, 0 }; int bfs() { queue que; done[0][0] = true; dist[0][0] = 0; que.push(0); while (!que.empty()) { int y = que.front() / 1010; int x = que.front() % 1010; que.pop(); if (y == H - 1 && x == W - 1) return dist[y][x]; rep(i, 0, 4) { int xx = x + dx[i]; int yy = y + dy[i]; if (xx < 0 || W <= xx) continue; if (yy < 0 || H <= yy) continue; if (i % 2 == 1) { if (0 == hor[y][x] || 0 == hor[yy][xx]) continue; } else { if (0 == vec[x][y] || 0 == vec[xx][yy]) continue; } if (done[yy][xx]) continue; done[yy][xx] = 1; dist[yy][xx] = dist[y][x] + 1; que.push(yy * 1010 + xx); } } return -1; } //----------------------------------------------------------------- int main() { cin >> W >> H >> N; rep(i, 0, N) { scanf("%d", &M[i]); rep(j, 0, M[i] + 1) scanf("%d", &B[i][j]); } makeEdge(); int ans = bfs(); if (0 <= ans) cout << ans << endl; else cout << "Odekakedekinai.." << endl; }