#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e3 + 10;
vector<pair<int, int> > e[maxn];
int col[maxn], res[maxn];
bool cyc;
void dfs(int v) {
  col[v] = 1;
  for (auto p : e[v]) {
    int to = p.first, id = p.second;
    if (col[to] == 0) {
      dfs(to);
      res[id] = 1;
    } else if (col[to] == 2) {
      res[id] = 1;
    } else {
      res[id] = 2;
      cyc = true;
    }
  }
  col[v] = 2;
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    e[u].push_back(pair<int, int>(v, i));
  }
  for (int i = 1; i <= n; i++) {
    if (col[i] == 0) dfs(i);
  }
  printf("%d\n", cyc ? 2 : 1);
  for (int i = 1; i <= m; i++) printf("%d ", res[i]);
}
