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.ArrayList;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
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 {

	// http://www.cs.utexas.edu/~EWD/ewd10xx/EWD1082a.PDF
	// Simplified for the case of the bipartite graph.
	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];
		for (int i = 0; i < m; i++) {
			addNewEdge(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;

	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;
			}
		}
		List<Integer> path = new ArrayList<>();
		for (int i = 0; i < c.length; i++) {
			c[i] = freeCol(i);
		}
		int cx = c[x];
		int cy = c[y];
		lastInPath = x;
		path.clear();
		findAlternatingPath(x, cx, cy, new boolean[n], path);
		if (lastInPath == y) {
			throw new AssertionError();
		}
		for (int i : path) {
			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, List<Integer> path) {
		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.add(e);
				lastInPath = u;
				findAlternatingPath(u, c1, c2, was, path);
			}
		}
	}

	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());
	}

}