/****************************** بسم الله الرحمن الرحيم ************************/
/*---------------------------------------------------------------------*/
#include<bits/stdc++.h>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
#define ordered_set tree<pair<int, int>, null_type,less<pair<int,int> >, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long int
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL)
#define compare bool operator <
#define main int main()
#define sf(n) scanf ("%d", &n)
#define pf(n) printf ("%d", n)
#define sfl(n) scanf ("%lld", &n)
#define pfl(n) printf ("%lld", n)
#define pfnewline() printf("\n")
#define pfspace() printf(" ")
#define mem(x,y) memset( (x), (y), sizeof(x) )
#define max3(a,b,c)   max(a , max(b,c) )
#define min3(a,b,c)  min(a, min(b,c) )
#define Ex 2.718281828
#define pi acos(-1.0)
#define toDecimal(n) fixed<< setprecision(n)
#define rep1(i, n) for(int i = 1; i<=n;i++)
#define rep(i, n) for(int i = 0; i<n;i++)
#define sfc(ch) scanf("%c",&ch)
#define sfs(ch) scanf("%[^\n]%*c", ch);

const ll mxn = 1e5+3;

/** ---8 moves --- **/
//int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1};
//int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};

/** ---4 moves--**/
//int dx[] = {-1,0,0,1};
//int dy[] = {0,-1,1,0};

using namespace __gnu_pbds;
using namespace std;

//ll lcm(ll a, ll b)  return a/__gcd(a,b)*b;
//ll bigmod(int a, ll b) { if(b == 0) return 1% mod;    ll x = bigmod(a,b/2  );    x = (x * x) % mod;    if(b % 2 == 1) x = (x*a)% mod;   return x;}
string toString(int A)
{
    stringstream ss;
    ss<<A;
    string tmp;
    ss>>tmp;
    return tmp;
}
int a[200005];
int copya[200005];
int p[200005];

int findp(int e)
{
    if(p[e] == e )
    {
        return e;
    }
    else return findp(p[e]);
}

main
{
    int n;
    cin >> n;
    rep1(i,n)
    {
        cin >> a[i];
        p[i] = i; //making n different set
        copya[i] = a[i];
    }

    int root = -1;
    for(int i = 1; i<=n;i++)
    {
        if( findp(i) != findp(a[i]) )
        {
            p[findp(i)] = findp(a[i]);
        }
        else if( findp(i) == findp(a[i]) )
        {

            if( a[i] == i && root == -1)
            {
                root = i;
            }
            a[i] = 0; // found cycle. marking the parent of ith index as the root
        }
    }
    for(int i = 1; i<=n;i++)
    {
        if(root == -1)
        {
            if( a[i] == 0 )
            {
                root = i;  ///If no self root found, then the place where the cycle stopped is considered to be the root
                break;
            }
        }
    }
    for(int i = 1; i<=n;i++)
    {
        if( a[i] == 0 )
        {
            a[i] = root; /// The places where the cycle stopped, connecting that place with the root
        }
    }
    int ans = 0;
     for(int i =1; i<=n;i++)
     {
         if( a[i] !=copya[i] )
         {
             ans++;
         }
     }
     cout << ans << endl;
     for(int i =1 ; i<= n;i++)
     {
         cout << a[i] << " ";
     }
     cout << endl;
}


#include <iostream>
#include <map>
#include <vector>
#include <cstring>
#include <utility>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <string>
#include <stack>
#include <set>
#include <deque>
#define mp make_pair
using namespace std;
int n,fa[200005],cnt,s,rot=-1;
bool used[200005],fi;
vector <int> g[200005];
vector <int> ve;
void dfs(int x,int prev){
	if (used[x] && x!=fa[x]){
		cnt++;
		fi=true;
		if (rot==-1) rot=x;
		ve.push_back(x);
		return;
	}
	if (used[x]) return;
	used[x]=true;
	for (int i=0;i<g[x].size() && !fi;i++){
		if (g[x][i]!=prev) dfs(g[x][i],x);
	}
}
int main(){
	ios::sync_with_stdio(false);
	cin>>n;
	for (int i=1;i<=n;i++){
		cin>>fa[i];
	}
	for (int i=1;i<=n;i++){
		if (i==fa[i] && rot==-1){
			rot=i;
		}else if (i==fa[i] && rot!=-1){
			ve.push_back(i);
			cnt++;
		}
		g[i].push_back(fa[i]);
		g[fa[i]].push_back(i);
	}
	for (int i=1;i<=n;i++){
		if (!used[i]){
			fi=false;
			dfs(i,0);
		}
	}
	cout<<int(ve.size())<<endl;
	for (int i=0;i<ve.size();i++) fa[ve[i]]=rot;
	for (int i=1;i<=n;i++){
		cout<<fa[i]<<' ';
	}
}


#include <iostream>
#include <map>
#include <vector>
#include <cstring>
#include <utility>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <string>
#include <stack>
#include <set>
#include <deque>
#define mp make_pair
using namespace std;
int n,fa[200005],cnt,s,rot=-1;
bool used[200005],fi;
vector <int> g[200005];
vector <int> ve;
void dfs(int x,int prev){
	if (used[x] && x!=fa[x]){
		cnt++;
		fi=true;
		ve.push_back(x);
		return;
	}
	if (used[x]) return;
	used[x]=true;
	for (int i=0;i<g[x].size() && !fi;i++){
		if (g[x][i]!=prev) dfs(g[x][i],x);
	}
}
int main(){
	ios::sync_with_stdio(false);
	cin>>n;
	for (int i=1;i<=n;i++){
		cin>>fa[i];
	}
	for (int i=1;i<=n;i++){
		if (i==fa[i] && rot==-1){
			rot=i;
		}else if (i==fa[i] && rot!=-1){
			ve.push_back(i);
			cnt++;
		}
		g[i].push_back(fa[i]);
		g[fa[i]].push_back(i);
	}
	for (int i=1;i<=n;i++){
		if (!used[i]){
			fi=false;
			dfs(i,0);
		}
	}
	cout<<int(ve.size())<<endl;
	for (int i=0;i<ve.size();i++) fa[ve[i]]=rot;
	for (int i=1;i<=n;i++){
		cout<<fa[i]<<' ';
	}
}


#include <iostream>
#include <map>
#include <vector>
#include <cstring>
#include <utility>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <string>
#include <stack>
#include <set>
#include <deque>
#define mp make_pair
using namespace std;
int n,fa[200005],cnt,s,rot=-1;
bool used[200005],fi;
vector <int> g[200005];
vector <int> ve;
void dfs(int x,int prev){
	if (used[x] && x!=fa[x]){
		cnt++;
		fi=true;
		ve.push_back(x);
		return;
	}
	if (used[x]) return;
	used[x]=true;
	for (int i=0;i<g[x].size() && !fi;i++){
		if (g[x][i]!=prev) dfs(g[x][i],x);
	}
}
int main(){
	ios::sync_with_stdio(false);
	cin>>n;
	for (int i=1;i<=n;i++){
		cin>>fa[i];
	}
	for (int i=1;i<=n;i++){
		if (i==fa[i] && rot==-1){
			rot=i;
		}else if (i==fa[i] && rot!=-1){
			ve.push_back(i);
			cnt++;
		}
		g[i].push_back(fa[i]);
		g[fa[i]].push_back(i);
	}
	if (rot==-1) rot=1;
	for (int i=1;i<=n;i++){
		if (!used[i]){
			fi=false;
			dfs(i,0);
		}
	}
	cout<<int(ve.size())<<endl;
	for (int i=0;i<ve.size();i++) fa[ve[i]]=rot;
	for (int i=1;i<=n;i++){
		cout<<fa[i]<<' ';
	}
}


#include <iostream>
#include <map>
#include <vector>
#include <cstring>
#include <utility>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <string>
#include <stack>
#include <set>
#include <deque>
#define mp make_pair
using namespace std;
int fa[200005],nf[200005],rot=-1,ans,n;
int gf(int x){
	if (nf[x]==x) return x; else return nf[x]=gf(nf[x]);
}
bool uni(int x,int y){
	x=gf(x);
	y=gf(y);
	if (x==y){
		return false;
	}else{
		nf[x]=y;
		return true;
	}
}
int main(){
	ios::sync_with_stdio(false);
	cin>>n;
	for (int i=1;i<=n;i++) cin>>fa[i];
	for (int i=1;i<=n;i++){
		if (fa[i]==i){
			ans=-1;
			rot=i;
			break;
		}
	}
	for  (int i=1;i<=n;i++) nf[i]=i;
	for (int i=1;i<=n;i++){
		if (!uni(i,fa[i])){
			if (rot==-1){
				rot=i;
				ans++;
			}else{
				fa[i]=rot;
				ans++;
			}
		}
	}
	cout<<ans<<endl;
	for (int i=1;i<=n;i++) cout<<fa[i]<<' ';
	return 0;
}


#include <iostream>
#include <map>
#include <vector>
#include <cstring>
#include <utility>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <string>
#include <stack>
#include <set>
#include <deque>
#define mp make_pair
using namespace std;
int fa[200005],nf[200005],rot=-1,ans,n;
int gf(int x){
	if (nf[x]==x) return x; else return nf[x]=gf(nf[x]);
}
bool uni(int x,int y){
	x=gf(x);
	y=gf(y);
	if (x==y){
		return false;
	}else{
		nf[x]=y;
		return true;
	}
}
int main(){
	ios::sync_with_stdio(false);
	cin>>n;
	for (int i=1;i<=n;i++) cin>>fa[i];
	for (int i=1;i<=n;i++){
		if (fa[i]==i){
			ans=-1;
			rot=i;
			break;
		}
	}
	for  (int i=1;i<=n;i++) nf[i]=i;
	for (int i=1;i<=n;i++){
		if (!uni(i,fa[i])){
			if (rot==-1){
				rot=i;
				ans++;
				fa[i]=rot;
			}else{
				fa[i]=rot;
				ans++;
			}
		}
	}
	cout<<ans<<endl;
	for (int i=1;i<=n;i++) cout<<fa[i]<<' ';
	return 0;
}


#include <iostream>
#include <map>
#include <vector>
#include <cstring>
#include <utility>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <string>
#include <stack>
#include <set>
#include <deque>
#define mp make_pair
using namespace std;
int n,fa[200005],col[200005],cnt,s,rot=-1;
bool used[200005],fi;
vector <int> ve[200005];
int dfs(int x){
	if (used[x] && !col[x]) col[x]=x;
	if (col[x]) return col[x];
	used[x]=true;
	return col[x]=dfs(fa[x]);
}
int main(){
	ios::sync_with_stdio(false);
	cin>>n;
	for (int i=1;i<=n;i++) cin>>fa[i];
	for (int i=1;i<=n;i++){
		ve[i].push_back(fa[i]);
		ve[fa[i]].push_back(i);
	}
	for (int i=1;i<=n;i++) if (!used[i]) dfs(i);
	for (int i=1;i<=n;i++){
		if (col[i]==i){
			if (fa[i]==i) rot=i;
		}
	}
	if (rot==-1){
		for (int i=1;i<=n;i++){
			if (col[i]==i && fa[i]!=i){
				cnt++;
				rot=i;
				fa[i]=i;
				break;
			}
		}
	}
	for (int i=1;i<=n;i++){
		if (i!=rot && col[i]==i){
			fa[i]=rot;
			cnt++;
		}
	}
	cout<<cnt<<endl;
	for (int i=1;i<=n;i++) cout<<fa[i]<<' ';
	return 0;
}


#include<bits/stdc++.h>
#define ull unsigned long long
#define ll long long int
#define pb push_back
#define f first
#define s second
#define len length
#define mk make_pair
#define inf  1e12
#define mini -1
#define MOD 1000000007
#define all(v) (v).begin(),(v).end()
#define mset(v,x) memset(v,x,sizeof(v))
#define MODSET(d) if ((d) >= MOD) d %= MOD;
#define inp(x) (scanf("%d",&x))
#define inp2(x,y) (scanf("%d%d",&x,&y))
#define inp3(x,y,z) (scanf("%d%d%d",&x,&y,&z))
#define inp4(x,y,z,w) (scanf("%d%d%d%d",&x,&y,&z,&w))
#define inpl(d) scanf("%lld",&d)
#define inpl2(a,b) scanf("%lld%lld",&a,&b)
#define inpl3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define inpl4(a,b,c,d) scanf("%lld%lld%lld%lld",&a,&b,&c,&d)
using namespace std;
vector<ll >v[200005];
//pair<int ,int>p[100005];
//set<ll >ans,st2;
//stack<int >st;
//priority_queue< pair <int ,int > >pq;
//vector<ll >ans;
//set<ll >st[5];
ll par[1000005],arr[1000005];
bool vis[1000005],inDfstree[1000005];
//map<ll ,ll >mp;
//map<int ,int >::iterator it,it2;
//set<ll >::iterator it,it2;
//vector< pair <int ,pair<int ,int > > >vpp;
//vector<pair <ll,ll > >vp;
//vector< pair < int,int > >v[200005],adj2[100005];
/********************************************* YOUR CODE GOES FROM HERE ************************************************/
int root=-1;
int dfs(int u)
{
    vis[u]=true;
    par[u]=arr[u];
    inDfstree[u]=true;
    if(v[u].size()==0&&par[u]==u)
        par[u]=root;
    for(int i=0;i<v[u].size();i++)
    {
        if(!vis[v[u][i]])
            dfs(v[u][i]);
        else
        {
            if(inDfstree[v[u][i]])
            {
                if(root==-1)
                    root=v[u][i];
                par[v[u][i]]=root;
            }
        }
    }
    inDfstree[u]=false;
}
int main()
{
    int n,x,ans=0;
    inp(n);
    for(int i=1;i<=n;i++)
    {
        inp(x);
        v[x].pb(i);
        arr[i]=x;
        if(i==x)
          root=i;
    }
    mset(vis,false);
    for(int i=1;i<=n;i++)
    {
        if(!vis[i])
            dfs(i);
    }
   for(int i=1;i<=n;i++)
   {
       if(par[i]!=arr[i])
        ans++;
   }
   cout<<ans<<"\n";
   for(int i=1;i<=n;i++)
    cout<<par[i]<<" ";
}


