#include <bits/stdc++.h>
using namespace std;
int head[200010], to[200010], nxt[200010], idx;
int shad[5010][5010], ans[200010], all, n, m;
bool vis[200010], tmp[200010];
void add(int a, int b) { nxt[++idx] = head[a], to[idx] = b, head[a] = idx; }
void dfs(int p, int from) {
  vis[p] = tmp[p] = 1;
  for (int i = head[p]; i; i = nxt[i]) {
    if (!tmp[to[i]]) ans[shad[p][to[i]]] = 1, all++;
    if (!vis[to[i]]) dfs(to[i], p);
  }
  tmp[p] = 0;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1, x, y; i <= m; i++)
    scanf("%d%d", &x, &y), shad[x][y] = i, add(x, y);
  for (int i = 1; i <= n; i++)
    if (!vis[i]) dfs(i, 0);
  if (all == m)
    printf("1\n");
  else
    printf("2\n");
  for (int i = 1; i <= m; i++)
    if (!ans[i])
      printf("2 ");
    else
      printf("1 ");
}
