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; ArrayList> graph = new ArrayList<>(); for (int i = 0; i < size; i++) { graph.add(new HashSet<>()); } 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) { for (int k = left; k < right; k++) { graph.get(k).add(k + 1); graph.get(k + 1).add(k); } } else { for (int k = left; k + w <= right; k += w) { graph.get(k).add(k + w); graph.get(k + w).add(k); } } prev = current; } } int[] costs = new int[size]; Arrays.fill(costs, Integer.MAX_VALUE); PriorityQueue queue = new PriorityQueue<>(); queue.add(new Path(0, 0)); 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; } } }