#include <bits/stdc++.h>
using namespace std;
int n, m;
int col[5005];
int nrSCC, T, t, top;
vector<int> V[5005], tV[5005], DfV[5005];
map<pair<int, int>, int> EdgeId;
int disc[5005], low[5005], st[5005], vis[5005];
bool inSt[5005];
static int Path;
queue<int> Q;
int ans;
bool isPath(int u, int v) {
  while (!Q.empty()) Q.pop();
  Q.push(u);
  ++Path;
  vis[u] = Path;
  while (!Q.empty()) {
    int nod = Q.front();
    Q.pop();
    if (nod == v) return true;
    for (int adj : DfV[nod])
      if (inSt[adj] && vis[adj] != Path) {
        vis[adj] = Path;
        Q.push(adj);
      }
  }
  return false;
}
void AddEdge(int u, int v) {
  V[u].push_back(v);
  tV[v].push_back(u);
}
void dfs(int u, int par) {
  disc[u] = low[u] = ++T;
  st[++top] = u;
  inSt[u] = true;
  for (int v : V[u])
    if (!disc[v]) {
      DfV[u].push_back(v);
      dfs(v, u);
      low[u] = min(low[u], low[v]);
    } else if (inSt[v]) {
      low[u] = min(low[u], disc[v]);
    }
  for (int v : tV[u])
    if (disc[v] && isPath(u, v) &&
        EdgeId.find(pair<int, int>{v, u}) != EdgeId.end())
      col[EdgeId[pair<int, int>{v, u}] - 1]++;
  if (low[u] == disc[u]) {
    if (st[top] != u) ++nrSCC;
    while (top > 0) {
      int crt = st[top];
      --top;
      if (crt == u) break;
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; ++i) {
    int u, v;
    col[i] = 1;
    scanf("%d%d", &u, &v);
    --u;
    --v;
    AddEdge(u, v);
    EdgeId[pair<int, int>{u, v}] = i + 1;
  }
  nrSCC = 0;
  T = 0;
  for (int i = 0; i < n; ++i)
    if (!disc[i]) dfs(i, -1);
  ans = 0;
  for (int i = 0; i < m; ++i)
    if (col[i] > ans) ans = col[i];
  printf("%d\n", ans);
  for (int i = 0; i < m; ++i) printf("%d ", col[i]);
  return 0;
}
