#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
class {
 public:
  int n, m;
  vector<pair<int, int> > V[5555];
  bool vis[5555], in[5555];
  int ans[5555];
  void solve() {
    cin >> n >> m;
    for (int i = 0; i < m; ++i) {
      int u, v;
      cin >> u >> v;
      V[u].push_back({v, i});
    }
    for (int i = 1; i <= n; ++i) {
      if (!vis[i]) dfs(i);
    }
    if (two)
      cout << 2 << "\n";
    else
      cout << 1 << "\n";
    for (int i = 0; i < m; ++i) {
      cout << ans[i] + 1 << " ";
    }
  }
  bool two = 0;
  void dfs(int u) {
    in[u] = 1;
    vis[u] = 1;
    for (auto v : V[u]) {
      if (!vis[v.first]) {
        dfs(v.first);
      } else if (in[v.first]) {
        ans[v.second] = 1;
        two = 1;
      }
    }
    in[u] = 0;
  }
} NSPACE;
int main() {
  ios_base::sync_with_stdio(false);
  cout.tie(0);
  cin.tie(0);
  NSPACE.solve();
}
