#include using namespace std; #undef _P #define _P(...) (void)printf(__VA_ARGS__) #define FOR(i,a,b) for (int i = (a); i < (b); i++) #define RFOR(i,a,b) for (int i = (b)-1; i >= (a); i--) #define REP(i,n) for (int i = 0; i < (n); i++) #define RREP(i,n) for (int i = (n)-1; i >= 0; i--) #define ALL(x) (x).begin(), (x).end() #define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++) #define RITR(x,c) for(__typeof(c.rbegin()) x=c.rbegin();x!=c.rend();x++) #define MIN(a,b) (a < b ? a : b) #define MAX(a,b) (a > b ? a : b) #define BIT(n) (1LL<<(n)) #define SZ(x) ((int)(x).size()) #define ZERO(a) memset(a,0,sizeof(a)) #define MINUS(a) memset(a,0xff,sizeof(a)) #define FILL(a,v) memset(a,v,sizeof(a)) #define bitcount(b) __builtin_popcount(b) #define GCD(a,b) (__gcd(a,b)) #define GCD3(a,b,c) (GCD(GCD(a,b), c)) #define LCM(a,b) (a/GCD(a,b)*b) #define LCM3(a,b,c) LCM(LCM(a,b),c) #define MOD 1000000007 template inline bool amax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template inline bool amin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } typedef long long ll; // ------------------------------------- typedef pair II; int W, H, N; int C[1001010]; vector E[1001010]; int main() { cin >> W >> H >> N; REP(i, N) { int M; cin >> M; int B[1010]; REP(j, M+1) { cin >> B[j]; } REP(j, M) { int f = B[j]; int t = B[j+1]; int x = MIN(f, t); int y = MAX(f, t); int d = (x / W == y / W) ? 1 : W; while (x != y) { E[x].push_back(x+d); E[x+d].push_back(x); x+=d; } } } priority_queue, greater > q; FILL(C, 0x3f); q.push({0, 0}); while (!q.empty()) { int c = q.top().first; int p = q.top().second; if (p == W * H - 1) { cout << c << '\n'; return 0; } q.pop(); if (C[p] <= c) continue; C[p] = c; ITR(it, E[p]) { if (c < C[*it]) q.push({c + 1, *it}); } } cout << "Odekakedekinai.." << '\n'; return 0; }