#include <bits/stdc++.h>
using namespace std;
const int N = 1001;
const int M = 1e5 + 7;
bool have[N << 1][N];
int n, col[M], G[N << 1][N << 1], pt[N << 1][N];
void dfs(int u, int c1, int c2, int pre) {
  int v = pt[u][c2];
  if (G[u][v])
    if (G[u][v] != pre && col[G[u][v]] == col[pre]) {
      have[v][col[G[u][v]]] = 0;
      pt[u][c2] = pt[v][c2] = 0;
      pt[u][c1] = v;
      col[G[u][v]] = c1;
      have[u][c1] = have[v][c1] = 1;
      dfs(v, c2, c1, G[u][v]);
      pt[v][c1] = u;
    }
}
int main() {
  int a, b, m;
  scanf("%d%d%d", &a, &b, &m);
  n = a + b;
  int ans = 0;
  for (int i = 1, u, v; i <= m; i++) {
    scanf("%d%d", &u, &v);
    v += a;
    G[u][v] = G[v][u] = i;
    int c1 = 1, c2 = 1;
    while (have[u][c1]) c1++;
    while (have[v][c2]) c2++;
    ans = max(ans, max(c1, c2));
    col[i] = c2;
    have[u][c2] = have[v][c2] = 1;
    pt[v][c2] = u;
    dfs(u, c1, c2, i);
    pt[u][c2] = v;
  }
  printf("%d\n", ans);
  for (int i = 1; i <= m; i++) printf("%d%c", col[i], " \n"[i == m]);
  return 0;
}
