#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5;
int head[maxn], cnt, rd[maxn], vis[maxn];
int u[maxn], v[maxn], zt[maxn];
int ans[maxn];
struct node {
  int next, to, c;
} edge[maxn * 10];
void add(int u, int v) {
  edge[cnt].to = v;
  edge[cnt].next = head[u];
  head[u] = cnt++;
}
int tuopu(int n) {
  queue<int> q;
  int i;
  for (i = 1; i <= n; i++)
    if (rd[i] == 0) q.push(i);
  while (!q.empty()) {
    int k = q.front();
    q.pop();
    for (i = head[k]; i != -1; i = edge[i].next) {
      int j = edge[i].to;
      rd[j]--;
      if (rd[j] == 0) q.push(j);
    }
  }
  for (i = 1; i <= n; i++)
    if (rd[i] != 0) return 1;
  return 0;
}
int main() {
  int i, j, k, n, m;
  while (scanf("%d%d", &n, &m) != EOF) {
    cnt = 0;
    memset(head, -1, sizeof(head));
    memset(zt, 0, sizeof(zt));
    for (int i = 0; i < maxn; i++) {
      rd[i] = 0;
      ans[i] = 0;
    }
    for (i = 1; i <= m; i++) {
      scanf("%d%d", &u[i], &v[i]);
      add(u[i], v[i]);
      rd[v[i]]++;
    }
    k = tuopu(n);
    if (k == 0) {
      printf("1\n");
      for (i = 1; i < m; i++) printf("1 ");
      printf("1\n");
      continue;
    }
    printf("2\n");
    for (i = 1; i <= m; i++) {
      if (v[i] > u[i])
        ans[i] = 1;
      else
        ans[i] = 2;
    }
    for (i = 1; i < m; i++) printf("%d ", ans[i]);
    printf("%d\n", ans[m]);
  }
}
