#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> edges[5555];
bool done[5555];
bool dfs(int curr, int target, int c) {
  if (curr == target) return true;
  if (done[curr]) return false;
  done[curr] = true;
  for (pair<int, int> vc : edges[curr]) {
    if (vc.second == c && dfs(vc.first, target, c)) return true;
  }
  return false;
}
int ans[5555];
int main() {
  int n, m;
  cin >> n >> m;
  int maxc = 1;
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    for (int c = 1;; c++) {
      maxc = max(maxc, c);
      for (int j = 1; j <= n; j++) done[j] = false;
      if (!dfs(v, u, c)) {
        ans[i] = c;
        edges[u].push_back({v, c});
        break;
      }
    }
  }
  cout << maxc << endl;
  for (int i = 1; i <= m; i++) {
    printf("%d ", ans[i]);
  }
  cout << endl;
}
