#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
const int MAXN = 1000;
const int MAXM = 100000;
int nl, nr, m;
int a[MAXM], b[MAXM];
int el[MAXN][MAXN];
int er[MAXN][MAXN];
int res[MAXM];
void run() {
  scanf("%d%d%d", &nl, &nr, &m);
  for (int i = (0); i < (m); ++i) scanf("%d%d", &a[i], &b[i]), --a[i], --b[i];
  memset(el, -1, sizeof(el));
  memset(er, -1, sizeof(er));
  memset(res, -1, sizeof(res));
  for (int i = (0); i < (m); ++i) {
    int ca = 0;
    while (el[a[i]][ca] != -1) ++ca;
    int cb = 0;
    while (er[b[i]][cb] != -1) ++cb;
    for (int x = i, cnt = 0;; ++cnt) {
      int y = er[b[x]][ca];
      el[a[x]][ca] = er[b[x]][ca] = x;
      res[x] = ca;
      if (y == -1) {
        if (x != i) er[b[x]][cb] = -1;
        break;
      }
      int z = el[a[y]][cb];
      el[a[y]][cb] = er[b[y]][cb] = y;
      res[y] = cb;
      if (z == -1) {
        el[a[y]][ca] = -1;
        break;
      }
      x = z;
    }
  }
  int ret = 0;
  for (int i = (0); i < (nl); ++i) {
    int cur = 0;
    for (int j = (0); j < (nr); ++j)
      if (el[i][j] != -1) ++cur;
    if (cur > ret) ret = cur;
  }
  for (int i = (0); i < (nr); ++i) {
    int cur = 0;
    for (int j = (0); j < (nl); ++j)
      if (er[i][j] != -1) ++cur;
    if (cur > ret) ret = cur;
  }
  printf("%d\n", ret);
  for (int i = (0); i < (m); ++i) {
    if (i != 0) printf(" ");
    printf("%d", res[i] + 1);
  }
  puts("");
}
int main() {
  run();
  return 0;
}
