import java.util.*; import java.io.*; public class Main { public static void main (String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] first = br.readLine().split(" ", 3); int w = Integer.parseInt(first[0]); int h = Integer.parseInt(first[1]); int n = Integer.parseInt(first[2]); int size = w * h; int[][] hImos = new int[w][h]; int[][] wImos = new int[h][w]; for (int i = 0; i < n; i++) { int m = Integer.parseInt(br.readLine()); String[] line = br.readLine().split(" ", m + 1); int prev = Integer.parseInt(line[0]); for (int j = 0; j < m; j++) { int next = Integer.parseInt(line[j + 1]); if (prev / w == next / w) { int max = Math.max(prev, next); int min = Math.min(prev, next); wImos[min / w][min % w]++; wImos[max / w][max % w]--; } else { int max = Math.max(prev, next); int min = Math.min(prev, next); hImos[min % w][min / w]++; hImos[max % w][max / w]--; } prev = next; } } ArrayList> graph = new ArrayList<>(); for (int i = 0; i < size; i++) { graph.add(new HashSet<>()); } for (int i = 0; i < w; i++) { for (int j = 1; j < h; j++) { hImos[i][j] += hImos[i][j - 1]; if (hImos[i][j - 1] > 0) { graph.get(i + (j - 1) * w).add(i + j * w); graph.get(i + j * w).add(i + (j - 1) * w); } } } for (int i = 0; i < h; i++) { for (int j = 1; j < w; j++) { wImos[i][j] += wImos[i][j - 1]; if (wImos[i][j - 1] > 0) { graph.get(i * w + j - 1).add(i * w + j); graph.get(i * w + j).add(i * w + j - 1); } } } PriorityQueue queue = new PriorityQueue<>(); queue.add(new Path(0, 0)); int[] costs = new int[size]; Arrays.fill(costs, Integer.MAX_VALUE); while (queue.size() > 0) { Path p = queue.poll(); if (costs[p.idx] <= p.value) { continue; } costs[p.idx] = p.value; for (int x : graph.get(p.idx)) { queue.add(new Path(x, p.value + 1)); } } if (costs[size - 1] == Integer.MAX_VALUE) { System.out.println("Odekakedekinai.."); } else { System.out.println(costs[size - 1]); } } static class Path implements Comparable { int idx; int value; public Path(int idx, int value) { this.idx = idx; this.value = value; } public int compareTo(Path another) { return value - another.value; } } }