/** * @FileName a.cpp * @Author kanpurin * @Created 2020.06.01 01:42:06 **/ #include "bits/stdc++.h" using namespace std; typedef long long ll; int w, h; vector< vector< int > > g1, g2; void f(int x, int y) { if (x > y) swap(x, y); if (x % w == y % w) { g1[x / w][x % w]++; g1[y / w + 1][y % w]--; } else { g2[x / w][x % w]++; g2[y / w][y % w + 1]--; } } int main() { int n; cin >> w >> h >> n; g1.resize(h + 1, vector< int >(w + 1,0));//縦 g2.resize(h + 1, vector< int >(w + 1,0));//横 for (int i = 0; i < n; i++) { int m; cin >> m; int pre = -1; for (int i = 0; i < m + 1; i++) { int b; cin >> b; if (pre != -1) { f(pre,b); } pre = b; } } for (int i = 0; i < w; i++) { for (int j = 0; j < h; j++) { g1[j+1][i] += g1[j][i]; } } for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { g2[i][j+1] += g2[i][j]; } } vector> dist(h,vector(w,-1)); dist[0][0] = 0; queue> que; que.push({0,0}); while(!que.empty()) { auto p = que.front(); que.pop(); int nx = p.first; int ny = p.second; { int x = nx + 1; int y = ny; if (x >= 0 && x < h && y >= 0 && y < w && g1[nx][ny] > 0 && g1[x][y] > 0 && dist[x][y] == -1) { dist[x][y] = dist[nx][ny] + 1; que.push({x,y}); } } { int x = nx - 1; int y = ny; if (x >= 0 && x < h && y >= 0 && y < w && g1[nx][ny] > 0 && g1[x][y] > 0 && dist[x][y] == -1) { dist[x][y] = dist[nx][ny] + 1; que.push({x,y}); } } { int x = nx; int y = ny + 1; if (x >= 0 && x < h && y >= 0 && y < w && g2[nx][ny] > 0 && g2[x][y] > 0 && dist[x][y] == -1) { dist[x][y] = dist[nx][ny] + 1; que.push({x,y}); } } { int x = nx; int y = ny - 1; if (x >= 0 && x < h && y >= 0 && y < w && g2[nx][ny] > 0 && g2[x][y] > 0 && dist[x][y] == -1) { dist[x][y] = dist[nx][ny] + 1; que.push({x,y}); } } } if (dist[h-1][w-1] == -1) { puts("Odekakedekinai.."); } else { cout << dist[h-1][w-1] << endl; } return 0; }