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.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

// Educational Codeforces Round 2
// Edge coloring of bipartite graph
public class Problem_600_F {
    

    public static void main(String[] args)
    {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        PrintWriter out = new PrintWriter(outputStream);
        Problem_600_F solver = new Problem_600_F();
        solver.solve(1, new InputStreamReader(inputStream), out);
        out.close();
    }

    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();
    }
    
    @SuppressWarnings("unchecked")
    public void solve(int test, Reader input, PrintWriter out)
    {
        MyScanner in = new MyScanner(input);
        
        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();    }

    //-----------MyScanner class for faster input----------
    public static class MyScanner {
        BufferedReader br;
        StringTokenizer st;

        public MyScanner(Reader reader) {
            br = new BufferedReader(reader);
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }
        
        int[] nextIntArray(int n)
        {
            int[] r = new int[n];
            for(int i = 0; i < n; i++)
                r[i] = nextInt();
            return r;
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine(){
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }

    }
    //--------------------------------------------------------

}