#include <bits/stdc++.h>
const int N = 1e6 + 10;
const int mod = 998244353;
using namespace std;
vector<pair<int, int> > v[5500];
int ans[5050], vis[5050], k = 1;
void dfs(int x, int co) {
  vis[x] = 1;
  for (auto e : v[x]) {
    if (vis[e.first] == 1)
      ans[e.second] = co + 1;
    else if (vis[e.first] == 0) {
      ans[e.second] = co;
      dfs(e.first, co);
    } else
      ans[e.second] = k;
  }
  vis[x] = 2;
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; ++i) {
    int x, y;
    scanf("%d%d", &x, &y);
    v[x].push_back({y, i});
  }
  for (int i = 1; i <= n; ++i)
    if (vis[i] == 0) k = 1, dfs(i, 1);
  for (int i = 1; i <= m; ++i) k = max(k, ans[i]);
  printf("%d\n", k);
  for (int i = 1; i <= m; ++i) printf("%d%c", ans[i], " \n"[i == m]);
  return 0;
}
