Problem: You are given an undirected bipartite graph without multiple edges. You should paint the edges of graph to minimal number of colours, so that no two adjacent edges have the same colour.

Input

The first line contains three integers a, b, m (1 ≤ a, b ≤ 1000, 0 ≤ m ≤ 105), a is the size of the first part, b is the size of the second part, m is the number of edges in the graph.

Each of the next m lines contains two integers x, y (1 ≤ x ≤ a, 1 ≤ y ≤ b), where x is the number of the vertex in the first part and y is the number of the vertex in the second part. It is guaranteed that there are no multiple edges.

Output

In the first line print integer c — the minimal number of colours. The second line should contain m integers from 1 to c — the colours of the edges (in the order they appear in the input).

If there are several solutions, you can print any one of them.

Examples

Input

4 3 5
1 2
2 2
3 2
4 1
4 3


Output

3
1 2 3 1 2

Solution (in JAVA):

Solution:
import java.util.List;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Random;
import java.util.StringTokenizer;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		FastScanner in = new FastScanner(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskF solver = new TaskF();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskF {
	public void solve(int testNumber, FastScanner in, PrintWriter out) {
		int L = in.nextInt();
		int R = in.nextInt();
		n = L + R;
		m = in.nextInt();
		d = 0;
		int[] deg = new int[n];
		edgeA = new int[m];
		edgeB = new int[m];
		for (int i = 0; i < m; i++) {
			edgeA[i] = in.nextInt() - 1;
			edgeB[i] = in.nextInt() - 1 + L;
			++deg[edgeA[i]];
			++deg[edgeB[i]];
		}
		for (int i = 0; i < n; i++) {
			d = Math.max(d, deg[i]);
		}
		incidentEdges = new List[n];
		for (int i = 0; i < n; i++) {
			incidentEdges[i] = new ArrayList<>();
		}
		incidentCol = new int[n][d + 2];
		c = new int[n];
		edgeCol = new int[m];
		int[] p = new int[m];
		for (int i = 0; i < m; i++) {
			p[i] = i;
		}
		Random random = new Random();
		for (int i = 0; i < m; i++) {
			int j = random.nextInt(i + 1);
			int t = p[i];
			p[i] = p[j];
			p[j] = t;
		}
		for (int i = 0; i < m; i++) {
			addNewEdge(p[i]);
		}
		out.println(d);
		for (int i = 0; i < m; i++) {
			if (i > 0) {
				out.print(" ");
			}
			out.print(edgeCol[i]);
		}
		out.println();
	}

	int n, m, d;
	List<Integer>[] incidentEdges;
	int[] edgeA;
	int[] edgeB;
	int[] edgeCol;
	int[][] incidentCol;
	int lastInPath;
	int[] c;
	boolean[] pathMark = new boolean[10000];
	int[] path = new int[10000];
	int pathLen;

	private void addNewEdge(int xy) {
		int x = edgeA[xy];
		int y = edgeB[xy];
		incidentEdges[x].add(xy);
		incidentEdges[y].add(xy);
		for (int i = 1; i <= d; i++) {
			if (incidentCol[x][i] == 0 && incidentCol[y][i] == 0) {
				paint(xy, i);
				return;
			}
		}
		for (int i = 0; i < c.length; i++) {
			c[i] = freeCol(i);
		}
		int cx = c[x];
		int cy = c[y];
		lastInPath = x;
		Arrays.fill(pathMark, 0, n, false);
		pathLen = 0;
		findAlternatingPath(x, cx, cy, pathMark);
		for (int j = 0; j < pathLen; j++) {
			int i = path[j];
			paint(i, cx ^ cy ^ edgeCol[i]);
		}
		paint(xy, cy);
	}

	private void paint(int e, int col) {
		int a = edgeA[e];
		int b = edgeB[e];
		int oldCol = edgeCol[e];
		--incidentCol[a][oldCol];
		--incidentCol[b][oldCol];
		edgeCol[e] = col;
		++incidentCol[a][col];
		++incidentCol[b][col];
	}

	private void findAlternatingPath(int v, int c1, int c2, boolean[] was) {
		was[v] = true;
		for (int e : incidentEdges[v]) {
			if (edgeCol[e] != c1 && edgeCol[e] != c2) {
				continue;
			}
			int u = edgeA[e] ^ edgeB[e] ^ v;
			if (!was[u]) {
				path[pathLen++] = e;
				lastInPath = u;
				findAlternatingPath(u, c1, c2, was);
			}
		}
	}

	private int freeCol(int v) {
		for (int i = 1; i < incidentCol[v].length; i++) {
			if (incidentCol[v][i] == 0) {
				return i;
			}
		}
		throw new AssertionError();
	}
}

class FastScanner {

	private BufferedReader in;
	private StringTokenizer st;

	public FastScanner(InputStream stream) {
		in = new BufferedReader(new InputStreamReader(stream));
	}

	public String next() {
		while (st == null || !st.hasMoreTokens()) {
			try {
				String rl = in.readLine();
				if (rl == null) {
					return null;
				}
				st = new StringTokenizer(rl);
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
		return st.nextToken();
	}

	public int nextInt() {
		return Integer.parseInt(next());
	}

}