#include <bits/stdc++.h>
using namespace std;
bool dfs(int v, vector<int> &flag, vector<vector<int> > &g) {
  if (flag[v] == 2) return false;
  if (flag[v] == 1) return true;
  flag[v] = 1;
  for (int z = 0; z < int(g[v].size()); z++) {
    int j = g[v][z];
    if (dfs(j, flag, g)) return true;
  }
  flag[v] = 2;
  return false;
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  vector<pair<int, int> > e(m);
  vector<vector<int> > g(n + 1);
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &e[i].first, &e[i].second);
    g[e[i].first].push_back(e[i].second);
  }
  vector<int> flag(n + 1, 0);
  bool has_cycle = false;
  for (int i = 1; i <= n; i++) has_cycle |= dfs(i, flag, g);
  vector<int> ans(m);
  for (int i = 0; i < m; i++) ans[i] = (e[i].first < e[i].second) ? 1 : 2;
  int k = 2;
  if (!has_cycle) fill(ans.begin(), ans.end(), k = 1);
  printf("%d\n", k);
  for (int i = 0; i < m; i++) printf("%d ", ans[i]);
  printf("\n");
  return 0;
}
