using System; using static System.Console; using System.Linq; using System.Collections.Generic; class Program { static int NN => int.Parse(ReadLine()); static int[] NList => ReadLine().Split().Select(int.Parse).ToArray(); static int[] NMi => ReadLine().Split().Select(c => int.Parse(c) - 1).ToArray(); static int[][] NMap(int n) => Enumerable.Repeat(0, n).Select(_ => NMi).ToArray(); public static void Main() { Solve(); } static void Solve() { var c = NList; var (h, w, n) = (c[0], c[1], c[2]); var map = NMap(n); WriteLine(Tour(h, w, n, map)); } static string Tour(int h, int w, int n, int[][] _map) { var map = new List<(int id, int h, int w)>(n); for (var i = 0; i < n; ++i) { map.Add((i, _map[i][0] - 1, _map[i][1] - 1)); } map.Sort((l, r) => { var d = l.h.CompareTo(r.h); if (d != 0) return d; return l.w.CompareTo(r.w); }); var tree = new List[n]; for (var i = 0; i < tree.Length; ++i) tree[i] = new List(); for (var i = 0; i + 1 < map.Count; ++i) { if (map[i].h == map[i + 1].h) { tree[map[i].id].Add(map[i + 1].id); tree[map[i + 1].id].Add(map[i].id); } } map.Sort((l, r) => { var d = l.w.CompareTo(r.w); if (d != 0) return d; return l.h.CompareTo(r.h); }); for (var i = 0; i + 1 < map.Count; ++i) { if (map[i].w == map[i + 1].w) { tree[map[i].id].Add(map[i + 1].id); tree[map[i + 1].id].Add(map[i].id); } } map.Sort((l, r) => l.id.CompareTo(r.id)); var plist = Enumerable.Repeat(-1, n).ToArray(); for (var i = 0; i < n; ++i) if (plist[i] < 0) { var q = new Queue<(int pos, int prev)>(); foreach (var next in tree[i]) { plist[next] = i; q.Enqueue((next, i)); } while (q.Count > 0) { var cur = q.Dequeue(); foreach (var next in tree[cur.pos]) { if (next == cur.prev) continue; if (plist[next] >= 0) { // ループ検知 var ans = Ans(i, cur.pos, next, plist, map); return $"{ans.Count}\n{string.Join(" ", ans.Select(ai => ai + 1))}"; } plist[next] = cur.pos; q.Enqueue((next, cur.pos)); } } } return "-1"; } static List Ans(int start, int cur, int next, int[] plist, List<(int id, int h, int w)> map) { var pos = cur; var li1 = new List(); while (pos != start) { li1.Add(pos); pos = plist[pos]; } pos = next; var li2 = new List(); while (pos >= 0) { li2.Add(pos); pos = plist[pos]; } li2.Reverse(); li1.AddRange(li2); var ans = new List(); var begin = -1; for (var i = 0; i < li1.Count; ++i) { var pi = (li1.Count - 1 + i) % li1.Count; var ni = (i + 1) % li1.Count; if (map[li1[pi]].h == map[li1[i]].h && map[li1[i]].w == map[li1[ni]].w) { ans.Add(li1[i]); begin = i; break; } } var tmp = (begin + 1) % li1.Count; while (tmp != begin) { var pi = (li1.Count - 1 + tmp) % li1.Count; var ni = (tmp + 1) % li1.Count; if ((map[li1[pi]].h == map[li1[tmp]].h) != (map[li1[tmp]].h == map[li1[ni]].h)) ans.Add(li1[tmp]); tmp = (tmp + 1) % li1.Count; } return ans; } }