#include <bits/stdc++.h>
using namespace std;
int n, m, E, u, v;
vector<pair<int, int> > edges;
int res;
int d[2001];
int g[2001][2001], ans[2001][2001];
bool vis[2001][2001];
void join(int x, int y, int c) {
  int cc = ans[x][y];
  if (cc != -1) {
    if (g[x][cc] == y) g[x][cc] = -1;
    if (g[y][cc] == x) g[y][cc] = -1;
  }
  ans[x][y] = ans[y][x] = c;
  g[x][c] = y;
  g[y][c] = x;
}
void color(int u, int v) {
  for (int i = 0, _i = (res); i < _i; ++i) {
    if (g[u][i] == -1 && g[v][i] == -1) {
      join(u, v, i);
      return;
    }
  }
  int c[2];
  for (int i = 0, _i = (res); i < _i; ++i) {
    if (g[u][i] == -1) c[1] = i;
    if (g[v][i] == -1) c[0] = i;
  }
  int sd = 1;
  while (true) {
    if (g[v][c[sd]] == -1) {
      join(u, v, c[sd]);
      break;
    }
    int tmp = g[v][c[sd]];
    join(u, v, c[sd]);
    u = v;
    v = tmp;
    sd = 1 - sd;
  }
}
int main() {
  cin >> n >> m >> E;
  for (int i = 0, _i = (E); i < _i; ++i) {
    cin >> u >> v;
    u--, v--;
    v += n;
    edges.push_back({u, v});
    res = max(res, ++d[u]);
    res = max(res, ++d[v]);
  }
  memset(g, -1, sizeof g);
  memset(ans, -1, sizeof ans);
  for (int i = 0, _i = (E); i < _i; ++i) {
    color(edges[i].first, edges[i].second);
  }
  cout << res << endl;
  for (int i = 0, _i = (E); i < _i; ++i) {
    u = edges[i].first, v = edges[i].second;
    cout << (ans[u][v] + 1) << " ";
  }
  return 0;
}