#include<bits/stdc++.h>
#define ull unsigned long long
#define ll long long int
#define pb push_back
#define f first
#define s second
#define len length
#define mk make_pair
#define inf  1e12
#define mini -1
#define MOD 1000000007
#define all(v) (v).begin(),(v).end()
#define mset(v,x) memset(v,x,sizeof(v))
#define MODSET(d) if ((d) >= MOD) d %= MOD;
#define inp(x) (scanf("%d",&x))
#define inp2(x,y) (scanf("%d%d",&x,&y))
#define inp3(x,y,z) (scanf("%d%d%d",&x,&y,&z))
#define inp4(x,y,z,w) (scanf("%d%d%d%d",&x,&y,&z,&w))
#define inpl(d) scanf("%lld",&d)
#define inpl2(a,b) scanf("%lld%lld",&a,&b)
#define inpl3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define inpl4(a,b,c,d) scanf("%lld%lld%lld%lld",&a,&b,&c,&d)
using namespace std;
vector<ll >v[100005];
//pair<int ,int>p[100005];
//set<ll >ans,st2;
//stack<int >st;
//priority_queue< pair <int ,int > >pq;
//vector<ll >ans;
//set<ll >st[5];
ll par[1000005],arr[1000005];
bool vis[1000005],inDfstree[1000005];
//map<ll ,ll >mp;
//map<int ,int >::iterator it,it2;
//set<ll >::iterator it,it2;
//vector< pair <int ,pair<int ,int > > >vpp;
//vector<pair <ll,ll > >vp;
//vector< pair < int,int > >v[200005],adj2[100005];
/********************************************* YOUR CODE GOES FROM HERE ************************************************/
int root=-1;
int dfs(int u)
{
    vis[u]=true;
    par[u]=arr[u];
    inDfstree[u]=true;
    if(v[u].size()==0&&par[u]==u)
        par[u]=root;
    for(int i=0;i<v[u].size();i++)
    {
        if(!vis[v[u][i]])
            dfs(v[u][i]);
        else
        {
            if(inDfstree[v[u][i]])
            {
                if(root==-1)
                    root=v[u][i];
                par[v[u][i]]=root;
            }
        }
    }
    inDfstree[u]=false;
}
int main()
{
    int n,x,ans=0;
    inp(n);
    for(int i=1;i<=n;i++)
    {
        inp(x);
        v[x].pb(i);
        arr[i]=x;
        if(i==x)
          root=i;
    }
    mset(vis,false);
    for(int i=1;i<=n;i++)
    {
        if(!vis[i])
            dfs(i);
    }
   for(int i=1;i<=n;i++)
   {
       if(par[i]!=arr[i])
        ans++;
   }
   cout<<ans<<"\n";
   for(int i=1;i<=n;i++)
    cout<<par[i]<<" ";
}


#include<bits/stdc++.h>
#define ull unsigned long long
#define ll long long int
#define pb push_back
#define f first
#define s second
#define len length
#define mk make_pair
#define inf  1e12
#define mini -1
#define MOD 1000000007
#define all(v) (v).begin(),(v).end()
#define mset(v,x) memset(v,x,sizeof(v))
#define MODSET(d) if ((d) >= MOD) d %= MOD;
#define inp(x) (scanf("%d",&x))
#define inp2(x,y) (scanf("%d%d",&x,&y))
#define inp3(x,y,z) (scanf("%d%d%d",&x,&y,&z))
#define inp4(x,y,z,w) (scanf("%d%d%d%d",&x,&y,&z,&w))
#define inpl(d) scanf("%lld",&d)
#define inpl2(a,b) scanf("%lld%lld",&a,&b)
#define inpl3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define inpl4(a,b,c,d) scanf("%lld%lld%lld%lld",&a,&b,&c,&d)
using namespace std;
vector<ll >v[100005];
//pair<int ,int>p[100005];
//set<ll >ans,st2;
//stack<int >st;
//priority_queue< pair <int ,int > >pq;
//vector<ll >ans;
//set<ll >st[5];
ll par[1000005],arr[1000005];
bool vis[1000005],inDfstree[1000005];
//map<ll ,ll >mp;
//map<int ,int >::iterator it,it2;
//set<ll >::iterator it,it2;
//vector< pair <int ,pair<int ,int > > >vpp;
//vector<pair <ll,ll > >vp;
//vector< pair < int,int > >v[200005],adj2[100005];
/********************************************* YOUR CODE GOES FROM HERE ************************************************/
int root=-1;
int dfs(int u)
{
    vis[u]=true;
    par[u]=arr[u];
    inDfstree[u]=true;
    if(v[u].size()==0&&par[u]==u)
        par[u]=root;
    for(int i=0;i<v[u].size();i++)
    {
        if(!vis[v[u][i]])
            dfs(v[u][i]);
        else
        {
            if(inDfstree[v[u][i]])
            {
                if(root==-1)
                    root=v[u][i];
                else
                    par[v[u][i]]=root;
            }
        }
        inDfstree[u]=false;
    }
}
int main()
{
    int n,x,ans=0;
    inp(n);
    for(int i=1;i<=n;i++)
    {
        inp(x);
        v[x].pb(i);
        arr[i]=x;
        if(i==x)
          root=i;
    }
    mset(vis,false);
    for(int i=1;i<=n;i++)
    {
        if(!vis[i])
            dfs(i);
    }
   for(int i=1;i<=n;i++)
   {
       if(par[i]!=arr[i])
        ans++;
   }
   cout<<ans<<"\n";
   for(int i=1;i<=n;i++)
    cout<<par[i]<<" ";
}


#include <bits/stdc++.h>

using namespace std;

#define int long long

struct _{_(){ ios_base::sync_with_stdio(0); cin.tie(0); }}_;

const int base = 311, mod = 1e9, N = 1e6 + 11, inf = INT_MAX;

int n, root = 0, cnt = 0, a[N], up[N];

int union_find(int u) {
	if(up[u] == u) {
		return u;
	} else {
		return up[u] = union_find(up[u]);
	}
}

void union_merge(int u, int v) {
	up[union_find(u)] = union_find(v);
}

signed main() {
	cin >> n;
	for(int i = 1; i <= n; i++) {
		up[i] = i;
	}
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
		union_merge(i, a[i]);
	}
	for(int i = 1; i <= n; i++) {
		if(union_find(i) == i) {
			if(a[i] == i) {
				root = i;
			}
			cnt++;
		}
	}
	if(root) {
		cout << cnt - 1 << endl;
	} else {
		cout << cnt << endl;
		root = 1;
	}
	for(int i = 1; i <= n; i++) {
		if(union_find(i) == i) {
			cout << root;
		} else {
			cout << a[i];
		}
		cout << ' ';
	}
}


#include <bits/stdc++.h>

using namespace std;

#define int long long

struct _{_(){ ios_base::sync_with_stdio(0); cin.tie(0); }}_;

const int base = 311, mod = 1e9, N = 1e6 + 11, inf = INT_MAX;

int n, root = 0, cnt = 0, a[N], up[N];

int union_find(int u) {
	if(up[u] == u) {
		return u;
	} else {
		return up[u] = union_find(up[u]);
	}
}

void union_merge(int u, int v) {
	up[union_find(u)] = union_find(v);
}

signed main() {
	cin >> n;
	for(int i = 1; i <= n; i++) {
		up[i] = i;
	}
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
		union_merge(i, a[i]);
	}
	for(int i = 1; i <= n; i++) {
		if(union_find(i) == i) {
			if(a[i] == i) {
				root = i;
			}
			cnt++;
		}
	}
	if(root) {
		cout << cnt - 1 << endl;
	} else {
		cout << cnt << endl;
		root = union_find(1);
	}
	for(int i = 1; i <= n; i++) {
		if(union_find(i) == i) {
			cout << root;
		} else {
			cout << a[i];
		}
		cout << ' ';
	}
}


#include <bits/stdc++.h>

using namespace std;

#define int long long

struct _{_(){ ios_base::sync_with_stdio(0); cin.tie(0); }}_;

const int base = 311, mod = 1e9, N = 1e6 + 11, inf = INT_MAX;

int n, root = 0, cnt = 0, a[N], up[N];

int union_find(int u) {
	if(up[u] == u) {
		return u;
	} else {
		return up[u] = union_find(up[u]);
	}
}

void union_merge(int u, int v) {
	up[union_find(u)] = union_find(v);
}

signed main() {
	cin >> n;
	for(int i = 1; i <= n; i++) {
		up[i] = i;
	}
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
		union_merge(i, a[i]);
	}
	for(int i = 1; i <= n; i++) {
		if(union_find(i) == i) {
			if(a[i] == i) {
				root = i;
			}
			cnt++;
		}
	}
	if(root) {
		cout << cnt << endl;
	} else {
		cout << cnt - 1 << endl;
		root = 1;
	}
	for(int i = 1; i <= n; i++) {
		if(union_find(i) == i) {
			cout << root;
		} else {
			cout << a[i];
		}
		cout << ' ';
	}
}


#include <bits/stdc++.h>

using namespace std;

#define int long long

struct _{_(){ ios_base::sync_with_stdio(0); cin.tie(0); }}_;

const int base = 311, mod = 1e9, N = 1e6 + 11, inf = INT_MAX;

int n, root = 0, cnt = 1, a[N], up[N];

int union_find(int u) {
	if(up[u] == u) {
		return u;
	} else {
		return up[u] = union_find(up[u]);
	}
}

void union_merge(int u, int v) {
	up[union_find(u)] = union_find(v);
}

signed main() {
	cin >> n;
	for(int i = 1; i <= n; i++) {
		up[i] = i;
	}
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
		union_merge(i, a[i]);
	}
	for(int i = 1; i <= n; i++) {
		if(union_find(i) == i) {
			if(a[i] == i) {
				root = i;
			}
			cnt++;
		}
	}
	if(root) {
		cout << cnt << endl;
	} else {
		cout << cnt - 1 << endl;
		root = 1;
	}
	for(int i = 1; i <= n; i++) {
		if(union_find(i) == i) {
			cout << root;
		} else {
			cout << a[i];
		}
		cout << ' ';
	}
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int maxn = 200010;

vector <int> e[maxn];

int n, m, f[maxn], v[maxn], root[maxn];

void dfs1(int x) {
	v[x] = 1;
	for(int i = 0;i < e[x].size();i ++)
		if(e[x][i] != x) dfs1(e[x][i]);
}

int dfs2(int x, int t, int y = 0) {
	if(v[x] == t) return 1;
	else if(v[x] != 0 && v[x] != t) return 0;
	v[x] = t;
	for(int i = 0;i < e[x].size();i ++)
		y |= dfs2(e[x][i], t);
	return y;
}

