#include <bits/stdc++.h>
using namespace std;
const int maxx = 600010;
const double pi = acos(-1.0);
const double eps = 1e-15;
vector<pair<int, int>> g[maxx];
stack<int> s;
int t, n, m, u, v;
int ans = 0, total = 0, sum = 0, cnt = 1, res = 0;
int a[maxx], b[maxx], bj[maxx], mk[maxx], low[maxx], dfn[maxx], vis[maxx],
    head[maxx];
void tarjan(int x) {
  if (ans) return;
  low[x] = dfn[x] = ++res, s.push(x), vis[x] = 1;
  for (int i = 0; i < g[x].size(); i++) {
    int to = g[x][i].first;
    if (!dfn[to]) {
      tarjan(to);
      low[x] = min(low[x], low[to]);
    } else if (vis[to])
      low[x] = min(low[x], dfn[to]);
  }
  if (low[x] == dfn[x]) {
    vis[x] = 0;
    sum = 1;
    while (s.top() != x) {
      sum++;
      vis[s.top()] = 0;
      s.pop();
    }
    if (sum >= 2) ans = 2;
    s.pop();
  }
}
void dfs(int x, int father) {
  vis[x] = 1;
  for (int i = 0; i < g[x].size(); i++) {
    int to = g[x][i].first, num = g[x][i].second;
    if (x < to)
      bj[num] = 1;
    else
      bj[num] = 2;
    if (to == father) continue;
    if (vis[to]) continue;
    dfs(to, x);
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", &u, &v);
    g[u].push_back(make_pair(v, i));
  }
  for (int i = 1; i <= n; i++)
    if (!dfn[i]) tarjan(i);
  if (ans) {
    memset(vis, 0, sizeof(vis));
    for (int i = 1; i <= n; i++)
      if (!vis[i]) dfs(i, i);
    printf("2\n");
    for (int i = 1; i <= m; i++) printf("%d ", bj[i]);
  } else {
    printf("1\n");
    for (int i = 1; i <= m; i++) printf("1 ");
  }
  return 0;
}
