#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100001;
vector<pair<int, int> > g1[1000], g2[1000];
int edge_colors[MAXN];
pair<int, int> colors1[1000][1000];
pair<int, int> colors2[1000][1000];
int ans;
bool used[MAXN];
bool dfs(int v, int c1, int c2) {
  if (used[v]) return false;
  used[v] = true;
  int to = colors1[v][c1].first;
  int num = colors1[v][c1].second;
  if (colors2[to][c2].first == -1) {
    colors1[v][c1].first = -1;
    colors1[v][c1].second = -1;
    colors2[to][c1].first = -1;
    colors2[to][c1].second = -1;
    edge_colors[colors1[v][c1].second] = -1;
    edge_colors[colors2[to][c1].second] = -1;
    colors1[v][c2].first = to;
    colors1[v][c2].second = num;
    colors2[to][c2].first = v;
    colors2[to][c2].second = num;
    edge_colors[num] = c2;
    return true;
  }
  int mto = colors2[to][c2].first;
  int mto_num = colors2[to][c2].second;
  if (colors1[mto][c1].first == -1) {
    colors1[mto][c2].first = -1;
    colors1[mto][c2].second = -1;
    edge_colors[colors1[mto][c2].second] = -1;
    colors1[mto][c1].first = to;
    colors1[mto][c1].second = mto_num;
    colors2[to][c1].first = mto;
    colors2[to][c1].second = mto_num;
    edge_colors[mto_num] = c1;
    colors1[v][c1].first = -1;
    colors1[v][c1].second = -1;
    edge_colors[colors1[v][c1].second] = -1;
    colors1[v][c2].first = to;
    colors1[v][c2].second = num;
    colors2[to][c2].first = v;
    colors2[to][c2].second = num;
    edge_colors[num] = c2;
    return true;
  } else if (dfs(mto, c1, c2)) {
    colors1[mto][c1].first = to;
    colors1[mto][c1].second = mto_num;
    colors2[to][c1].first = mto;
    colors2[to][c1].second = mto_num;
    edge_colors[mto_num] = c1;
    colors1[v][c1].first = -1;
    colors1[v][c1].second = -1;
    edge_colors[colors1[v][c1].second] = -1;
    colors1[v][c2].first = to;
    colors1[v][c2].second = num;
    colors2[to][c2].first = v;
    colors2[to][c2].second = num;
    edge_colors[num] = c2;
    return true;
  }
  return false;
}
int main() {
  ios_base::sync_with_stdio(false);
  int a, b, m;
  cin >> a >> b >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    g1[x].emplace_back(make_pair(y, i));
    g2[y].emplace_back(make_pair(x, i));
  }
  for (int i = 0; i < a; i++)
    if (g1[i].size() > ans) ans = g1[i].size();
  for (int i = 0; i < b; i++)
    if (g2[i].size() > ans) ans = g2[i].size();
  for (int i = 0; i < 1000; i++)
    for (int j = 0; j < 1000; j++) {
      colors1[i][j] = make_pair(-1, -1);
      colors2[i][j] = make_pair(-1, -1);
    }
  fill(edge_colors, edge_colors + m, -1);
  for (int i = 0; i < a; i++) {
    for (int j = 0; j < g1[i].size(); j++) {
      int to = g1[i][j].first;
      int num = g1[i][j].second;
      if (edge_colors[num] == -1) {
        bool ok = false;
        for (int q = 0; q < ans; q++) {
          if ((colors1[i][q].first == -1) && (colors2[to][q].first == -1)) {
            edge_colors[num] = q;
            colors1[i][q].first = to;
            colors1[i][q].second = num;
            colors2[to][q].first = i;
            colors2[to][q].second = num;
            ok = true;
            break;
          }
        }
      }
    }
  }
  for (int i = 0; i < a; i++) {
    for (int j = 0; j < g1[i].size(); j++) {
      int to = g1[i][j].first;
      int num = g1[i][j].second;
      if (edge_colors[num] == -1) {
        bool ok = false;
        for (int q = 0; q < ans; q++) {
          if ((colors1[i][q].first == -1) && (colors2[to][q].first == -1)) {
            edge_colors[num] = q;
            colors1[i][q].first = to;
            colors1[i][q].second = num;
            colors2[to][q].first = i;
            colors2[to][q].second = num;
            ok = true;
            break;
          }
        }
        if (!ok) {
          int c1 = -1, c2 = -1;
          for (int q = 0; q < ans; q++) {
            if ((colors1[i][q].first != -1) && (colors2[to][q].first == -1))
              c1 = q;
            if ((colors1[i][q].first == -1) && (colors2[to][q].first != -1))
              c2 = q;
          }
          fill(used, used + a, false);
          used[i] = true;
          if ((c1 == -1) || (c2 == -1))
            while (true)
              ;
          int mto = colors2[to][c2].first;
          int mto_num = colors2[to][c2].second;
          if (colors1[mto][c1].first == -1) {
            colors1[mto][c2].first = -1;
            colors1[mto][c2].second = -1;
            edge_colors[colors1[mto][c2].second] = -1;
            colors1[mto][c1].first = to;
            colors1[mto][c1].second = mto_num;
            colors2[to][c1].first = mto;
            colors2[to][c1].second = mto_num;
            edge_colors[mto_num] = c1;
            colors1[i][c2].first = to;
            colors1[i][c2].second = num;
            colors2[to][c2].first = i;
            colors2[to][c2].second = num;
            edge_colors[num] = c2;
          } else if (dfs(mto, c1, c2)) {
            colors1[mto][c1].first = to;
            colors1[mto][c1].second = mto_num;
            colors2[to][c1].first = mto;
            colors2[to][c1].second = mto_num;
            edge_colors[mto_num] = c1;
            colors1[i][c2].first = to;
            colors1[i][c2].second = num;
            colors2[to][c2].first = i;
            colors2[to][c2].second = num;
            edge_colors[num] = c2;
          }
        }
      }
    }
  }
  for (int i = 0; i < a; i++)
    for (int q = 0; q < ans; q++)
      if (colors1[i][q].first != -1) edge_colors[colors1[i][q].second] = q;
  cout << ans << endl;
  for (int i = 0; i < m; i++) {
    cout << edge_colors[i] + 1 << ' ';
  }
  return 0;
}
