#include <bits/stdc++.h>
using namespace std;
vector<int> status, ans;
vector<vector<pair<int, int>>> graph;
void dfs(int node) {
  status[node] = 1;
  for (auto x : graph[node]) {
    if (status[x.first] == 1) {
      ans[x.second] = 2;
    } else {
      ans[x.second] = 1;
      if (status[x.first] == 0) dfs(x.first);
    }
  }
  status[node] = 2;
}
int main() {
  int nodeCount, edgeCount;
  cin >> nodeCount >> edgeCount;
  graph = vector<vector<pair<int, int>>>(nodeCount + 1);
  ans = vector<int>(edgeCount, 0);
  status = vector<int>(nodeCount + 1, 0);
  int from, to;
  for (int i = 0; i < edgeCount; i++) {
    cin >> from >> to;
    graph[from].push_back({to, i});
  }
  for (int i = 1; i <= nodeCount; i++) {
    if (status[i] == 0) {
      dfs(i);
    }
  }
  cout << *max_element(ans.begin(), ans.end()) << endl;
  for (int i = 0; i < edgeCount; i++) cout << ans[i] << " ";
  cout << endl;
}
