#include <bits/stdc++.h>
using namespace std;
struct gg {
  int u, v, next;
} side[5500 * 2];
int cnt, n, m, ans;
int head[5500], in[5500];
pair<int, int> ed[5500 * 2];
queue<int> q;
void insert(int u, int v) {
  struct gg add = {u, v, head[u]};
  side[++cnt] = add;
  head[u] = cnt;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    insert(u, v);
    in[v]++;
    ed[i] = make_pair(u, v);
  }
  for (int i = 1; i <= n; i++)
    if (!in[i]) q.push(i);
  while (!q.empty()) {
    int now = q.front();
    q.pop();
    ans++;
    for (int i = head[now]; i; i = side[i].next) {
      int v = side[i].v;
      in[v]--;
      if (!in[v]) q.push(v);
    }
  }
  if (ans == n)
    printf("1\n");
  else
    printf("2\n");
  if (ans == n) {
    for (int i = 1; i <= m; i++) printf("1 ");
  } else {
    for (int i = 1; i <= m; i++) {
      if (ed[i].first > ed[i].second)
        printf("1 ");
      else
        printf("2 ");
    }
  }
  return 0;
}
