#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void read(T& num) {
  num = 0;
  bool f = true;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = false;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    num = num * 10 + ch - '0';
    ch = getchar();
  }
  num = f ? num : -num;
}
int out[100];
template <class T>
inline void write(T x, char ch) {
  if (x == 0) {
    putchar('0');
    putchar(ch);
    return;
  }
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  int num = 0;
  while (x) {
    out[num++] = (x % 10);
    x = x / 10;
  }
  for (int i = (num - 1); i >= (0); i--) putchar(out[i] + '0');
  putchar(ch);
}
const int N = 1024;
int id[N][N], cx[N][N], cy[N][N], dx[N], dy[N], ans[N], nx, ny;
void dfs(int x[][N], int y[][N], int u, int v, int cu, int cv) {
  int w = y[v][cv];
  if (!x[w][cu]) {
    x[w][cu] = v, y[v][cu] = w;
    x[w][cv] = 0;
  } else
    dfs(y, x, v, w, cv, cu);
  x[u][cv] = v;
  y[v][cv] = u;
}
int main() {
  int m;
  read(nx);
  read(ny);
  read(m);
  int u, v;
  for (int i = 1; i <= m; ++i) {
    read(u);
    read(v);
    id[u][v] = i;
    ++dx[u];
    ++dy[v];
  }
  int d =
      max(*max_element(dx + 1, dx + 1 + nx), *max_element(dy + 1, dy + 1 + ny));
  write(d, '\n');
  for (int u = (1); u <= (nx); u++)
    for (int v = (1); v <= (ny); v++)
      if (id[u][v]) {
        int c = 1;
        while (cx[u][c] || cy[v][c]) ++c;
        if (c <= d) {
          cx[u][c] = v;
          cy[v][c] = u;
          continue;
        }
        int cu = 1, cv = 1;
        while (cy[v][cu]) ++cu;
        while (cx[u][cv]) ++cv;
        dfs(cx, cy, u, v, cu, cv);
      }
  for (int u = (1); u <= (nx); u++)
    for (int c = (1); c <= (d); c++)
      if (cx[u][c]) ans[id[u][cx[u][c]]] = c;
  for (int i = (1); i <= (m); i++) write(ans[i], ' ');
  puts("");
  return 0;
}
