#include <bits/stdc++.h>
using namespace std;
int vis[5555], dp[5555], vis2[5555], tt;
vector<pair<int, int> > v[5555];
void recur(int now) {
  vis[now] = 1;
  vis2[now] = 1;
  for (int e = 0; e < v[now].size(); e++) {
    int next = v[now][e].first;
    int nextn = v[now][e].second;
    if (vis[next] == 1) {
      dp[nextn] = 2;
      tt = 1;
    } else {
      dp[nextn] = 1;
      if (vis2[next] == 0) recur(next);
    }
  }
  vis[now] = 0;
}
int main(void) {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int e = 0; e < m; e++) {
    int a, b;
    scanf("%d%d", &a, &b);
    v[a].push_back(make_pair(b, e));
  }
  for (int e = 1; e <= n; e++) {
    if (vis2[e] == 0) {
      recur(e);
    }
  }
  if (tt == 1) {
    printf("2\n");
    for (int e = 0; e < m; e++) printf("%d ", dp[e]);
  } else {
    printf("1\n");
    for (int e = 0; e < m; e++) printf("%d ", dp[e]);
  }
}
