#include using namespace std; #define LM 1000000 int l[LM][4], dist[LM], th[LM][2]; int main(){ int w, h, n; scanf("%d%d%d", &w, &h, &n); for (int i = 1; i < LM; ++i) dist[i] = LM; for (int i = 0; i < n; ++i){ int m, b[1001]; scanf("%d", &m); for (int j = 0; j < m + 1; ++j){ scanf("%d", &b[j]); if (!j) continue; int sm = min(b[j], b[j - 1]); int la = max(b[j], b[j - 1]); ++th[sm][la - sm < w]; --th[la][la - sm < w]; } } for (int i = 0; i < w; ++i){ int sum = 0; for (int j = i; j < w * h; j += w){ sum += th[j][0]; l[j][0] = l[j + w][2] = sum; } } for (int i = 0; i < w * h; i += w){ int sum = 0; for (int j = i; j < w + i; ++j){ sum += th[j][1]; l[j][1] = l[j + 1][3] = sum; } } queue q; q.push(0); while (!q.empty()){ int p = q.front(); q.pop(); if (p == w * h - 1) break; int pd = dist[p] + 1; if (l[p][0] && dist[p + w] > pd){ dist[p + w] = pd; q.push(p + w); } if (l[p][1] && dist[p + 1] > pd){ dist[p + 1] = pd; q.push(p + 1); } if (l[p][2] && dist[p - w] > pd){ dist[p - w] = pd; q.push(p - w); } if (l[p][3] && dist[p - 1] > pd){ dist[p - 1] = pd; q.push(p - 1); } } if (dist[w * h - 1] >= LM) printf("Odekakedekinai..\n"); else printf("%d\n", dist[w * h - 1]); }