#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
long long color[5000] = {}, pp = 1;
long long ans[5000] = {};
map<pair<long long, long long>, long long> ma;
set<long long> c;
long long f = 1;
long long n, m;
vector<long long> v[5000];
void dfs(long long u) {
  color[u] = 1;
  for (auto i : v[u]) {
    if (color[i] == 1) {
      ans[ma[{u, i}]] = 2;
      f = 2;
    }
    if (!color[i]) dfs(i);
  }
  color[u] = 2;
}
int main() {
  cin >> n >> m;
  for (long long i = 0; i < m; i++) {
    long long a, b;
    cin >> a >> b;
    v[--a].push_back(--b);
    ma[{a, b}] = i;
  }
  for (long long i = 0; i < n; i++)
    if (!color[i]) {
      dfs(i);
    }
  cout << f << endl;
  long long yy = 1;
  for (long long i = 0; i < m; i++) cout << max(ans[i], yy) << " ";
}