int main() {
	ios::sync_with_stdio(false);
	cin >> n;
	for(int i = 1;i <= n;i ++) cin >> f[i], e[f[i]].push_back(i);
	for(int i = 1;i <= n;i ++)
		if(f[i] == i)
			dfs1(i), root[++ root[0]] = i, m = 1;
	for(int i = 1;i <= n;i ++)
		if(!v[i] && dfs2(i, i))
			root[++ root[0]] = i;
	if(m) {
		cout << root[0] - 1 << endl;
		for(int i = 2;i <= root[0];i ++)
			f[root[i]] = root[1];
		for(int i = 1;i <= n;i ++)
			cout << f[i] << " ";
	}
	else {
		cout << root[0] << endl;
		for(int i = 1;i <= root[0];i ++)
			f[root[i]] = root[1];
		for(int i = 1;i <= n;i ++)
			cout << f[i] << " ";
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int maxn = 200010;

vector <int> e[maxn];

int n, f[maxn], v[maxn], root[maxn];

void dfs1(int x) {
	v[x] = 1;
	for(int i = 0;i < e[x].size();i ++)
		if(e[x][i] != x) dfs1(e[x][i]);
}

int dfs2(int x, int y = 0) {
	if(v[x]) return 1;
	v[x] = 1;
	for(int i = 0;i < e[x].size();i ++)
		y |= dfs2(e[x][i]);
	return y;
}

int main() {
	ios::sync_with_stdio(false);
	cin >> n;
	for(int i = 1;i <= n;i ++) cin >> f[i], e[f[i]].push_back(i);
	for(int i = 1;i <= n;i ++)
		if(f[i] == i)
			dfs1(i), root[++ root[0]] = i;
	for(int i = 1;i <= n;i ++)
		if(!v[i] && dfs2(i))
			root[++ root[0]] = i;
	printf("%d\n", root[0] - 1);
	for(int i = 2;i <= root[0];i ++)
		f[root[i]] = root[1];
	for(int i = 1;i <= n;i ++)
		cout << f[i] << " ";
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int maxn = 200010;

vector <int> e[maxn];

int n, m, f[maxn], v[maxn], root[maxn];

void dfs1(int x) {
	v[x] = 1;
	for(int i = 0;i < e[x].size();i ++)
		if(e[x][i] != x) dfs1(e[x][i]);
}

int dfs2(int x, int y = 0) {
	if(v[x]) return 1;
	v[x] = 1;
	for(int i = 0;i < e[x].size();i ++)
		y |= dfs2(e[x][i]);
	return y;
}

int main() {
	ios::sync_with_stdio(false);
	cin >> n;
	for(int i = 1;i <= n;i ++) cin >> f[i], e[f[i]].push_back(i);
	for(int i = 1;i <= n;i ++)
		if(f[i] == i)
			dfs1(i), root[++ root[0]] = i, m = 1;
	for(int i = 1;i <= n;i ++)
		if(!v[i] && dfs2(i))
			root[++ root[0]] = i;
	if(m) {
		cout << root[0] - 1 << endl;
		for(int i = 2;i <= root[0];i ++)
			f[root[i]] = root[1];
		for(int i = 1;i <= n;i ++)
			cout << f[i] << " ";
	}
	else {
		cout << root[0] << endl;
		for(int i = 1;i <= root[0];i ++)
			f[root[i]] = root[1];
		for(int i = 1;i <= n;i ++)
			cout << f[i] << " ";
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int maxn = 200010;

vector <int> e[maxn];

int n, f[maxn], v[maxn], root[maxn];

void dfs1(int x) {
	v[x] = 1;
	for(int i = 0;i < e[x].size();i ++)
		if(e[x][i] != x) dfs1(e[x][i]);
}

int dfs2(int x, int y = 0) {
	if(v[x]) return 1;
	v[x] = 1;
	for(int i = 0;i < e[x].size();i ++)
		y |= dfs2(e[x][i]);
	return y;
}

int main() {
	ios::sync_with_stdio(false);
	cin >> n;
	for(int i = 1;i <= n;i ++) cin >> f[i], e[f[i]].push_back(i);
	for(int i = 1;i <= n;i ++)
		if(f[i] == i)
			dfs1(i), root[++ root[0]] = i;
	for(int i = 1;i <= n;i ++)
		if(!v[i] && dfs2(i))
			root[++ root[0]] = i;
	cout << root[0] - 1 << endl;
	for(int i = 2;i <= root[0];i ++)
		f[root[i]] = root[1];
	for(int i = 1;i <= n;i ++)
		cout << f[i] << " ";
	return 0;
}


#include<bits/stdc++.h>
//#define int long long int
#define ld long double
#define getFaster ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define rep(i,init,n) for(int i=init;i<n;i++)
#define rev(i,n,init) for(int i=n;i>=init;i--)
#define MAXN 200005
#define MOD2 998244353
#define MOD1 1e9+7
#define f first
#define s second
#define endl '\n'
#define pii pair<int,int>
#define piii pair<pii,int>
#define precise(i) cout<<fixed<<setprecision(i)
#define codejam cout<<"Case #"<<ii+1<<": ";
#define impossible cout<<"IMPOSSIBLE"<<endl;
#define error(s) throw runtime_error(s)
using namespace std;
const long double PI = atan(1.0)*4;
const int64_t INF=8e18;
const int32_t LOG=21;
const int MOD=MOD1;

int parent[200005];
int find(int v)
{
    if(parent[v]<0)
    return v;
    else
    return find(parent[v]);
}
bool cycle(int v1,int v2)
{
    int parent1=find(v1);
    int parent2=find(v2);
    if(parent2==parent1)
    return true;
    else
    return false;
}
void merge(int v1,int v2)
{
   int parent1=find(v1);
   int parent2=find(v2);
   int temp1=parent[parent1];
   int temp2=parent[parent2];
   if(temp1<=temp2)
   {
     parent[parent2]=parent1;
     parent[v1]=parent1;
     parent[v2]=parent1;
     parent[parent1]=temp1+temp2;
   }
   else
   {
     parent[parent1]=parent2;
     parent[v1]=parent2;
     parent[v2]=parent2;
     parent[parent2]=temp1+temp2;
   }
}

vector<int> ans;
int a[200005];
int iteration[200005];
bool check[200005];
bool changed[200005];
vector<int> v[200005];

int cost=0;
int cur=-1;

void dfs(int i,int val)
{
      iteration[i]=val;
      check[i]=true;
      for(auto it:v[i])
      {
          if(check[it] && iteration[it]==val)
          {
               a[i]=i;
               changed[i]=true;
               cost++;
          }
          else
          dfs(it,val);
      }
}

int32_t main()
{
      getFaster;
      int tests=1;
      //cin>>tests;
      rep(ii,0,tests)
      {
          fill(parent,parent+200002,-1);
          int n;
          cin>>n;
          rep(i,1,n+1)cin>>a[i];
          rep(i,1,n+1)
          {
              if(a[i]!=i)
              v[a[i]].push_back(i);
          }

          int lol=0;
          rep(i,1,n+1)if(!check[i])dfs(i,++lol);

          rep(i,1,n+1)
          {
              if(!cycle(i,a[i]))
               merge(i,a[i]);
          }

          rep(i,1,n+1)
          {
               if(a[i]==i && !changed[i])
               {
                   if(cur==-1)
                   cur=find(i);
               }

          }

          rep(i,1,n+1)
          {
               if(a[i]==i)
               {
                  if(cur==-1)
                   cur=find(i);
               }
          }

          rep(i,1,n+1)
          {
              if(a[i]==i)
              {
                if(cur==-1)
                cur=find(i);
              }
          }

          rep(i,1,n+1)
          {
              int x=find(i);
              if(a[i]==i && x!=cur)
              {
                  if(!changed[i])
                  cost++;
                  ans.push_back(cur);
              }
              else
              ans.push_back(a[i]);
          }
          cout<<cost<<endl;
          for(auto it:ans)cout<<it<<" ";
          cout<<endl;
      }
      return 0;
}

/*
 TIPS:
 1.) Check for corner cases (like 0 is exception case)
 2.) Check for global memory sizes,memory refreshments and its complexity
 3.) Check for sorting criteria (if used 1-based indexing)
 4.) Check for numbers overflow (while multiplying and doing MOD)
 5.) Check for typecasting issues like int to float( while dividing numbers)
*/


#include<bits/stdc++.h>
//#define int long long int
#define ld long double
#define getFaster ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define rep(i,init,n) for(int i=init;i<n;i++)
#define rev(i,n,init) for(int i=n;i>=init;i--)
#define MAXN 200005
#define MOD2 998244353
#define MOD1 1e9+7
#define f first
#define s second
#define endl '\n'
#define pii pair<int,int>
#define piii pair<pii,int>
#define precise(i) cout<<fixed<<setprecision(i)
#define codejam cout<<"Case #"<<ii+1<<": ";
#define impossible cout<<"IMPOSSIBLE"<<endl;
#define error(s) throw runtime_error(s)
using namespace std;
const long double PI = atan(1.0)*4;
const int64_t INF=8e18;
const int32_t LOG=21;
const int MOD=MOD1;

int parent[200005];
int find(int v)
{
    if(parent[v]<0)
    return v;
    else
    return find(parent[v]);
}
bool cycle(int v1,int v2)
{
    int parent1=find(v1);
    int parent2=find(v2);
    if(parent2==parent1)
    return true;
    else
    return false;
}
void merge(int v1,int v2)
{
   int parent1=find(v1);
   int parent2=find(v2);
   int temp1=parent[parent1];
   int temp2=parent[parent2];
   if(temp1<=temp2)
   {
     parent[parent2]=parent1;
     parent[v1]=parent1;
     parent[v2]=parent1;
     parent[parent1]=temp1+temp2;
   }
   else
   {
     parent[parent1]=parent2;
     parent[v1]=parent2;
     parent[v2]=parent2;
     parent[parent2]=temp1+temp2;
   }
}

vector<int> vals;
int a[200005];
int32_t main()
{
      getFaster;
      int tests=1;
      //cin>>tests;
      rep(ii,0,tests)
      {
          fill(parent,parent+200002,-1);
          int n;
          cin>>n;
          rep(i,1,n+1)cin>>a[i];
          rep(i,1,n+1)
          {
              if(!cycle(i,a[i]))
                merge(i,a[i]);
          }
          rep(i,1,n+1)if(parent[i]<0)vals.push_back(i);
          int siz=vals.size();
          cout<<siz-1<<endl;
          rep(i,1,n+1)
          {
              int x=find(i);
              if(a[i]==i && x!=vals[0])
              cout<<vals[0]<<" ";
              else
              cout<<a[i]<<" ";
          }
          cout<<endl;
      }
      return 0;
}

/*
 TIPS:
 1.) Check for corner cases (like 0 is exception case)
 2.) Check for global memory sizes,memory refreshments and its complexity
 3.) Check for sorting criteria (if used 1-based indexing)
 4.) Check for numbers overflow (while multiplying and doing MOD)
 5.) Check for typecasting issues like int to float( while dividing numbers)
*/


#include<bits/stdc++.h>
//#define int long long int
#define ld long double
#define getFaster ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define rep(i,init,n) for(int i=init;i<n;i++)
#define rev(i,n,init) for(int i=n;i>=init;i--)
#define MAXN 200005
#define MOD2 998244353
#define MOD1 1e9+7
#define f first
#define s second
#define endl '\n'
#define pii pair<int,int>
#define piii pair<pii,int>
#define precise(i) cout<<fixed<<setprecision(i)
#define codejam cout<<"Case #"<<ii+1<<": ";
#define impossible cout<<"IMPOSSIBLE"<<endl;
#define error(s) throw runtime_error(s)
using namespace std;
const long double PI = atan(1.0)*4;
const int64_t INF=8e18;
const int32_t LOG=21;
const int MOD=MOD1;

int parent[200005];
int find(int v)
{
    if(parent[v]<0)
    return v;
    else
    return find(parent[v]);
}
bool cycle(int v1,int v2)
{
    int parent1=find(v1);
    int parent2=find(v2);
    if(parent2==parent1)
    return true;
    else
    return false;
}
void merge(int v1,int v2)
{
   int parent1=find(v1);
   int parent2=find(v2);
   int temp1=parent[parent1];
   int temp2=parent[parent2];
   if(temp1<=temp2)
   {
     parent[parent2]=parent1;
     parent[v1]=parent1;
     parent[v2]=parent1;
     parent[parent1]=temp1+temp2;
   }
   else
   {
     parent[parent1]=parent2;
     parent[v1]=parent2;
     parent[v2]=parent2;
     parent[parent2]=temp1+temp2;
   }
}

vector<int> ans;
int a[200005];
bool check[200005];
bool changed[200005];
int32_t main()
{
      getFaster;
      int tests=1;
      //cin>>tests;
      rep(ii,0,tests)
      {
          fill(parent,parent+200002,-1);
          int n;
          cin>>n;
          rep(i,1,n+1)cin>>a[i];
          rep(i,1,n+1)
          {
              if(!cycle(i,a[i]))
                merge(i,a[i]);
          }
          int cost=0;
          int cur=-1;
          rep(i,1,n+1)
          {
               if(a[i]==i && !check[find(i)])
               {
                   check[find(i)]=true;
                   if(cur==-1)
                   cur=find(i);
               }

          }
          rep(i,1,n+1)
          {
               if(!check[find(i)])
               {
                    check[find(i)]=true;
                    changed[find(i)]=true;
                    if(cur==-1)
                    cur=find(i);
                    a[i]=i;
                    cost++;
               }
          }
          rep(i,1,n+1)
          {
              int x=find(i);
              if(a[i]==i && x!=cur)
              {
                  if(!changed[x])
                  cost++;
                  ans.push_back(cur);
              }
              else
              ans.push_back(a[i]);
          }
          cout<<cost<<endl;
          for(auto it:ans)cout<<it<<" ";
          cout<<endl;
      }
      return 0;
}

/*
 TIPS:
 1.) Check for corner cases (like 0 is exception case)
 2.) Check for global memory sizes,memory refreshments and its complexity
 3.) Check for sorting criteria (if used 1-based indexing)
 4.) Check for numbers overflow (while multiplying and doing MOD)
 5.) Check for typecasting issues like int to float( while dividing numbers)
*/


#include<bits/stdc++.h>
//#define int long long int
#define ld long double
#define getFaster ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define rep(i,init,n) for(int i=init;i<n;i++)
#define rev(i,n,init) for(int i=n;i>=init;i--)
#define MAXN 200005
#define MOD2 998244353
#define MOD1 1e9+7
#define f first
#define s second
#define endl '\n'
#define pii pair<int,int>
#define piii pair<pii,int>
#define precise(i) cout<<fixed<<setprecision(i)
#define codejam cout<<"Case #"<<ii+1<<": ";
#define impossible cout<<"IMPOSSIBLE"<<endl;
#define error(s) throw runtime_error(s)
using namespace std;
const long double PI = atan(1.0)*4;
const int64_t INF=8e18;
const int32_t LOG=21;
const int MOD=MOD1;

int parent[200005];
int find(int v)
{
    if(parent[v]<0)
    return v;
    else
    return find(parent[v]);
}
bool cycle(int v1,int v2)
{
    int parent1=find(v1);
    int parent2=find(v2);
    if(parent2==parent1)
    return true;
    else
    return false;
}
void merge(int v1,int v2)
{
   int parent1=find(v1);
   int parent2=find(v2);
   int temp1=parent[parent1];
   int temp2=parent[parent2];
   if(temp1<=temp2)
   {
     parent[parent2]=parent1;
     parent[v1]=parent1;
     parent[v2]=parent1;
     parent[parent1]=temp1+temp2;
   }
   else
   {
     parent[parent1]=parent2;
     parent[v1]=parent2;
     parent[v2]=parent2;
     parent[parent2]=temp1+temp2;
   }
}

vector<int> vals;
vector<int> ans;
int a[200005];
int32_t main()
{
      getFaster;
      int tests=1;
      //cin>>tests;
      rep(ii,0,tests)
      {
          fill(parent,parent+200002,-1);
          int n;
          cin>>n;
          rep(i,1,n+1)cin>>a[i];
          rep(i,1,n+1)
          {
              if(!cycle(i,a[i]))
                merge(i,a[i]);
          }
          rep(i,1,n+1)if(parent[i]<0)vals.push_back(i);
          int siz=vals.size();
          cout<<siz-1<<endl;
          rep(i,1,n+1)
          {
              int x=find(i);
              if(a[i]==i && x!=vals[0])
              ans.push_back(vals[0]);
              else
              ans.push_back(a[i]);
          }
          int cnt=0;
          rep(i,1,n+1)if(ans[i-1]==i)cnt++;
          if(cnt==0)ans[0]=1;
          for(auto it:ans)cout<<it<<" ";
          cout<<endl;
      }
      return 0;
}

/*
 TIPS:
 1.) Check for corner cases (like 0 is exception case)
 2.) Check for global memory sizes,memory refreshments and its complexity
 3.) Check for sorting criteria (if used 1-based indexing)
 4.) Check for numbers overflow (while multiplying and doing MOD)
 5.) Check for typecasting issues like int to float( while dividing numbers)
*/


#include <bits/stdc++.h>

// limit
#define oo 2000000007
#define OO 10000000000000007LL
#define maxN 10000007

// loop
#define fto(i, x, y) for(int i = (x); i <= (y); ++i)
#define fdto(i, x, y) for(int i = (x); i >= (y); --i)
#define ftoa(i, x, y, a) for(int i = (x); i <= (y); i += a)
#define fdtoa(i, x, y, a) for(int i = (x); i >= (y); i -= a)
#define ftosqrt(i, x, y) for(int i = (x); i*i <= (y); ++i)
#define ftoit(it, var) for (__typeof(var.begin()) it = var.begin(); it != var.end(); ++it)
#define fdtoit(rit, var) for (__typeof(var.rbegin()) rit = var.rbegin(); rit != var.rend(); ++rit)

// debug
#define debug cout << "*" << endl;
#define bug1d(a, x, y) { cout << #a << ": "; fto(_, x, y) cout << a[_] << ' '; cout << endl; }
#define bug2d(a, x, y, u, v) { cout << #a << ": " << endl; fto(i, x, y) {fto(j, u, v) cout << a[i][j] << ' '; cout << endl;}; cout << endl;}
#define bug(a) cout << #a << " = " << a << endl;
#define bug2(a, b) cout << #a << " = " << a << "; "; cout << #b << " = " << b << endl;
#define bug3(a, b, c) cout << #a << " = " << a << "; "; cout << #b << " = " << b << "; "; cout << #c << " = " << c << endl;

// operation
#define mp make_pair
#define pb push_back
#define pf push_front
#define mt make_tuple

// structure
#define ii pair<int, int>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<ii>
#define matrix vector<vi>
#define iii tuple <int, int, int>
#define iiii tuple <int, int, int, int>

// get value
#define FF first
#define SS second
#define get(x, a) get<(x)>(a)

// data type
#define ll long long
#define ull unsigned long long

// function
#define lb lower_bound
#define ub upper_bound

// const value
#define pi 3.14159265358979323846264338327950288419716939937510

using namespace std;

int pSet[maxN], a[maxN];
int n;

void initSet() {
    fto (i, 1, n) pSet[i] = i;
    return;
}

int findSet(int i) {
    return (pSet[i] == i) ? i : pSet[i] = findSet(pSet[i]);
}

void unionSet(int i, int j) {
    pSet[findSet(i)] = findSet(j);
    return;
}

bool isSameSet(int i, int j) {
    return findSet(i) == findSet(j);
}

int main() {
    #ifndef ONLINE_JUDGE
        freopen("tmp.inp", "r", stdin);
        freopen("tmp.out", "w", stdout);
    #endif // ONLINE_JUDGE

    scanf("%d", &n);
    initSet();

    int cnt = 0;
    fto (i, 1, n) {
        scanf("%d", &a[i]);
        if (isSameSet(a[i], i)) {
            ++cnt;
            a[i] = i;
        }
        unionSet(a[i], i);
    }

    int root = 0;
    fto (i, 1, n) if (a[i] == i) root = i;
    fto (i, 1, n) if (a[i] == i) a[i] = root;

    printf("%d\n", cnt-1);
    fto (i, 1, n) printf("%d ", a[i]);

    return 0;
}


#include <bits/stdc++.h>

// limit
#define oo 2000000007
#define OO 10000000000000007LL
#define maxN 10000007

// loop
#define fto(i, x, y) for(int i = (x); i <= (y); ++i)
#define fdto(i, x, y) for(int i = (x); i >= (y); --i)
#define ftoa(i, x, y, a) for(int i = (x); i <= (y); i += a)
#define fdtoa(i, x, y, a) for(int i = (x); i >= (y); i -= a)
#define ftosqrt(i, x, y) for(int i = (x); i*i <= (y); ++i)
#define ftoit(it, var) for (__typeof(var.begin()) it = var.begin(); it != var.end(); ++it)
#define fdtoit(rit, var) for (__typeof(var.rbegin()) rit = var.rbegin(); rit != var.rend(); ++rit)

// debug
#define debug cout << "*" << endl;
#define bug1d(a, x, y) { cout << #a << ": "; fto(_, x, y) cout << a[_] << ' '; cout << endl; }
#define bug2d(a, x, y, u, v) { cout << #a << ": " << endl; fto(i, x, y) {fto(j, u, v) cout << a[i][j] << ' '; cout << endl;}; cout << endl;}
#define bug(a) cout << #a << " = " << a << endl;
#define bug2(a, b) cout << #a << " = " << a << "; "; cout << #b << " = " << b << endl;
#define bug3(a, b, c) cout << #a << " = " << a << "; "; cout << #b << " = " << b << "; "; cout << #c << " = " << c << endl;

// operation
#define mp make_pair
#define pb push_back
#define pf push_front
#define mt make_tuple

// structure
#define ii pair<int, int>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<ii>
#define matrix vector<vi>
#define iii tuple <int, int, int>
#define iiii tuple <int, int, int, int>

// get value
#define FF first
#define SS second
#define get(x, a) get<(x)>(a)

// data type
#define ll long long
#define ull unsigned long long

// function
#define lb lower_bound
#define ub upper_bound

// const value
#define pi 3.14159265358979323846264338327950288419716939937510

using namespace std;

int pSet[maxN], a[maxN], b[maxN];
int n;

void initSet() {
    fto (i, 1, n) pSet[i] = i;
    return;
}

int findSet(int i) {
    return (pSet[i] == i) ? i : pSet[i] = findSet(pSet[i]);
}

void unionSet(int i, int j) {
    pSet[findSet(i)] = findSet(j);
    return;
}

bool isSameSet(int i, int j) {
    return findSet(i) == findSet(j);
}

int main() {
    #ifndef ONLINE_JUDGE
        freopen("tmp.inp", "r", stdin);
        freopen("tmp.out", "w", stdout);
    #endif // ONLINE_JUDGE

    scanf("%d", &n);
    initSet();

    fto (i, 1, n) {
        scanf("%d", &a[i]);
        b[i] = a[i];
        if (isSameSet(a[i], i)) a[i] = i;
        unionSet(a[i], i);
    }

    int root = 0;
    fto (i, 1, n) if (a[i] == i) root = i;
    fto (i, 1, n) if (a[i] == i) a[i] = root;

    int cnt = 0;
    fto (i, 1, n) if (a[i] != b[i]) ++cnt;

    printf("%d\n", cnt);
    fto (i, 1, n) printf("%d ", a[i]);

    return 0;
}


#include <bits/stdc++.h>

// limit
#define oo 2000000007
#define OO 10000000000000007LL
#define maxN 10000007

// loop
#define fto(i, x, y) for(int i = (x); i <= (y); ++i)
#define fdto(i, x, y) for(int i = (x); i >= (y); --i)
#define ftoa(i, x, y, a) for(int i = (x); i <= (y); i += a)
#define fdtoa(i, x, y, a) for(int i = (x); i >= (y); i -= a)
#define ftosqrt(i, x, y) for(int i = (x); i*i <= (y); ++i)
#define ftoit(it, var) for (__typeof(var.begin()) it = var.begin(); it != var.end(); ++it)
#define fdtoit(rit, var) for (__typeof(var.rbegin()) rit = var.rbegin(); rit != var.rend(); ++rit)

// debug
#define debug cout << "*" << endl;
#define bug1d(a, x, y) { cout << #a << ": "; fto(_, x, y) cout << a[_] << ' '; cout << endl; }
#define bug2d(a, x, y, u, v) { cout << #a << ": " << endl; fto(i, x, y) {fto(j, u, v) cout << a[i][j] << ' '; cout << endl;}; cout << endl;}
#define bug(a) cout << #a << " = " << a << endl;
#define bug2(a, b) cout << #a << " = " << a << "; "; cout << #b << " = " << b << endl;
#define bug3(a, b, c) cout << #a << " = " << a << "; "; cout << #b << " = " << b << "; "; cout << #c << " = " << c << endl;

// operation
#define mp make_pair
#define pb push_back
#define pf push_front
#define mt make_tuple

// structure
#define ii pair<int, int>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<ii>
#define matrix vector<vi>
#define iii tuple <int, int, int>
#define iiii tuple <int, int, int, int>

// get value
#define FF first
#define SS second
#define get(x, a) get<(x)>(a)

// data type
#define ll long long
#define ull unsigned long long

// function
#define lb lower_bound
#define ub upper_bound

// const value
#define pi 3.14159265358979323846264338327950288419716939937510

using namespace std;

int pSet[maxN], a[maxN], b[maxN], checkRoot[maxN];
int n;

void initSet() {
    fto (i, 1, n) pSet[i] = i;
    return;
}

int findSet(int i) {
    return (pSet[i] == i) ? i : pSet[i] = findSet(pSet[i]);
}

void unionSet(int i, int j) {
    pSet[findSet(i)] = findSet(j);
    return;
}

bool isSameSet(int i, int j) {
    return findSet(i) == findSet(j);
}

int main() {
    #ifndef ONLINE_JUDGE
        freopen("tmp.inp", "r", stdin);
        freopen("tmp.out", "w", stdout);
    #endif // ONLINE_JUDGE

    scanf("%d", &n);
    initSet();

    fto (i, 1, n) {
        scanf("%d", &a[i]);
        b[i] = a[i];
        if (a[i] == i) checkRoot[i] = 1;
        if (isSameSet(a[i], i)) a[i] = i;
        unionSet(a[i], i);
    }

    int root = 0;
    fto (i, 1, n) if (a[i] == i && checkRoot[i]) root = i;

    if (!root) fto (i, 1, n) if (a[i] == i) root = i;
    fto (i, 1, n) if (a[i] == i) a[i] = root;

    int cnt = 0;
    fto (i, 1, n) if (a[i] != b[i]) ++cnt;

    printf("%d\n", cnt);
    fto (i, 1, n) printf("%d ", a[i]);

    return 0;
}


#include <bits/stdc++.h>

// limit
#define oo 2000000007
#define OO 10000000000000007LL
#define maxN 10000007

// loop
#define fto(i, x, y) for(int i = (x); i <= (y); ++i)
#define fdto(i, x, y) for(int i = (x); i >= (y); --i)
#define ftoa(i, x, y, a) for(int i = (x); i <= (y); i += a)
#define fdtoa(i, x, y, a) for(int i = (x); i >= (y); i -= a)
#define ftosqrt(i, x, y) for(int i = (x); i*i <= (y); ++i)
#define ftoit(it, var) for (__typeof(var.begin()) it = var.begin(); it != var.end(); ++it)
#define fdtoit(rit, var) for (__typeof(var.rbegin()) rit = var.rbegin(); rit != var.rend(); ++rit)

// debug
#define debug cout << "*" << endl;
#define bug1d(a, x, y) { cout << #a << ": "; fto(_, x, y) cout << a[_] << ' '; cout << endl; }
#define bug2d(a, x, y, u, v) { cout << #a << ": " << endl; fto(i, x, y) {fto(j, u, v) cout << a[i][j] << ' '; cout << endl;}; cout << endl;}
#define bug(a) cout << #a << " = " << a << endl;
#define bug2(a, b) cout << #a << " = " << a << "; "; cout << #b << " = " << b << endl;
#define bug3(a, b, c) cout << #a << " = " << a << "; "; cout << #b << " = " << b << "; "; cout << #c << " = " << c << endl;

// operation
#define mp make_pair
#define pb push_back
#define pf push_front
#define mt make_tuple

// structure
#define ii pair<int, int>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<ii>
#define matrix vector<vi>
#define iii tuple <int, int, int>
#define iiii tuple <int, int, int, int>

// get value
#define FF first
#define SS second
#define get(x, a) get<(x)>(a)

// data type
#define ll long long
#define ull unsigned long long

// function
#define lb lower_bound
#define ub upper_bound

// const value
#define pi 3.14159265358979323846264338327950288419716939937510

using namespace std;

int pSet[maxN], a[maxN], b[maxN], checkRoot[maxN];
int n;

void initSet() {
    fto (i, 1, n) pSet[i] = i;
    return;
}

int findSet(int i) {
    return (pSet[i] == i) ? i : pSet[i] = findSet(pSet[i]);
}

void unionSet(int i, int j) {
    pSet[findSet(i)] = findSet(j);
    return;
}

bool isSameSet(int i, int j) {
    return findSet(i) == findSet(j);
}

int main() {
    #ifndef ONLINE_JUDGE
        freopen("tmp.inp", "r", stdin);
        freopen("tmp.out", "w", stdout);
    #endif // ONLINE_JUDGE

    scanf("%d", &n);
    initSet();

    fto (i, 1, n) {
        scanf("%d", &a[i]);
        b[i] = a[i];
        if (a[i] == i) checkRoot[i] = 1;
        if (isSameSet(a[i], i)) a[i] = i;
        unionSet(a[i], i);
    }

    int root = 0;
    fto (i, 1, n) if (a[i] == i && checkRoot[i]) root = i;
    fto (i, 1, n) if (a[i] == i) a[i] = root;

    int cnt = 0;
    fto (i, 1, n) if (a[i] != b[i]) ++cnt;

    printf("%d\n", cnt);
    fto (i, 1, n) printf("%d ", a[i]);

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define FOR(a, b, c) for(int a = b; a <= c; ++a)
#define pb push_back

const int N = 2e5 + 10;

int n, cnt, ans;
int chain[N], a[N];
bool visit[N], flag[N];
vector<int> vi[N];

void check(int pre, int u)	{
	visit[u] = true;
	if(a[u] == u) chain[cnt] = u;
	for(int v: vi[u])	if(v != pre && v != u)	{
		if(visit[v]) {
			flag[cnt] = true;
			chain[cnt] = u;
		}
		else check(u, v);
	}
}

signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin >> n;
	FOR(i, 1, n) {
		int x; cin >> x;
		vi[i].pb(x); vi[x].pb(i);
		a[i] = x;
	}
	FOR(i, 1, n) if(!visit[i]) {
		cnt ++; flag[cnt] = false;
		chain[cnt] = i;
		check(i, i);
		ans += flag[cnt];
	}
	bool tmp = false;
	FOR(i, 1, cnt) if(!flag[i]) ans ++, tmp = true;
	ans -= tmp;
	cout << ans <<  '\n';
	int root = 0;
	FOR(i, 1, cnt) if(!flag[i]) root = chain[i];
	if(root == 0)	{
		FOR(i, 1, cnt) if(flag[i])	{
			root = chain[i];
			break;
		}
	}
	FOR(i, 1, cnt) if(chain[i] != root)
		a[ chain[i] ] = root;
	FOR(i, 1, n) cout << a[i] << ' ';
	// :D  trace

}


#include<bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define FOR(a, b, c) for(int a = b; a <= c; ++a)
#define pb push_back

const int N = 2e5 + 10;

int n, cnt, ans;
int chain[N], a[N];
bool visit[N], flag[N];
vector<int> vi[N];

void check(int pre, int u)	{
	visit[u] = true;
	if(a[u] == u) chain[cnt] = u;
	for(int v: vi[u])	if(v != pre && v != u)	{
		if(visit[v]) {
			flag[cnt] = true;
			chain[cnt] = u;
		}
		else check(u, v);
	}
}

signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin >> n;
	FOR(i, 1, n) {
		int x; cin >> x;
		vi[i].pb(x); vi[x].pb(i);
		a[i] = x;
	}
	FOR(i, 1, n) if(!visit[i]) {
		cnt ++; flag[cnt] = false;
		chain[cnt] = i;
		check(i, i);
		ans += flag[cnt];
	}
	bool tmp = false;
	FOR(i, 1, cnt) if(!flag[i]) ans ++, tmp = true;
	ans -= tmp;
	cout << ans <<  '\n';
	int root = 0;
	FOR(i, 1, cnt) if(!flag[i]) root = chain[i];
	if(root == 0)	{
		FOR(i, 1, cnt) if(flag[i])	{
			root = chain[i];
			break;
		}
	}
	//cout << root << '\n';
	a[root] = root;
	FOR(i, 1, cnt) if(chain[i] != root) 	a[ chain[i] ] = root;
	FOR(i, 1, n) cout << a[i] << ' ';
	// :D  trace

}


#include<bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define FOR(a, b, c) for(int a = b; a <= c; ++a)
#define pb push_back

const int N = 2e5 + 10;

int n, cnt, ans;
int chain[N], a[N];
bool visit[N], flag[N];
vector<int> vi[N];

void check(int pre, int u)	{
	visit[u] = true;
	if(a[u] == u) chain[cnt] = u;
	for(int v: vi[u])	if(v != pre && v != u)	{
		if(visit[v]) {
			flag[cnt] = true;
			chain[cnt] = u;
		}
		else check(u, v);
	}
}

signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin >> n;
	FOR(i, 1, n) {
		int x; cin >> x;
		vi[i].pb(x); vi[x].pb(i);
		a[i] = x;
	}
	FOR(i, 1, n) if(!visit[i]) {
		cnt ++; flag[cnt] = false;
		chain[cnt] = i;
		check(i, i);
		ans += flag[cnt];
	}
	FOR(i, 1, cnt) if(!flag[i]) ans ++;
	ans --;
	cout << ans <<  '\n';
	int root = 0;
	FOR(i, 1, cnt) if(!flag[i]) root = chain[i];
	if(root == 0)	{
		FOR(i, 1, cnt) if(flag[i])	{
			root = chain[i];
			break;
		}
	}
	FOR(i, 1, cnt) if(chain[i] != root)
		a[ chain[i] ] = root;
	FOR(i, 1, n) cout << a[i] << ' ';
	// :D  trace

}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 1;
int p[maxn];
int a[maxn];
int Find(int v)
{
    int t1,t2 = v;
    while(v != p[v])
    {
        v = p[v];
    }
    while(t2 != p[t2])
    {
        t1 = p[t2];
        p[t2] = v;
        t2 = t1;
    }
    return v;
}
void Merge(int a,int b)
{
    int fa = Find(a);
    int fb = Find(b);
    if(fa != fb)
    {
        p[fa] = fb;
    }
}
int main()
{
    int n;
    cin>>n;
    for(int i = 1;i<=n;i++)
    {
        p[i] = i;
    }
    for(int i = 1;i<=n;i++)
    {

        cin>>a[i];
        Merge(i,a[i]);
    }
    vector<int> can;
    for(int i = 1;i<=n;i++)
    {
        //cout<<i<<" "<<Find(i)<<endl;
        if(Find(i) == i) can.push_back(i);
    }
    int root = can[0];
    if(a[root] != root)
    {
        cout<<can.size()<<endl;
        a[root] = root;
    }
    else
    {
        cout<<can.size()-1<<endl;
    }
    for(int i =1 ;i<can.size();i++)
    {
        a[can[i]] = root;
    }
    for(int i = 1;i<=n;i++)
    {
        cout<<a[i]<<" ";
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5;
int p[maxn];
int a[maxn];
int Find(int v)
{
    int t1,t2 = v;
    while(v != p[v])
    {
        v = p[v];
    }
    while(t2 != p[t2])
    {
        t1 = p[t2];
        p[t2] = v;
        t2 = t1;
    }
    return v;
}
void Merge(int a,int b)
{
    int fa = Find(a);
    int fb = Find(b);
    if(fa != fb)
    {
        p[fb] = fa;
    }
}
int main()
{
    int n;
    cin>>n;
    for(int i = 1;i<=n;i++)
    {
        p[i] = i;
    }
    for(int i = 1;i<=n;i++)
    {

        cin>>a[i];
        Merge(a[i],i);
    }
    vector<int> can;
    for(int i = 1;i<=n;i++)
    {
        //cout<<i<<" "<<p[i]<<endl;
        if(p[i] == i) can.push_back(i);
    }
    int root = can.back();
    can.pop_back();
    cout<<can.size()<<endl;
    for(int i : can)
    {
        a[i] = root;
    }
    for(int i = 1;i<=n;i++)
    {
        cout<<a[i]<<" ";
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 1;
int p[maxn];
int a[maxn];
int Find(int v)
{
    int t1,t2 = v;
    while(v != p[v])
    {
        v = p[v];
    }
    while(t2 != p[t2])
    {
        t1 = p[t2];
        p[t2] = v;
        t2 = t1;
    }
    return v;
}
void Merge(int a,int b)
{
    int fa = Find(a);
    int fb = Find(b);
    if(fa != fb)
    {
        p[fa] = fb;
    }
}
int main()
{
    int n;
    cin>>n;
    for(int i = 1;i<=n;i++)
    {
        p[i] = i;
    }
    for(int i = 1;i<=n;i++)
    {
        cin>>a[i];
        Merge(i,a[i]);
    }
    int flag = 0;
    if(n == 200000 && a[1] == 67158) flag = 1;
    vector<int> can;
    for(int i = 1;i<=n;i++)
    {
        if(flag) cout<<i<<" "<<Find(i)<<endl;
        if(Find(i) == i) can.push_back(i);
    }
    int root = can[0];
    if(a[root] != root)
    {
        cout<<can.size()<<endl;
        a[root] = root;
    }
    else
    {
        cout<<can.size()-1<<endl;
    }
    for(int i =1 ;i<can.size();i++)
    {
        a[can[i]] = root;
    }
    for(int i = 1;i<=n;i++)
    {
        cout<<a[i]<<" ";
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int p[maxn];
int a[maxn];
int cnt= 0 ;
int Find(int v)
{
   return p[v] == v ? v : p[v] = Find(p[v]);
}
int main()
{
    int n;
    cin>>n;
    for(int i = 1;i<=n;i++)
    {
        p[i] = i;
    }
    int t;
    for(int i = 1;i<=n;i++)
    {
        cin>>a[i];
        if(a[i] == i) t =  i,cnt++;
        else
        {
            int fa = Find(i),fb = Find(a[i]);
            if(fa  == fb)
            {
                a[i] = i;
                cnt ++;
            }
            else p[fa] = fb;
        }
    }
    if(t == 0)
    {
        for(int i = 1;i<=n;i++)
        {
            if(a[i] == i) t = i;
        }
        cnt++;
    }
    cout<<cnt-1<<endl;
    for(int i = 1;i<=n;i++)
    {
        if(a[i] == i) a[i] = t;
        cout<<a[i]<<" ";
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int p[maxn];
int a[maxn];
int cnt= 0 ;
int Find(int v)
{
   return p[v] == v ? v : p[v] = Find(p[v]);
}
int main()
{
    int n;
    cin>>n;
    for(int i = 1;i<=n;i++)
    {
        p[i] = i;
    }
    int t = 0;
    for(int i = 1;i<=n;i++)
    {
        cin>>a[i];
        if(a[i] == i) t = i,cnt++;
        else
        {
            int fa = Find(i),fb = Find(a[i]);
            if(fa  == fb)
            {
                a[i] = i;
                cnt ++;
            }
            else p[fa] = fb;
        }
    }
    if(t == 0)
    {
        for(int i = 1;i<=n;i++)
        {
            if(a[i] == i) t = i;
        }
        cnt++;
    }
    cout<<cnt-1<<endl;
    for(int i = 1;i<=n;i++)
    {
        if(a[i] == i) a[i] = t;
        cout<<a[i]<<" ";
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5;
int p[maxn];
int a[maxn];
int Find(int v)
{
    int t1,t2 = v;
    while(v != p[v])
    {
        v = p[v];
    }
    while(t2 != p[t2])
    {
        t1 = p[t2];
        p[t2] = v;
        t2 = t1;
    }
    return v;
}
void Merge(int a,int b)
{
    int fa = Find(a);
    int fb = Find(b);
    if(fa != fb)
    {
        p[fa] = fb;
    }
}
int main()
{
    int n;
    cin>>n;
    for(int i = 1;i<=n;i++)
    {
        p[i] = i;
    }
    for(int i = 1;i<=n;i++)
    {

        cin>>a[i];
        Merge(i,a[i]);
    }
    vector<int> can;
    for(int i = 1;i<=n;i++)
    {
        //cout<<i<<" "<<Find(i)<<endl;
        if(Find(i) == i) can.push_back(i);
    }
    int root = can[0];
    if(a[root] != root)
    {
        cout<<can.size()<<endl;
        a[root] = root;
    }
    else
    {
        cout<<can.size()-1<<endl;
    }
    for(int i =1 ;i<can.size();i++)
    {
        a[can[i]] = root;
    }
    for(int i = 1;i<=n;i++)
    {
        cout<<a[i]<<" ";
    }
    return 0;
}


#include<bits/stdc++.h>
#define Ll long long
using namespace std;
const int N=2e5+5;
struct cs{int to,nxt;bool ok;}a[N*4];
int head[N],ll=1;
int aa[N];
bool vi[N];
int n,m,x,y,z,S,ans,T;
void init(int x,int y){
	a[++ll].to=y;
	a[ll].nxt=head[x];
	head[x]=ll;
}
void dfs(int x){
	vi[x]=1;
	for(int k=head[x];k;k=a[k].nxt)
		if(!a[k].ok)
			if(!vi[a[k].to])a[k].ok=a[k^1].ok=1,dfs(a[k].to);
				else T=a[k].to;
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&x);
		aa[i]=x;
		init(x,i);init(i,x);
		if(i==x)S=i;
	}
	if(!S){
		ans++;
		aa[1]=1;
		S=1;
	}
	dfs(S);
	for(int i=1;i<=n;i++)
		if(!vi[i]&&aa[i]==i){
			ans++;
			aa[i]=S;
			dfs(i);
		}
	for(int i=1;i<=n;i++)
		if(!vi[i]){
			ans++;
			dfs(i);
			aa[T]=S;
		}
	printf("%d\n",ans);
	for(int i=1;i<=n;i++)printf("%d ",aa[i]);
}


#include<bits/stdc++.h>
#define Ll long long
using namespace std;
const int N=2e5+5;
struct cs{int to,nxt;}a[N*4];
int head[N],ll;
int aa[N];
bool vi[N];
int n,m,x,y,z,S,ans;
void init(int x,int y){
	a[++ll].to=y;
	a[ll].nxt=head[x];
	head[x]=ll;
}
void dfs(int x){
	vi[x]=1;
	for(int k=head[x];k;k=a[k].nxt)
		if(!vi[a[k].to])dfs(a[k].to);
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&x);
		aa[i]=x;
		init(x,i);init(i,x);
		if(i==x)S=i;
	}
	if(!S){
		ans++;
		aa[1]=1;
		S=1;
	}
	dfs(S);
	for(int i=1;i<=n;i++)
		if(!vi[i]&&aa[i]==i){
			ans++;
			aa[i]=S;
			dfs(i);
		}
	for(int i=1;i<=n;i++)
		if(!vi[i]){
			ans++;
			aa[i]=S;
			dfs(i);
		}
	printf("%d\n",ans);
	for(int i=1;i<=n;i++)printf("%d ",aa[i]);
}


#include<bits/stdc++.h>
#define Ll long long
using namespace std;
const int N=2e5+5;
struct cs{int to,nxt;bool ok;}a[N*4];
int head[N],ll=1;
int aa[N];
bool vi[N];
int n,m,x,y,z,S,ans,T;
void init(int x,int y){
	a[++ll].to=y;
	a[ll].nxt=head[x];
	head[x]=ll;
}
void dfs(int x){
	vi[x]=1;
	for(int k=head[x];k;k=a[k].nxt)
		if(!a[k].ok)
			if(!vi[a[k].to])a[k].ok=a[k^1].ok=1,dfs(a[k].to);
				else T=a[k].to;
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&x);
		aa[i]=x;
		init(x,i);init(i,x);
		if(i==x)S=i;
	}
	if(!S){
		ans++;
		dfs(1);
		aa[T]=T;
		S=T;
	}else dfs(S);
	for(int i=1;i<=n;i++)
		if(!vi[i]&&aa[i]==i){
			ans++;
			aa[i]=S;
			dfs(i);
		}
	for(int i=1;i<=n;i++)
		if(!vi[i]){
			ans++;
			dfs(i);
			aa[T]=S;
		}
	printf("%d\n",ans);
	for(int i=1;i<=n;i++)printf("%d ",aa[i]);
}


#include<bits/stdc++.h>
#define Ll long long
using namespace std;
const int N=2e5+5;
struct cs{int to,nxt;}a[N*4];
int head[N],ll;
int aa[N];
bool vi[N];
int n,m,x,y,z,S,ans,T;
void init(int x,int y){
	a[++ll].to=y;
	a[ll].nxt=head[x];
	head[x]=ll;
}
void dfs(int x,int y){
	vi[x]=1;
	for(int k=head[x];k;k=a[k].nxt)
		if(!vi[a[k].to])dfs(a[k].to,x);
		else if(a[k].to==y)T=x;
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&x);
		aa[i]=x;
		init(x,i);init(i,x);
		if(i==x)S=i;
	}
	if(!S){
		ans++;
		aa[1]=1;
		S=1;
	}
	dfs(S,-1);
	for(int i=1;i<=n;i++)
		if(!vi[i]&&aa[i]==i){
			ans++;
			aa[i]=S;
			dfs(i,-1);
		}
	for(int i=1;i<=n;i++)
		if(!vi[i]){
			ans++;
			dfs(i,-1);
			aa[T]=S;
		}
	printf("%d\n",ans);
	for(int i=1;i<=n;i++)printf("%d ",aa[i]);
}


#include<bits/stdc++.h>
#define Ll long long
using namespace std;
const int N=2e5+5;
struct cs{int to,nxt;}a[N*4];
int head[N],ll;
int aa[N];
bool vi[N];
int n,m,x,y,z,S,ans;
void init(int x,int y){
	a[++ll].to=y;
	a[ll].nxt=head[x];
	head[x]=ll;
}
void dfs(int x){
	queue<int>Q;
	Q.push(x);vi[x]=1;
	while(!Q.empty()){
		x=Q.front();Q.pop();
		for(int k=head[x];k;k=a[k].nxt)
			if(!vi[a[k].to])Q.push(a[k].to),vi[a[k].to]=1;
	}
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&x);
		aa[i]=x;
		init(x,i);init(i,x);
		if(i==x)S=i;
	}
	if(!S){
		ans++;
		aa[1]=1;
		S=1;
	}
	dfs(S);
	for(int i=1;i<=n;i++)
		if(!vi[i]&&aa[i]==i){
			ans++;
			aa[i]=S;
			dfs(i);
		}
	for(int i=1;i<=n;i++)
		if(!vi[i]){
			ans++;
			aa[i]=S;
			dfs(i);
		}
	printf("%d\n",ans);
	for(int i=1;i<=n;i++)printf("%d ",aa[i]);
}


#include<bits/stdc++.h>
#define Ll long long
using namespace std;
const int N=2e5+5;
struct cs{int to,nxt;}a[N*4];
int head[N],ll;
int aa[N];
bool vi[N];
int n,m,x,y,z,S,ans,T;
void init(int x,int y){
	a[++ll].to=y;
	a[ll].nxt=head[x];
	head[x]=ll;
}
void dfs(int x,int y){
	vi[x]=1;
	for(int k=head[x];k;k=a[k].nxt)
		if(!vi[a[k].to])dfs(a[k].to,x);
		else if(a[k].to!=y)T=x;
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&x);
		aa[i]=x;
		init(x,i);init(i,x);
		if(i==x)S=i;
	}
	if(!S){
		ans++;
		aa[1]=1;
		S=1;
	}
	dfs(S,-1);
	for(int i=1;i<=n;i++)
		if(!vi[i]&&aa[i]==i){
			ans++;
			aa[i]=S;
			dfs(i,-1);
		}
	for(int i=1;i<=n;i++)
		if(!vi[i]){
			ans++;
			dfs(i,-1);
			aa[T]=S;
		}
	printf("%d\n",ans);
	for(int i=1;i<=n;i++)printf("%d ",aa[i]);
}


#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>

using namespace std;

int parent[200010];
bool visit[200010];

int main() {
    int n, root = -1, cnt = 0;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> parent[i];
        if (i == parent[i] && root == -1) root = i;
        else if (i == parent[i]) {
            cnt++;
            parent[i] = root;
        }
    }
    visit[root] = true;
    for (int i = 1; i <= n; i++) {
        if (!visit[i]) {
            int curr = i;
            visit[curr] = true;
            while (true) {
                curr = parent[curr];
                if (visit[curr]) {
                    if (curr == i) {
                        cnt++;
                        parent[curr] = root;
                    }
                    break;
                }
                visit[curr] = true;
            }
        }
    }
    cout << cnt << endl;
    for (int i = 1; i <= n; i++) cout << parent[i] << " ";
    cout << endl;
}


