#include <bits/stdc++.h>
using namespace std;
template <typename T>
void print(T a) {
  for (auto x : a) cout << x << ' ';
  cout << endl;
}
vector<vector<pair<long long, long long> > > g;
vector<long long> used;
vector<long long> ans;
void dfs(long long v) {
  used[v] = 1;
  for (pair<long long, long long> x : g[v]) {
    long long to = x.first, id = x.second;
    if (!used[to])
      dfs(to);
    else if (used[to] == 1) {
      ans[id] = 2;
    }
  }
  used[v] = 2;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n, m;
  cin >> n >> m;
  g.resize(n);
  for (long long i = 0; i < m; i++) {
    long long a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back({b, i});
  }
  used.assign(n, 0);
  ans.assign(m, 1);
  for (long long i = 0; i < n; i++)
    if (!used[i]) dfs(i);
  cout << *max_element((ans).begin(), (ans).end()) << endl;
  print(ans);
  return 0;
}
