import java.util.*; public class Main { public static void main (String[] args) { Scanner sc = new Scanner(System.in); int w = sc.nextInt(); int h = sc.nextInt(); int n = sc.nextInt(); int size = w * h; int[][] hfield = new int[h][w]; int[][] vfield = new int[w][h]; for (int i = 0; i < n; i++) { int count = sc.nextInt(); int prev = sc.nextInt(); for (int j = 0; j < count; j++) { int current = sc.nextInt(); int left = Math.min(prev, current); int right = Math.max(prev, current); if (right - left < w) { int height = left / w; hfield[height][left % w]++; hfield[height][right % w]--; } else { int width = left % w; vfield[width][left / w]++; vfield[width][right / w]--; } prev = current; } } for (int i = 0; i < h; i++) { for (int j = 1; j < w; j++) { hfield[i][j] += hfield[i][j - 1]; } } for (int i = 0; i < w; i++) { for (int j = 1; j < h; j++) { vfield[i][j] += vfield[i][j - 1]; } } int[][] costs = new int[h][w]; for (int i = 0; i < h; i++) { Arrays.fill(costs[i], Integer.MAX_VALUE); } PriorityQueue queue = new PriorityQueue<>(); queue.add(new Path(0, 0, 0)); while (queue.size() > 0) { Path p = queue.poll(); if (costs[p.height][p.width] <= p.value) { continue; } costs[p.height][p.width] = p.value; if (p.height > 0 && vfield[p.width][p.height - 1] > 0) { queue.add(new Path(p.height - 1, p.width, p.value + 1)); } if (p.height < h - 1 && vfield[p.width][p.height] > 0) { queue.add(new Path(p.height + 1, p.width, p.value + 1)); } if (p.width > 0 && hfield[p.height][p.width - 1] > 0) { queue.add(new Path(p.height, p.width - 1, p.value + 1)); } if (p.width < w - 1 && hfield[p.height][p.width] > 0) { queue.add(new Path(p.height, p.width + 1, p.value + 1)); } } if (costs[h - 1][w - 1] == Integer.MAX_VALUE) { System.out.println("Odekakedekinai.."); } else { System.out.println(costs[h - 1][w - 1]); } } static class Path implements Comparable { int height; int width; int value; public Path(int height, int width, int value) { this.height = height; this.width = width; this.value = value; } public int compareTo(Path another) { return value - another.value; } } }