#include <bits/stdc++.h>
using namespace std;
const int N = 1024;
int id[N][N], cx[N][N], cy[N][N], dx[N], dy[N], ans[N], nx, ny;
void dfs(int x[][N], int y[][N], int u, int v, int cu, int cv) {
  int w = y[v][cv];
  if (!x[w][cu]) {
    x[w][cu] = v;
    y[v][cu] = w;
    x[w][cv] = 0;
  } else
    dfs(y, x, v, w, cv, cu);
  x[u][cv] = v;
  y[v][cv] = u;
}
int main() {
  int m;
  cin >> nx >> ny >> m;
  int u, v;
  for (int i = 1; i <= m; ++i) {
    scanf("%d%d", &u, &v);
    id[u][v] = i;
    ++dx[u];
    ++dy[v];
  }
  int d =
      max(*max_element(dx + 1, dx + 1 + nx), *max_element(dy + 1, dy + 1 + ny));
  cout << d << endl;
  for (int u = 1; u <= nx; ++u)
    for (int v = 1; v <= ny; ++v)
      if (id[u][v]) {
        int c = 1;
        while (cx[u][c] || cy[v][c]) ++c;
        if (c <= d) {
          cx[u][c] = v;
          cy[v][c] = u;
          continue;
        }
        int cu = 1, cv = 1;
        while (cy[v][cu]) ++cu;
        while (cx[u][cv]) ++cv;
        dfs(cx, cy, u, v, cu, cv);
      }
  for (int u = 1; u <= nx; ++u)
    for (int c = 1; c <= d; ++c)
      if (cx[u][c]) ans[id[u][cx[u][c]]] = c;
  for (int i = 1; i <= m; ++i) printf("%d ", ans[i]);
  puts("");
  return 0;
}
