#include <bits/stdc++.h>
using namespace std;
struct edge {
  int a, b;
};
int n1, n2, m, c;
int deg1[1000 + 1], deg2[1000 + 1];
int adj[1000 + 1][1000 + 1], adjc[1000 + 1][1000 + 1];
int adjcv1[1000 + 1][1000 + 1], adjcv2[1000 + 1][1000 + 1];
edge e[100000];
void change1(int x, int from, int to);
void change2(int x, int from, int to);
void change1(int x, int from, int to) {
  int flag = 0;
  if (adjcv2[adjcv1[x][from]][to])
    change2(adjcv1[x][from], to, from);
  else
    flag = 1;
  adjc[x][adjcv1[x][from]] = to;
  adjcv1[x][to] = adjcv1[x][from];
  adjcv2[adjcv1[x][to]][to] = x;
  if (flag) adjcv2[adjcv1[x][from]][from] = 0;
  adjcv1[x][from] = 0;
}
void change2(int x, int from, int to) {
  int flag = 0;
  if (adjcv1[adjcv2[x][from]][to])
    change1(adjcv2[x][from], to, from);
  else
    flag = 1;
  adjc[adjcv2[x][from]][x] = to;
  adjcv2[x][to] = adjcv2[x][from];
  adjcv1[adjcv2[x][to]][to] = x;
  if (flag) adjcv1[adjcv2[x][from]][from] = 0;
  adjcv2[x][from] = 0;
}
void solve(int n1, int n2) {
  if (n1 == 0) return;
  solve(n1 - 1, n2);
  for (int i = 1, cn = 1; i <= n2; ++i) {
    if (adj[n1][i]) {
      if (adjcv2[i][cn]) {
        for (int j = 1; j <= c; ++j) {
          if (adjcv2[i][j] == 0) {
            change2(i, cn, j);
            break;
          }
        }
      }
      adjcv1[n1][cn] = i;
      adjcv2[i][cn] = n1;
      adjc[n1][i] = cn++;
    }
  }
}
int main() {
  scanf("%d%d%d", &n1, &n2, &m);
  for (int i = 0; i < m; ++i) scanf("%d%d", &e[i].a, &e[i].b);
  for (int i = 0; i < m; ++i) {
    adj[e[i].a][e[i].b] = 1;
    deg1[e[i].a]++;
    deg2[e[i].b]++;
  }
  for (int i = 1; i <= n1; ++i) c = max(c, deg1[i]);
  for (int i = 1; i <= n1; ++i) c = max(c, deg2[i]);
  solve(n1, n2);
  printf("%d\n", c);
  for (int i = 0; i < m; ++i) printf("%d ", adjc[e[i].a][e[i].b]);
  return 0;
}
