#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e4 + 50;
struct node {
  int y;
  int id;
};
vector<node> edge[maxn];
int n, m;
int vis[maxn];
int ans;
int col[maxn];
void dfs(int u) {
  vis[u] = 1;
  for (int i = 0; i < edge[u].size(); i++) {
    int v = edge[u][i].y;
    int id = edge[u][i].id;
    if (vis[v] == 1) {
      col[id] = 2;
      ans = 2;
    } else {
      if (vis[v] == 0) dfs(v);
    }
  }
  vis[u] = 2;
  return;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int a, b;
    cin >> a >> b;
    edge[a].push_back(node{b, i});
    col[i] = 1;
  }
  ans = 1;
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) dfs(i);
  }
  cout << ans << endl;
  for (int i = 1; i <= m; i++) {
    cout << col[i] << (i == m ? '\n' : ' ');
  }
  cout << endl;
  return 0;
}
