#include <bits/stdc++.h>
using namespace std;
const int N = 5e3 + 10;
int mark[N], c[N], f;
vector<pair<int, int> > adj[N];
void dfs(int v) {
  mark[v] = 1;
  for (auto p : adj[v]) {
    int u = p.first, id = p.second;
    if (mark[u] == 0) {
      c[id] = 1;
      dfs(u);
    } else if (mark[u] == 1) {
      c[id] = 2;
      f = 1;
    } else {
      c[id] = 1;
    }
  }
  mark[v] = 2;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back({v, i});
  }
  for (int i = 1; i <= n; i++)
    if (!mark[i]) dfs(i);
  cout << f + 1 << '\n';
  for (int i = 0; i < m; i++) cout << c[i] << ' ';
  return 0;
}
