#include <bits/stdc++.h>
using namespace std;
const int N = 5005;
vector<pair<int, int> > adj[N];
int cycle[N], mx = 1;
int n, m, res[N];
void dfs(int u) {
  cycle[u] = 1;
  for (int i = 0; i < (int)adj[u].size(); ++i) {
    int v = adj[u][i].second;
    int first = adj[u][i].first;
    if (cycle[v] == 1) {
      res[first] = 2;
      continue;
    } else {
      res[first] = 1;
      if (cycle[v] == 2) continue;
      dfs(v);
    }
  }
  cycle[u] = 2;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    adj[u].push_back(pair<int, int>(i, v));
  }
  for (int i = 1; i <= n; ++i)
    if (cycle[i] == 0) dfs(i);
  mx = *max_element(res + 1, res + m + 1);
  printf("%d\n", mx);
  for (int i = 1; i <= m; ++i) {
    char c = (i == m ? '\n' : ' ');
    printf("%d%c", res[i], c);
  }
  return 0;
}