#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>

using namespace std;

int parent[200010];
int visit[200010];

int main() {
    int n, root = -1, cnt = 0, tmp = 1;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> parent[i];
        if (i == parent[i] && root == -1) root = i;
        else if (i == parent[i]) {
            cnt++;
            parent[i] = root;
        }
    }
    if (root != -1) visit[root] = tmp++;
    for (int i = 1; i <= n; i++) {
        if (visit[i] == 0) {
            int curr = i;
            visit[curr] = tmp;
            while (true) {
                curr = parent[curr];
                if (visit[curr] != 0) {
                    tmp++;
                    if (visit[curr] == visit[i]) {
                        cnt++;
                        if (root == -1) root = curr;
                        parent[curr] = root;
                    }
                    break;
                }
                visit[curr] = tmp;
            }
        }
    }
    cout << cnt << endl;
    for (int i = 1; i <= n; i++) cout << parent[i] << " ";
    cout << endl;
}


#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>

using namespace std;

int parent[200010];
bool visit[200010];

int main() {
    int n, root = -1, cnt = 0;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> parent[i];
        if (i == parent[i] && root == -1) root = i;
        else if (i == parent[i]) {
            cnt++;
            parent[i] = root;
        }
    }
    visit[root] = true;
    for (int i = 1; i <= n; i++) {
        if (!visit[i]) {
            int curr = i;
            visit[curr] = true;
            while (true) {
                curr = parent[curr];
                if (visit[curr]) {
                    if (curr == i) {
                        cnt++;
                        parent[curr] = root;
                    }
                    break;
                }
            }
        }
    }
    cout << cnt << endl;
    for (int i = 1; i <= n; i++) cout << parent[i] << " ";
    cout << endl;
}


