/**
n m
a1 b1
...
am bm
*/
#include <cstdio>
#include <vector>
using namespace std;
#define forn(i, n) for (int i = 0; i < (int)(n); i++)
const int N = 1e5;
vector<int> c[N], List[N];
int n, m, cc, u[N];
vector <int> p;
void dfs( int v ) { // O(V)
List[cc-1].push_back(v);
u[v] = cc;
for (auto x : c[v]) // O(E)
if (!u[x])
dfs(x);
p.push_back(v);
}
int main() {
scanf("%d%d", &n, &m);
forn(i, m) {
int a, b;
scanf("%d%d", &a, &b), a--, b--; // 0..n-1
c[a].push_back(b);
}
forn(i, n)
if (!u[i])
cc++, dfs(i);
printf("%d\n", cc);
forn(i, n)
printf("%d ", u[i]);
}