#include <bits/stdc++.h>
using namespace std;
struct edge {
  int x, y;
  int color = -1;
};
int main() {
  int a, b, m, i, j;
  edge e;
  scanf("%d%d%d", &a, &b, &m);
  vector<edge> edges;
  vector<vector<pair<int, int>>> graph(a + b, vector<pair<int, int>>());
  for (i = 0; i < m; ++i) {
    scanf("%d%d", &e.x, &e.y);
    e.x = e.x - 1;
    e.y = e.y + a - 1;
    edges.push_back(e);
    graph[e.x].push_back(make_pair(e.y, i));
    graph[e.y].push_back(make_pair(e.x, i));
  }
  int colors = 0;
  for (i = 0; i < a + b; ++i) colors = max((int)graph[i].size(), colors);
  printf("%d\n", colors);
  vector<bool> used_color_x(colors, false);
  vector<bool> used_color_y(colors, false);
  for (i = 0; i < m; ++i) {
    for (j = 0; j < colors; ++j) {
      used_color_x[j] = false;
      used_color_y[j] = false;
    }
    for (auto edge : graph[edges[i].x])
      if (edges[edge.second].color > -1)
        used_color_x[edges[edge.second].color] = true;
    for (auto edge : graph[edges[i].y])
      if (edges[edge.second].color > -1)
        used_color_y[edges[edge.second].color] = true;
    int not_used_color = -1;
    for (j = 0; j < colors; ++j)
      if (!used_color_x[j] && !used_color_y[j]) {
        not_used_color = j;
        break;
      }
    if (not_used_color == -1) {
      int color_x, color_y;
      for (j = 0; j < colors; ++j)
        if (used_color_x[j] && !used_color_y[j]) {
          color_x = j;
          break;
        }
      for (j = 0; j < colors; ++j)
        if (!used_color_x[j] && used_color_y[j]) {
          color_y = j;
          break;
        }
      int temp_edge = i;
      int v = edges[i].x;
      edges[temp_edge].color = color_x;
      while (true) {
        int color_x_count = 0;
        for (auto edge : graph[v])
          if (edges[edge.second].color == color_x) color_x_count++;
        if (color_x_count == 2) {
          for (auto edge : graph[v])
            if (edges[edge.second].color == color_x &&
                edge.second != temp_edge) {
              temp_edge = edge.second;
              v = edge.first;
              edges[temp_edge].color = color_y;
              swap(color_x, color_y);
              break;
            }
        } else {
          break;
        }
      }
    } else {
      edges[i].color = not_used_color;
    }
  }
  for (i = 0; i < m; ++i) printf("%d ", edges[i].color + 1);
  return 0;
}