#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>

using namespace std;

int parent[200010];
bool visit[200010];

int main() {
    int n, root = -1, cnt = 0;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> parent[i];
        if (i == parent[i] && root == -1) root = i;
        else if (i == parent[i]) {
            cnt++;
            parent[i] = root;
        }
    }
    if (root == -1) {
        root = 1;
        cnt++;
        parent[1] = 1;
    }
    visit[root] = true;
    for (int i = 1; i <= n; i++) {
        if (!visit[i]) {
            int curr = i;
            visit[curr] = true;
            while (true) {
                curr = parent[curr];
                if (visit[curr]) {
                    if (curr == i) {
                        cnt++;
                        parent[curr] = root;
                    }
                    break;
                }
                visit[curr] = true;
            }
        }
    }
    cout << cnt << endl;
    for (int i = 1; i <= n; i++) cout << parent[i] << " ";
    cout << endl;
}


#include <iostream>
#include <vector>
using namespace std;
struct Dot
{
	int father;
	bool wasHere = false;
	bool marked = false;
	bool isNice = false; //отметка хорошей цепи
};
int main()
{
	int count = 0;
	int father = -1;
	int n;
	cin >> n;
	vector<Dot> arr(n + 1);
	int current;
	for (int i = 1; i <= n; i++)
	{
		cin >> current;
		arr[i].father = current;
	}
	for (int i = 1; i <= n&&father == -1; i++)
	{
		if (arr[i].father == i)
		{

			father = i;
			arr[father].isNice = true;
		}
	}
	if (father == -1)
	{
		current = 1;
		while (!arr[arr[current].father].wasHere)
		{
			arr[current].wasHere = true;
			current = arr[current].father;
		}
		father = current;
		arr[father].marked = true;
		arr[father].isNice = true;
		if(father!=1)
			current = 1;
		else current = arr[1].father;
		while (!arr[current].isNice)
		{
			arr[current].isNice = true;
			current = arr[current].father;
		}
	}
	for (int i = 1; i <= n; i++)
	{
		current = i;
		if (!arr[current].isNice)
		{
			int newcur = current;
			while (!arr[newcur].wasHere&&arr[newcur].father != newcur && !arr[newcur].isNice)
			{
				arr[newcur].wasHere = true;
				newcur = arr[newcur].father;
			}
			if (!arr[newcur].isNice)
			{
				arr[newcur].marked = true;
				if (current == newcur)
					current = arr[current].father;
			}
			while (!arr[current].isNice)
			{
				arr[current].isNice = true;
				current = arr[current].father;
			}
		}
	}
	for (int i = 1; i <= n; i++)
		if (arr[i].marked)
		{
			count++;
			arr[i].father = father;
		}
	cout << count<<'\n';
	for (int i = 1; i <= n; i++)
		cout << arr[i].father << ' ';
}


