mod = 1000000007 eps = 10**-9 NN = 2*10 ** 5 def main(): import sys from collections import deque input = sys.stdin.buffer.readline H, W, N = map(int, input().split()) adj = [[] for _ in range(H + W + 1)] E_idx = {} for i in range(1, N+1): x, y = map(int, input().split()) E_idx[x * (NN+1) + y + H] = i E_idx[(y + H) * (NN+1) + x] = i adj[x].append(y + H) adj[y + H].append(x) seen = [-1] * (H + W + 1) par = [0] * (H + W + 1) for v0 in range(1, H+1): if seen[v0] != -1: continue que = deque() que.append(v0) seen[v0] = 0 B = [] while que: v = que.popleft() for u in adj[v]: if seen[u] == -1: seen[u] = seen[v] + 1 par[u] = v que.append(u) else: if u != par[v]: B.append((u, v)) if B: u, v = B[0] U = [] uu = u while uu != v0: U.append(uu) uu = par[uu] V = [] vv = v while vv != 0: V.append(vv) vv = par[vv] V.reverse() U.extend(V) if U[0] > H: U_new = [U[-1]] + U[:-1] U = U_new ans = [] L = len(U) for i in range(L): x, y = U[i], U[(i+1)%L] if x > y: x, y = y, x #print(x, y) ans.append(E_idx[x * (NN+1) + y]) print(len(ans)) print(*ans) exit() #print(seen) print(-1) if __name__ == '__main__': main()