#include <bits/stdc++.h>
using namespace std;
int A, B, m;
int d[1005 * 2];
int link[1005][1005], link1[1005][1005];
int id[1005][1005];
int ans[1005 * 1005];
int pri;
int main() {
  scanf("%d%d%d", &A, &B, &m);
  for (int i = 1; i <= m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    pri = max(pri, max(++d[x], ++d[y + A]));
    id[x][y] = i;
    int cx = 1, cy = 1;
    while (link[x][cx]) cx++;
    while (link1[y][cy]) cy++;
    if (cx == cy) {
      link[x][cx] = y;
      link1[y][cy] = x;
      continue;
    }
    int l = x, r = y;
    link[l][cx] = r;
    while (1) {
      int tmp = link1[r][cx];
      link1[r][cx] = l;
      link1[r][cy] = tmp;
      if (!tmp) break;
      int tmp0 = link[tmp][cy];
      link[tmp][cy] = r;
      link[tmp][cx] = tmp0;
      if (!tmp0) break;
      l = tmp;
      r = tmp0;
    }
  }
  printf("%d\n", pri);
  for (int i = 1; i <= A; i++) {
    for (int j = 1; j <= pri; j++) {
      if (!link[i][j]) continue;
      ans[id[i][link[i][j]]] = j;
    }
  }
  for (int i = 1; i <= m; i++) printf("%d ", ans[i]);
  return 0;
}
