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[][] ver = new int[h + 1][w + 1]; int[][] hor = new int[h + 1][w + 1]; int n = sc.nextInt(); for (int i = 0; i < n; i++) { int m = sc.nextInt(); int prev = sc.nextInt(); for (int j = 0; j < m; j++) { int x = sc.nextInt(); int min = Math.min(prev, x); int max = Math.max(prev, x); if (max - min >= w) { ver[min / w][min % w]++; ver[max / w + 1][max % w]--; } else { hor[min / w][min % w]++; hor[max / w][max % w + 1]--; } prev = x; } } for (int i = 0; i < h; i++) { for (int j = 1; j < w; j++) { hor[i][j] += hor[i][j - 1]; } } for (int i = 0; i < w; i++) { for (int j = 1; j < h; j++) { ver[j][i] += ver[j - 1][i]; } } PriorityQueue queue = new PriorityQueue<>(); queue.add(new Path(0, 0, 0)); int[][] costs = new int[h][w]; for (int[] arr : costs) { Arrays.fill(arr, Integer.MAX_VALUE); } while (queue.size() > 0) { Path p = queue.poll(); if (costs[p.r][p.c] <= p.value) { continue; } costs[p.r][p.c] = p.value; if (p.r > 0 && ver[p.r][p.c] > 0 && ver[p.r - 1][p.c] > 0) { queue.add(new Path(p.r - 1, p.c, p.value + 1)); } if (p.r < h - 1 && ver[p.r][p.c] > 0 && ver[p.r + 1][p.c] > 0) { queue.add(new Path(p.r + 1, p.c, p.value + 1)); } if (p.c > 0 && hor[p.r][p.c] > 0 && hor[p.r][p.c - 1] > 0) { queue.add(new Path(p.r, p.c - 1, p.value + 1)); } if (p.c < w - 1 && hor[p.r][p.c] > 0 && hor[p.r][p.c + 1] > 0) { queue.add(new Path(p.r, p.c + 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 r; int c; int value; public Path(int r, int c, int value) { this.r = r; this.c = c; this.value = value; } public int compareTo(Path another) { return value - another.value; } } }