#include "bits/stdc++.h" using namespace std; #define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i)) #define rep(i,j) FOR(i,0,j) #define each(x,y) for(auto &(x):(y)) #define mp make_pair #define all(x) (x).begin(),(x).end() #define debug(x) cout<<#x<<": "<<(x)< pii; typedef vector vi; typedef vector vll; typedef vector> Intervals; Intervals mergeIntervals(Intervals a){ Intervals res; sort(all(a)); each(b, a){ if(res.size() == 0 || res.back().second < b.first - 1) res.push_back(b); else res.back().second = max(res.back().second, b.second); } return res; } const int DY[] = {-1,0,1,0}; const int DX[] = {0,1,0,-1}; const int MAX = 1000; int W, H, N, ro[MAX][MAX][4], vis[MAX][MAX]; Intervals ir[MAX], ic[MAX]; void init(){ cin >> W >> H >> N; rep(i, N){ int M; cin >> M; vi B(M + 1); rep(j, M + 1)scanf("%d", &B[j]); rep(j, M){ int ay = B[j] / W; int ax = B[j] % W; int by = B[j+1] / W; int bx = B[j+1] % W; if(ay > by)swap(ay, by); if(ax > bx)swap(ax, bx); if(ay == by) ir[by].emplace_back(ax, bx); if(ax == bx) ic[bx].emplace_back(ay, by); assert(ay == by || ax == bx); } } rep(y, H){ ir[y] = mergeIntervals(ir[y]); each(in, ir[y]){ int a, b; tie(a, b) = in; FOR(ax, a, b){ int bx = ax + 1; ro[y][ax][1] = 1; ro[y][bx][3] = 1; } } } rep(x, W){ ic[x] = mergeIntervals(ic[x]); each(in, ic[x]){ int a, b; tie(a, b) = in; FOR(ay, a, b){ int by = ay + 1; ro[ay][x][2] = 1; ro[by][x][0] = 1; } } } } void solve(){ string NG = "Odekakedekinai.."; queue> Q; Q.push(make_tuple(0, 0, 0)); while(sz(Q)){ int y, x, d; tie(y, x, d) = Q.front(); Q.pop(); if(vis[y][x]++)continue; if(y == H - 1 && x == W - 1){ cout << d << endl; return; } rep(i, 4)if(ro[y][x][i]){ int ny = y + DY[i]; int nx = x + DX[i]; if(vis[ny][nx])continue; Q.push(make_tuple(ny, nx, d + 1)); } } cout << NG << endl; } int main(){ init(); solve(); }