#include using namespace std; int W, H, N; int isgo[1000][1000][2]; int bfs() { queue< pair< int, int > > que; int min_cost[1000][1000]; const static int vx[] = {0, 1, 0, -1}, vy[] = {1, 0, -1, 0}; memset(min_cost, -1, sizeof(min_cost)); que.push({0, 0}); min_cost[0][0] = 0; while(!que.empty()) { int x, y; tie(x, y) = que.front(); que.pop(); if(x == W - 1 && y == H - 1) return(min_cost[x][y]); for(int i = 0; i < 4; i++) { int nx = x + vx[i], ny = y + vy[i]; if(nx < 0 || ny < 0 || nx >= W || ny >= H) continue; if(!isgo[min(x, nx)][min(y, ny)][ny == y]) continue; if(min_cost[nx][ny] == -1) { min_cost[nx][ny] = min_cost[x][y] + 1; que.push({nx, ny}); } } } return(-1); } int main() { cin >> W >> H >> N; for(int i = 0; i < N; i++) { int M, B[2]; cin >> M; cin >> B[1]; for(int j = 0; j < M; j++) { cin >> B[j & 1]; const int U = min(B[0], B[1]), V = max(B[0], B[1]); const int X1 = U % W, Y1 = U / W; const int X2 = V % W, Y2 = V / W; ++isgo[X1][Y1][Y1 == Y2]; --isgo[X2][Y2][Y1 == Y2]; } } for(int i = 0; i < H; i++) { for(int j = 0; j < W; j++) { if(i > 0) isgo[j][i][0] += isgo[j][i - 1][0]; if(j > 0) isgo[j][i][1] += isgo[j - 1][i][1]; } } int ret = bfs(); if(ret == -1) cout << "Odekakedekinai.." << endl; else cout << ret << endl; }