#include <iostream>
#include <vector>
using namespace std;
struct Dot
{
	int father;
	bool wasHere = false;
	bool marked = false;
	bool isNice = false; //отметка хорошей цепи
};
int main()
{
	int count = 0;
	int father = -1;
	int n;
	cin >> n;
	vector<Dot> arr(n + 1);
	int current;
	for (int i = 1; i <= n; i++)
	{
		cin >> current;
		arr[i].father = current;
	}
	for (int i = 1; i <= n&&father == -1; i++)
	{
		if (arr[i].father == i)
		{

			father = i;
			arr[father].isNice = true;
		}
	}
	if (father == -1)
	{
		current = 1;
		while (!arr[current].wasHere)
		{
			arr[current].wasHere = true;
			current = arr[current].father;
		}
		father = current;
		arr[father].marked = true;
		arr[father].isNice = true;
		current = 1;
		while (!arr[current].isNice)
		{
			arr[current].isNice = true;
			current = arr[current].father;
		}
	}
	for (int i = 1; i <= n; i++)
	{
		current = i;
		if (!arr[current].isNice)
		{
			int newcur = current;
			while (!arr[newcur].wasHere&&arr[newcur].father != newcur && !arr[newcur].isNice)
			{
				arr[newcur].wasHere = true;
				newcur = arr[newcur].father;
			}
			if (arr[newcur].isNice)
			{
			}
			else if (arr[newcur].father == newcur&&arr[newcur].father !=father || arr[newcur].wasHere)
			{
				arr[newcur].isNice = true;
				arr[newcur].marked = true;
			}
			arr[current].isNice = true;
			current = arr[current].father;
			while (!arr[current].isNice)
			{
				arr[current].isNice = true;
				current = arr[current].father;
			}
		}
	}
	for (int i = 1; i <= n; i++)
		if (arr[i].marked)
		{
		    if (arr[i].father!=father)
			count++;
			arr[i].father = father;
		}
	cout << count<<'\n';
	for (int i = 1; i <= n; i++)
		cout << arr[i].father << ' ';
}


#include <iostream>
#include <vector>
using namespace std;
struct Dot
{
	int father;
	bool wasHere = false;
	bool marked = false;
	bool secondChain = false;
};
int main()
{
	int count = 0;
	int father = -1;
	int n;
	cin >> n;
	vector<Dot> pos(n + 1);
	int tmp;
	for (int i = 1; i <= n; i++)
	{
		cin >> tmp;
		pos[i].father = tmp;
	}
	for (int i = 1; i <= n&&father==-1; i++)
	{
		if (pos[i].father == i)
			father = i;
	}
	for (int i = 1; i <= n; i++)
	{
		int current = i;
		while (pos[current].father != current && !pos[current].wasHere)
		{
			pos[current].wasHere = true;
			current = pos[current].father;
		}
		if (father == -1)
		{
			if (pos[current].father != current)
				pos[current].marked = true;
			father = current;
		}
		else
		{
			if (pos[current].father != father&&pos[current].father == current)
				pos[current].marked = true;
			else
			{
				int next = current;
				while (pos[next].father != current&&pos[next].father!=father&&!pos[next].marked)
				{
					next = pos[next].father;
				}
				if (pos[next].father == current)

					pos[next].marked = true;
			}
		}

	}
	for (int i = 1; i <= n; i++)
		if (pos[i].marked&&pos[i].father!=father)
		{
			count++;
			pos[i].father = father;
		}
	cout << count<<'\n';
	for (int i = 1; i <= n; i++)
		cout << pos[i].father << ' ';
}


#include <iostream>
#include <vector>
using namespace std;
struct Dot
{
	int father;
	bool wasHere = false;
	bool marked = false;
	bool isNice = false; //отметка хорошей цепи
};
int main()
{
	int count = 0;
	int father = -1;
	int n;
	cin >> n;
	vector<Dot> arr(n + 1);
	int current;
	for (int i = 1; i <= n; i++)
	{
		cin >> current;
		arr[i].father = current;
	}
	for (int i = 1; i <= n&&father == -1; i++)
	{
		if (arr[i].father == i)
		{

			father = i;
			arr[father].isNice = true;
		}
	}
	if (father == -1)
	{
		current = 1;
		while (!arr[arr[current].father].wasHere)
		{
			arr[current].wasHere = true;
			current = arr[current].father;
		}
		father = current;
		arr[father].marked = true;
		arr[father].isNice = true;
		if(father!=1)
			current = 1;
		else current = arr[1].father;
		while (!arr[current].isNice)
		{
			arr[current].isNice = true;
			current = arr[current].father;
		}
	}
	for (int i = 1; i <= n; i++)
	{
		current = i;
		if (!arr[current].isNice)
		{
			int newcur = current;
			while (!arr[newcur].wasHere&&arr[newcur].father != newcur && !arr[newcur].isNice)
			{
				arr[newcur].wasHere = true;
				newcur = arr[newcur].father;
			}
			if (!arr[newcur].isNice)
			{
				arr[newcur].isNice = true;
				arr[newcur].marked = true;
				if (current == newcur)
					current = arr[current].father;
			}
			while (!arr[current].isNice)
			{
				arr[current].isNice = true;
				current = arr[current].father;
			}
		}
	}
	for (int i = 1; i <= n; i++)
		if (arr[i].marked)
		{
			count++;
			arr[i].father = father;
		}
	cout << count<<'\n';
	for (int i = 1; i <= n; i++)
		cout << arr[i].father << ' ';
}


#include <iostream>
#include <vector>
using namespace std;
struct Dot
{
	int father;
	bool wasHere = false;
	bool marked = false;
	bool isNice = false; //отметка хорошей цепи
};
int main()
{
	int count = 0;
	int father = -1;
	int n;
	cin >> n;
	vector<Dot> arr(n + 1);
	int current;
	for (int i = 1; i <= n; i++)
	{
		cin >> current;
		arr[i].father = current;
	}
	for (int i = 1; i <= n&&father == -1; i++)
	{
		if (arr[i].father == i)
		{

			father = i;
			arr[father].isNice = true;
		}
	}
	if (father == -1)
	{
		current = 1;
		while (!arr[current].wasHere)
		{
			arr[current].wasHere = true;
			current = arr[current].father;
		}
		father = current;
		arr[father].marked = true;
		arr[father].isNice = true;
		current = 1;
		while (!arr[current].isNice)
		{
			arr[current].isNice = true;
			current = arr[current].father;
		}
	}
	for (int i = 1; i <= n; i++)
	{
		current = i;
		if (!arr[current].isNice)
		{
			int newcur = current;
			while (!arr[newcur].wasHere&&arr[newcur].father != newcur && !arr[newcur].isNice)
			{
				arr[newcur].wasHere = true;
				newcur = arr[newcur].father;
			}
			if (arr[newcur].isNice)
			{
			}
			else if (arr[newcur].father == newcur&&arr[newcur].father !=father || arr[newcur].wasHere)
			{
				arr[newcur].isNice = true;
				arr[newcur].marked = true;
			}
			arr[current].isNice = true;
			current = arr[current].father;
			while (!arr[current].isNice)
			{
				arr[current].isNice = true;
				current = arr[current].father;
			}
		}
	}
	for (int i = 1; i <= n; i++)
		if (arr[i].marked)
		{
			count++;
			arr[i].father = father;
		}
	cout << count<<'\n';
	for (int i = 1; i <= n; i++)
		cout << arr[i].father << ' ';
}


#include<bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second
#define pi pair<long long,long long>
#define pt pair<int,int>
#define ll long long
#define pb push_back
using namespace std;

int read()
{
	int x=0,f=1;char c=getchar();
	while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
	while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
	return x*f;
}

int vis[200010];
vector<int> graph[200010];

void dfs(int root){
	if(vis[root]){
		return;
	}
	vis[root] = 1;
	for(int i=0;i<graph[root].size();i++){
		dfs(graph[root][i]);
	}
}

int main()
{
	int n;
	n = read();
	int p[n+1];
	int root = 0;
	int id = 1;
	for(int i=1;i<=n;i++){
		p[i] = read();
		graph[p[i]].push_back(i);
	}
	memset(vis,0,sizeof(vis));
	int comp = 0;
	for(int i=1;i<=n;i++){
		if(vis[i])
			continue;
		if(p[i]==i){
			if(!root)
				root = i;
			else{
				p[i] = root;
				comp++;
			}
			dfs(i);
		}
	}
	for(int i=1;i<=n;i++){
		if(vis[i])
			continue;
		int j = i;
		while(!vis[j]){
			dfs(j);
			j = p[j];
		}
		if(!root)
			root = p[j] = j;
		else p[j] = root;
		comp++;
	}
	cout<<comp<<endl;
	for(int i=1;i<=n;i++)
		cout<<p[i]<<" ";
	return 0;
}


