#include <bits/stdc++.h>
using namespace std;
inline int re_ad() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') x = x * 10 + (ch ^ 48), ch = getchar();
  return x * f;
}
int a, b, m;
int rd[2010], ans;
int col[2010][2010];
struct node {
  int u, v;
} e[100010];
int main() {
  register int i, j, x, y;
  register int co[2], now;
  a = re_ad();
  b = re_ad();
  m = re_ad();
  for (i = 1; i <= m; ++i) {
    e[i].u = x = re_ad();
    e[i].v = y = re_ad() + a;
    ++rd[x];
    ++rd[y];
  }
  for (i = 1; i <= a + b; ++i) ans = max(ans, rd[i]);
  for (i = 1; i <= m; ++i) {
    for (j = 1; j <= ans; ++j)
      if (!col[e[i].u][j]) {
        co[0] = j;
        break;
      }
    for (j = 1; j <= ans; ++j)
      if (!col[e[i].v][j]) {
        co[1] = j;
        break;
      }
    col[e[i].u][co[0]] = e[i].v;
    col[e[i].v][co[1]] = e[i].u;
    if (co[0] == co[1]) continue;
    now = 1;
    x = e[i].v;
    while (x) {
      swap(col[x][co[1]], col[x][co[0]]);
      x = col[x][co[now]];
      now ^= 1;
    }
  }
  printf("%d\n", ans);
  for (i = 1; i <= m; ++i) {
    for (j = 1; j <= ans; ++j) {
      if (col[e[i].u][j] == e[i].v) {
        printf("%d ", j);
        break;
      }
    }
  }
}
