#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e3 + 5, maxm = 5e3 + 5;
struct edge {
  int v, next;
} e[maxm];
int color[maxn];
int tot, head[maxn], n, m, flag, ok, vis[maxn];
void init() {
  memset(head, -1, sizeof(head));
  tot = 0;
}
void insert(int u, int v) {
  e[tot].v = v;
  e[tot].next = head[u];
  head[u] = tot++;
}
void dfs(int u, int v) {
  if (flag == 0) return;
  if (u == v) {
    flag = 0;
    return;
  }
  vis[u] = 1;
  for (int i = head[u]; i != -1; i = e[i].next) {
    if (!vis[e[i].v]) dfs(e[i].v, v);
  }
}
int main() {
  scanf("%d%d", &n, &m);
  init();
  ok = 0;
  for (int i = 0; i < m; i++) {
    int x, y;
    flag = 1;
    scanf("%d%d", &x, &y);
    memset(vis, 0, sizeof(vis));
    dfs(y, x);
    if (flag) {
      insert(x, y);
      color[i] = 1;
    } else
      color[i] = 2, ok = 1;
  }
  if (ok)
    cout << 2 << "\n";
  else
    cout << 1 << "\n";
  for (int i = 0; i < m; i++) printf("%d%c", color[i], " \n"[i == m - 1]);
  return 0;
}
