#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using ii = pair<int, int>;
using vi = vector<int>;
using vl = vector<ll>;
using vii = vector<ii>;
using graph = vector<vi>;
using matrix = graph;
const int MAXN = 2e3 + 4;
const int INF = 0x3f3f3f3f;
const ll INFL = 1e18 + 4;
const ll mod = 1e9 + 7;
int cor[MAXN][MAXN], dg[MAXN], core[MAXN][MAXN];
vii edges;
graph g;
int main() {
  int n1, n2, m, n;
  scanf("%d %d %d", &n1, &n2, &m);
  n = n1 + n2;
  g.assign(n + 1, vi());
  int u, v, mx = 0;
  for (int i = 0; i < m; ++i) {
    scanf("%d %d", &u, &v);
    v += n1;
    edges.emplace_back(u, v);
    g[u].emplace_back(v);
    g[v].emplace_back(u);
    dg[u]++;
    dg[v]++;
    mx = max(mx, max(dg[u], dg[v]));
  }
  bool yes;
  for (int x, i = 1; i <= n1; ++i) {
    for (auto y : g[i]) {
      x = i;
      yes = 0;
      for (int c = 1; c <= mx; ++c) {
        if (!cor[x][c] && !cor[y][c]) {
          cor[x][c] = y;
          cor[y][c] = x;
          core[x][y] = c;
          core[y][x] = c;
          yes = 1;
          break;
        }
      }
      if (!yes) {
        int c1 = 1, c2 = 1;
        while (cor[x][c1]) c1++;
        while (cor[y][c2]) c2++;
        while (y) {
          int nxt = cor[y][c1];
          core[x][y] = c1;
          core[y][x] = c1;
          cor[x][c1] = y;
          cor[y][c1] = x;
          swap(c1, c2);
          x = y;
          y = nxt;
        }
        cor[x][c1] = 0;
      }
    }
  }
  printf("%d\n", mx);
  for (auto e : edges) {
    printf("%d ", core[e.first][e.second]);
  }
}
