#include <bits/stdc++.h>
using namespace std;
const int N = 2e3 + 5, M = 1e5 + 5;
int n, n1, k, cc, res, C[N], Dt[N], Assigned[N], color[M], visited[N];
vector<pair<int, int>> S[N];
void Enter() {
  cin >> n1 >> n >> k;
  n += n1;
  int u, v;
  for (int i = 1; i <= k; i++) {
    cin >> u >> v;
    v += n1;
    S[u].emplace_back(v, i);
    S[v].emplace_back(u, i);
    C[u]++;
    C[v]++;
  }
}
bool FindPath(int u) {
  if (visited[u] != cc)
    visited[u] = cc;
  else
    return false;
  if (C[u] != res) {
    Dt[u] = 0;
    Assigned[u] = 0;
    return true;
  }
  for (pair<int, int> e : S[u])
    if (!color[e.second] || color[e.second] == res) {
      int v = e.first;
      if (!Assigned[v]) {
        Assigned[v] = u;
        Assigned[u] = v;
        color[Dt[v]] = 0;
        Dt[u] = Dt[v] = e.second;
        color[Dt[v]] = res;
        return true;
      }
    }
  for (pair<int, int> e : S[u])
    if (!color[e.second] || color[e.second] == res) {
      int v = e.first;
      if (FindPath(Assigned[v])) {
        Assigned[v] = u;
        Assigned[u] = v;
        color[Dt[v]] = 0;
        Dt[u] = Dt[v] = e.second;
        color[Dt[v]] = res;
        return true;
      }
    }
  return false;
}
void Process() {
  for (int i = 1; i <= n; i++) res = max(res, C[i]);
  cout << res << '\n';
  for (; res; res--) {
    fill(Dt + 1, Dt + n + 1, 0);
    fill(Assigned + 1, Assigned + n + 1, 0);
    for (int j = 1; j <= n; j++)
      if (!Assigned[j] && C[j] == res) {
        ++cc;
        FindPath(j);
      }
    for (int j = 1; j <= n; j++)
      if (!Assigned[j]) {
        ++cc;
        FindPath(j);
      }
    for (int j = 1; j <= n; j++)
      if (Assigned[j]) C[j]--;
  }
  for (int i = 1; i <= k; i++) cout << color[i] << ' ';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  Enter();
  Process();
}
