#include <bits/stdc++.h>
using namespace std;
int n, m, co[2][3000][3000], r1[300000], r2[300000], id[3000][3000],
    res1[3000][3000], d[2][3000];
void dfs(int p, int u, int v, int c1, int c2) {
  int t = co[p ^ 1][v][c1];
  co[p][u][c1] = v;
  co[p ^ 1][v][c1] = u;
  if (!t) {
    co[p ^ 1][v][c2] = 0;
    return;
  }
  dfs(p ^ 1, v, t, c2, c1);
}
int main() {
  int num;
  cin >> n >> m >> num;
  int res = 0;
  int u, v;
  for (int i = 1; i <= num; i++) {
    cin >> u >> v;
    id[u][v] = 1;
    d[0][u]++, d[1][v]++;
    res = max(res, max(d[0][u], d[1][v]));
    r1[i] = u;
    r2[i] = v;
  }
  for (int i = 1; i <= num; i++) {
    int u = r1[i], v = r2[i];
    int ret = 0;
    for (int j = 1; j <= res; j++)
      if (!co[0][u][j] and !co[1][v][j]) ret = j;
    if (ret) {
      co[0][u][ret] = v;
      co[1][v][ret] = u;
      continue;
    }
    int c1 = 0, c2 = 0;
    for (int j = 1; j <= res; j++) {
      if (!co[0][u][j]) c1 = j;
      if (!co[1][v][j]) c2 = j;
    }
    dfs(0, u, v, c1, c2);
  }
  for (int k = 1; k <= n; k++)
    for (int j = 1; j <= res; j++)
      if (co[0][k][j]) res1[k][co[0][k][j]] = j;
  cout << res << "\n";
  for (int j = 1; j <= num; j++) cout << res1[r1[j]][r2[j]] << " ";
}