#include<bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second
#define pi pair<long long,long long>
#define pt pair<int,int>
#define ll long long
#define pb push_back
using namespace std;

int read()
{
	int x=0,f=1;char c=getchar();
	while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
	while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
	return x*f;
}
int vis[200010];
vector<int> graph[200010];

vector<int> cycles;

void dfs(int root){
	if(vis[root]){
		cycles.pb(root);
		return;
	}
	vis[root] = 1;
	for(int i=0;i<graph[root].size();i++){
		dfs(graph[root][i]);
	}
}

int main()
{
	int n;
	n = read();
	int p[n+1];
	int root = 0;
	for(int i=1;i<=n;i++){
		p[i] = read();
		graph[p[i]].push_back(i);

		if(p[i]==i)
			root = i;
	}
	memset(vis,0,sizeof(vis));
	int comp = 0;
	for(int i=1;i<=n;i++){
		if(vis[i])
			continue;
		if(p[i]==i){
			comp++;
			dfs(i);
		}
	}
	for(int i=1;i<=n;i++){
		if(vis[i])
			continue;
		int j = i;
		while(!vis[j]){
			dfs(j);
			j = p[j];
		}
		comp++;
	}
	cout<<comp-1<<endl;
	for(int i=1;i<=n;i++){
		if(p[i]==i)
			p[i] = root;
	}
	for(int i=0;i<cycles.size();i++){
		p[cycles[i]] = root;
	}
	for(int i=1;i<=n;i++)
		cout<<p[i]<<" ";
	return 0;
}


#include<bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second
#define pi pair<long long,long long>
#define pt pair<int,int>
#define ll long long
#define pb push_back
using namespace std;

int read()
{
	int x=0,f=1;char c=getchar();
	while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
	while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
	return x*f;
}
int vis[200010];
vector<int> graph[200010];

vector<int> cycles;

void dfs(int root){
	if(vis[root]){
		cycles.pb(root);
		return;
	}
	vis[root] = 1;
	for(int i=0;i<graph[root].size();i++){
		dfs(graph[root][i]);
	}
}

int main()
{
	int n;
	n = read();
	int p[n];
	int root = 0;
	for(int i=1;i<=n;i++){
		p[i] = read();
		graph[p[i]].push_back(i);

		if(p[i]==i)
			root = i;
	}
	memset(vis,0,sizeof(vis));
	int comp = 0;
	for(int i=1;i<=n;i++){
		if(vis[i])
			continue;
		if(p[i]==i){
			comp++;
			dfs(i);
		}
	}
	for(int i=1;i<=n;i++){
		if(vis[i])
			continue;
		int j = i;
		while(!vis[j]){
			dfs(j);
			j = p[j];
		}
		comp++;
	}
	cout<<comp-1<<endl;
	for(int i=1;i<=n;i++){
		if(p[i]==i)
			p[i] = root;
	}
	for(int i=0;i<cycles.size();i++){
		p[cycles[i]] = root;
	}
	for(int i=1;i<=n;i++)
		cout<<p[i]<<" ";
	return 0;
}


/**********************************************
            Author : smiley007
***********************************************/

//Data Structure Includes
#include <vector>
#include <queue>
#include <deque>
#include <bitset>
#include <stack>
#include <list>
#include <set>
#include <map>
#include <unordered_set>

//Other Includes
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <cctype>
#include <cassert>
#include <numeric>
#include <algorithm>
#include <ctime>
#include <sstream>
#include <climits>
#include <iomanip>

using namespace std ;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<vvll> vvvll;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
typedef vector<vpii> vvpii;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;
typedef vector<vvpll> vvvpll;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef vector<vs> vvs;

#ifdef LocalHost
    #define eprintf(...) fprintf(stderr,__VA_ARGS__)
#endif

//Code Begins Here ------ >>>
const int N = 2e5 + 55;
vi g[N];
int p[N], vis[N], ans[N];
int n;

void dfs(int u, int comp) {
    vis[u] = comp;
    for (auto v : g[u]) {
        if (vis[v]) {
            if (vis[v] == comp) p[u] = -1;
            return;
        } else dfs(v, comp);
    }
}

int main(){
    #ifdef LocalHost
        freopen("input.txt","r",stdin);
    #endif

    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &p[i]);
        if (p[i] != i)  g[p[i]].push_back(i);
    }
    for (int i = 1; i <= n; i++) {
        if (!vis[i])
            dfs(i, i);
    }
    int idx, a, b;
    idx = a = b = 0;
    for (int i = 1; i <= n; i++) if (p[i] == i || p[i] == -1) {
        ans[idx++] = i;
        if (p[i] == -1) a++;
        else b++;
    }
    b = max(b - 1, 0);
    int res = max(a + b, 0);
    cout << res << "\n";
    if (res) {
        for (int i = 0; i < idx - 1; i++)
            p[ans[i]] = ans[i + 1];
        p[ans[idx - 1]] = ans[idx - 1];
    }
    for (int i = 1; i <= n; i++)
        cout << p[i] << " ";

    #ifdef LocalHost
        cout<<endl<<"Execution time = "<<(float)clock()/CLOCKS_PER_SEC <<" s"<<endl;
    #endif

    return 0;
}


/**********************************************
            Author : smiley007
***********************************************/

//Data Structure Includes
#include <vector>
#include <queue>
#include <deque>
#include <bitset>
#include <stack>
#include <list>
#include <set>
#include <map>
#include <unordered_set>

//Other Includes
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <cctype>
#include <cassert>
#include <numeric>
#include <algorithm>
#include <ctime>
#include <sstream>
#include <climits>
#include <iomanip>

using namespace std ;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<vvll> vvvll;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
typedef vector<vpii> vvpii;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;
typedef vector<vvpll> vvvpll;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef vector<vs> vvs;

#ifdef LocalHost
    #define eprintf(...) fprintf(stderr,__VA_ARGS__)
#endif

//Code Begins Here ------ >>>
const int N = 2e5 + 55;
vi g[N];
int p[N], vis[N], ans[N];
int n;

void dfs(int u, int comp) {
    vis[u] = comp;
    for (auto v : g[u]) {
        if (vis[v]) {
            if (vis[v] == comp) p[u] = u;
            return;
        } else dfs(v, comp);
    }
}

int main(){
    #ifdef LocalHost
        freopen("input.txt","r",stdin);
    #endif

    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &p[i]);
        if (p[i] != i)  g[p[i]].push_back(i);
    }
    for (int i = 1; i <= n; i++) {
        if (!vis[i])
            dfs(i, i);
    }
    int idx = 0;
    for (int i = 1; i <= n; i++) if (p[i] == i) {
        ans[idx++] = i;
    }
    if (idx > 1)    cout << idx - 1 << "\n";
    else    cout << "0\n";
    if (idx > 1) {
        for (int i = 0; i < idx - 1; i++)
            p[ans[i]] = ans[i + 1];
    }
    for (int i = 1; i <= n; i++)
        cout << p[i] << " ";

    #ifdef LocalHost
        cout<<endl<<"Execution time = "<<(float)clock()/CLOCKS_PER_SEC <<" s"<<endl;
    #endif

    return 0;
}


/**********************************************
            Author : smiley007
***********************************************/

//Data Structure Includes
#include <vector>
#include <queue>
#include <deque>
#include <bitset>
#include <stack>
#include <list>
#include <set>
#include <map>
#include <unordered_set>

//Other Includes
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <cctype>
#include <cassert>
#include <numeric>
#include <algorithm>
#include <ctime>
#include <sstream>
#include <climits>
#include <iomanip>

using namespace std ;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<vvll> vvvll;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
typedef vector<vpii> vvpii;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;
typedef vector<vvpll> vvvpll;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef vector<vs> vvs;

#ifdef LocalHost
    #define eprintf(...) fprintf(stderr,__VA_ARGS__)
#endif

//Code Begins Here ------ >>>
const int N = 2e5 + 55;
vi g[N];
int p[N], vis[N];
vi cycles, comp;
int n;

void dfs(int u, int comp) {
    vis[u] = comp;
    for (auto v : g[u]) {
        if (vis[v]) {
            if (vis[v] == comp) p[u] = -1;
        } else dfs(v, comp);
    }
}

int main(){
    #ifdef LocalHost
        freopen("input.txt","r",stdin);
    #endif

    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &p[i]);
        if (p[i] != i)  g[p[i]].push_back(i);
    }

    for (int i = 1; i <= n; i++) {
        if (!vis[i])
            dfs(i, i);
    }

    for (int i = 1; i <= n; i++) {
        if (p[i] == -1) cycles.push_back(i);
        else if (p[i] == i)  comp.push_back(i);
    }

    int a = (int)cycles.size(), b = (int)comp.size();
    b = max(b - 1, 0);
    int res = a + b;
    cout << res << "\n";
    if (res) {
        b = (int)comp.size();
        for (int i = 0; i + 1 < a; i++)
            p[cycles[i]] = cycles[i + 1];
        if (a)
            p[cycles[a - 1]] = cycles[a - 1];
        for (int i = 0; i + 1 < b; i++)
            p[comp[i]] = comp[i + 1];
        if (a && b) {
            p[cycles[a - 1]] = comp[0];
        }
    }
    for (int i = 1; i <= n; i++)
        cout << p[i] << " ";

    #ifdef LocalHost
        cout<<endl<<"Execution time = "<<(float)clock()/CLOCKS_PER_SEC <<" s"<<endl;
    #endif

    return 0;
}


/**********************************************
            Author : smiley007
***********************************************/

//Data Structure Includes
#include <vector>
#include <queue>
#include <deque>
#include <bitset>
#include <stack>
#include <list>
#include <set>
#include <map>
#include <unordered_set>

//Other Includes
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <cctype>
#include <cassert>
#include <numeric>
#include <algorithm>
#include <ctime>
#include <sstream>
#include <climits>
#include <iomanip>

using namespace std ;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<vvll> vvvll;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
typedef vector<vpii> vvpii;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;
typedef vector<vvpll> vvvpll;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef vector<vs> vvs;

#ifdef LocalHost
    #define eprintf(...) fprintf(stderr,__VA_ARGS__)
#endif

//Code Begins Here ------ >>>
const int N = 2e5 + 55;
vi g[N];
int p[N], vis[N], ans[N];
int n;

void dfs(int u, int comp) {
    vis[u] = comp;
    for (auto v : g[u]) {
        if (vis[v]) {
            if (vis[v] == comp) p[u] = -1;
        } else dfs(v, comp);
    }
}

int main(){
    #ifdef LocalHost
        freopen("input.txt","r",stdin);
    #endif

    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &p[i]);
        if (p[i] != i)  g[p[i]].push_back(i);
    }
    for (int i = 1; i <= n; i++) {
        if (!vis[i])
            dfs(i, i);
    }
    int idx, a, b;
    idx = a = b = 0;
    for (int i = 1; i <= n; i++) if (p[i] == i || p[i] == -1) {
        ans[idx++] = i;
        if (p[i] == -1) a++;
        else b++;
    }
    b = max(b - 1, 0);
    int res = a + b;
    cout << res << "\n";
    if (res) {
        for (int i = 0; i < idx - 1; i++)
            p[ans[i]] = ans[i + 1];
        p[ans[idx - 1]] = ans[idx - 1];
    }
    for (int i = 1; i <= n; i++)
        cout << p[i] << " ";

    #ifdef LocalHost
        cout<<endl<<"Execution time = "<<(float)clock()/CLOCKS_PER_SEC <<" s"<<endl;
    #endif

    return 0;
}


/**********************************************
            Author : smiley007
***********************************************/

//Data Structure Includes
#include <vector>
#include <queue>
#include <deque>
#include <bitset>
#include <stack>
#include <list>
#include <set>
#include <map>
#include <unordered_set>

//Other Includes
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <cctype>
#include <cassert>
#include <numeric>
#include <algorithm>
#include <ctime>
#include <sstream>
#include <climits>
#include <iomanip>

using namespace std ;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<vvll> vvvll;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
typedef vector<vpii> vvpii;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;
typedef vector<vvpll> vvvpll;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef vector<vs> vvs;

#ifdef LocalHost
    #define eprintf(...) fprintf(stderr,__VA_ARGS__)
#endif

//Code Begins Here ------ >>>
const int N = 2e5 + 55;
vi g[N];
int p[N], vis[N];
vi cycles, comp;
int n;

void dfs(int u, int comp) {
    vis[u] = comp;
    for (auto v : g[u]) {
        if (vis[v]) {
            if (vis[v] == comp) p[u] = -1;
        } else dfs(v, comp);
    }
}

int main(){
    #ifdef LocalHost
        freopen("input.txt","r",stdin);
    #endif

    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &p[i]);
        if (p[i] != i)  g[p[i]].push_back(i);
    }
    for (int i = 1; i <= n; i++) {
        if (!vis[i])
            dfs(i, i);
    }
    for (int i = 1; i <= n; i++) {
        if (p[i] == -1) cycles.push_back(i);
        else if (p[i] == i)  comp.push_back(i);
    }
    int a = (int)cycles.size(), b = (int)comp.size();
    b = max(b - 1, 0);
    int res = a + b;
    cout << res << "\n";
    if (res) {
        for (int i = 0; i < a - 1; i++)
            p[cycles[i]] = cycles[i + 1];
        if (a)
            p[cycles[a - 1]] = cycles[a - 1];
        for (int i = 0; i < b - 1; i++)
            p[comp[i]] = comp[i + 1];
        if (a && b) {
            p[cycles[a - 1]] = comp[0];
        }
    }
    for (int i = 1; i <= n; i++)
        cout << p[i] << " ";

    #ifdef LocalHost
        cout<<endl<<"Execution time = "<<(float)clock()/CLOCKS_PER_SEC <<" s"<<endl;
    #endif

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int OO = (int) 1e9;
const double eps = 1e-10;

#define db(x) 			cout<<"> "<<#x<<" = "<<(x)<<'\n'
#define db2(x,y) 		cout<<"> "<<#x<<" = "<<(x)<<", "<<#y<<" = "<<(y)<<'\n'
#define db3(x,y,z) 		cout<<"> "<<#x<<" = "<<(x)<<", "<<#y<<" = "<<(y)<<", "<<#z<<" = "<<(z)<<'\n'
#define sz(v) 			int(v.size())
#define trav(a,x) 		for(auto&a:x)
#define rep(i,v)		for(int i=0;i<sz(v);++i)
#define lp(i,n) 		for(int i=0;i<int(n);++i)
#define lpi(i,j,n) 		for(int i=j;i<int(n);++i)
#define lpd(i,j,n) 		for(int i=j;i>=int(n);--i)
#define ndl 			cout<<'\n'
#define all(v) 			v.begin(),v.end()
#define rall(v) 		v.rbegin(),v.rend()
#define present(a,x) 	(a.find(x)!=a.end())
#define cpresent(a,x) 	(find(all(a),x)!=a.end())
#define clr(v, d) 		memset(v,d,sizeof(v))
#define pb 				push_back
#define mp 				make_pair
#define fs 				first
#define sc 				second

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<vector<int> > vvi;
typedef map<int, int> mii;
typedef set<int> si;

