#include <bits/stdc++.h>
using namespace std;
long long po(long long a, long long b) {
  long long res = 1;
  while (b) {
    if (b & 1) {
      res = ((res) % 1000000007 * a) % 1000000007;
    }
    a = (a * a) % 1000000007;
    b = b / 2;
  }
  return res % 1000000007;
}
vector<pair<int, int> > adj[100005];
int visited[100005] = {0};
int ans[100005] = {0};
int z = 1;
void dfs(int v) {
  visited[v] = 1;
  for (auto p : adj[v]) {
    int to = p.first, id = p.second;
    if (visited[to] == 0) {
      ans[id] = 1;
      dfs(to);
    } else if (visited[to] == 2) {
      ans[id] = 1;
    } else {
      z = 2;
      ans[id] = 2;
    }
  }
  visited[v] = 2;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  {
    int n, m;
    cin >> n >> m;
    int k = m;
    for (int i = 1; i <= m; i++) {
      int u, v;
      cin >> u >> v;
      adj[u].push_back(make_pair(v, i));
    }
    for (int i = 1; i <= n; i++) {
      if (visited[i] == 0) {
        dfs(i);
      }
    }
    cout << z << endl;
    for (int i = 1; i <= k; i++) {
      cout << ans[i] << " ";
    }
    cout << endl;
  }
  return 0;
}
