#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  long long r;
  while (b != 0) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
const int MOD = (int)1e9 + 7;
const int INF = (int)1e9;
const long long LINF = (long long)1e18;
const long double PI = 2 * acos((long double)0);
const int maxn = 2010;
int a, b, m;
vector<pair<int, int> > edges;
int d[maxn];
int g[maxn][maxn];
int ans[maxn][maxn];
int vis[maxn][maxn];
void join(int u, int v, int c) {
  int old = ans[u][v];
  if (old) {
    if (g[u][old] == v) g[u][old] = 0;
    if (g[v][old] == u) g[v][old] = 0;
  }
  g[u][c] = v;
  g[v][c] = u;
  ans[u][v] = ans[v][u] = c;
}
void solve() {
  scanf("%d%d%d", &a, &b, &m);
  int D = 0;
  for (int i = (0); i < (m); i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    v += a;
    edges.push_back(make_pair(u, v));
    D = max(D, ++d[u]);
    D = max(D, ++d[v]);
  }
  for (int i = (0); i < (m); i++) {
    int u = edges[i].first;
    int v = edges[i].second;
    int flag = 0;
    for (int j = (1); j < (D + 1); j++)
      if (!g[u][j] && !g[v][j]) {
        join(u, v, j);
        flag = 1;
        break;
      }
    if (!flag) {
      int c[2];
      for (int j = (1); j < (D + 1); j++) {
        if (g[u][j] && !g[v][j]) c[0] = j;
        if (!g[u][j] && g[v][j]) c[1] = j;
      }
      int cur = v, cnt = 1;
      vector<int> t;
      while (1) {
        t.push_back(cur);
        int nxt = g[cur][c[cnt]];
        if (!nxt || vis[cur][nxt]) break;
        vis[cur][nxt] = 1;
        cur = nxt;
        cnt ^= 1;
      }
      cnt = 0;
      for (int j = (0); j < (int((t).size()) - 1); j++) {
        join(t[j], t[j + 1], c[cnt]);
        vis[t[j]][t[j + 1]] = 0;
        cnt ^= 1;
      }
      join(u, v, c[1]);
    }
  }
  printf("%d\n", D);
  for (int i = (0); i < (m); i++) {
    int u = edges[i].first;
    int v = edges[i].second;
    printf("%d ", ans[u][v]);
  }
}
int main() {
  solve();
  return 0;
}
