#include <bits/stdc++.h>
using namespace std;
const int N = 2002;
int a, b;
vector<pair<int, int> > g[N];
vector<pair<int, int> > edges;
int C[N][N];
int D;
bool seen[N];
bool dfs(int x, int y, int cx, int cy) {
  if (seen[x]) return false;
  seen[x] = true;
  if (C[y][cy] == -1) {
    assert(C[x][cx] == y && C[y][cx] == x);
    C[x][cx] = -1;
    C[y][cx] = -1;
    C[x][cy] = y;
    C[y][cy] = x;
    return true;
  } else if (dfs(y, C[y][cy], cy, cx)) {
    C[y][cy] = x;
    C[x][cy] = y;
    return true;
  }
  return false;
}
int main() {
  ios_base::sync_with_stdio(false);
  int a, b, m;
  cin >> a >> b >> m;
  for (int _n(m), i(0); i < _n; i++) {
    int x, y;
    cin >> x >> y;
    --x, --y;
    y += a;
    g[x].emplace_back(y, i);
    g[y].emplace_back(x, i);
    edges.emplace_back(x, y);
  }
  D = 0;
  for (int _n(a + b), i(0); i < _n; i++) D = max(D, (int)g[i].size());
  memset((C), (-1), sizeof(C));
  for (int k = 0; k < (int)edges.size(); ++k) {
    int x = edges[k].first;
    int y = edges[k].second;
    bool done = false;
    for (int i = 0; i < D; ++i) {
      if (C[x][i] == -1 && C[y][i] == -1) {
        C[x][i] = y;
        C[y][i] = x;
        done = true;
        break;
      }
    }
    if (!done) {
      int cx = -1, cy = -1;
      for (int i = 0; i < D; ++i) {
        if (C[y][i] == -1 && C[x][i] != -1 && cx == -1) {
          cx = i;
        }
        if (C[y][i] != -1 && C[x][i] == -1 && cy == -1) {
          cy = i;
        }
      }
      memset((seen), (0), sizeof(seen));
      dfs(x, y, cx, cy);
    }
  }
  cout << D << endl;
  for (auto el : edges) {
    int x = el.first;
    int y = el.second;
    int c = -1;
    for (int _n(D), i(0); i < _n; i++)
      if (C[x][i] == y && C[y][i] == x) {
        c = i;
        break;
      }
    assert(c != -1);
    cout << c + 1 << " ";
  }
  cout << endl;
  return 0;
}
