from collections import deque def dfs(H, W, G): que = deque() parents = [None] * (H + W) nonvisited = set(range(H + W)) while nonvisited: x = next(iter(nonvisited)) # get single element from set but not remove que.append((x, -1, -1)) while que: x, p, j = que.pop() parents[x] = (p, j) nonvisited.remove(x) for y, i in G[x]: if parents[x] == (y, i): continue if y in nonvisited: que.append((y, x, i)) else: return x, y, i, parents print(-1) exit() H, W, N = map(int, input().split()) G = [[] for _ in range(H + W)] points = [None] * N for i in range(1, N + 1): x, y = map(lambda x: int(x) - 1, input().split()) y += H G[x].append((y, i)) G[y].append((x, i)) points[i - 1] = (x, y) X, Y, I, parents = dfs(H, W, G) path = [I] while X != Y: path.append(parents[X][1]) X, _ = parents[X] if points[path[0] - 1][1] != points[path[1] - 1][1]: assert points[path[0] - 1][0] == points[path[1] - 1][0] path = path[::-1] # pathは偶数長なので単に反転させるだけでよい print(len(path)) print(*path)