const int MX=2e5+5;
int n,p[MX];
bool vis[MX],f[MX],done;
vvi adj(MX);

void dfs(int node,int parent=-1){/*
	db2(node,parent);*/
	vis[node]=true;
	trav(child,adj[node])
		if (child!=parent){/*
			db2(child,vis[child]);*/
			if (!vis[child])
				dfs(child,node);
			else if (vis[child]&&!done)
				p[node]=node,f[node]=done=true;
		}
}

int main() {
	ios::sync_with_stdio(false);
	cout.precision(10);
	cin>>n;
	lpi(i,1,n+1){
		cin>>p[i];
		if (p[i]!=i)
			adj[p[i]].pb(i),adj[i].pb(p[i]);
	}
	lpi(i,1,n+1)
		if (!vis[i])
			done=false,dfs(i);
	int root=-1;
	lpi(i,1,n+1)
		if (!f[i]&&p[i]==i)
			root=i;/*
	db(root);
	lpi(i,1,n+1)
		db(f[i]);*/
	if (root==-1)
		lpi(i,1,n+1)
			if (p[i]==i)
				root=i;
	lpi(i,1,n+1)
		if (p[i]==i&&i!=root)
			p[i]=root,f[i]=true;
	int ans=0;
	lpi(i,1,n+1)
		ans+=f[i];
	cout<<ans<<'\n';
	lpi(i,1,n+1)
		cout<<p[i]<<" \n"[i==n];
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int OO = (int) 1e9;
const double eps = 1e-10;

#define db(x) 			cout<<"> "<<#x<<" = "<<(x)<<'\n'
#define db2(x,y) 		cout<<"> "<<#x<<" = "<<(x)<<", "<<#y<<" = "<<(y)<<'\n'
#define db3(x,y,z) 		cout<<"> "<<#x<<" = "<<(x)<<", "<<#y<<" = "<<(y)<<", "<<#z<<" = "<<(z)<<'\n'
#define sz(v) 			int(v.size())
#define trav(a,x) 		for(auto&a:x)
#define rep(i,v)		for(int i=0;i<sz(v);++i)
#define lp(i,n) 		for(int i=0;i<int(n);++i)
#define lpi(i,j,n) 		for(int i=j;i<int(n);++i)
#define lpd(i,j,n) 		for(int i=j;i>=int(n);--i)
#define ndl 			cout<<'\n'
#define all(v) 			v.begin(),v.end()
#define rall(v) 		v.rbegin(),v.rend()
#define present(a,x) 	(a.find(x)!=a.end())
#define cpresent(a,x) 	(find(all(a),x)!=a.end())
#define clr(v, d) 		memset(v,d,sizeof(v))
#define pb 				push_back
#define mp 				make_pair
#define fs 				first
#define sc 				second

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<vector<int> > vvi;
typedef map<int, int> mii;
typedef set<int> si;

const int MX=2e5+5;
int n,p[MX];
bool vis[MX],f[MX];
vvi adj(MX);

int dfs(int node,int parent=-1){
	vis[node]=true;
	int ret=p[node]==node;
	trav(child,adj[node])
		if (child!=parent&&!vis[child])
			ret+=dfs(child,node);
	return ret;
}

int main() {
	ios::sync_with_stdio(false);
	cout.precision(10);
	cin>>n;
	lpi(i,1,n+1){
		cin>>p[i];
		if (p[i]!=i)
			adj[p[i]].pb(i),adj[i].pb(p[i]);
	}
	lpi(i,1,n+1)
		if (!vis[i])
			if (!dfs(i))
				p[i]=i,f[i]=true;
	int root=-1;
	lpi(i,1,n+1)
		if (!f[i]&&p[i]==i)
			root=i;
	if (root==-1)
		lpi(i,1,n+1)
			if (p[i]==i)
				root=i;
	lpi(i,1,n+1)
		if (p[i]==i&&i!=root)
			p[i]=root,f[i]=true;
	int ans=0;
	lpi(i,1,n+1)
		ans+=f[i];
	cout<<ans<<'\n';
	lpi(i,1,n+1)
		cout<<p[i]<<" \n"[i==n];
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int OO = (int) 1e9;
const double eps = 1e-10;

#define db(x) 			cout<<"> "<<#x<<" = "<<(x)<<'\n'
#define db2(x,y) 		cout<<"> "<<#x<<" = "<<(x)<<", "<<#y<<" = "<<(y)<<'\n'
#define db3(x,y,z) 		cout<<"> "<<#x<<" = "<<(x)<<", "<<#y<<" = "<<(y)<<", "<<#z<<" = "<<(z)<<'\n'
#define sz(v) 			int(v.size())
#define trav(a,x) 		for(auto&a:x)
#define rep(i,v)		for(int i=0;i<sz(v);++i)
#define lp(i,n) 		for(int i=0;i<int(n);++i)
#define lpi(i,j,n) 		for(int i=j;i<int(n);++i)
#define lpd(i,j,n) 		for(int i=j;i>=int(n);--i)
#define ndl 			cout<<'\n'
#define all(v) 			v.begin(),v.end()
#define rall(v) 		v.rbegin(),v.rend()
#define present(a,x) 	(a.find(x)!=a.end())
#define cpresent(a,x) 	(find(all(a),x)!=a.end())
#define clr(v, d) 		memset(v,d,sizeof(v))
#define pb 				push_back
#define mp 				make_pair
#define fs 				first
#define sc 				second

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<vector<int> > vvi;
typedef map<int, int> mii;
typedef set<int> si;

const int MX=2e5+5;
int n,p[MX];
bool vis[MX],f[MX],done;
vvi adj(MX);

void dfs(int node,int parent=-1){/*
	db2(node,parent);*/
	vis[node]=true;
	trav(child,adj[node])
		if (child!=parent&&!done){/*
			db2(child,vis[child]);*/
			if (!vis[child])
				dfs(child,node);
			else
				p[node]=node,f[node]=done=true;
		}
}

int main() {
	ios::sync_with_stdio(false);
	cout.precision(10);
	cin>>n;
	lpi(i,1,n+1){
		cin>>p[i];
		if (p[i]!=i)
			adj[p[i]].pb(i),adj[i].pb(p[i]);
	}
	lpi(i,1,n+1)
		if (!vis[i])
			done=false,dfs(i);
	int root=-1;
	lpi(i,1,n+1)
		if (!f[i]&&p[i]==i)
			root=i;
	if (root==-1)
		lpi(i,1,n+1)
			if (p[i]==i)
				root=i;
	lpi(i,1,n+1)
		if (p[i]==i&&i!=root)
			p[i]=root,f[i]=true;
	int ans=0;
	lpi(i,1,n+1)
		ans+=f[i];
	cout<<ans<<'\n';
	lpi(i,1,n+1)
		cout<<p[i]<<" \n"[i==n];
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int max_n = 200000 + 64;
int N, inp[max_n], setNum[max_n], Ans(0), root(0);

void findOwnRoot(int n){

	static int setNo = 0;

	setNo++;

	setNum[n] = setNo;
	while(setNum[inp[n]] == 0)
		setNum[ n = inp[n] ] = setNo;

	if (setNum[inp[n]] == setNo && inp[n] != root){

		Ans++;

		if(root==0) root = inp[n]; // first one

		inp[inp[n]] = root;

	}

}

int main(){

	cin >> N;
	for(int i=1;i<=N;i++){
		cin >> inp[i];
		if(inp[i]==i) root = i;
	}

	if (root == 0){
		root =  1;
		inp[1] = 1;
	}

	for(int i=1;i<=N;i++){
		if(setNum[i] == 0)
			findOwnRoot(i);
	}

	cout << Ans << endl;
	for(int i=1;i<=N;i++){
		cout << inp[i] << ' ';
	}

}


#include <bits/stdc++.h>
using namespace std;
const int max_n = 200000 + 64;
int N, inp[max_n], setNum[max_n], Ans(0), root(0);

void findOwnRoot(int n){

	static int setNo = 0;

	setNo++;

	setNum[n] = setNo;
	while(setNum[inp[n]] == 0)
		setNum[ n = inp[n] ] = setNo;

	if (setNum[inp[n]] == setNo && inp[n] != root){

		Ans++;

		if(root==0) root = inp[n]; // first one

		inp[n] = root;

	}

}

int main(){

	cin >> N;
	for(int i=1;i<=N;i++){
		cin >> inp[i];
		if(inp[i]==i) root = i;
	}

	if (root == 0){
		root =  1;
		inp[1] = 1;
	}

	for(int i=1;i<=N;i++){
		if(setNum[i] == 0)
			findOwnRoot(i);
	}

	cout << Ans << endl;
	for(int i=1;i<=N;i++){
		cout << inp[i] << ' ';
	}

}


#include <bits/stdc++.h>
using namespace std;

vector<int> vec,unio,uniq;

int fir(int n){
	return unio[n] = unio[n]==n? n : fir(unio[n]);
}

int main(){

	cin.sync_with_stdio(0);

	int N,ans=0;
	cin >> N;
	vec.resize(N+1);
	unio.resize(N+1);
	iota(begin(unio),end(unio),0);

	int root = 0;

	for(int i=1;i<=N;i++){
		cin >> vec[i];
		if(vec[i]==i) root = i;
	}

	if(root == 0){
		ans++;
		vec[1] = 1;
	}

	for(int i=1;i<=N;i++)
		unio[fir(i)] = fir(vec[i]);

	for(int i=1; i<=N;i++)
		if(fir(i)==i)
			uniq.push_back(i);

	for(int i : uniq){
		if(vec[i] != root)
			ans++;
		vec[i] = root;
	}

	cout << ans << endl;

	for(int i=1;i<=N;i++)
		cout << vec[i] << ' ';
}


#include <bits/stdc++.h>
using namespace std;

vector<int> vec,unio,uniq;

int fir(int n){
	return unio[n] = unio[n]==n? n : fir(unio[n]);
}

int main(){

	cin.sync_with_stdio(0);

	int N;
	cin >> N;
	vec.resize(N+1);
	unio.resize(N+1);
	iota(begin(unio),end(unio),0);
	for(int i=1;i<=N;i++)
		cin >> vec[i];

	for(int i=1;i<=N;i++)
		unio[fir(i)] = fir(vec[i]);

	for(int i=1; i<=N;i++)
		if(fir(i)==i){
			uniq.push_back(i);

	cout << uniq.size()-1 << endl;

	for(int i : uniq)
		vec[i] = uniq[0];

	for(int i=1;i<=N;i++)
		cout << vec[i] << ' ';
}


#include <bits/stdc++.h>
using namespace std;

vector<int> vec,unio,uniq;

int fir(int n){
	return unio[n] = unio[n]==n? n : fir(unio[n]);
}

int main(){

	cin.sync_with_stdio(0);

	int N;
	cin >> N;
	vec.resize(N+1);
	unio.resize(N+1);
	iota(begin(unio),end(unio),0);
	for(int i=1;i<=N;i++)
		cin >> vec[i];

	for(int i=1;i<=N;i++)
		unio[fir(i)] = fir(vec[i]);

	for(int i=1; i<=N;i++)
		if(fir(i)==i)
			uniq.push_back(i);

	cout << uniq.size()-1 << endl;

	for(int i : uniq)
		vec[i] = uniq[0];

	for(int i=1;i<=N;i++)
		cout << vec[i] << ' ';
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;

struct edge{
    int u, pre;
};

const int N = 2e5 + 10;
const int INF = 1e9;

vector<edge> edges[N];
int sup[N];
int check[N];
int mp[N];

void solve(){

    int n, v, u;
    cin >> n;

    for(int i = 1; i < n; i++){
        cin >> v >> u;
        int pre = min(v,u);
        edges[v].push_back({u,pre});
        edges[u].push_back({v,pre});
        sup[v] = min(sup[v] == 0 ? INF : sup[v] ,pre);
        sup[u] = min(sup[u] == 0 ? INF : sup[u], pre);
    }

    for(int i = 1; i <= n; i++){
        cin >> check[i];
        mp[check[i]]++;
        if(mp[check[i]] == 2){
            cout << "No";
            return;
        }
    }

    if(check[1] != 1){
        cout << "No";
        return;
    }

    int ind = 2;

    fill(mp, mp + n + 1, 0);
    int sz;
    for(int i = 1; i <= n; i++){

        sz = edges[check[i]].size();

        if(i != 1)
            sz--;

        for(int j = ind; j < ind + sz; j++){
            mp[check[j]] = 1;
        }

        for(auto u : edges[check[i]]){
            if(!mp[u.u] && u.u != sup[check[i]]){
                cout << "No";
                return;
            }
        }
        ind += sz;
    }

    cout << "Yes";
}

int main(){
    //freopen("in.txt", "r", stdin);
    solve();

}


#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;

struct edge{
    int u, pre;
};

const int N = 2e5 + 10;

vector<edge> edges[N];
vector<int> sup;
int check[N];
int mp[N];
int f = 0;

void bfs(edge s){

    queue<edge> q;
    q.push(s);
    edge v;
    sup.push_back(1);

    while(!q.empty()){
        v = q.front();
        q.pop();

        for(auto u : edges[v.u]){
            if(u.u != v.pre){
                sup.push_back(u.u);
                q.push(u);
            }
        }
    }
}

void solve(){

    int n, v, u;
    cin >> n;

    for(int i = 1; i < n; i++){
        cin >> v >> u;
        int pre = min(v,u);
        edges[v].push_back({u,pre});
        edges[u].push_back({v,pre});
    }

    for(int i = 1; i <= n; i++){
        cin >> check[i];
        mp[check[i]]++;
        if(mp[check[i]] == 2){
            cout << "No";
            return;
        }
    }

    if(check[1] != 1){
        cout << "No";
        return;
    }

    int ind = 2;

    fill(mp, mp + n + 1, 0);

    for(int i = 1; i <= n; i++){

        for(int j = ind; j < ind + edges[check[i]].size(); j++){
            mp[check[j]] = 1;
        }

        for(auto u : edges[check[i]]){
            if(!mp[u.u] && u.u > i){
                cout << "No";
                return;
            }
        }
        ind += edges[check[i]].size() - 1;
    }

    cout << "Yes";
}

int main(){
    //freopen("in.txt", "r", stdin);
    solve();

}