#include <bits/stdc++.h>
long long q_pow(long long x, long long n, long long mod) {
  long long ret = 1;
  while (n) {
    if (n & 1) ret = x * ret % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return ret;
}
long long __gcd(long long x, long long y) {
  if (!y) return x;
  return __gcd(y, x % y);
}
long long getbit(long long now) {
  int cnt = 0;
  while (now) {
    cnt++;
    now -= (now) & (-now);
  }
  return cnt;
}
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
using namespace std;
const long long INF = 0x3f3f3f3f;
const long long IINF = 0x3f3f3f3f3f3f3f3f;
const int maxn = 5000 + 9;
const int maxm = 30;
const double eps = 1e-6;
const int mod = 1e9 + 7;
int n, m;
vector<int> edge[maxn];
int in[maxn];
vector<pair<int, int> > vec;
bool check(int n) {
  int cnt = n;
  queue<int> q;
  for (int i = 1; i <= n; i++) {
    if (in[i] == 0) q.push(i), cnt--;
  }
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (auto to : edge[u]) {
      in[to]--;
      if (in[to] == 0) {
        cnt--;
        q.push(to);
      }
    }
  }
  if (cnt == 0) return 1;
  return 0;
}
signed main(int argc, char *argv[]) {
  if (argc == 2 && strcmp("-debug", argv[1]) == 0) {
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
  }
  scanf("%d%d", &n, &m);
  int u, v;
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", &u, &v);
    edge[u].push_back(v);
    in[v]++;
    vec.push_back(make_pair(u, v));
  }
  int flag = check(n);
  if (flag == 1) {
    cout << 1 << endl;
    for (int i = 1; i <= m; i++) printf("1 ");
    printf("\n");
    return 0;
  }
  cout << 2 << endl;
  for (auto to : vec) {
    u = to.first, v = to.second;
    if (u < v)
      printf("1 ");
    else
      printf("2 ");
  }
  return 0;
}
