#include <bits/stdc++.h>
using namespace std;
int n, m, k, ta, tb, i, ans[100005], tc;
vector<int> va[2][1005], vb[2][1005], vc[2][1005], vd[2][1005], ve, vf;
int main() {
  scanf("%d%d%d", &n, &m, &k);
  for (int a = 0; a < k; a++) {
    scanf("%d%d", &ta, &tb);
    ta--;
    tb--;
    va[0][ta].push_back(tb);
    va[1][tb].push_back(ta);
    vb[0][ta].push_back(a);
    vb[1][tb].push_back(a);
    vd[0][ta].push_back(va[1][tb].size() - 1);
    vd[1][tb].push_back(va[0][ta].size() - 1);
    ans[a] = -1;
  }
  for (int a = 0; a < n; a++) i = max(i, (int)va[0][a].size());
  for (int a = 0; a < m; a++) i = max(i, (int)va[1][a].size());
  for (int a = 0; a < n; a++)
    for (int b = 0; b < i; b++) vc[0][a].push_back(-1);
  for (int a = 0; a < m; a++)
    for (int b = 0; b < i; b++) vc[1][a].push_back(-1);
  for (int a = 0; a < n; a++)
    for (int b = 0; b < va[0][a].size(); b++) {
      tc = -1;
      ta = -1;
      tb = -1;
      for (int c = 0; c < i; c++) {
        if (vc[0][a][c] == -1 && vc[1][va[0][a][b]][c] == -1) {
          tc = c;
          break;
        } else if (vc[1][va[0][a][b]][c] == -1)
          ta = c;
        else if (vc[0][a][c] == -1)
          tb = c;
      }
      if (tc != -1)
        tb = tc;
      else if (ta == -1 || tb == -1)
        while (1) {
        }
      ve.clear();
      vf.clear();
      ve.push_back(a);
      vf.push_back(b);
      while (1) {
        tc = vc[ve.size() % 2][va[1 - ve.size() % 2][ve.back()][vf.back()]]
               [(ve.size() & 1) ? tb : ta];
        if (tc == -1) break;
        ve.push_back(va[1 - ve.size() % 2][ve.back()][vf.back()]);
        vf.push_back(tc);
      }
      for (int c = 0; c < ve.size(); c++)
        if (ans[vb[c % 2][ve[c]][vf[c]]] != -1) {
          vc[c % 2][ve[c]][ans[vb[c % 2][ve[c]][vf[c]]]] = -1;
          vc[1 - c % 2][va[c % 2][ve[c]][vf[c]]][ans[vb[c % 2][ve[c]][vf[c]]]] =
              -1;
        }
      for (int c = 0; c < ve.size(); c++) {
        ans[vb[c % 2][ve[c]][vf[c]]] = (c & 1) ? ta : tb;
        vc[c % 2][ve[c]][(c & 1) ? ta : tb] = vf[c];
        vc[1 - c % 2][va[c % 2][ve[c]][vf[c]]][(c & 1) ? ta : tb] =
            vd[c % 2][ve[c]][vf[c]];
      }
    }
  printf("%d\n", i);
  for (int a = 0; a < k; a++)
    printf("%d%c", ans[a] + 1, a == k - 1 ? '\n' : ' ');
  return 0;
}
