//Collecting the dream I threw away
//Road to VOI
#include<bits/stdc++.h>
#define el '\n'
#define ll long long
#define ull unsigned long long
#define fumo ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define pb push_back
#define str string
#define sz(v) (int)v.size()
#define ldb long double
#define db double
#define reimu(v) v.begin(), v.end()
#define ret return
#define fi first
#define se second
//#define int long long
using namespace std;
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
//ᗜˬᗜ
const int N=1e5+7,base=37,block=500;
const ll mod=1e9+9,inf=1e18;
int n,m;
vector<int> adj[N];
int matchx[N],matchy[N],D[N];
int res;

bool check()
{
    queue<int> q;
    for(int u=1;u<=n;++u)
    {
        if(!matchy[u])
        {
            D[u]=0;
            q.push(u);
        }
        else D[u]=-1;
    }
    bool found=0;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(int v:adj[u])
        {
            if(!matchx[v]) found=1;
            else if(D[matchx[v]]==-1)
            {
                D[matchx[v]]=D[u]+1;
                q.push(matchx[v]);
            }
        }
    }
    ret found;
}

bool dfs(int u)
{
    for(int v:adj[u]) if(!matchx[v] || (D[matchx[v]]==D[u]+1 && dfs(matchx[v])))
    {
        matchx[v]=u;
        matchy[u]=v;
        ret 1;
    }
    D[u]=-1;
    ret 0;
}

void sol()
{
    cin>>n>>m;
    int i0,j0;
    while(cin>>i0>>j0) adj[i0].pb(j0);
    while(check())
    {
        for(int u=1;u<=n;++u) if(!matchy[u]) res+=dfs(u);
    }
    cout<<res<<el;
    for(int i=1;i<=m;++i) cout<<matchx[i]<<' ';
}

void ffopen(const str &alice)
{
    if(fopen((alice+".inp").c_str(), "r")){
        freopen((alice+".inp").c_str(), "r", stdin);
        //freopen((alice+".out").c_str(), "w", stdout);
    }
}

signed main()
{
    fumo
    ffopen("TASK");
    int t=1;
    //cin>>t;
    while(t--) sol();
    ret 0;
}
