#include <bits/stdc++.h>
using namespace std;
const int N = 5005;
int n, m;
vector<int> G[N];
int vis[N], col[N];
int h[N], cnt;
struct Edge {
  int u, v, nxt;
} e[N];
inline void add(int u, int v) {
  e[++cnt] = Edge{u, v, h[u]};
  h[u] = cnt;
}
int fp;
void dfs(int u) {
  vis[u] = 1;
  for (int i = h[u]; i; i = e[i].nxt) {
    int v = e[i].v;
    if (!vis[v])
      dfs(v);
    else if (vis[v] == 1) {
      col[i] = 2;
      fp = 2;
    }
  }
  vis[u] = 2;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    add(u, v);
    col[i] = 1;
  }
  fp = 1;
  for (int i = 1; i <= n; i++)
    if (!vis[i]) dfs(i);
  cout << fp << '\n';
  for (int i = 1; i <= m; i++) cout << col[i] << ' ';
}
