#include <bits/stdc++.h>
using namespace std;
int vis[5001];
bool isit[5001][5001];
vector<long long> cld[5002];
int an[5001][5001];
void go(long long crnd, long long par) {
  if (vis[crnd]) return;
  vis[crnd] = 1;
  isit[par][crnd] = true;
  long long i;
  long long szz = cld[crnd].size();
  szz--;
  for (i = 0; i <= szz; i++) {
    go(cld[crnd][i], par);
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long int i, j, x, y, n, m, cnt = 0, k, g, flg, t;
  cin >> n >> m;
  int ed[m + 1][3];
  for (i = 1; i <= m; i++) {
    cin >> x >> y;
    cld[x].push_back(y);
    ed[i][1] = x;
    ed[i][2] = y;
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) vis[j] = 0;
    go(i, i);
  }
  flg = 0;
  for (i = 1; i <= n; i++) {
    sort(cld[i].begin(), cld[i].end());
    k = cld[i].size();
    for (j = k - 1; j >= 0; j--) {
      x = cld[i][j];
      if (x < i) break;
      if (isit[x][i]) {
        an[i][x] = 2;
        flg = 1;
      }
    }
  }
  if (flg)
    cout << 2 << endl;
  else
    cout << 1 << endl;
  for (i = 1; i <= m; i++) {
    x = ed[i][1];
    y = ed[i][2];
    if (!an[x][y])
      cout << 1 << " ";
    else
      cout << an[x][y] << " ";
  }
  cout << endl;
}
