#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
const int maxn = 5e3 + 5;
vector<pair<int, int>> g[maxn];
int vis[maxn], ans[maxn];
void dfs(int x) {
  vis[x] = 1;
  for (int i = (0); i < (g[x].size()); i++) {
    int v = g[x][i].first;
    if (!vis[v]) {
      ans[g[x][i].second] = 1;
      dfs(v);
    } else if (vis[v] == 1) {
      ans[g[x][i].second] = 2;
    } else
      ans[g[x][i].second] = 1;
  }
  vis[x] = 2;
}
int main() {
  int n, m;
  scanf("%d", &n);
  scanf("%d", &m);
  for (int i = (1); i <= (m); i++) {
    int u, v;
    scanf("%d", &u);
    scanf("%d", &v);
    g[u].push_back(make_pair(v, i));
  }
  for (int i = (1); i <= (n); i++)
    if (!vis[i]) dfs(i);
  int s = 1;
  for (int i = (1); i <= (m); i++)
    if (ans[i] == 2) {
      s = 2;
      break;
    }
  printf("%d", s);
  puts("");
  for (int i = (1); i <= (m); i++) printf("%d ", ans[i]);
}
