#include <bits/stdc++.h>
#pragma GCC optimze("Ofast")
using namespace std;
const long long infl = 0x3f3f3f3f3f3f3f3fLL;
const int infi = 0x3f3f3f3f;
const int mod = 1000000007;
int powm(int a, int b) {
  int res = 1;
  while (b) {
    if (b & 1) res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}
vector<pair<int, int> > edge[5001];
int vis[5001];
int ans[5001];
bool cycle;
void dfs(int v) {
  vis[v] = 1;
  for (pair<int, int> p : edge[v]) {
    int to = p.first, id = p.second;
    if (vis[to] == 0) {
      dfs(to);
      ans[id] = 1;
    } else if (vis[to] == 1) {
      ans[id] = 2;
      cycle = true;
    } else {
      ans[id] = 1;
    }
  }
  vis[v] = 2;
}
void solve() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int x, y;
    cin >> x >> y;
    edge[x].push_back(pair<int, int>(y, i));
  }
  for (int i = 1; i <= n; i++) {
    if (vis[i] == 0) dfs(i);
  }
  cout << (cycle ? 2 : 1) << '\n';
  for (int i = 1; i <= m; i++) cout << ans[i] << " ";
}
signed main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cout << fixed << setprecision(12);
  int t = 1;
  while (t--) solve();
  return 0;
}
