def main(): import sys input = sys.stdin.read().split() idx = 0 L = int(input[idx]); idx +=1 R = int(input[idx]); idx +=1 M = int(input[idx]); idx +=1 edges = [] for _ in range(M): a = int(input[idx]); idx +=1 b = int(input[idx]); idx +=1 edges.append((a, b)) # Compute degrees deg_v1 = [0] * L deg_v2 = [0] * R for a, b in edges: deg_v1[a] +=1 deg_v2[b] +=1 min_degree = min(min(deg_v1), min(deg_v2)) min_size = min(L, R) K = min(min_degree, min_size) # Assign colors color = [0] * M for i, (a, b) in enumerate(edges): # Use a simple heuristic: color = (a + b) % K color[i] = (a + b) % K # Output print(K) print('\n'.join(map(str, color))) if __name__ == "__main__": main()