#include<bits/stdc++.h> usingnamespace std; typedeflonglong LL; structEdge { int to, id; }; constint MAX_N = 5000 + 5; vector <Edge> g[MAX_N]; int n, m, c[MAX_N]; int dfn[MAX_N], idx; int ins[MAX_N]; stack <int> stk; voiddfs(int u){ dfn[u] = ++idx; stk.push(u); ins[u] = 1; for (int i = 0; i < g[u].size(); ++i) { Edge &e = g[u][i]; if (!dfn[e.to]) { c[e.id] = 1; dfs(e.to); } elseif (ins[e.to]) { c[e.id] = 2; } } stk.pop(); ins[u] = 0; } intmain(){ scanf("%d%d", &n, &m); int u, v; for (int i = 1; i <= m; ++i) { scanf("%d%d", &u, &v); g[u].push_back({v, i}); } for (int i = 1; i <= n; ++i) { if (!dfn[i]) dfs(i); } int cnt = 1; for (int i = 1; i <= m; ++i) { if (!c[i]) c[i] = 1; if (c[i] == 2) cnt = 2; } printf("%d\n", cnt); for (int i = 1; i <= m; ++i) { printf("%d%c", c[i], i == m ? '\n' : ' '); } return0; }