#include <bits/stdc++.h>
#pragma GCC optimize("O2")
using namespace std;
const int N = 5e3 + 1;
vector<int> co[N];
vector<int> vis(N, 0);
vector<int> eul;
void dfs(int x) {
  vis[x] = 1;
  for (auto y : co[x])
    if (!vis[y]) dfs(y);
  eul.push_back(x);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  vector<pair<int, int>> ed(m);
  for (auto &x : ed) {
    cin >> x.first >> x.second;
    co[x.first].push_back(x.second);
  }
  for (int i = 1; i <= n; i++)
    if (!vis[i]) dfs(i);
  reverse(eul.begin(), eul.end());
  vector<int> pos(n + 1);
  for (int i = 0; i < n; i++) pos[eul[i]] = i;
  vector<int> ans(m);
  int used = 1;
  for (int i = 0; i < m; i++) {
    auto x = ed[i];
    if (pos[x.first] < pos[x.second]) {
      ans[i] = 1;
    } else {
      used = 2;
      ans[i] = 2;
    }
  }
  cout << used << "\n";
  for (auto x : ans) cout << x << " ";
}
