#include <bits/stdc++.h>
using namespace std;
const int MAX = 5000;
int head[MAX + 10], nxt[MAX + 10], to[MAX + 10], clr[MAX + 10], tot;
bool vis[MAX + 10], is[MAX + 10];
void dfs(int x) {
  vis[x] = is[x] = true;
  for (int e = head[x]; e; e = nxt[e]) {
    if (!vis[to[e]])
      clr[e] = 1;
    else
      clr[e] = 2;
    if (!is[to[e]]) dfs(to[e]);
  }
  vis[x] = false;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m, u, v;
  cin >> n >> m;
  for (int i = 1; i <= m; ++i) {
    cin >> u >> v;
    nxt[++tot] = head[u];
    to[head[u] = tot] = v;
  }
  for (int i = 1; i <= n; ++i) {
    if (!is[i]) dfs(i);
  }
  int k = 0;
  for (int i = 1; i <= m; ++i) {
    k = max(k, clr[i]);
  }
  cout << k << '\n';
  for (int i = 1; i <= m; ++i) {
    cout << clr[i] << ' ';
  }
  return 0;
}
