#include #include #include #include #include #include #include #include #include #include #include #include #include #define repd(i,a,b) for (int i=(a);i<(b);i++) #define rep(i,n) repd(i,0,n) #define var auto #define mod 1000000007 #define inf 2147483647 #define mp make_pair #define pb push_back typedef long long ll; using namespace std; template inline void output(T a, int p) { if(p){ cout << fixed << setprecision(p) << a << "\n"; } else{ cout << a << "\n"; } } // end of template int main() { cin.tie(0); ios::sync_with_stdio(0); // source code int W, H, N; cin >> W >> H >> N; vector R(W * H, 0), U(W * H, 0), D(W * H, mod); // Right, Up の最大距離, dist vector> L(W * H); // 辺リスト rep(i, N){ int M; cin >> M; int pre, now; cin >> pre; rep(i, M){ cin >> now; if (abs(pre - now) < W) R[min(pre, now)] = max(R[min(pre, now)], abs(pre - now)); else U[min(pre, now)] = max(U[min(pre, now)], abs(pre / W - now / W)); pre = now; } } rep(y, H) rep(x, W){ int c = x + y * W; if (x) R[c] = max(R[c], R[c - 1] - 1); if (y) U[c] = max(U[c], U[c - W] - 1); if (R[c]) L[c].pb(c + 1), L[c + 1].pb(c); if (U[c]) L[c].pb(c + W), L[c + W].pb(c); } D[0] = 0; queue q; q.push(0); while (!q.empty()) { int t = q.front(); q.pop(); rep(i, L[t].size()){ if (D[L[t][i]] > D[t] + 1) { q.push(L[t][i]); D[L[t][i]] = D[t] + 1; } } } if (D[W * H - 1] != mod) { output(D[W * H - 1], 0); } else{ output("Odekakedekinai..", 0); } return 0; }