#include <bits/stdc++.h>
const int N = 5e3 + 10;
int head[N], edge[N], ver[N], nex[N];
int tot = 0;
void addEdge(int x, int y) {
  ver[++tot] = y;
  nex[tot] = head[x];
  head[x] = tot;
}
int n, m, xx[N], yy[N];
bool flag = false;
int vis[N];
void dfs(int x) {
  if (flag) return;
  vis[x] = 1;
  for (int i = head[x]; i; i = nex[i]) {
    int y = ver[i];
    if (vis[y] == 1) {
      flag = true;
      return;
    } else if (!vis[y])
      dfs(y);
  }
  vis[x] = 2;
}
void solve() {
  for (int i = 1; i <= n; i++)
    if (!vis[i]) dfs(i);
  if (flag) {
    puts("2");
    for (int i = 1; i <= m; i++) {
      if (xx[i] > yy[i])
        printf("2 ");
      else
        printf("1 ");
    }
  } else {
    puts("1");
    for (int i = 1; i <= m; i++) printf("1 ");
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", &xx[i], &yy[i]);
    addEdge(xx[i], yy[i]);
  }
  solve();
  return 0;
}
