#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  if (a < b) return gcd(b, a);
  return b == 0 ? a : gcd(b, a % b);
}
const int N = 5e3 + 9;
int in[N], h[N], ans[N];
int cnt = 0, n, m;
bool vis[N];
bool vis1[N];
struct edge {
  int to, nex, pre;
} e[N];
struct node {
  int u, col;
} a[N];
void add(int u, int v) {
  e[++cnt] = (edge){v, h[u], u};
  h[u] = cnt;
}
bool judge() {
  priority_queue<pair<int, int>, vector<pair<int, int> >,
                 greater<pair<int, int> > >
      q;
  for (int i = 1; i <= n; ++i) q.push(make_pair(in[i], i));
  bool ok = 1;
  while (!q.empty()) {
    int num = q.top().first;
    int u = q.top().second;
    q.pop();
    if (vis1[u]) continue;
    vis1[u] = 1;
    if (num) {
      ok = 0;
      break;
    }
    for (int i = h[u]; i; i = e[i].nex) {
      int v = e[i].to;
      if (vis1[v]) continue;
      --in[v];
      q.push(make_pair(in[v], v));
    }
  }
  return ok;
}
void dfs(int x) {
  int u = e[x].to;
  for (int i = h[u]; i; i = e[i].nex) {
    int v = e[i].to;
    if (vis[i]) continue;
    vis[i] = 1;
    if (v > u)
      ans[i] = 1;
    else
      ans[i] = 2;
    dfs(i);
  }
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= m; ++i) {
    int u, v;
    scanf("%d %d", &u, &v);
    ++in[v];
    add(u, v);
  }
  if (judge()) {
    puts("1");
    for (int i = 1; i <= m; ++i) printf("%d ", 1);
    return 0;
  }
  for (int i = 1; i <= m; ++i) {
    if (!vis[i]) {
      vis[i] = 1;
      if (e[i].to > e[i].pre)
        ans[i] = 1;
      else
        ans[i] = 2;
      dfs(i);
    }
  }
  puts("2");
  for (int i = 1; i <= m; ++i) printf("%d ", ans[i]);
  return 0;
}
