#include <bits/stdc++.h>
using namespace std;
const long long N = 1e4 + 5;
bool f = false;
vector<pair<long long, long long> > G[N];
long long n, m, u, v, ans[N], vis[N];
void dfs(long long u) {
  vis[u] = 1;
  for (long long i = 0; i < G[u].size(); i++) {
    long long v = G[u][i].first, idx = G[u][i].second;
    if (vis[v] == 0 || vis[v] == 2) {
      ans[idx] = 1;
      if (!vis[v]) {
        dfs(v);
      }
    } else if (vis[v] == 1) {
      f = true;
      ans[idx] = 2;
    }
  }
  vis[u] = 2;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m;
  memset(ans, 0, sizeof(ans));
  memset(vis, 0, sizeof(vis));
  for (long long i = 1; i <= m; i++) {
    cin >> u >> v;
    G[u].push_back(make_pair(v, i));
  }
  for (long long i = 1; i <= n; i++) {
    if (!vis[i]) {
      dfs(i);
    }
  }
  cout << (f ? 2 : 1) << endl;
  for (long long i = 1; i <= m; i++) {
    cout << ans[i] << " ";
  }
  return 0;
}
