# include # include # include # include # include # include # include # include # include # include # include # include # include # include # include # include # include # include # include # include using namespace std; using LL = long long; using ULL = unsigned long long; constexpr int INF = 2147483647; constexpr int HINF = INF / 2; constexpr double DINF = 100000000000000000.0; constexpr double HDINF = 50000000000000000.0; constexpr long long LINF = 9223372036854775807; constexpr long long HLINF = 4500000000000000000; const double PI = acos(-1); int dx[4] = { 0,1,0,-1 }, dy[4] = { 1,0,-1,0 }; template T_char TL(T_char cX) { return tolower(cX); }; template T_char TU(T_char cX) { return toupper(cX); }; # define ALL(x) (x).begin(),(x).end() # define UNIQ(c) (c).erase(unique(ALL((c))),(c).end()) # define LOWER(s) transform(ALL((s)),(s).begin(),TL) # define UPPER(s) transform(ALL((s)),(s).begin(),TU) # define mp make_pair # define eb emplace_back # define FOR(i,a,b) for(LL i=(a);i<(b);i++) # define RFOR(i,a,b) for(LL i=(a);i>=(b);i--) # define REP(i,n) FOR(i,0,n) # define INIT std::ios::sync_with_stdio(false);std::cin.tie(0) int w, h, n; int m, b[1010]; int cnt[1010][1010], cnt2[1010][1010]; bool isok1[1010][1010], isok2[1010][1010]; int sx, sy, gx, gy; int d[1001][1001]; int bfs() { queue> que; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { d[i][j] = HINF; } } que.push(pair(sx, sy)); d[sy][sx] = 0; while (que.size()) { pair p = que.front(); que.pop(); for (int i = 0; i < 4; i++) { int nx = p.first + dx[i], ny = p.second + dy[i]; if (nx != p.first) { if (!isok1[ny][max(p.first, nx)])continue; } if (ny != p.second) { if (!isok2[max(p.second, ny)][nx])continue; } if (0 <= nx&&nx < w && 0 <= ny&&ny < h &&d[ny][nx] == HINF) { que.push(pair(nx, ny)); d[ny][nx] = d[p.second][p.first] + 1; } } } return d[gy][gx]; } int main() { cin >> w >> h >> n; REP(po, n) { cin >> m; REP(k, m + 1) { cin >> b[k]; if (k) { int ki = b[k] / w; int kj = b[k] % w; int prei = b[k - 1] / w; int prej = b[k - 1] % w; if (prei == ki) { int mij = min(kj, prej); int maj = max(kj, prej); cnt[ki][mij]++; cnt[kj][maj + 1]--; } else { int mii = min(ki, prei); int mai = max(ki, prei); cnt2[mii][kj]++; cnt2[mai + 1][kj]--; } } } } REP(i, h)REP(j, w) { if (j) { cnt[i][j] += cnt[i][j - 1]; if (cnt[i][j] > 0 && cnt[i][j - 1] > 0)isok1[i][j] = true; } } REP(i, h)REP(j, w) { if (i) { cnt2[i][j] += cnt2[i - 1][j]; if (cnt2[i][j] > 0 && cnt2[i - 1][j] > 0)isok2[i][j] = true; } } gy = h - 1, gx = w - 1; LL ret = bfs(); if (ret == HINF) { cout << "Odekakedekinai.." << endl; } else { cout << ret << endl; } //system("pause"); }