#include <bits/stdc++.h>
#pragma warning(disable : 4996)
#pragma comment(linker, "/STACK:36777216")
using namespace std;
const long double EPS = 1e-9;
vector<vector<pair<int, int>>> g;
vector<pair<int, int>> es;
vector<int> cs;
int max_deg;
void dfs(int v, const vector<int> &cc, int it, int le) {
  for (auto vv : g[v]) {
    if (vv.second == le) continue;
    if (cs[vv.second] == cc[it]) {
      dfs(vv.first, cc, it ^ 1, vv.second);
      cs[vv.second] = cc[it ^ 1];
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << fixed << setprecision(14);
  int n1, n2, m;
  cin >> n1 >> n2 >> m;
  int n = n1 + n2;
  g.resize(n);
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    b += n1;
    g[a].push_back(make_pair(b, i));
    g[b].push_back(make_pair(a, i));
    es.push_back(make_pair(a, b));
    max_deg = max(max_deg, (int)g[a].size());
    max_deg = max(max_deg, (int)g[b].size());
  }
  cs.assign(m, -1);
  for (int ie = 0; ie < es.size(); ++ie) {
    int a = es[ie].first;
    int b = es[ie].second;
    vector<bool> csa(max_deg, false), csb(max_deg, false);
    for (auto to : g[a]) {
      int col = cs[to.second];
      if (col != -1) csa[col] = true;
    }
    for (auto to : g[b]) {
      int col = cs[to.second];
      if (col != -1) csb[col] = true;
    }
    int col = -1;
    for (int i = 0; i < max_deg; ++i) {
      if (!csa[i] && !csb[i]) {
        col = i;
        break;
      }
    }
    if (col != -1) {
      cs[ie] = col;
      continue;
    }
    int c1 = -1, c2 = -1;
    for (int i = 0; i < max_deg; ++i) {
      if (csa[i] && !csb[i]) {
        c1 = i;
      } else if (csb[i] && !csa[i]) {
        c2 = i;
      }
    }
    vector<int> cc = {c1, c2};
    dfs(b, cc, 1, -1);
    cs[ie] = c2;
  }
  cout << max_deg << endl;
  for (int i = 0; i < cs.size(); ++i) {
    cout << cs[i] + 1 << ' ';
  }
  return 0;
};
