using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Contest { class Program { static void Main(string[] args) { new Program().Solve(); } void Solve() { string[] temp = Console.ReadLine().Split(); int w = int.Parse(temp[0]); int h = int.Parse(temp[1]); int n = int.Parse(temp[2]); int[,] imos = new int[1010, 1010]; int[,] imos2 = new int[1010, 1010]; for (int i = 0; i < n; i++) { int m = int.Parse(Console.ReadLine()) + 1; int[] ys = new int[m]; int[] xs = new int[m]; int[] b = Console.ReadLine().Split().Select(int.Parse).ToArray(); for (int j = 0; j < m; j++) { ys[j] = b[j] / w; xs[j] = b[j] % w; } for (int j = 0; j < m - 1; j++) { int y1 = Math.Min(ys[j], ys[j + 1]); int x1 = Math.Min(xs[j], xs[j + 1]); int y2 = Math.Max(ys[j], ys[j + 1]); int x2 = Math.Max(xs[j], xs[j + 1]); if (y1 == y2) { imos[y1, x1]++; imos[y1, x2]--; imos[y2 + 1, x1]--; imos[y2 + 1, x2]++; } else { imos2[y1, x1]++; imos2[y1, x2 + 1]--; imos2[y2, x1]--; imos2[y2, x2 + 1]++; } } } for (int i = 0; i < 1009; i++) { for (int j = 0; j < 1009; j++) { imos[i, j + 1] += imos[i, j]; imos2[i, j + 1] += imos2[i, j]; } } for (int j = 0; j < 1009; j++) { for (int i = 0; i < 1009; i++) { imos[i + 1, j] += imos[i, j]; imos2[i + 1, j] += imos2[i, j]; } } Queue qy = new Queue(); Queue qx = new Queue(); qy.Enqueue(0); qx.Enqueue(0); const int Inf = (int)1e9; int[,] dist = new int[1010, 1010]; for (int i = 0; i < 1010; i++) { for (int j = 0; j < 1010; j++) { dist[i, j] = Inf; } } dist[0, 0] = 0; int[] dy = { 0, 1, 0, -1 }; int[] dx = { 1, 0, -1, 0 }; while (qx.Count > 0) { int y = qy.Dequeue(); int x = qx.Dequeue(); for (int k = 0; k < 4; k++) { int ny = y + dy[k]; int nx = x + dx[k]; if (ny < 0 || ny >= h || nx < 0 || nx >= w) continue; if (k == 0 && imos[y, x] <= 0) continue; if (k == 1 && imos2[y, x] <= 0) continue; if (k == 2 && imos[ny, nx] <= 0) continue; if (k == 3 && imos2[ny, nx] <= 0) continue; if (dist[ny, nx] > dist[y, x] + 1) { dist[ny, nx] = dist[y, x] + 1; qy.Enqueue(ny); qx.Enqueue(nx); } } } int ans = dist[h - 1, w - 1]; if (ans >= Inf) { Console.WriteLine("Odekakedekinai.."); } else { Console.WriteLine(ans); } } } }