#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long N = 5e3 + 5;
stack<long long> st;
vector<long long> vp[N];
vector<long long> vp1[N];
set<long long> s;
long long vis[N];
long long a[N], b[N];
void dfs(long long n) {
  vis[n] = 1;
  for (long long i = 0; i < vp[n].size(); i++) {
    if (!vis[vp[n][i]]) {
      dfs(vp[n][i]);
    }
  }
  st.push(n);
}
void dfs1(long long n) {
  vis[n] = 1;
  for (long long i = 0; i < vp1[n].size(); i++) {
    if (!vis[vp1[n][i]]) {
      dfs1(vp1[n][i]);
    }
  }
  s.insert(n);
}
int32_t main() {
  long long n, m;
  cin >> n >> m;
  for (long long i = 1; i <= m; i++) {
    cin >> a[i] >> b[i];
    vp[a[i]].push_back(b[i]);
    vp1[b[i]].push_back(a[i]);
  }
  for (long long i = 1; i <= n; i++) {
    if (!vis[i]) dfs(i);
  }
  memset(vis, 0, sizeof(vis));
  long long flag = 0;
  while (!st.empty()) {
    if (!vis[st.top()]) {
      s.clear();
      dfs1(st.top());
      if (s.size() > 1) {
        flag = 1;
        break;
      }
    }
    st.pop();
  }
  if (flag == 0) {
    cout << "1" << endl;
    for (long long i = 1; i <= m; i++) {
      cout << 1 << " ";
    }
    cout << endl;
    return 0;
  }
  cout << "2" << endl;
  for (long long i = 1; i <= m; i++) {
    if (a[i] > b[i]) {
      cout << "2 ";
    } else {
      cout << "1 ";
    }
  }
  cout << endl;
}
