#include <bits/stdc++.h>
using namespace std;
int n, m, vis[5005], ans[5005], cnt = 1;
vector<pair<int, int>> v[5005];
void dfs(int a) {
  vis[a] = 1;
  for (auto& i : v[a])
    if (vis[i.first] == 0) {
      ans[i.second] = 1;
      dfs(i.first);
    } else if (vis[i.first] == 1) {
      ans[i.second] = 2;
      cnt = 2;
    } else
      ans[i.second] = 1;
  vis[a] = 2;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    int ta, tb;
    scanf("%d%d", &ta, &tb);
    v[ta].emplace_back(tb, i);
  }
  for (int i = 1; i <= n; i++)
    if (!vis[i]) dfs(i);
  printf("%d\n", cnt);
  for (int i = 0; i < m; i++) printf("%d%c", ans[i], " \n"[i == m - 1]);
}
