#include #include #include #include #include #define repeat(i,n) for (int i = 0; (i) < (n); ++(i)) #define repeat_from(i,m,n) for (int i = (m); (i) < (n); ++(i)) using namespace std; struct state_t { int dist, v; }; int main() { int w, h, n; cin >> w >> h >> n; vector > g(h * w); repeat (i,n) { int m; cin >> m; vector b(m+1); repeat (i,m+1) cin >> b[i]; repeat (i,m) { int y0 = b[i] / w; int y1 = b[i+1] / w; int x0 = b[i] % w; int x1 = b[i+1] % w; if (y0 == y1) { int y = y0; repeat_from (z,min(x0,x1),max(x0,x1)) { int u = y * w + z; int v = y * w + (z+1); g[u].insert(v); g[v].insert(u); } } else if (x0 == x1) { int x = x0; repeat_from (z,min(y0,y1),max(y0,y1)) { int u = z * w + x; int v = (z+1) * w + x; g[u].insert(v); g[v].insert(u); } } else { assert (false); } } } vector dist(h * w); queue que; dist[0] = 1; que.push((state_t) { 1, 0 }); while (not que.empty()) { state_t s = que.front(); que.pop(); for (int u : g[s.v]) { if (dist[u]) continue; state_t t; dist[u] = t.dist = s.dist + 1; t.v = u; que.push(t); } } if (dist[h * w - 1]) { cout << dist[h * w - 1] - 1 << endl; } else { cout << "Odekakedekinai.." << endl; } return 0; }