#include <bits/stdc++.h>
using namespace std;
int flag, used[1111111], used1[1111111];
vector<pair<int, int>> g[1111111];
void cycle(int v) {
  used1[v] = 1;
  for (int i = 0; i < g[v].size(); i++) {
    int to = g[v][i].first;
    if (used[g[v][i].second] == 1) {
      continue;
    }
    if (used1[to] == 1) {
      used[g[v][i].second] = 1;
      flag = 1;
    }
    if (used1[to] == 0) {
      cycle(to);
    }
  }
  used1[v] = 2;
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int v, u;
    cin >> v >> u;
    g[v].push_back({u, i});
  }
  for (int i = 1; i <= n; i++) {
    if (!used1[i]) {
      cycle(i);
    }
  }
  if (!flag) {
    cout << 1 << endl;
    for (int i = 1; i <= m; i++) {
      cout << 1 << ' ';
    }
    return 0;
  }
  while (flag) {
    flag = 0;
    for (int i = 1; i <= n; i++) {
      if (!used1[i]) {
        cycle(i);
      }
    }
  }
  cout << 2 << endl;
  for (int i = 1; i <= m; i++) {
    cout << used[i] + 1 << ' ';
  }
}
