#include <bits/stdc++.h>
using namespace std;
vector<int> g[5005];
int u[5005];
int v[5005];
int vis[5005];
bool flag = false;
void dfs(int u) {
  if (flag) return;
  vis[u] = 1;
  for (int i = 0; i < (int)g[u].size(); i++) {
    int v = g[u][i];
    if (!vis[v])
      dfs(v);
    else if (vis[v] == 1) {
      flag = true;
      return;
    }
  }
  vis[u] = 2;
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", &u[i], &v[i]);
    g[u[i]].push_back(v[i]);
  }
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      dfs(u[i]);
    }
    if (flag) break;
  }
  if (!flag) {
    cout << 1 << endl;
    cout << 1;
    for (int i = 2; i <= m; i++) {
      cout << " " << 1;
    }
    cout << endl;
  } else {
    cout << 2 << endl;
    for (int i = 1; i <= m; i++) {
      if (u[i] < v[i])
        cout << 1 << " ";
      else
        cout << 2 << " ";
    }
    cout << endl;
  }
}
