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 CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
const int inf = (int)1.01e9;
const long long infll = (long long)1.01e18;
const long double eps = 1e-9;
const long double pi = acos((long double)-1);
mt19937 mrand(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x) { return mrand() % x; }
void precalc() {}
const int maxn = 1005, maxm = (int)1e5 + 5;
int n[2], m;
vector<int> g[2][maxn], ids[2][maxn];
bool read() {
  if (scanf("%d%d%d", &n[0], &n[1], &m) < 3) {
    return false;
  }
  for (int t = 0; t < 2; t++) {
    for (int v = 0; v < n[t]; v++) {
      g[t][v].clear();
      ids[t][v].clear();
    }
  }
  for (int i = 0; i < m; i++) {
    int v, u;
    scanf("%d%d", &v, &u);
    v--;
    u--;
    g[0][v].push_back(u);
    ids[0][v].push_back(i);
    g[1][u].push_back(v);
    ids[1][u].push_back(i);
  }
  return true;
}
int c[maxm];
int p[2][maxn], pe[2][maxn];
int used[2][maxn];
int curu;
bool dfs(int t, int v, int d) {
  used[t][v] = curu;
  for (int i = 0; i < ((int)(g[t][v]).size()); i++) {
    int u = g[t][v][i], e = ids[t][v][i];
    int w = p[!t][u], ee = pe[!t][u];
    if (w == -1) {
      p[!t][u] = v;
      pe[!t][u] = e;
      p[t][v] = u;
      pe[t][v] = e;
      c[e] = d;
      return true;
    }
    if (((int)(g[t][w]).size()) < d) {
      p[t][w] = -1;
      pe[t][w] = -1;
      c[ee] = -1;
      p[!t][u] = v;
      pe[!t][u] = e;
      p[t][v] = u;
      pe[t][v] = e;
      c[e] = d;
      return true;
    }
  }
  for (int i = 0; i < ((int)(g[t][v]).size()); i++) {
    int u = g[t][v][i], e = ids[t][v][i];
    int w = p[!t][u], ee = pe[!t][u];
    if (used[t][w] != curu && dfs(t, w, d)) {
      c[ee] = -1;
      p[!t][u] = v;
      pe[!t][u] = e;
      p[t][v] = u;
      pe[t][v] = e;
      c[e] = d;
      return true;
    }
  }
  return false;
}
int perm[maxn];
void solve() {
  for (int i = 0; i < m; i++) {
    c[i] = -1;
  }
  int d = 0;
  for (int t = 0; t < 2; t++) {
    for (int v = 0; v < n[t]; v++) {
      d = max(d, ((int)(g[t][v]).size()));
      vector<pair<int, int>> es;
      for (int i = 0; i < ((int)(g[t][v]).size()); i++) {
        es.push_back(make_pair(g[t][v][i], ids[t][v][i]));
      }
      shuffle(es.begin(), es.end(), mrand);
      for (int i = 0; i < ((int)(g[t][v]).size()); i++) {
        g[t][v][i] = es[i].first;
        ids[t][v][i] = es[i].second;
      }
    }
  }
  for (int dd = d; dd > 0; dd--) {
    for (int t = 0; t < 2; t++) {
      for (int v = 0; v < n[t]; v++) {
        p[t][v] = -1;
        pe[t][v] = -1;
        int k = 0;
        for (int i = 0; i < ((int)(g[t][v]).size()); i++) {
          int u = g[t][v][i], e = ids[t][v][i];
          if (c[e] == -1) {
            g[t][v][k] = u;
            ids[t][v][k] = e;
            k++;
          }
        }
        g[t][v].resize(k);
        ids[t][v].resize(k);
      }
    }
    for (int t = 0; t < 2; t++) {
      for (int v = 0; v < n[t]; v++) {
        perm[v] = v;
      }
      shuffle(perm, perm + n[t], mrand);
      for (int i = 0; i < n[t]; i++) {
        int v = perm[i];
        if (p[t][v] == -1 && ((int)(g[t][v]).size()) == dd) {
          curu++;
          assert(dfs(t, v, dd));
        }
      }
    }
  }
  printf("%d\n", d);
  for (int i = 0; i < m; i++) {
    printf("%d ", c[i]);
  }
  printf("\n");
}
int main() {
  precalc();
  while (read()) {
    solve();
  }
  return 0;
}