#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
vector<pair<int, int> > graph[5000];
bool visited[5000] = {}, calculated[5000] = {};
int ans[5000];
void dfs(int ver) {
  for (auto pr : graph[ver]) {
    if (visited[pr.first]) {
      ans[pr.second] = (calculated[pr.first] ? 1 : 2);
    } else {
      visited[pr.first] = true;
      ans[pr.second] = 1;
      dfs(pr.first);
    }
  }
  calculated[ver] = true;
}
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  for (int i = (0); i < (m); ++i) {
    int u, v;
    cin >> u >> v;
    --u;
    --v;
    graph[u].emplace_back(v, i);
  }
  for (int i = (0); i < (n); ++i) {
    if (!visited[i]) {
      visited[i] = true;
      dfs(i);
    }
  }
  cout << (count(ans, ans + m, 2) > 0 ? 2 : 1) << '\n';
  for (int i = (0); i < (m); ++i) cout << ans[i] << (i + 1 == m ? '\n' : ' ');
  return 0;
}
