#include <bits/stdc++.h>
using namespace std;
const int N = 5000;
bool viz[1 + N];
bool used[1 + N];
vector<pair<int, int>> gr[1 + N];
int ans[1 + N];
void dfs(int node) {
  used[node] = true;
  viz[node] = true;
  for (auto son : gr[node]) {
    if (used[son.first]) {
      ans[son.second] = 2;
      continue;
    }
    if (viz[son.first]) continue;
    dfs(son.first);
  }
  used[node] = false;
}
void solve() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int x, y;
    cin >> x >> y;
    gr[x].push_back({y, i});
  }
  for (int i = 1; i <= m; i++) ans[i] = 1;
  for (int i = 1; i <= n; i++)
    if (!viz[i]) dfs(i);
  int k = 0;
  for (int i = 1; i <= m; i++) k = max(k, ans[i]);
  cout << k << "\n";
  for (int i = 1; i <= m; i++) cout << ans[i] << " ";
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  while (t--) solve();
  return 0;
}
