#include <bits/stdc++.h>
using namespace std;
inline int in() {
  int32_t x;
  scanf("%d", &x);
  return x;
}
inline long long lin() {
  long long x;
  scanf("%lld", &x);
  return x;
}
inline string get() {
  char ch[2000010];
  scanf("%s", ch);
  return ch;
}
const int MAX_LG = 21;
const int maxn = 2e3 + 10;
const int maxm = 1e5 + 10;
const int base = 29;
const int mod = 1e9 + 7;
const int INF = 1e9 + 100;
const int SQ = 317;
int Cmp;
int mark[maxn];
int v[maxm], u[maxm], deg[maxn], has[maxn][maxn], e[maxm];
int vc[5];
inline void dfs(int node, int id) {
  int edgeid = has[node][vc[id]] - 1;
  if (edgeid < 0) return;
  int nxt = v[edgeid] == node ? u[edgeid] : v[edgeid];
  has[node][vc[id]] = 0;
  has[nxt][vc[id]] = 0;
  dfs(nxt, id ^ 1);
  has[node][vc[id ^ 1]] = edgeid + 1;
  has[nxt][vc[id ^ 1]] = edgeid + 1;
  e[edgeid] = vc[id ^ 1];
}
int res;
int32_t main() {
  int a = in(), b = in(), m = in();
  for (int i = 0; i < m; i++) {
    v[i] = in(), u[i] = in() + a;
    deg[v[i]]++;
    deg[u[i]]++;
    res = max(res, max(deg[v[i]], deg[u[i]]));
  }
  for (int i = 0; i < m; i++) {
    bool fl = false;
    for (int col = 1; col <= res; col++) {
      if (has[v[i]][col] == 0 && has[u[i]][col] == 0 && col <= res) {
        has[v[i]][col] = has[u[i]][col] = i + 1;
        e[i] = col;
        fl = true;
        break;
      }
    }
    if (!fl) {
      Cmp++;
      for (int col = 1; col <= res; col++) {
        if (has[v[i]][col] && !has[u[i]][col]) vc[0] = col;
        if (has[v[i]][col] == 0 && has[u[i]][col]) vc[1] = col;
      }
      dfs(v[i], 0);
      has[v[i]][vc[0]] = has[u[i]][vc[0]] = i + 1;
      e[i] = vc[0];
    }
  }
  cout << res << "\n";
  for (int i = 0; i < m; i++) cout << e[i] << " ";
}
