#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m;
vector<int>p[200001];
int low[200001],num[200001],dele[200001],scc=0,cc=1;
stack<int>st;
void nhap()
{
cin>>n>>m;
for (int i=1;i<=m;i++)
{
int a,b;
cin>>a>>b;
p[a].push_back(b);
}
}
void dfs(int u)
{
low[u]=num[u]=cc++;
st.push(u);
for (int v:p[u])
{
if (dele[v]) continue;
if (!num[v])
{
dfs(v);
low[u]=min(low[u],low[v]);
}
else low[u]=min(low[u],num[v]);
}
if (low[u]==num[u])
{
scc++;
while (true)
{
int v=st.top();
st.pop();
dele[v]=scc;
if (v == u) break;
}
}
}
signed main()
{
nhap();
for (int i=1;i<=n;i++)
{
if (!num[i]) dfs(i);
}
cout<<scc<<"\n";
for (int i=1;i<=n;i++)
{
cout<<dele[i]<<" ";
}
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KI2RlZmluZSBpbnQgbG9uZyBsb25nCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CmludCBuLG07CnZlY3RvcjxpbnQ+cFsyMDAwMDFdOwppbnQgbG93WzIwMDAwMV0sbnVtWzIwMDAwMV0sZGVsZVsyMDAwMDFdLHNjYz0wLGNjPTE7CnN0YWNrPGludD5zdDsKdm9pZCBuaGFwKCkKewogICAgY2luPj5uPj5tOwogICAgZm9yIChpbnQgaT0xO2k8PW07aSsrKQogICAgewogICAgICAgIGludCBhLGI7CiAgICAgICAgY2luPj5hPj5iOwogICAgICAgIHBbYV0ucHVzaF9iYWNrKGIpOwogICAgfQp9CnZvaWQgZGZzKGludCB1KQp7CiAgICBsb3dbdV09bnVtW3VdPWNjKys7CiAgICBzdC5wdXNoKHUpOwogICAgZm9yIChpbnQgdjpwW3VdKQogICAgewogICAgICAgIGlmIChkZWxlW3ZdKSBjb250aW51ZTsKICAgICAgICBpZiAoIW51bVt2XSkKICAgICAgICB7CiAgICAgICAgICAgIGRmcyh2KTsKICAgICAgICAgICAgbG93W3VdPW1pbihsb3dbdV0sbG93W3ZdKTsKICAgICAgICB9CiAgICAgICAgZWxzZSBsb3dbdV09bWluKGxvd1t1XSxudW1bdl0pOwogICAgfQogICAgaWYgKGxvd1t1XT09bnVtW3VdKQogICAgewogICAgICAgIHNjYysrOwogICAgICAgIHdoaWxlICh0cnVlKQogICAgICAgIHsKICAgICAgICAgICAgaW50IHY9c3QudG9wKCk7CiAgICAgICAgICAgIHN0LnBvcCgpOwogICAgICAgICAgICBkZWxlW3ZdPXNjYzsKICAgICAgICAgICAgaWYgKHYgPT0gdSkgYnJlYWs7CiAgICAgICAgfQogICAgfQp9CnNpZ25lZCBtYWluKCkKewogICAgbmhhcCgpOwogICAgZm9yIChpbnQgaT0xO2k8PW47aSsrKQogICAgewogICAgICAgIGlmICghbnVtW2ldKSBkZnMoaSk7CiAgICB9CiAgICBjb3V0PDxzY2M8PCJcbiI7CiAgICBmb3IgKGludCBpPTE7aTw9bjtpKyspCiAgICB7CiAgICAgICAgY291dDw8ZGVsZVtpXTw8IiAiOwogICAgfQp9Cg==