#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > edge[5005];
int col[5005], vis[5005];
bool cycle = false;
void DFS(int u) {
  vis[u] = 1;
  for (auto v : edge[u]) {
    if (!vis[v.first]) {
      col[v.second] = 1;
      DFS(v.first);
    } else if (vis[v.first] == 2) {
      col[v.second] = 1;
    } else {
      col[v.second] = 2;
      cycle = true;
    }
  }
  vis[u] = 2;
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    edge[u].push_back({v, i});
  }
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) DFS(i);
  }
  cout << (cycle ? 2 : 1) << endl;
  for (int i = 1; i <= m; i++) cout << col[i] << " ";
  return 0;
}
