#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > adj[5005];
vector<int> intime(5005, 0), outtime(5005, 0), color(5005, 0);
int colcnt = 1, tme = 1;
void dfs(int node) {
  intime[node] = tme++;
  for (auto it : adj[node]) {
    if (intime[it.first] != 0 && outtime[it.first] == 0) {
      colcnt = 2;
      color[it.second] = 1;
    } else if (outtime[it.first] == 0) {
      dfs(it.first);
    }
  }
  outtime[node] = tme++;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int x, y;
    cin >> x >> y;
    adj[x].push_back({y, i});
  }
  for (int i = 1; i <= n; ++i)
    if (intime[i] == 0) dfs(i);
  cout << colcnt << "\n";
  for (int i = 0; i < m; ++i) {
    cout << color[i] + 1 << " ";
  }
  return 0;
}
