#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using uint = unsigned int;
template <class T>
using V = vector<T>;
template <class T>
using VV = vector<vector<T>>;
template <class T, class U>
void chmax(T& x, U y) {
  if (x < y) x = y;
}
template <class T, class U>
void chmin(T& x, U y) {
  if (y < x) x = y;
}
template <class T>
void mkuni(V<T>& v) {
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
}
template <class S, class T>
ostream& operator<<(ostream& o, const pair<S, T>& p) {
  return o << "(" << p.first << "," << p.second << ")";
}
template <class T>
ostream& operator<<(ostream& o, const vector<T>& vc) {
  o << "{";
  for (const T& v : vc) o << v << ",";
  o << "}";
  return o;
}
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }
V<int> bipartite_edge_coloring(int N, V<pair<int, int>> es) {
  using P = pair<int, int>;
  int M = es.size();
  int D = 0;
  {
    V<int> d(N);
    for (P e : es) d[e.first]++, d[e.second]++;
    for (int i = 0; i < int(N); i++) chmax(D, d[i]);
  }
  VV<int> vc2e(N, V<int>(D, -1));
  V<int> col(M, -1);
  auto color = [&](int e, int c) {
    int x = es[e].first, y = es[e].second;
    if (col[e] != -1) {
      vc2e[x][col[e]] = -1;
      vc2e[y][col[e]] = -1;
    }
    col[e] = c;
    vc2e[x][c] = e, vc2e[y][c] = e;
  };
  V<int> buf(M);
  for (int t = 0; t < int(M); t++) {
    int x = es[t].first, y = es[t].second;
    int c0 = 0, c1 = 0;
    while (vc2e[x][c0] != -1) c0++;
    while (vc2e[y][c1] != -1) c1++;
    void(0);
    void(0);
    void(0);
    void(0);
    if (c0 == c1) {
      color(t, c0);
      continue;
    }
    int v = x, c = c1, I = 0;
    while (vc2e[v][c] != -1) {
      int e = vc2e[v][c];
      void(0);
      v ^= es[e].first ^ es[e].second;
      c ^= c0 ^ c1;
      buf[I++] = e;
    }
    v = x;
    for (int i = 0; i < int(I); i++) {
      int e = buf[i];
      col[e] ^= c0 ^ c1;
      swap(vc2e[v][c0], vc2e[v][c1]);
      v ^= es[e].first ^ es[e].second;
    }
    swap(vc2e[v][c0], vc2e[v][c1]);
    color(t, c1);
  }
  return col;
}
int main() {
  int A, B, M;
  cin >> A >> B >> M;
  using P = pair<int, int>;
  V<P> es;
  for (int i = 0; i < int(M); i++) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    es.emplace_back(x, y + A);
  }
  auto c = bipartite_edge_coloring(A + B, es);
  int D = 0;
  for (int v : c) chmax(D, v + 1);
  cout << D << endl;
  for (int v : c) cout << v + 1 << " ";
  cout << endl;
}
