#include <bits/stdc++.h>
using namespace std;
const int maxn = 5005;
vector<int> G[maxn];
int id[maxn][maxn];
void insert(int u, int v, int i) {
  G[u].push_back(v);
  id[u][v] = i;
}
int vis[maxn], col[maxn];
int cyc = 0;
void dfs(int u) {
  vis[u] = 1;
  for (int v : G[u]) {
    if (!vis[v]) {
      col[id[u][v]] = 1;
      dfs(v);
    } else if (vis[v] == 1) {
      col[id[u][v]] = 2;
      cyc = 1;
    } else {
      col[id[u][v]] = 1;
    }
  }
  vis[u] = 2;
}
int n, m;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1, u, v; i <= m; i++) {
    scanf("%d%d", &u, &v);
    insert(u, v, i);
  }
  for (int i = 1; i <= n; i++)
    if (!vis[i]) dfs(i);
  if (cyc)
    puts("2");
  else
    puts("1");
  for (int i = 1; i <= m; i++) printf("%d ", col[i]);
  return 0;
}
