using System; using System.IO; using System.Linq; using System.Text; using System.Collections.Generic; using System.Diagnostics; using System.Numerics; using Enu = System.Linq.Enumerable; class Program { static readonly int INF = (int)1e9; static readonly string Impossible = "Odekakedekinai.."; static readonly int[] DR = { 0, 1, 0, -1 }; static readonly int[] DC = { 1, 0, -1, 0 }; public void Solve() { int W = Reader.Int(), H = Reader.Int(), N = Reader.Int(); var table = Reader.IntTable(N * 2); var row = Enu.Range(0, H).Select(i => new List()).ToArray(); var col = Enu.Range(0, W).Select(i => new List()).ToArray(); for (int ti = 1; ti < N * 2; ti += 2) { var t = table[ti]; for (int i = 0; i + 1 < t.Length; i++) { int r = t[i] / W, c = t[i] % W; int nr = t[i + 1] / W, nc = t[i + 1] % W; if (r != nr) { col[c].Add(new Position(Math.Min(r, nr), 1)); col[c].Add(new Position(Math.Max(r, nr), -1)); } else { row[r].Add(new Position(Math.Min(c, nc), 1)); row[r].Add(new Position(Math.Max(c, nc), -1)); } } } bool[, ,] G = new bool[H, W, 4]; for (int r = 0; r < H; r++) { int last = 0, balance = 0; row[r].Sort(); foreach (var p in row[r]) { if (balance > 0) for (int c = last; c < p.Pos; c++) G[r, c, 0] = G[r, c + 1, 2] = true; balance += p.Delta; last = p.Pos; } } for (int c = 0; c < W; c++) { int last = 0, balance = 0; col[c].Sort(); foreach (var p in col[c]) { if (balance > 0) for (int r = last; r < p.Pos; r++) G[r, c, 1] = G[r + 1, c, 3] = true; balance += p.Delta; last = p.Pos; } } int ans = BFS(G, H, W); Console.WriteLine(ans == INF ? Impossible : ans + ""); } struct Position : IComparable { public int Pos; public int Delta; public Position(int Pos, int Delta) { this.Pos = Pos; this.Delta = Delta; } public int CompareTo(Position other) { return Pos - other.Pos; } public override string ToString() { return "Pos:" + Pos + " Delta:" + Delta; } } private int BFS(bool[, ,] G, int H, int W) { if (H == 1 && W == 1) return 0; var minCost = new int[H, W]; for (int r = 0; r < H; r++) for (int c = 0; c < W; c++) minCost[r, c] = INF; minCost[0, 0] = 0; var que = new Queue(); que.Enqueue(0); que.Enqueue(0); while (que.Count > 0) { int r = que.Dequeue(), c = que.Dequeue(); for (int d = 0; d < DR.Length; d++) if (G[r, c, d]) { int nr = r + DR[d], nc = c + DC[d]; int nextCost = minCost[r, c] + 1; if (nextCost < minCost[nr, nc]) { if (nr == H - 1 && nc == W - 1) return nextCost; minCost[nr, nc] = nextCost; que.Enqueue(nr); que.Enqueue(nc); } } } return INF; } } class Entry { static void Main() { new Program().Solve(); } } class Reader { private static TextReader reader = Console.In; private static readonly char[] separator = { ' ' }; private static readonly StringSplitOptions op = StringSplitOptions.RemoveEmptyEntries; private static string[] A = new string[0]; private static int i; private static void Init() { A = new string[0]; } public static void Set(TextReader r) { reader = r; Init(); } public static void Set(string file) { reader = new StreamReader(file); Init(); } public static bool HasNext() { return CheckNext(); } public static string String() { return Next(); } public static int Int() { return int.Parse(Next()); } public static long Long() { return long.Parse(Next()); } public static double Double() { return double.Parse(Next()); } public static int[] IntLine() { return Array.ConvertAll(Split(Line()), int.Parse); } public static int[] IntArray(int N) { return Enu.Range(0, N).Select(i => Int()).ToArray(); } public static int[][] IntTable(int H) { return Enu.Range(0, H).Select(i => IntLine()).ToArray(); } public static string[] StringArray(int N) { return Enu.Range(0, N).Select(i => Next()).ToArray(); } public static string Line() { return reader.ReadLine().Trim(); } private static string[] Split(string s) { return s.Split(separator, op); } private static string Next() { CheckNext(); return A[i++]; } private static bool CheckNext() { if (i < A.Length) return true; string line = reader.ReadLine(); if (line == null) return false; if (line == "") return CheckNext(); A = Split(line); i = 0; return true; } }