#include <bits/stdc++.h>
#pragma GCC optimize("-O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#define mp make_pair
#define ll long long
#define fi first
#define se second
#define fr front()
#define pb push_back
#define sqr(a) ((a)*(a))
#define pll pair<ll,ll>
#define y1 Enkognit

using namespace std;

const ll MOD = 998244353;

ll n, m, k, q, l, r, i, j, x, y, ans, a[200001], b[200001];
ll tt[200001];
ll z[200001];
char c[200001];

int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    for (int i = 0; i < n; i++)
    {
        cin >> b[i];
    }
    ll p=0;
    for (int i = 0; i < n; i++)
    {
        if (tt[a[i]]) p--; else p++;
        tt[a[i]]++;
        if (tt[b[i]]) p--; else p++;
        tt[b[i]]++;
        if (p==0) z[i]=1;
    }
    ll r=0;
    for (int i = 0; i < n; i++)
    {
        c[i]=r+'a';
        r+=z[i];
        if (r+'a'>'z') r--;
    }
    if (r>=m) {cout << "YES\n";for (int i = 0; i < n; i++) cout << c[i];} else cout <<"NO\n";
    return 0;
}
/*
10 3
9 30 45 2 5 7 10 3 16 22
*/


#include <bits/stdc++.h>
#pragma GCC optimize("-O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#define mp make_pair
#define ll long long
#define fi first
#define se second
#define fr front()
#define pb push_back
#define sqr(a) ((a)*(a))
#define pll pair<ll,ll>
#define y1 Enkognit

using namespace std;

const ll MOD = 998244353;

ll n, m, k, q, l, r, i, j, x, y, ans, a[300001], b[300001];
ll tt[300001];
ll z[300001];
char c[300001];

int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    for (int i = 0; i < n; i++)
    {
        cin >> b[i];
    }
    ll p=0;
    for (int i = 0; i < n; i++)
    {
        if (tt[a[i]]) p--; else p++;
        tt[a[i]]++;
        if (tt[b[i]]) p--; else p++;
        tt[b[i]]++;
        if (p==0) z[i]=1;
    }
    ll r=0;
    for (int i = 0; i < n; i++)
    {
        c[a[i]]=r+'a';
        r+=z[i];
        if (r+'a'>'z' || r>m) r--;
    }
    if (r>=m) {cout << "YES\n";for (int i = 1; i <= n; i++) cout << c[i];} else cout <<"NO\n";
    return 0;
}
/*
10 3
9 30 45 2 5 7 10 3 16 22
*/


#include <bits/stdc++.h>
#pragma GCC optimize("-O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#define mp make_pair
#define ll long long
#define fi first
#define se second
#define fr front()
#define pb push_back
#define sqr(a) ((a)*(a))
#define pll pair<ll,ll>
#define y1 Enkognit

using namespace std;

const ll MOD = 998244353;

ll n, m, k, q, l, r, i, j, x, y, ans, a[200001], b[200001];
ll tt[200001];
ll z[200001];
char c[200001];

int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    for (int i = 0; i < n; i++)
    {
        cin >> b[i];
    }
    ll p=0;
    for (int i = 0; i < n; i++)
    {
        if (tt[a[i]]) p--; else p++;
        tt[a[i]]++;
        if (tt[b[i]]) p--; else p++;
        tt[b[i]]++;
        if (p==0) z[i]=1;
    }
    ll r=0;
    for (int i = 0; i < n; i++)
    {
        c[i]=r+'a';
        r+=z[i];
        if (r+'a'>'z' || r>m) r--;
    }
    if (r>=m) {cout << "YES\n";for (int i = 0; i < n; i++) cout << c[i];} else cout <<"NO\n";
    return 0;
}
/*
10 3
9 30 45 2 5 7 10 3 16 22
*/


#include <bits/stdc++.h>
#pragma GCC optimize("-O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#define mp make_pair
#define ll long long
#define fi first
#define se second
#define fr front()
#define pb push_back
#define sqr(a) ((a)*(a))
#define pll pair<ll,ll>
#define y1 Enkognit

using namespace std;

const ll MOD = 998244353;

ll n, m, k, q, l, r, i, j, x, y, ans, a[200001], b[200001];
ll tt[200001];
ll z[200001];
char c[200001];

int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    for (int i = 0; i < n; i++)
    {
        cin >> b[i];
    }
    ll p=0;
    for (int i = 0; i < n; i++)
    {
        if (tt[a[i]]) p--; else p++;
        tt[a[i]]++;
        if (tt[b[i]]) p--; else p++;
        tt[b[i]]++;
        if (p==0) z[i]=1;
    }
    ll r=0;
    for (int i = 0; i < n; i++)
    {
        c[i]=r+'a';
        r+=z[i];
    }
    if (r+1>=m) {cout << "YES\n";for (int i = 0; i < n; i++) cout << c[i];} else cout <<"NO\n";
    return 0;
}
/*
10 3
9 30 45 2 5 7 10 3 16 22
*/


#include <bits/stdc++.h>
#pragma GCC optimize("-O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#define mp make_pair
#define ll long long
#define fi first
#define se second
#define fr front()
#define pb push_back
#define sqr(a) ((a)*(a))
#define pll pair<ll,ll>
#define y1 Enkognit

using namespace std;

const ll MOD = 998244353;

ll n, m, k, q, l, r, i, j, x, y, ans, a[200001], b[200001];
ll tt[200001];
ll z[200001];
char c[200001];

int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    for (int i = 0; i < n; i++)
    {
        cin >> b[i];
    }
    ll p=0;
    for (int i = 0; i < n; i++)
    {
        if (tt[a[i]]) p--; else p++;
        tt[a[i]]++;
        if (tt[b[i]]) p--; else p++;
        tt[b[i]]++;
        if (p==0) z[i]=1;
    }
    ll r=0;
    for (int i = 0; i < n; i++)
    {
        c[i]=r+'a';
        r+=z[i];
    }
    if (r+1>=k) {cout << "YES\n";for (int i = 0; i < n; i++) cout << c[i];} else cout <<"NO\n";
    return 0;
}
/*
10 3
9 30 45 2 5 7 10 3 16 22
*/


#include <bits/stdc++.h>
#pragma GCC optimize("-O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#define mp make_pair
#define ll long long
#define fi first
#define se second
#define fr front()
#define pb push_back
#define sqr(a) ((a)*(a))
#define pll pair<ll,ll>
#define y1 Enkognit

using namespace std;

const ll MOD = 998244353;

ll n, m, k, q, l, r, i, j, x, y, ans, a[200001], b[200001];
ll tt[200001];
ll z[200001];
char c[200001];

int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    for (int i = 0; i < n; i++)
    {
        cin >> b[i];
    }
    ll p=0;
    for (int i = 0; i < n; i++)
    {
        if (tt[a[i]]) p--; else p++;
        tt[a[i]]++;
        if (tt[b[i]]) p--; else p++;
        tt[b[i]]++;
        if (p==0) z[i]=1;
    }
    ll r=0;
    for (int i = 0; i < n; i++)
    {
        c[i]=r+'a';
        r+=z[i];
    }
    if (r>=m) {cout << "YES\n";for (int i = 0; i < n; i++) cout << c[i];} else cout <<"NO\n";
    return 0;
}
/*
10 3
9 30 45 2 5 7 10 3 16 22
*/


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

int n, k, p[200005], q[200005], gi[200005], rc, mp[200005], up[200005], am, rf, shg[200005];
vector<int> v[200005];
vector<pair<int, int> > ve;
bool bo[200005], boo[200005], ce[200005];
int fp[200005], sg[2][200005];
char ffp[200005];

int bp(int dg) {
	if(dg == up[dg]) return dg;
	return up[dg] = bp(up[dg]);
}

void ts(int dg) {
	bo[dg] = 1;
	for(int i = 0; i < v[dg].size(); ++i) {
		if(!bo[v[dg][i]]) {
			mp[v[dg][i]] = dg;
			ts(v[dg][i]);
		} else if(!boo[v[dg][i]]) {
			int rg = dg;
			while(true) {
				ce[rg] = 1;
				up[bp(rg)] = bp(v[dg][i]);
				if(bp(rg) == bp(v[dg][i])) break;
				rg = mp[rg];
			}
		} else if(ce[v[dg][i]]) {
			if(!boo[bp(v[dg][i])]) {
				int rg = dg;
				while(true) {
					ce[rg] = 1;
					up[bp(rg)] = bp(v[dg][i]);
					if(bp(rg) == bp(v[dg][i])) break;
					rg = mp[rg];
				}
			}
		}
	}
	gi[++rc] = dg;
	boo[dg] = 1;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cin >> n >> k;
	for(int i = 1; i <= n; ++i) {
		sg[0][i] = 1e9;
		up[i] = i;
		cin >> p[i];
		if(i > 1) {
			v[p[i]].push_back(p[i - 1]);
		}
	}
	for(int i = 1; i <= n; ++i) {
		cin >> p[i];
		if(i > 1) {
			v[p[i]].push_back(p[i - 1]);
		}
	}
	for(int i = 1; i <= n; ++i) {
		if(!bo[i]) {
			ts(i);
		}
	}
	for(int i = 1; i <= n; ++i) {
		fp[gi[i]] = i;
	}
	for(int i = 1; i <= n; ++i) {
		sg[0][bp(i)] = min(sg[0][bp(i)], fp[i]);
		sg[1][bp(i)] = max(sg[1][bp(i)], fp[i]);
		//cout << bp(i) << " ";
	}
	for(int i = 1; i <= n; ++i) {
		if(sg[0][i] != 1e9) {
			ve.push_back({sg[0][i], sg[1][i]});
		}
	}
	sort(ve.begin(), ve.end());
	for(int i = 0; i < ve.size(); ++i) {
		//cout << ve[i].first << " " << ve[i].second << endl;
		if(am < ve[i].first) {
			++rf;
			for(int j = ve[i].first; j <= ve[i].second; ++j) {
				shg[j] = rf;
			}
			am = ve[i].second;
		} else {
			for(int j = am + 1; j <= ve[i].second; ++j) {
				shg[j] = rf;
			}
			am = max(am, ve[i].second);
		}
	}
	int shk = 0;
	for(int i = 1; i <= n; ++i) {
	    if(shg[i] <= 0) {
	        cout << shg[i] << " " << i << endl;
	    }
		ffp[gi[i]] = min('z', (char)('a' + shg[i] - 1));
		shk = max(shk, shg[i]);
	}
	if(shk < k) {
		cout << "NO" << endl;
		return 0;
	}
	cout << "YES" << endl;
	for(int i = 1; i <= n; ++i) {
		cout << ffp[i];
	}
	cout << endl;
	return 0;
}


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

int n, k, p[200005], q[200005], gi[200005], rc, mp[200005], up[200005], am, rf, shg[200005];
vector<int> v[200005];
vector<pair<int, int> > ve;
bool bo[200005], boo[200005], ce[200005];
int fp[200005], sg[2][200005];
char ffp[200005];

int bp(int dg) {
	if(dg == up[dg]) return dg;
	return up[dg] = bp(up[dg]);
}

void ts(int dg) {
	bo[dg] = 1;
	for(int i = 0; i < v[dg].size(); ++i) {
		if(!bo[v[dg][i]]) {
			mp[v[dg][i]] = dg;
			ts(v[dg][i]);
		} else if(!boo[v[dg][i]]) {
			int rg = dg;
			while(true) {
				ce[rg] = 1;
				up[bp(rg)] = bp(v[dg][i]);
				if(bp(rg) == bp(v[dg][i])) break;
				rg = mp[rg];
			}
		} else if(ce[v[dg][i]]) {
			if(!boo[bp(v[dg][i])]) {
				int rg = dg;
				while(true) {
					ce[rg] = 1;
					up[bp(rg)] = bp(v[dg][i]);
					if(bp(rg) == bp(v[dg][i])) break;
					rg = mp[rg];
				}
			}
		}
	}
	gi[++rc] = dg;
	boo[dg] = 1;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cin >> n >> k;
	for(int i = 1; i <= n; ++i) {
		sg[0][i] = 1e9;
		up[i] = i;
		cin >> p[i];
		if(i > 1) {
			v[p[i]].push_back(p[i - 1]);
		}
	}
	for(int i = 1; i <= n; ++i) {
		cin >> p[i];
		if(i > 1) {
			v[p[i]].push_back(p[i - 1]);
		}
	}
	for(int i = 1; i <= n; ++i) {
		if(!bo[i]) {
			ts(i);
		}
	}
	for(int i = 1; i <= n; ++i) {
		fp[gi[i]] = i;
	}
	for(int i = 1; i <= n; ++i) {
		sg[0][bp(i)] = min(sg[0][bp(i)], fp[i]);
		sg[1][bp(i)] = max(sg[1][bp(i)], fp[i]);
		//cout << bp(i) << " ";
	}
	for(int i = 1; i <= n; ++i) {
		if(sg[0][i] != 1e9) {
			ve.push_back({sg[0][i], sg[1][i]});
		}
	}
	sort(ve.begin(), ve.end());
	for(int i = 0; i < ve.size(); ++i) {
		//cout << ve[i].first << " " << ve[i].second << endl;
		if(am < ve[i].first) {
			++rf;
			for(int j = ve[i].first; j <= ve[i].second; ++j) {
				shg[j] = rf;
			}
			am = ve[i].second;
		} else {
			for(int j = am + 1; j <= ve[i].second; ++j) {
				shg[j] = rf;
			}
			am = max(am, ve[i].second);
		}
	}
	int shk = 0;
	for(int i = 1; i <= n; ++i) {
	    if(shg[i] == 0) shg[i] = 1;
		ffp[gi[i]] = min('z', (char)('a' + shg[i] - 1));
		shk = max(shk, shg[i]);
	}
	if(shk < k) {
		cout << "NO" << endl;
		return 0;
	}
	cout << "YES" << endl;
	for(int i = 1; i <= n; ++i) {
		cout << ffp[i];
	}
	cout << endl;
	return 0;
}


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

int n, k, p[200005], q[200005], gi[200005], rc, mp[200005], up[200005], am, rf, shg[200005];
vector<int> v[200005];
vector<pair<int, int> > ve;
bool bo[200005], boo[200005], ce[200005];
int fp[200005], sg[2][200005];
char ffp[200005];

int bp(int dg) {
	if(dg == up[dg]) return dg;
	return up[dg] = bp(up[dg]);
}

void ts(int dg) {
	bo[dg] = 1;
	for(int i = 0; i < v[dg].size(); ++i) {
		if(!bo[v[dg][i]]) {
			mp[v[dg][i]] = dg;
			ts(v[dg][i]);
		} else if(!boo[v[dg][i]]) {
			int rg = dg;
			while(true) {
				ce[rg] = 1;
				up[bp(rg)] = bp(v[dg][i]);
				if(bp(rg) == bp(v[dg][i])) break;
				rg = mp[rg];
			}
		} else if(ce[v[dg][i]]) {
			if(!boo[bp(v[dg][i])]) {
				int rg = dg;
				while(true) {
					ce[rg] = 1;
					up[bp(rg)] = bp(v[dg][i]);
					if(bp(rg) == bp(v[dg][i])) break;
					rg = mp[rg];
				}
			}
		}
	}
	gi[++rc] = dg;
	boo[dg] = 1;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cin >> n >> k;
	for(int i = 1; i <= n; ++i) {
		sg[0][i] = 1e9;
		up[i] = i;
		cin >> p[i];
		if(i > 1) {
			v[p[i]].push_back(p[i - 1]);
		}
	}
	for(int i = 1; i <= n; ++i) {
		cin >> p[i];
		if(i > 1) {
			v[p[i]].push_back(p[i - 1]);
		}
	}
	for(int i = 1; i <= n; ++i) {
		if(!bo[i]) {
			ts(i);
		}
	}
	for(int i = 1; i <= n; ++i) {
		fp[gi[i]] = i;
	}
	for(int i = 1; i <= n; ++i) {
		sg[0][bp(i)] = min(sg[0][bp(i)], fp[i]);
		sg[1][bp(i)] = max(sg[1][bp(i)], fp[i]);
		//cout << bp(i) << " ";
	}
	for(int i = 1; i <= n; ++i) {
		if(sg[0][i] != 1e9) {
			ve.push_back({sg[0][i], sg[1][i]});
		}
	}
	sort(ve.begin(), ve.end());
	for(int i = 0; i < ve.size(); ++i) {
		//cout << ve[i].first << " " << ve[i].second << endl;
		if(am < ve[i].first) {
			++rf;
			for(int j = ve[i].first; j <= ve[i].second; ++j) {
				shg[j] = rf;
			}
			am = ve[i].second;
		} else {
			for(int j = am + 1; j <= ve[i].second; ++j) {
				shg[j] = rf;
			}
			am = max(am, ve[i].second);
		}
	}
	int shk = 0;
	for(int i = 1; i <= n; ++i) {
		ffp[gi[i]] = min('z', (char)('a' + shg[i] - 1));
		shk = max(shk, shg[i]);
	}
	if(shk < k) {
		cout << "NO" << endl;
		return 0;
	}
	cout << "YES" << endl;
	for(int i = 1; i <= n; ++i) {
		cout << ffp[i];
	}
	cout << endl;
	return 0;
}


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

int n, k, p[200005], q[200005], gi[200005], rc, mp[200005], up[200005], am, rf, shg[200005];
vector<int> v[200005];
vector<pair<int, int> > ve;
bool bo[200005], boo[200005], ce[200005];
int fp[200005], sg[2][200005];
char ffp[200005];

int bp(int dg) {
	if(dg == up[dg]) return dg;
	return up[dg] = bp(up[dg]);
}

void ts(int dg) {
	bo[dg] = 1;
	for(int i = 0; i < v[dg].size(); ++i) {
		if(!bo[v[dg][i]]) {
			mp[v[dg][i]] = dg;
			ts(v[dg][i]);
		} else if(!boo[v[dg][i]]) {
			int rg = dg;
			while(true) {
				ce[rg] = 1;
				up[bp(rg)] = bp(v[dg][i]);
				if(bp(rg) == bp(v[dg][i])) break;
				rg = mp[rg];
			}
		} else if(ce[v[dg][i]]) {
			if(!boo[bp(v[dg][i])]) {
				int rg = dg;
				while(true) {
					ce[rg] = 1;
					up[bp(rg)] = bp(v[dg][i]);
					if(bp(rg) == bp(v[dg][i])) break;
					rg = mp[rg];
				}
			}
		}
	}
	gi[++rc] = dg;
	boo[dg] = 1;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cin >> n >> k;
	for(int i = 1; i <= n; ++i) {
		sg[0][i] = 1e9;
		up[i] = i;
		cin >> p[i];
		if(i > 1) {
			v[p[i]].push_back(p[i - 1]);
		}
	}
	for(int i = 1; i <= n; ++i) {
		cin >> p[i];
		if(i > 1) {
			v[p[i]].push_back(p[i - 1]);
		}
	}
	for(int i = 1; i <= n; ++i) {
		if(!bo[i]) {
			ts(i);
		}
	}
	for(int i = 1; i <= n; ++i) {
		fp[gi[i]] = i;
	}
	for(int i = 1; i <= n; ++i) {
		sg[0][bp(i)] = min(sg[0][bp(i)], fp[i]);
		sg[1][bp(i)] = max(sg[1][bp(i)], fp[i]);
		//cout << bp(i) << " ";
	}
	for(int i = 1; i <= n; ++i) {
		if(sg[0][i] != 1e9) {
			ve.push_back({sg[0][i], sg[1][i]});
		}
	}
	sort(ve.begin(), ve.end());
	for(int i = 0; i < ve.size(); ++i) {
		//cout << ve[i].first << " " << ve[i].second << endl;
		if(am < ve[i].first) {
			++rf;
			for(int j = ve[i].first; j <= ve[i].second; ++j) {
				shg[j] = rf;
			}
			am = ve[i].second;
		} else {
			for(int j = am + 1; j <= ve[i].second; ++j) {
				shg[j] = rf;
			}
			am = max(am, ve[i].second);
		}
	}
	int shk = 0;
	for(int i = 1; i <= n; ++i) {
		ffp[gi[i]] = min('z', (char)('a' + shg[i] - 1));
	    assert(ffp[gi[i]] >= 'a' && ffp[gi[i]] <= 'z');
		shk = max(shk, shg[i]);
	}
	if(shk < k) {
		cout << "NO" << endl;
		return 0;
	}
	cout << "YES" << endl;
	for(int i = 1; i <= n; ++i) {
		cout << ffp[i];
	}
	cout << endl;
	return 0;
}


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

int n, k, p[200005], q[200005], gi[200005], rc, mp[200005], up[200005], am, rf, shg[200005];
vector<int> v[200005];
vector<pair<int, int> > ve;
bool bo[200005], boo[200005], ce[200005];
int fp[200005], sg[2][200005];
char ffp[200005];

int bp(int dg) {
	if(dg == up[dg]) return dg;
	return up[dg] = bp(up[dg]);
}

void ts(int dg) {
	bo[dg] = 1;
	for(int i = 0; i < v[dg].size(); ++i) {
		if(!bo[v[dg][i]]) {
			mp[v[dg][i]] = dg;
			ts(v[dg][i]);
		} else if(!boo[v[dg][i]]) {
			int rg = dg;
			while(true) {
				ce[rg] = 1;
				up[bp(rg)] = bp(v[dg][i]);
				if(bp(rg) == bp(v[dg][i])) break;
				rg = mp[rg];
			}
		} else if(ce[v[dg][i]]) {
			if(!boo[bp(v[dg][i])]) {
				int rg = dg;
				while(true) {
					ce[rg] = 1;
					up[bp(rg)] = bp(v[dg][i]);
					if(bp(rg) == bp(v[dg][i])) break;
					rg = mp[rg];
				}
			}
		}
	}
	gi[++rc] = dg;
	boo[dg] = 1;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cin >> n >> k;
	for(int i = 1; i <= n; ++i) {
		sg[0][i] = 1e9;
		up[i] = i;
		cin >> p[i];
		if(i > 1) {
			v[p[i]].push_back(p[i - 1]);
		}
	}
	for(int i = 1; i <= n; ++i) {
		cin >> p[i];
		if(i > 1) {
			v[p[i]].push_back(p[i - 1]);
		}
	}
	for(int i = 1; i <= n; ++i) {
		if(!bo[i]) {
			ts(i);
		}
	}
	for(int i = 1; i <= n; ++i) {
		fp[gi[i]] = i;
	}
	for(int i = 1; i <= n; ++i) {
		sg[0][bp(i)] = min(sg[0][bp(i)], fp[i]);
		sg[1][bp(i)] = max(sg[1][bp(i)], fp[i]);
		//cout << bp(i) << " ";
	}
	for(int i = 1; i <= n; ++i) {
		if(sg[0][i] != 1e9) {
			ve.push_back({sg[0][i], sg[1][i]});
		}
	}
	sort(ve.begin(), ve.end());
	for(int i = 0; i < ve.size(); ++i) {
		//cout << ve[i].first << " " << ve[i].second << endl;
		if(am < ve[i].first) {
			++rf;
			for(int j = ve[i].first; j <= ve[i].second; ++j) {
				shg[j] = rf;
			}
			am = ve[i].second;
		} else {
			for(int j = am + 1; j <= ve[i].second; ++j) {
				shg[j] = rf;
			}
			am = max(am, ve[i].second);
		}
	}
	int shk = 0;
	for(int i = 1; i <= n; ++i) {
	    if(shg[i] > 26) shg[i] = 26;
		ffp[gi[i]] = (char)('a' + shg[i] - 1);
		shk = max(shk, shg[i]);
	}
	if(shk < k) {
		cout << "NO" << endl;
		return 0;
	}
	cout << "YES" << endl;
	for(int i = 1; i <= n; ++i) {
		cout << ffp[i];
	}
	cout << endl;
	return 0;
}


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

int n, k, p[200005], q[200005], gi[200005], rc, mp[200005], up[200005], am, rf, shg[200005];
vector<int> v[200005];
vector<pair<int, int> > ve;
bool bo[200005], boo[200005], ce[200005];
int fp[200005], sg[2][200005];
char ffp[200005];

int bp(int dg) {
	if(dg == up[dg]) return dg;
	return up[dg] = bp(up[dg]);
}

void ts(int dg) {
	bo[dg] = 1;
	for(int i = 0; i < v[dg].size(); ++i) {
		if(!bo[v[dg][i]]) {
			mp[v[dg][i]] = dg;
			ts(v[dg][i]);
		} else if(!boo[v[dg][i]]) {
			int rg = dg;
			while(true) {
				ce[rg] = 1;
				up[bp(rg)] = bp(v[dg][i]);
				if(bp(rg) == bp(v[dg][i])) break;
				rg = mp[rg];
			}
		} else if(ce[v[dg][i]]) {
			if(!boo[bp(v[dg][i])]) {
				int rg = dg;
				while(true) {
					ce[rg] = 1;
					up[bp(rg)] = bp(v[dg][i]);
					if(bp(rg) == bp(v[dg][i])) break;
					rg = mp[rg];
				}
			}
		}
	}
	gi[++rc] = dg;
	boo[dg] = 1;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cin >> n >> k;
	for(int i = 1; i <= n; ++i) {
		sg[0][i] = 1e9;
		up[i] = i;
		cin >> p[i];
		if(i > 1) {
			v[p[i]].push_back(p[i - 1]);
		}
	}
	for(int i = 1; i <= n; ++i) {
		cin >> p[i];
		if(i > 1) {
			v[p[i]].push_back(p[i - 1]);
		}
	}
	for(int i = 1; i <= n; ++i) {
		if(!bo[i]) {
			ts(i);
		}
	}
	for(int i = 1; i <= n; ++i) {
		fp[gi[i]] = i;
	}
	for(int i = 1; i <= n; ++i) {
		sg[0][bp(i)] = min(sg[0][bp(i)], fp[i]);
		sg[1][bp(i)] = max(sg[1][bp(i)], fp[i]);
		//cout << bp(i) << " ";
	}
	for(int i = 1; i <= n; ++i) {
		if(sg[0][i] != 1e9) {
			ve.push_back({sg[0][i], sg[1][i]});
		}
	}
	sort(ve.begin(), ve.end());
	for(int i = 0; i < ve.size(); ++i) {
		//cout << ve[i].first << " " << ve[i].second << endl;
		if(am < ve[i].first) {
			++rf;
			for(int j = ve[i].first; j <= ve[i].second; ++j) {
				shg[j] = rf;
			}
			am = ve[i].second;
		} else {
			for(int j = am + 1; j <= ve[i].second; ++j) {
				shg[j] = rf;
			}
			am = max(am, ve[i].second);
		}
	}
	int shk = 0;
	for(int i = 1; i <= n; ++i) {
	    assert(shg[i]);
		ffp[gi[i]] = min('z', (char)('a' + shg[i] - 1));
		shk = max(shk, shg[i]);
	}
	if(shk < k) {
		cout << "NO" << endl;
		return 0;
	}
	cout << "YES" << endl;
	for(int i = 1; i <= n; ++i) {
		cout << ffp[i];
	}
	cout << endl;
	return 0;
}


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

int n, k, p[200005], q[200005], gi[200005], rc, mp[200005], up[200005], am, rf, shg[200005];
vector<int> v[200005];
vector<pair<int, int> > ve;
bool bo[200005], boo[200005], ce[200005];
int fp[200005], sg[2][200005];
char ffp[200005];

int bp(int dg) {
	if(dg == up[dg]) return dg;
	return up[dg] = bp(up[dg]);
}

void ts(int dg) {
	bo[dg] = 1;
	for(int i = 0; i < v[dg].size(); ++i) {
		if(!bo[v[dg][i]]) {
			mp[v[dg][i]] = dg;
			ts(v[dg][i]);
		} else if(!boo[v[dg][i]]) {
			int rg = dg;
			while(true) {
				ce[rg] = 1;
				up[bp(rg)] = bp(v[dg][i]);
				if(bp(rg) == bp(v[dg][i])) break;
				rg = mp[rg];
			}
		} else if(ce[v[dg][i]]) {
			if(!boo[bp(v[dg][i])]) {
				int rg = dg;
				while(true) {
					ce[rg] = 1;
					up[bp(rg)] = bp(v[dg][i]);
					if(bp(rg) == bp(v[dg][i])) break;
					rg = mp[rg];
				}
			}
		}
	}
	gi[++rc] = dg;
	boo[dg] = 1;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cin >> n >> k;
	for(int i = 1; i <= n; ++i) {
		sg[0][i] = 1e9;
		up[i] = i;
		cin >> p[i];
		if(i > 1) {
			v[p[i]].push_back(p[i - 1]);
		}
	}
	for(int i = 1; i <= n; ++i) {
		cin >> p[i];
		if(i > 1) {
			v[p[i]].push_back(p[i - 1]);
		}
	}
	for(int i = 1; i <= n; ++i) {
		if(!bo[i]) {
			ts(i);
		}
	}
	for(int i = 1; i <= n; ++i) {
		fp[gi[i]] = i;
	}
	for(int i = 1; i <= n; ++i) {
		sg[0][bp(i)] = min(sg[0][bp(i)], fp[i]);
		sg[1][bp(i)] = max(sg[1][bp(i)], fp[i]);
		//cout << bp(i) << " ";
	}
	for(int i = 1; i <= n; ++i) {
		if(sg[0][i] != 1e9) {
			ve.push_back({sg[0][i], sg[1][i]});
		}
	}
	sort(ve.begin(), ve.end());
	for(int i = 0; i < ve.size(); ++i) {
		//cout << ve[i].first << " " << ve[i].second << endl;
		if(am < ve[i].first) {
			++rf;
			for(int j = ve[i].first; j <= ve[i].second; ++j) {
				shg[j] = rf;
			}
			am = ve[i].second;
		} else {
			for(int j = am + 1; j <= ve[i].second; ++j) {
				shg[j] = rf;
			}
			am = max(am, ve[i].second);
		}
	}
	int shk = 0;
	for(int i = 1; i <= n; ++i) {
		ffp[i] = min('z', (char)('a' + shg[i] - 1));
		shk = max(shk, shg[i]);
	}
	if(shk < k) {
		cout << "NO" << endl;
		return 0;
	}
	cout << "YES" << endl;
	for(int i = 1; i <= n; ++i) {
		cout << ffp[i];
	}
	cout << endl;
	return 0;
}


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

int n, k, p[200005], q[200005], gi[200005], rc, mp[200005], up[200005], am, rf, shg[200005];
vector<int> v[200005];
vector<pair<int, int> > ve;
bool bo[200005], boo[200005], ce[200005];
int fp[200005], sg[2][200005];
char ffp[200005];

int bp(int dg) {
	if(dg == up[dg]) return dg;
	return up[dg] = bp(up[dg]);
}

void ts(int dg) {
	bo[dg] = 1;
	for(int i = 0; i < v[dg].size(); ++i) {
		if(!bo[v[dg][i]]) {
			mp[v[dg][i]] = dg;
			ts(v[dg][i]);
		} else if(!boo[v[dg][i]]) {
			int rg = dg;
			while(true) {
				ce[rg] = 1;
				up[bp(rg)] = bp(v[dg][i]);
				if(bp(rg) == bp(v[dg][i])) break;
				rg = mp[rg];
			}
		} else if(ce[v[dg][i]]) {
			if(!boo[bp(v[dg][i])]) {
				int rg = dg;
				while(true) {
					ce[rg] = 1;
					up[bp(rg)] = bp(v[dg][i]);
					if(bp(rg) == bp(v[dg][i])) break;
					rg = mp[rg];
				}
			}
		}
	}
	gi[++rc] = dg;
	boo[dg] = 1;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cin >> n >> k;
	for(int i = 1; i <= n; ++i) {
		sg[0][i] = 1e9;
		up[i] = i;
		cin >> p[i];
		if(i > 1) {
			v[p[i]].push_back(p[i - 1]);
		}
	}
	for(int i = 1; i <= n; ++i) {
		cin >> p[i];
		if(i > 1) {
			v[p[i]].push_back(p[i - 1]);
		}
	}
	for(int i = 1; i <= n; ++i) {
		if(!bo[i]) {
			ts(i);
		}
	}
	for(int i = 1; i <= n; ++i) {
		fp[gi[i]] = i;
	}
	for(int i = 1; i <= n; ++i) {
		sg[0][bp(i)] = min(sg[0][bp(i)], fp[i]);
		sg[1][bp(i)] = max(sg[1][bp(i)], fp[i]);
		//cout << bp(i) << " ";
	}
	for(int i = 1; i <= n; ++i) {
		if(sg[0][i] != 1e9) {
			ve.push_back({sg[0][i], sg[1][i]});
		}
	}
	sort(ve.begin(), ve.end());
	for(int i = 0; i < ve.size(); ++i) {
		//cout << ve[i].first << " " << ve[i].second << endl;
		if(am < ve[i].first) {
			++rf;
			for(int j = ve[i].first; j <= ve[i].second; ++j) {
				shg[j] = rf;
			}
			am = ve[i].second;
		} else {
			for(int j = am + 1; j <= ve[i].second; ++j) {
				shg[j] = rf;
			}
			am = max(am, ve[i].second);
		}
	}
	int shk = 0;
	for(int i = 1; i <= n; ++i) {
		ffp[gi[i]] = min('z', (char)('a' + shg[i]));
		shk = max(shk, shg[i] + 1);
	}
	if(shk < k) {
		cout << "NO" << endl;
		return 0;
	}
	cout << "YES" << endl;
	for(int i = 1; i <= n; ++i) {
		cout << ffp[i];
	}
	cout << endl;
	return 0;
}


#pragma GCC optimize("O3")

#include <vector>
#include <string>
#include <iostream>
#include <cstdint>
#include <type_traits>
#include <algorithm>
#include <stack>
#include <set>
#include <iomanip>
#include <ctime>
#include <numeric>

using ll = int64_t;
using ull = uint64_t;
using ld = long double;

using vec_l = std::vector<int32_t>;
using vec_ll = std::vector<ll>;
using vec_ull = std::vector<ull>;
using vec_pair_ll = std::vector<std::pair<ll, ll>>;
using vec_pair_ull = std::vector<std::pair<ull, ull>>;

void AdjustStreams() {
  std::cin.tie(nullptr);
  std::cout.sync_with_stdio(false);
  std::cin.sync_with_stdio(false);
}

int main() {
  AdjustStreams();

  int n;
  int k;

  std::cin >> n >> k;

  std::vector<int> p;
  std::vector<int> q;

  p.reserve(n);
  q.reserve(n);

  for (int i = 0; i < n; ++i) {
    int t;
    std::cin >> t;
    p.push_back(t);
  }

  for (int i = 0; i < n; ++i) {
    int t;
    std::cin >> t;
    q.push_back(t);
  }

  std::set<int> s1;
  std::set<int> s2;

  int count = 0;
  char curr_letter = 'a';
  std::string result;
  result.reserve(n);

  for (int i = 0; i < n; ++i) {
    s1.insert(p[i]);
    s2.insert(q[i]);

    if (s1 == s2) {
      result += std::string(s1.size(), curr_letter);
      curr_letter = curr_letter == 'z' ? curr_letter : curr_letter + 1;
      s1.clear();
      s2.clear();
    }
  }

  if ((curr_letter - 'a') < k) {
    std::cout << "NO" << std::endl;
    return 0;
  }

  std::cout << "YES\n" << result << std::endl;

  return 0;
}


#pragma GCC optimize("O3")

#include <vector>
#include <string>
#include <iostream>
#include <cstdint>
#include <type_traits>
#include <algorithm>
#include <stack>
#include <set>
#include <iomanip>
#include <ctime>
#include <numeric>

using ll = int64_t;
using ull = uint64_t;
using ld = long double;

using vec_l = std::vector<int32_t>;
using vec_ll = std::vector<ll>;
using vec_ull = std::vector<ull>;
using vec_pair_ll = std::vector<std::pair<ll, ll>>;
using vec_pair_ull = std::vector<std::pair<ull, ull>>;

void AdjustStreams() {
  std::cin.tie(nullptr);
  std::cout.sync_with_stdio(false);
  std::cin.sync_with_stdio(false);
}

int main() {
  AdjustStreams();

  int n;
  int k;

  std::cin >> n >> k;

  std::vector<int> p;
  std::vector<int> q;

  p.reserve(n);
  q.reserve(n);

  for (int i = 0; i < n; ++i) {
    int t;
    std::cin >> t;
    p.push_back(t - 1);
  }

  for (int i = 0; i < n; ++i) {
    int t;
    std::cin >> t;
    q.push_back(t - 1);
  }

  std::set<int> s1;
  std::set<int> s2;

  int count = 0;
  char curr_letter = 'a';

  std::vector<int> pos;

  for (int i = 0; i < n; ++i) {
    s1.insert(p[i]);
    s2.insert(q[i]);

    if (s1 == s2) {
      s1.clear();
      s2.clear();
      pos.push_back(i);
    }
  }

  if (pos.size() < k) {
    std::cout << "NO" << std::endl;
    return 0;
  }

  std::string result(n, ' ');

  int l = 0;
  for (int i = 0; i < pos.size(); ++i) {
    const char letter = 'a' + std::min(25, i);
    for (int j = l; j <= pos[i]; ++j) {
      result[p[j]] = letter;
    }
    l = pos[i] + 1;
  }

  std::cout << "YES\n" << result << std::endl;

  return 0;
}


#pragma GCC optimize("O3")

#include <vector>
#include <string>
#include <iostream>
#include <cstdint>
#include <type_traits>
#include <algorithm>
#include <stack>
#include <set>
#include <iomanip>
#include <ctime>
#include <numeric>

using ll = int64_t;
using ull = uint64_t;
using ld = long double;

using vec_l = std::vector<int32_t>;
using vec_ll = std::vector<ll>;
using vec_ull = std::vector<ull>;
using vec_pair_ll = std::vector<std::pair<ll, ll>>;
using vec_pair_ull = std::vector<std::pair<ull, ull>>;

void AdjustStreams() {
  std::cin.tie(nullptr);
  std::cout.sync_with_stdio(false);
  std::cin.sync_with_stdio(false);
}

int main() {
  AdjustStreams();

  int n;
  int k;

  std::cin >> n >> k;

  std::vector<int> p;
  std::vector<int> q;

  p.reserve(n);
  q.reserve(n);

  for (int i = 0; i < n; ++i) {
    int t;
    std::cin >> t;
    p.push_back(t);
  }

  for (int i = 0; i < n; ++i) {
    int t;
    std::cin >> t;
    q.push_back(t);
  }

  std::set<int> s1;
  std::set<int> s2;

  int count = 0;
  char curr_letter = 'a';
  std::string result;
  result.reserve(n);

  for (int i = 0; i < n; ++i) {
    s1.insert(p[i]);
    s2.insert(q[i]);

    if (s1 == s2) {
      result += std::string(s1.size(), curr_letter);
      curr_letter = curr_letter == 'z' ? curr_letter : curr_letter + 1;
      s1.clear();
      s2.clear();
    }
  }

  if ((curr_letter - 'a') < k-1) {
    std::cout << "NO" << std::endl;
    return 0;
  }

  std::cout << "YES\n" << result << std::endl;

  return 0;
}


#pragma GCC optimize("O3")

#include <vector>
#include <string>
#include <iostream>
#include <cstdint>
#include <type_traits>
#include <algorithm>
#include <stack>
#include <set>
#include <iomanip>
#include <ctime>
#include <numeric>

using ll = int64_t;
using ull = uint64_t;
using ld = long double;

using vec_l = std::vector<int32_t>;
using vec_ll = std::vector<ll>;
using vec_ull = std::vector<ull>;
using vec_pair_ll = std::vector<std::pair<ll, ll>>;
using vec_pair_ull = std::vector<std::pair<ull, ull>>;

void AdjustStreams() {
  std::cin.tie(nullptr);
  std::cout.sync_with_stdio(false);
  std::cin.sync_with_stdio(false);
}

int main() {
  AdjustStreams();

  int n;
  int k;

  std::cin >> n >> k;

  std::vector<int> p;
  std::vector<int> q;

  p.reserve(n);
  q.reserve(n);

  for (int i = 0; i < n; ++i) {
    int t;
    std::cin >> t;
    p.push_back(t);
  }

  for (int i = 0; i < n; ++i) {
    int t;
    std::cin >> t;
    q.push_back(t);
  }

  std::set<int> s1;
  std::set<int> s2;

  int count = 0;
  char curr_letter = 'a';

  std::string result;
  result.reserve(n);

  for (int i = 0; i < n; ++i) {
    s1.insert(p[i]);
    s2.insert(q[i]);

    if (s1 == s2) {
      result += std::string(s1.size(), curr_letter);
      curr_letter = (curr_letter == 'z' ? curr_letter : (curr_letter + 1));
      s1.clear();
      s2.clear();
    }
  }

  if ((curr_letter - 'a') < k) {
    std::cout << "NO" << std::endl;
    return 0;
  }

  std::cout << "YES\n" << result << std::endl;

  return 0;
}


#pragma GCC optimize("O3")

#include <vector>
#include <string>
#include <iostream>
#include <cstdint>
#include <type_traits>
#include <algorithm>
#include <stack>
#include <set>
#include <iomanip>
#include <ctime>
#include <numeric>

using ll = int64_t;
using ull = uint64_t;
using ld = long double;

using vec_l = std::vector<int32_t>;
using vec_ll = std::vector<ll>;
using vec_ull = std::vector<ull>;
using vec_pair_ll = std::vector<std::pair<ll, ll>>;
using vec_pair_ull = std::vector<std::pair<ull, ull>>;

void AdjustStreams() {
  std::cin.tie(nullptr);
  std::cout.sync_with_stdio(false);
  std::cin.sync_with_stdio(false);
}

int main() {
  AdjustStreams();

  int n;
  int k;

  std::cin >> n >> k;

  std::vector<int> p;
  std::vector<int> q;

  p.reserve(n);
  q.reserve(n);

  for (int i = 0; i < n; ++i) {
    int t;
    std::cin >> t;
    p.push_back(t);
  }

  for (int i = 0; i < n; ++i) {
    int t;
    std::cin >> t;
    q.push_back(t);
  }

  std::set<int> s1;
  std::set<int> s2;

  int count = 0;
  char curr_letter = 'a';

  std::string result;
  result.reserve(n);

  for (int i = 0; i < n; ++i) {
    s1.insert(p[i]);
    s2.insert(q[i]);

    if (s1 == s2) {
      result += std::string(s1.size(), curr_letter);
      curr_letter = (curr_letter == 'z' ? curr_letter : (curr_letter + 1));
      s1.clear();
      s2.clear();
    }
  }

  if (curr_letter != 'z' && (curr_letter - 'a') < k) {
    std::cout << "NO" << std::endl;
    return 0;
  }

  std::cout << "YES\n" << result << std::endl;

  return 0;
}


#pragma GCC optimize("O3")

#include <vector>
#include <string>
#include <iostream>
#include <cstdint>
#include <type_traits>
#include <algorithm>
#include <stack>
#include <set>
#include <iomanip>
#include <ctime>
#include <numeric>

using ll = int64_t;
using ull = uint64_t;
using ld = long double;

using vec_l = std::vector<int32_t>;
using vec_ll = std::vector<ll>;
using vec_ull = std::vector<ull>;
using vec_pair_ll = std::vector<std::pair<ll, ll>>;
using vec_pair_ull = std::vector<std::pair<ull, ull>>;

void AdjustStreams() {
  std::cin.tie(nullptr);
  std::cout.sync_with_stdio(false);
  std::cin.sync_with_stdio(false);
}

int main() {
  AdjustStreams();

  int n;
  int k;

  std::cin >> n >> k;

  std::vector<int> p;
  std::vector<int> q;

  p.reserve(n);
  q.reserve(n);

  for (int i = 0; i < n; ++i) {
    int t;
    std::cin >> t;
    p.push_back(t - 1);
  }

  for (int i = 0; i < n; ++i) {
    int t;
    std::cin >> t;
    q.push_back(t - 1);
  }

  std::set<int> s1;
  std::set<int> s2;

  int count = 0;
  char curr_letter = 'a';

  std::vector<int> pos;

  for (int i = 0; i < n; ++i) {
    if (s2.find(p[i]) == std::end(s2)) {
      s1.insert(p[i]);
    }
    else {
      s2.erase(p[i]);
    }

    if (s1.find(q[i]) == std::end(s1)) {
      s2.insert(q[i]);
    }
    else {
      s1.erase(q[i]);
    }

    if (s1.empty() && s2.empty()) {
      pos.push_back(i);
      s1.clear();
      s2.clear();
    }
  }

  if (pos.size() < k) {
    std::cout << "NO" << std::endl;
    return 0;
  }

  std::string result(n, ' ');

  int l = 0;
  for (int i = 0; i < pos.size(); ++i) {
    const char letter = 'a' + std::min(25, i);
    for (int j = l; j <= pos[i]; ++j) {
      result[p[j]] = letter;
    }
    l = pos[i] + 1;
  }

  std::cout << "YES\n" << result << std::endl;

  return 0;
}


#pragma GCC optimize("O3")

#include <vector>
#include <string>
#include <iostream>
#include <cstdint>
#include <type_traits>
#include <algorithm>
#include <stack>
#include <set>
#include <iomanip>
#include <ctime>
#include <numeric>

using ll = int64_t;
using ull = uint64_t;
using ld = long double;

using vec_l = std::vector<int32_t>;
using vec_ll = std::vector<ll>;
using vec_ull = std::vector<ull>;
using vec_pair_ll = std::vector<std::pair<ll, ll>>;
using vec_pair_ull = std::vector<std::pair<ull, ull>>;

void AdjustStreams() {
  std::cin.tie(nullptr);
  std::cout.sync_with_stdio(false);
  std::cin.sync_with_stdio(false);
}

int main() {
  AdjustStreams();

  int n;
  int k;

  std::cin >> n >> k;

  std::vector<int> p;
  std::vector<int> q;

  p.reserve(n);
  q.reserve(n);

  for (int i = 0; i < n; ++i) {
    int t;
    std::cin >> t;
    p.push_back(t - 1);
  }

  for (int i = 0; i < n; ++i) {
    int t;
    std::cin >> t;
    q.push_back(t - 1);
  }

  std::set<int> s1;
  std::set<int> s2;

  int count = 0;
  char curr_letter = 'a';

  std::vector<int> pos;

  for (int i = 0; i < n; ++i) {
    if (s2.find(p[i]) == std::end(s2)) {
      s2.insert(p[i]);
    }
    else {
      s2.erase(p[i]);
    }

    if (s1.find(q[i]) == std::end(s1)) {
      s1.insert(q[i]);
    }
    else {
      s1.erase(q[i]);
    }

    if (s1.empty() == s2.empty()) {
      pos.push_back(i);
    }
  }

  if (pos.size() < k) {
    std::cout << "NO" << std::endl;
    return 0;
  }

  std::string result(n, ' ');

  int l = 0;
  for (int i = 0; i < pos.size(); ++i) {
    const char letter = 'a' + std::min(25, i);
    for (int j = l; j <= pos[i]; ++j) {
      result[p[j]] = letter;
    }
    l = pos[i] + 1;
  }

  std::cout << "YES\n" << result << std::endl;

  return 0;
}


#pragma GCC optimize("O3")

#include <vector>
#include <string>
#include <iostream>
#include <cstdint>
#include <type_traits>
#include <algorithm>
#include <stack>
#include <set>
#include <iomanip>
#include <ctime>
#include <numeric>

using ll = int64_t;
using ull = uint64_t;
using ld = long double;

using vec_l = std::vector<int32_t>;
using vec_ll = std::vector<ll>;
using vec_ull = std::vector<ull>;
using vec_pair_ll = std::vector<std::pair<ll, ll>>;
using vec_pair_ull = std::vector<std::pair<ull, ull>>;

void AdjustStreams() {
  std::cin.tie(nullptr);
  std::cout.sync_with_stdio(false);
  std::cin.sync_with_stdio(false);
}

int main() {
  AdjustStreams();

  int n;
  int k;

  std::cin >> n >> k;

  std::vector<int> p;
  std::vector<int> q;

  p.reserve(n);
  q.reserve(n);

  for (int i = 0; i < n; ++i) {
    int t;
    std::cin >> t;
    p.push_back(t);
  }

  for (int i = 0; i < n; ++i) {
    int t;
    std::cin >> t;
    q.push_back(t);
  }

  std::set<int> s1;
  std::set<int> s2;

  int count = 0;
  char curr_letter = 'a';
  std::string result;
  result.reserve(n);

  for (int i = 0; i < n; ++i) {
    s1.insert(p[i]);
    s2.insert(q[i]);

    if (s1 == s2) {
      result += std::string(s1.size(), curr_letter);
      curr_letter = (curr_letter - 'a') == k ? curr_letter : curr_letter + 1;

      s1.clear();
      s2.clear();
    }
  }

  if ((curr_letter - 'a') < k) {
    std::cout << "NO" << std::endl;
    return 0;
  }

  std::cout << "YES\n" << result << std::endl;

  return 0;
}


#pragma GCC optimize("O3")

#include <vector>
#include <string>
#include <iostream>
#include <cstdint>
#include <type_traits>
#include <algorithm>
#include <stack>
#include <set>
#include <iomanip>
#include <ctime>
#include <numeric>

using ll = int64_t;
using ull = uint64_t;
using ld = long double;

using vec_l = std::vector<int32_t>;
using vec_ll = std::vector<ll>;
using vec_ull = std::vector<ull>;
using vec_pair_ll = std::vector<std::pair<ll, ll>>;
using vec_pair_ull = std::vector<std::pair<ull, ull>>;

void AdjustStreams() {
  std::cin.tie(nullptr);
  std::cout.sync_with_stdio(false);
  std::cin.sync_with_stdio(false);
}

int main() {
  AdjustStreams();

  int n;
  int k;

  std::cin >> n >> k;

  std::vector<int> p;
  std::vector<int> q;

  p.reserve(n);
  q.reserve(n);

  for (int i = 0; i < n; ++i) {
    int t;
    std::cin >> t;
    p.push_back(t);
  }

  for (int i = 0; i < n; ++i) {
    int t;
    std::cin >> t;
    q.push_back(t);
  }

  std::set<int> s1;
  std::set<int> s2;

  int count = 0;
  char curr_letter = 'a';
  std::string result;
  result.reserve(n);

  for (int i = 0; i < n; ++i) {
    s1.insert(p[i]);
    s2.insert(q[i]);

    if (s1 == s2) {
      result += std::string(s1.size(), curr_letter);
      curr_letter = (curr_letter - 'a') >= (k-1) ? curr_letter : curr_letter + 1;

      s1.clear();
      s2.clear();
    }
  }

  if ((curr_letter - 'a') < (k-1)) {
    std::cout << "NO" << std::endl;
    return 0;
  }

  std::cout << "YES\n" << result << std::endl;

  return 0;
}


#include <iostream>
#include <stdio.h>
#include <set>
#include <stack>
#include <queue>
#include <map>
#include <cmath>
#include <iomanip>
#include <vector>
#include <algorithm>
using namespace std;
int arr[200005];
bool visited[200005];
int p1[200005];
int p2[200005];
int i1[200005];
int i2[200005];
signed main(){
    ios::sync_with_stdio(0);
	cin.tie(NULL); cout.tie(NULL);
	int n , k ;
	cin>>n>>k;
	for(int i = 1;i<=n;i++){
        cin>>p1[i];
	}
	for(int i = 1;i<=n;i++){
        i1[p1[i] ] = i;
	}
	for(int i = 1;i<=n;i++){
        cin>>p2[i];
	}
	for(int i = 1;i<=n;i++){
        i2[p2[i] ] = i;
	}
	int now = 0;
	for(int i = 1;i<=n;i++){
        if(visited[p1[i] ] ){continue;}
        int j = i2[p1[i] ];
        for(;j>=1;j--){
            if(visited[p2[j] ] ){break;}
            visited[p2[j] ] = 1;
            arr[p2[j] ] = now;
        }
        now++;
	}
	if(now<k ){
        cout<<"NO";
	}
	else{
        cout<<"YES\n";
        for(int i = 1;i<=n;i++){
            int z = min(k-1, arr[ p1[i] ] );
            cout<<char('a'+z );
        }
	}
}


/*
    Arnab Chanda
    https://codeforces.com/problemset/problem/315/A
*/

// All imports here

import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.stream.Collectors;

// Template code starts here //

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Debugger debug = new Debugger(out);
        Objectify objectify = new Objectify(debug);
        Task solver = new Task();
        int test = 1;
        while(test-->0){
            solver.solve(1, in, out, debug, objectify);
        }
		out.close();
	}
}

class InputReader {
    public BufferedReader reader;
    public StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream));
        tokenizer = null;
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(reader.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }

    public long nextLong() {
        return Long.parseLong(next());
    }

    public Double nextDouble() {
        return Double.parseDouble(next());
    }

    public float nextFloat() {
        return Float.parseFloat(next());
    }

}

class Debugger{
    PrintWriter out;

    Debugger(PrintWriter out){
        this.out = out;
    }

    public <T> void printList(List<T> arrayList){
        for( Object ob: arrayList){
            out.print(ob+" ");
        }
        out.println();
    }

    public <T> void printSet(Set<T> set){
        for(Object ob: set){
            out.print(ob+" ");
        }
        out.println();
    }

    public <T> void printMap(Map<?,?> map){
        for(Object ob: map.keySet()){
            System.out.println(ob+" : "+map.get(ob));
        }
    }
}

class Objectify{

    Debugger debug;

    Objectify(Debugger ob){ debug = ob; }

    public void printArr(int[] arr){ debug.printList(Arrays.stream(arr).boxed().collect(Collectors.toList())); }
    public void printArr(double[] arr){ debug.printList(Arrays.stream(arr).boxed().collect(Collectors.toList())); }
    public void printArr(long[] arr){ debug.printList(Arrays.stream(arr).boxed().collect(Collectors.toList())); }
    public void printArr(char[] arr){ debug.printList( String.valueOf(arr).chars().mapToObj(c -> (char) c).collect(Collectors.toList())); }
    public void printArr(String[] arr){ debug.printList(Arrays.asList(arr)); }

    public void printMatrix(int[][] arr){ for(int a[]:arr) printArr(a); }
    public void printMatrix(double[][] arr){ for(double a[]:arr) printArr(a); }
    public void printMatrix(long[][] arr){ for(long a[]:arr) printArr(a); }
    public void printMatrix(char[][] arr){ for(char a[]:arr) printArr(a); }
    public void printMatrix(String[][] arr){ for(String a[]:arr) printArr(a); }

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// Template code ends here

class Task {

    final long MOD = (int)Math.pow(10,9)+7;

    public void solve(int testNumber, InputReader sc, PrintWriter out, Debugger debug, Objectify objectify) {

        // write your code here
        int n = sc.nextInt();

        int[][] bottles = new int[n][2];

        for(int i  = 0; i < n; i++){
            int a = sc.nextInt();
            int b = sc.nextInt();
            bottles[i][0] = a;
            bottles[i][1] = b;
        }

        int ans = 0;

        for(int i = 0; i < bottles.length; i++) {
            int bottleWeNeed = bottles[i][1];
            int flag = 0;
            for(int j = 0; j < bottles.length; j++){
                if (bottles[j][0] == bottleWeNeed && i!=j){
                    flag = 1;
                    break;
                }
            }

            if (flag == 1) ans++;
        }

        out.println(n - ans);
    }
}


/*
    Arnab Chanda
    https://codeforces.com/problemset/problem/315/A
*/

// All imports here

import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.stream.Collectors;

// Template code starts here //

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Debugger debug = new Debugger(out);
        Objectify objectify = new Objectify(debug);
        Task solver = new Task();
        int test = 1;
        while(test-->0){
            solver.solve(1, in, out, debug, objectify);
        }
		out.close();
	}
}

class InputReader {
    public BufferedReader reader;
    public StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream));
        tokenizer = null;
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(reader.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }

    public long nextLong() {
        return Long.parseLong(next());
    }

    public Double nextDouble() {
        return Double.parseDouble(next());
    }

    public float nextFloat() {
        return Float.parseFloat(next());
    }

}

class Debugger{
    PrintWriter out;

    Debugger(PrintWriter out){
        this.out = out;
    }

    public <T> void printList(List<T> arrayList){
        for( Object ob: arrayList){
            out.print(ob+" ");
        }
        out.println();
    }

    public <T> void printSet(Set<T> set){
        for(Object ob: set){
            out.print(ob+" ");
        }
        out.println();
    }

    public <T> void printMap(Map<?,?> map){
        for(Object ob: map.keySet()){
            System.out.println(ob+" : "+map.get(ob));
        }
    }
}

class Objectify{

    Debugger debug;

    Objectify(Debugger ob){ debug = ob; }

    public void printArr(int[] arr){ debug.printList(Arrays.stream(arr).boxed().collect(Collectors.toList())); }
    public void printArr(double[] arr){ debug.printList(Arrays.stream(arr).boxed().collect(Collectors.toList())); }
    public void printArr(long[] arr){ debug.printList(Arrays.stream(arr).boxed().collect(Collectors.toList())); }
    public void printArr(char[] arr){ debug.printList( String.valueOf(arr).chars().mapToObj(c -> (char) c).collect(Collectors.toList())); }
    public void printArr(String[] arr){ debug.printList(Arrays.asList(arr)); }

    public void printMatrix(int[][] arr){ for(int a[]:arr) printArr(a); }
    public void printMatrix(double[][] arr){ for(double a[]:arr) printArr(a); }
    public void printMatrix(long[][] arr){ for(long a[]:arr) printArr(a); }
    public void printMatrix(char[][] arr){ for(char a[]:arr) printArr(a); }
    public void printMatrix(String[][] arr){ for(String a[]:arr) printArr(a); }

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// Template code ends here

class Task {

    final long MOD = (int)Math.pow(10,9)+7;

    public void solve(int testNumber, InputReader sc, PrintWriter out, Debugger debug, Objectify objectify) {

        // write your code here
        int n = sc.nextInt();

        int[][] bottles = new int[n][2];

        for(int i  = 0; i < n; i++){
            int a = sc.nextInt();
            int b = sc.nextInt();
            bottles[i][0] = a;
            bottles[i][1] = b;
        }

        int ans = 0;

        for(int i = 0; i < bottles.length; i++) {
            int bottleWeNeed = bottles[i][1];

            for(int j = 0; j < bottles.length; j++){
                if (bottles[j][0] == bottleWeNeed && i!=j){
                    ans++;
                    break;
                }
            }
        }

        out.println(n - ans);
    }
}


/*
    Arnab Chanda
    https://codeforces.com/problemset/problem/315/A
*/

// All imports here

import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.stream.Collectors;

// Template code starts here //

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Debugger debug = new Debugger(out);
        Objectify objectify = new Objectify(debug);
        Task solver = new Task();
        int test = 1;
        while(test-->0){
            solver.solve(1, in, out, debug, objectify);
        }
		out.close();
	}
}

class InputReader {
    public BufferedReader reader;
    public StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream));
        tokenizer = null;
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(reader.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }

    public long nextLong() {
        return Long.parseLong(next());
    }

    public Double nextDouble() {
        return Double.parseDouble(next());
    }

    public float nextFloat() {
        return Float.parseFloat(next());
    }

}

class Debugger{
    PrintWriter out;

    Debugger(PrintWriter out){
        this.out = out;
    }

    public <T> void printList(List<T> arrayList){
        for( Object ob: arrayList){
            out.print(ob+" ");
        }
        out.println();
    }

    public <T> void printSet(Set<T> set){
        for(Object ob: set){
            out.print(ob+" ");
        }
        out.println();
    }

    public <T> void printMap(Map<?,?> map){
        for(Object ob: map.keySet()){
            System.out.println(ob+" : "+map.get(ob));
        }
    }
}

class Objectify{

    Debugger debug;

    Objectify(Debugger ob){ debug = ob; }

    public void printArr(int[] arr){ debug.printList(Arrays.stream(arr).boxed().collect(Collectors.toList())); }
    public void printArr(double[] arr){ debug.printList(Arrays.stream(arr).boxed().collect(Collectors.toList())); }
    public void printArr(long[] arr){ debug.printList(Arrays.stream(arr).boxed().collect(Collectors.toList())); }
    public void printArr(char[] arr){ debug.printList( String.valueOf(arr).chars().mapToObj(c -> (char) c).collect(Collectors.toList())); }
    public void printArr(String[] arr){ debug.printList(Arrays.asList(arr)); }

    public void printMatrix(int[][] arr){ for(int a[]:arr) printArr(a); }
    public void printMatrix(double[][] arr){ for(double a[]:arr) printArr(a); }
    public void printMatrix(long[][] arr){ for(long a[]:arr) printArr(a); }
    public void printMatrix(char[][] arr){ for(char a[]:arr) printArr(a); }
    public void printMatrix(String[][] arr){ for(String a[]:arr) printArr(a); }

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// Template code ends here

class Task {

    final long MOD = (int)Math.pow(10,9)+7;

    public void solve(int testNumber, InputReader sc, PrintWriter out, Debugger debug, Objectify objectify) {

        // write your code here
        int n = sc.nextInt();

        int[][] bottles = new int[n][2];

        for(int i  = 0; i < n; i++){
            int a = sc.nextInt();
            int b = sc.nextInt();
            bottles[i][0] = a;
            bottles[i][1] = b;
        }

        int ans = 0;

        for(int i = 0; i < bottles.length; i++) {
            int bottleWeNeed = bottles[i][1];

            for(int j = 0; j < bottles.length; j++){
                if (bottles[j][0] == bottleWeNeed && i!=j) ans++;
            }
        }

        out.println(n - ans);
    }
}


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

int main() {
	// your code goes here
	long int n,c=0,i,a,b,d=0,j;
	cin>>n;
	vector <long int> v;
	vector <long int> vt;
	for(i=0;i<n;i++)
	{
	    cin>>a>>b;
		v.push_back(a);
		vt.push_back(b);
	}
	for(i=0;i<n;i++)
	{
		if(v[i]==vt[i])c++;
	}
	for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            if(v[i]==vt[j])
                d++;
        }
    }
    //cout<<c<<" "<<d<<endl;
    cout<<2*n-(d-c+n)<<endl;
	return 0;
}


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

int main() {
	// your code goes here
	long int n,c=0,i,a,b,d=0,j;
	cin>>n;
	vector <long int> v;
	vector <long int> vt;
	for(i=0;i<n;i++)
	{
	    cin>>a>>b;
		v.push_back(a);
		vt.push_back(b);
	}
	for(i=0;i<n;i++)
	{
		if(v[i]==vt[i])c++;
	}
	for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            if(v[i]==vt[j])
                d++;
        }
    }
    set <long int> s1;
    set <long int> s2;
    for(i=0;i<n;i++)
    {
        s1.insert(v[i]);
        s2.insert(vt[i]);
    }
    long int xc=n*2 - (s1.size()+s2.size());
   // cout<<c<<" "<<d<<endl;
    //cout<<d-c+n<<" "<<xc<<endl;
    if((s1.size())<n && s2.size()<n){
    cout<<2*n-(d-c+n-xc)<<endl;
    }
    else{
        cout<<2*n-(d-c+n)<<endl;
    }
	return 0;
}


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

int main() {
	// your code goes here
	long int n,c=0,i,a,b,d=0,j;
	cin>>n;
	vector <long int> v;
	vector <long int> vt;
	for(i=0;i<n;i++)
	{
	    cin>>a>>b;
		v.push_back(a);
		vt.push_back(b);
	}
	for(i=0;i<n;i++)
	{
		if(v[i]==vt[i])c++;
	}
	for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            if(v[i]==vt[j])
                d++;
        }
    }
    set <long int> s1;
    set <long int> s2;
    for(i=0;i<n;i++)
    {
        s1.insert(v[i]);
        s2.insert(vt[i]);
    }
    long int xc=n*2 - (s1.size()+s2.size());
    //cout<<c<<" "<<d<<endl;
    //cout<<d-c+n<<" "<<xc<<endl;
    cout<<2*n-(d-c+n-xc)<<endl;
	return 0;
}


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

int main() {
	// your code goes here
	long int n,c=0,i,a,b;
	cin>>n;
	while(n--)
	{
		cin>>a>>b;
		if(a==b)c++;

	}
	cout<<c<<endl;
	return 0;
}


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

int main() {
	// your code goes here
	long int n,c=0,i,a,b,d=0,j;
	cin>>n;
	vector <long int> v;
	vector <long int> vt;
	for(i=0;i<n;i++)
	{
	    cin>>a>>b;
		v.push_back(a);
		vt.push_back(b);
	}
	for(i=0;i<n;i++)
	{
		if(v[i]==vt[i])c++;
	}
	for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            if(v[i]==vt[j])
                d++;
        }
    }
    cout<<n-(c+d)<<endl;
	return 0;
}


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

int main() {
	// your code goes here
	long int n,c=0,i,a,b,d=0,j;
	cin>>n;
	vector <long int> v;
	vector <long int> vt;
	for(i=0;i<n;i++)
	{
	    cin>>a>>b;
		v.push_back(a);
		vt.push_back(b);
	}
	for(i=0;i<n;i++)
	{
		if(v[i]==vt[i])c++;
	}
	for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            if(v[i]==vt[j])
                d++;
        }
    }
    cout<<n-(c-d)<<endl;
	return 0;
}


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

int main() {
	// your code goes here
	long int n,c=0,i,a,b;
	cin>>n;
	vector <pair<long int,long int>> v;
	for(i=0;i<n;i++)
	{
		cin>>a>>b;
		v.push_back(make_pair(a,b));
	}
	for(i=0;i<n;i++)
	{
		if(v[i].second!=v[i+1].first)c++;
	}
	if(v[0].first!=v[n-1].second)c++;
	cout<<c-1<<endl;
	return 0;
}


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

int main() {
	// your code goes here
	long int n,c=0,i,a,b,d=0,j;
	cin>>n;
	vector <long int> v;
	vector <long int> vt;
	for(i=0;i<n;i++)
	{
	    cin>>a>>b;
		v.push_back(a);
		vt.push_back(b);
	}
	for(i=0;i<n;i++)
	{
		if(v[i]==vt[i])c++;
	}
	for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            if(v[i]==vt[j])
                d++;
        }
    }
    set <long int> s1;
    set <long int> s2;
    for(i=0;i<n;i++)
    {
        s1.insert(v[i]);
        s2.insert(vt[i]);
    }
    long int xc=n*2 - (s1.size()+s2.size());
   // cout<<c<<" "<<d<<endl;
   // cout<<d-c+n<<" "<<xc<<endl;
    if(s2.size()<n){
    cout<<2*n-(d-c+n-xc)<<endl;
    }
    else{
        cout<<2*n-(d-c+n)<<endl;
    }
	return 0;
}


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

int main() {
	// your code goes here
	long long int n,i,j,c=0;
	cin>>n;
	long long int arr[n],brr[n];
	for(i=0;i<n;i++)
	{
		cin>>arr[i]>>brr[i];
	}
	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++){
			if(arr[i]==brr[j] && i!=j){
				c++;
				break;
			}
		}
	}
	cout<<n-c<<endl;
	return 0;
}


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

int main(int argc, char const *argv[])
{
	int n;

	cin >> n;
	bool open[n];
	for (int i = 0; i < n; ++i)
	{
		open[i] = 0;
	}

	map<int, int> map;
	int a,b;

	for (int i = 0; i < n; ++i)
	{
		cin >> a >> b;
		if(a != b){
			open[b-1] = 1;
		}
	}

	int count = 0;

	for (int i = 0; i < n; ++i)
	{
		if(open[i] == 0){
			count++;
		}
	}

	cout << count << endl;

	return 0;
}


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

int main(int argc, char const *argv[])
{
    int n;

    cin >> n;
    bool open[1001] = {0};

    int count = 0;
    int a[n],b[n];
    for (int i = 0; i < n; ++i)
    {
        cin >> a[i] >> b[i];
    }

    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < n; ++j)
        {
            if((a[j] == b[i]) && (!open[j])){
                if(i != j){
                    count++;
                    open[j] = true;
                }
            }
        }
    }

    cout << n-count << endl;

    return 0;
}


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

int main(int argc, char const *argv[])
{
	int n;

	cin >> n;
	bool open[n];
	for (int i = 0; i < n; ++i)
	{
		open[i] = 0;
	}

	map<int, int> map;
	int a,b;

	for (int i = 0; i < n; ++i)
	{
		cin >> a >> b;
		if(a != b && b < n){
			open[b-1] = 1;
		}
	}

	int count = 0;

	for (int i = 0; i < n; ++i)
	{
		if(open[i] == 0){
			count++;
		}
	}

	cout << count << endl;

	return 0;
}


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

int main(int argc, char const *argv[])
{
    int n;

    cin >> n;
    bool open[1001];
    for (int i = 0; i < 1001; ++i)
    {
        open[i] = 0;
    }

    map<int, int> map;
    int a[n],b[n];

    for (int i = 0; i < n; ++i)
    {
        cin >> a[i] >> b[i];
        if(a[i] != b[i]){
            open[b[i]] = 1;
        }
    }

    int count = 0;

    for (int i = 0; i < n; ++i)
    {
        if(open[a[i]] == 0){
            count++;
            cout << a[i] << endl;
        }
    }

    cout << count<< endl;

    return 0;
}


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

int main(int argc, char const *argv[])
{
    int n;

    cin >> n;
    bool open[1001];
    for (int i = 0; i < 1001; ++i)
    {
        open[i] = 0;
    }

    map<int, int> map;
    int a[n],b[n];

    for (int i = 0; i < n; ++i)
    {
        cin >> a[i] >> b[i];
        if(a[i] != b[i]){
            open[b[i]] = 1;
        }
    }

    int count = 0;

    for (int i = 0; i < n; ++i)
    {
        if(open[a[i]] == 0){
            count++;
        }
    }

    cout << count<< endl;

    return 0;
}


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

int main(int argc, char const *argv[])
{
	int n;

	cin >> n;
	bool open[n];
	for (int i = 0; i < n; ++i)
	{
		open[i] = 0;
	}

	map<int, int> map;
	int a,b;

	for (int i = 0; i < n; ++i)
	{
		cin >> a >> b;
		if(a != b && b <= n){
			open[b-1] = 1;
		}
	}

	int count = 0;

	for (int i = 0; i < n; ++i)
	{
		if(open[i] == 0){
			count++;
		}
	}

	cout << count << endl;

	return 0;
}


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

int main(int argc, char const *argv[])
{
    int n;

    cin >> n;
    bool open[1001];
    for (int i = 0; i < 1001; ++i)
    {
        open[i] = 0;
    }

    map<int, int> map;
    int a[n],b[n];

    for (int i = 0; i < n; ++i)
    {
        cin >> a[i] >> b[i];
        if(a[i] != b[i] && b[i] <= n){
            open[b[i]] = 1;
        }
    }

    int count = 0;

    for (int i = 0; i < n; ++i)
    {
        if(open[a[i]] == 0){
            count++;
        }
    }

    cout << count<< endl;

    return 0;
}


#include <bits/stdc++.h>
#define ll long long int
using namespace std;

int main()
{
     int n;
     cin>>n;
     int x=0;
     map<int,int>mp;
     int a[n],b[n];
     for(int i=0;i<n;i++)
     {
          // int a,b;
           cin>>a[i]>>b[i];
           mp[b[i]]++;
     }
     for(int i=0;i<n;i++)
     {
           if(mp[a[i]]>0)
           x++;
     }
     cout<<n-x<<endl;

}


#include <bits/stdc++.h>
#define ll long long int
using namespace std;

int main()
{
     int n;
     cin>>n;
     int x=0;
     map<int,int>mp,mp1,mp3;
     int a[n],b[n];
     for(int i=0;i<n;i++)
     {
          // int a,b;
           cin>>a[i]>>b[i];
           if(a[i]!=b[i])
           mp[b[i]]++;
           mp1[a[i]]++;
     }
     for(int i=0;i<n;i++)
     {
           if(a[i]==b[i] && mp3[a[i]]==0)
           {
              if(mp1[a[i]]>1)
              {
                    x+=mp1[a[i]];
              }
              mp3[a[i]]++;
           }
     }
     //cout<<x<<endl;
     for(int i=0;i<n;i++)
     {
           if(mp[a[i]]>0  && mp3[a[i]]==0)
           {
              x++;
           }

     }
     cout<<n-x<<endl;

}


#include <bits/stdc++.h>
#define ll long long int
using namespace std;

int main()
{
     int n;
     cin>>n;
     int x=0;
     map<int,int>mp;
     int a[n],b[n];
     for(int i=0;i<n;i++)
     {
          // int a,b;
           cin>>a[i]>>b[i];
           if(a[i]!=b[i])
           mp[b[i]]++;
     }
     for(int i=0;i<n;i++)
     {
           if(mp[a[i]]>0)
           {
              x++;
           }

     }
     cout<<n-x<<endl;

}


#include <bits/stdc++.h>
#define ll long long int
using namespace std;

int main()
{
     int n;
     cin>>n;
     int x=0;
     map<int,int>mp;
     int a[n],b[n];
     for(int i=0;i<n;i++)
     {
          // int a,b;
           cin>>a[i]>>b[i];
           mp[b[i]]++;
     }
     for(int i=0;i<n;i++)
     {
           if(mp[a[i]]>0  && a[i]!=b[i])
           {
              x++;
           }

     }
     cout<<n-x<<endl;

}


#include <bits/stdc++.h>
#define ll long long int
using namespace std;

int main()
{
     int n;
     cin>>n;
     int x=0;
     for(int i=0;i<n;i++)
     {
           int a,b;
           cin>>a>>b;
           if(a>b)
            x++;
     }
     if(x==1)
     cout<<0<<endl;
     else
     cout<<n<<endl;
      return 0;
}


#include <bits/stdc++.h>
#define ll long long int
using namespace std;

int main()
{
     int n;
     cin>>n;
     int x=0;
     for(int i=0;i<n;i++)
     {
           int a,b;
           cin>>a>>b;
           if(a>b)
            x++;
     }
     if(x==1)
     cout<<n<<endl;
     else
     cout<<0<<endl;
      return 0;
}


#include <bits/stdc++.h>
#define ll long long int
using namespace std;

int main()
{
     int n;
     cin>>n;
     int x=0;
     map<int,int>mp,mp1,mp3;
     int a[n],b[n];
     for(int i=0;i<n;i++)
     {
          // int a,b;
           cin>>a[i]>>b[i];
           if(a[i]!=b[i])
           mp[b[i]]++;
           mp1[a[i]]++;
     }
     for(int i=0;i<n;i++)
     {
           if(a[i]==b[i]&& mp3[a[i]]==0)
           {
              if(mp1[a[i]]>1)
              {
                    x+=mp1[a[i]];
              }
              mp3[a[i]]++;
           }
     }
     for(int i=0;i<n;i++)
     {
           if(mp[a[i]]>0)
           {
              x++;
           }

     }
     cout<<n-x<<endl;

}


#include <bits/stdc++.h>
#define ll long long int
using namespace std;

int main()
{
     int n;
     cin>>n;
     int x=0;
     map<int,int>mp,mp1,mp3;
     int a[n],b[n];
     for(int i=0;i<n;i++)
     {
          // int a,b;
           cin>>a[i]>>b[i];
           if(a[i]!=b[i])
           mp[b[i]]++;
           mp1[a[i]]++;
     }
     for(int i=0;i<n;i++)
     {
           if(a[i]==b[i] && mp3[a[i]]==0)
           {
              if(mp1[a[i]]>1)
              {
                    x+=mp1[a[i]];
                    mp3[a[i]]++;
              }

           }
           //cout<<mp3[a[i]]<<endl;
     }
     //cout<<x<<endl;
     for(int i=0;i<n;i++)
     {
           if(mp[a[i]]>0  && mp3[a[i]]==0)
           {
              x++;
           }

     }
     cout<<n-x<<endl;

}


#include <bits/stdc++.h>
#define ll long long int
using namespace std;

int main()
{
     int n;
     cin>>n;
     int x=0;
     for(int i=0;i<n;i++)
     {
           int a,b;
           cin>>a>>b;
           if(a==b)
            x++;
     }
     cout<<x<<endl;
      return 0;
}


#include <bits/stdc++.h>
#define ll long long int
using namespace std;

int main()
{
     int n;
     cin>>n;
     int x=0;
     map<int,int>mp,mp1,mp3;
     int a[n],b[n];
     for(int i=0;i<n;i++)
     {
          // int a,b;
           cin>>a[i]>>b[i];
           mp[b[i]]++;
     }
    /* for(int i=0;i<n;i++)
     {
           if(a[i]==b[i] && mp3[a[i]]==0)
           {
              if(mp1[a[i]]>1)
              {
                    x+=mp1[a[i]];
                    mp3[a[i]]++;
              }

           }
           //cout<<mp3[a[i]]<<endl;
     }*/
     //cout<<x<<endl;
     for(int i=0;i<n;i++)
     {
           if(mp[a[i]]>0)
           {
                 if(a[i]==b[i]  && mp[b[i]]>1)
                  x++;
                 else if(a[i]!=b[i])
                  x++;
           }
     }
     cout<<n-x<<endl;

}


#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define pb push_back
#define fl(i,a,n) for(ll i=a;i<n;i++)
#define rl(i,a,n) for(ll i=a;i>=n;i--)
#define fc(i,a) for(auto& i: a)
#define all(a) a.begin(), a.end()
#define gcd(a,b) __gcd(a,b)
#define ll long long
#define VLL vector<ll>
#define VVLL vector<vector<ll>>

double tick(){
    static clock_t oldt,newt=clock();
    double diff=1.0* (newt-oldt)/CLOCKS_PER_SEC;
    oldt=newt;
    return diff;
}

ll mod(ll n,ll m=1000000007){
    if(n<0){
        return (m-abs(n)%m)%m;
    }
    else return n%m;
}

vector<bool> prime(10000001,true);

void sieve(){
    prime[0]=prime[1]=false;
    for(ll i=2;i<10000001;i++){
        if(prime[i]){
            for(ll j=i*i;j<10000001;j+=i)
                prime[j]=false;
        }
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif

    ll t=1;
    // cin>>t;
    while(t--){
        ll n;
        cin>>n;
        VVLL arr(1001,VLL(2));
        VLL digits;
        fl(i,0,n){
        	ll a,b;
        	cin>>a>>b;
        	arr[a][0]++;
        	arr[a][1]=b;
        	if(arr[a][0]==1) digits.pb(a);
        }
        ll cnt=n;
        fl(i,0,digits.size()){
        		// cout<<cnt<<endl;
        	if(arr[digits[i]][1]==digits[i]){
        		if(arr[digits[i]][0]>1){
	        		cnt-=arr[digits[i]][0];
        		}
        	}
        	else{
        		cnt-=arr[digits[i]][0];
        	}
        }
        cout<<cnt<<endl;
    }
    cerr<<"\nExecution time: "<<tick()<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define pb push_back
#define fl(i,a,n) for(ll i=a;i<n;i++)
#define rl(i,a,n) for(ll i=a;i>=n;i--)
#define fc(i,a) for(auto& i: a)
#define all(a) a.begin(), a.end()
#define gcd(a,b) __gcd(a,b)
#define ll long long
#define VLL vector<ll>
#define VVLL vector<vector<ll>>

double tick(){
    static clock_t oldt,newt=clock();
    double diff=1.0* (newt-oldt)/CLOCKS_PER_SEC;
    oldt=newt;
    return diff;
}

ll mod(ll n,ll m=1000000007){
    if(n<0){
        return (m-abs(n)%m)%m;
    }
    else return n%m;
}

vector<bool> prime(10000001,true);

void sieve(){
    prime[0]=prime[1]=false;
    for(ll i=2;i<10000001;i++){
        if(prime[i]){
            for(ll j=i*i;j<10000001;j+=i)
                prime[j]=false;
        }
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif

    ll t=1;
    // cin>>t;
    while(t--){
        ll n;
        cin>>n;
        VVLL arr(1001,VLL(2,0));
        VLL digits;
        fl(i,0,n){
        	ll a,b;
        	cin>>a>>b;
        	arr[a][0]++;
        	arr[b][1]=a;
        	if(arr[a][0]==1) digits.pb(a);
        }
        ll cnt=n;
        fl(i,0,digits.size()){
        	if(arr[digits[i]][1]==digits[i]){
        		if(arr[digits[i]][0]>1){
	        		cnt-=arr[digits[i]][0];
        		}
        	}
        	else {
        		cnt-=arr[arr[digits[i]][1]][0];
        	}
        		// cout<<cnt<<endl;
        }
        cout<<cnt<<endl;
    }
    cerr<<"\nExecution time: "<<tick()<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define pb push_back
#define fl(i,a,n) for(ll i=a;i<n;i++)
#define rl(i,a,n) for(ll i=a;i>=n;i--)
#define fc(i,a) for(auto& i: a)
#define all(a) a.begin(), a.end()
#define gcd(a,b) __gcd(a,b)
#define ll long long
#define VLL vector<ll>
#define VVLL vector<vector<ll>>

double tick(){
    static clock_t oldt,newt=clock();
    double diff=1.0* (newt-oldt)/CLOCKS_PER_SEC;
    oldt=newt;
    return diff;
}

ll mod(ll n,ll m=1000000007){
    if(n<0){
        return (m-abs(n)%m)%m;
    }
    else return n%m;
}

vector<bool> prime(10000001,true);

void sieve(){
    prime[0]=prime[1]=false;
    for(ll i=2;i<10000001;i++){
        if(prime[i]){
            for(ll j=i*i;j<10000001;j+=i)
                prime[j]=false;
        }
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif

    ll t=1;
    // cin>>t;
    while(t--){
        ll n;
        cin>>n;
        VVLL arr(1001,VLL(2,0));
        VLL digits;
        fl(i,0,n){
        	ll a,b;
        	cin>>a>>b;
        	arr[a][0]++;
        	if(a==b){
        		if(arr[b][1]==0)
		        	arr[b][1]=a;
        	}
        	else{
        		arr[b][1]=a;
        	}
        	if(arr[a][0]==1) digits.pb(a);
        }
        ll cnt=n;
        fl(i,0,digits.size()){
        	if(arr[digits[i]][1]==digits[i]){
        		if(arr[digits[i]][0]>1){
	        		cnt-=arr[digits[i]][0];
        		}
        	}
        	else if(arr[arr[digits[i]][1]][0]){
        		cnt-=arr[digits[i]][0];
        	}
        		// cout<<cnt<<endl;
        }
        cout<<cnt<<endl;
    }
    cerr<<"\nExecution time: "<<tick()<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define pb push_back
#define fl(i,a,n) for(ll i=a;i<n;i++)
#define rl(i,a,n) for(ll i=a;i>=n;i--)
#define fc(i,a) for(auto& i: a)
#define all(a) a.begin(), a.end()
#define gcd(a,b) __gcd(a,b)
#define ll long long
#define VLL vector<ll>
#define VVLL vector<vector<ll>>

double tick(){
    static clock_t oldt,newt=clock();
    double diff=1.0* (newt-oldt)/CLOCKS_PER_SEC;
    oldt=newt;
    return diff;
}

ll mod(ll n,ll m=1000000007){
    if(n<0){
        return (m-abs(n)%m)%m;
    }
    else return n%m;
}

vector<bool> prime(10000001,true);

void sieve(){
    prime[0]=prime[1]=false;
    for(ll i=2;i<10000001;i++){
        if(prime[i]){
            for(ll j=i*i;j<10000001;j+=i)
                prime[j]=false;
        }
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif

    ll t=1;
    // cin>>t;
    while(t--){
        ll n;
        cin>>n;
        VVLL arr(1001,VLL(2,0));
        VLL digits;
        fl(i,0,n){
        	ll a,b;
        	cin>>a>>b;
        	arr[a][0]++;
        	if(a==b){
        		if(arr[b][1]==0)
		        	arr[b][1]=a;
        	}
        	else{
        		arr[b][1]=a;
        	}
        	if(arr[a][0]==1) digits.pb(a);
        }
        ll cnt=n;
        fl(i,0,digits.size()){
        	if(arr[digits[i]][1]==digits[i]){
        		if(arr[digits[i]][0]>1){
	        		cnt-=arr[digits[i]][0];
	        		cnt++;
        		}
        	}
        	else if(arr[arr[digits[i]][1]][0]){
        		cnt-=arr[digits[i]][0];
        	}
        		// cout<<cnt<<endl;
        }
        cout<<cnt<<endl;
    }
    cerr<<"\nExecution time: "<<tick()<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define pb push_back
#define fl(i,a,n) for(ll i=a;i<n;i++)
#define rl(i,a,n) for(ll i=a;i>=n;i--)
#define fc(i,a) for(auto& i: a)
#define all(a) a.begin(), a.end()
#define gcd(a,b) __gcd(a,b)
#define ll long long
#define VLL vector<ll>
#define VVLL vector<vector<ll>>

double tick(){
    static clock_t oldt,newt=clock();
    double diff=1.0* (newt-oldt)/CLOCKS_PER_SEC;
    oldt=newt;
    return diff;
}

ll mod(ll n,ll m=1000000007){
    if(n<0){
        return (m-abs(n)%m)%m;
    }
    else return n%m;
}

vector<bool> prime(10000001,true);

void sieve(){
    prime[0]=prime[1]=false;
    for(ll i=2;i<10000001;i++){
        if(prime[i]){
            for(ll j=i*i;j<10000001;j+=i)
                prime[j]=false;
        }
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif

    ll t=1;
    // cin>>t;
    while(t--){
        ll n;
        cin>>n;
        VVLL arr(1001,VLL(3,0));
        VLL digits;
        fl(i,0,n){
        	ll a,b;
        	cin>>a>>b;
        	arr[a][0]++;
        	if(a==b){
        		if(arr[b][1]==0)
		        	arr[b][1]=a;
        	}
        	else{
        		arr[b][1]=a;
        	}
        	arr[b][2]++;
        	if(arr[a][0]==1) digits.pb(a);
        }
        ll cnt=n;
        fl(i,0,digits.size()){
        	if(arr[digits[i]][1]==digits[i]){
        		if(arr[digits[i]][0]>1){
	        		cnt-=arr[digits[i]][0];
        			if(arr[digits[i]][2]==1)
	        			cnt++;
        		}
        	}
        	else if(arr[arr[digits[i]][1]][0]){
        		cnt-=arr[digits[i]][0];
        	}
        		// cout<<cnt<<endl;
        }
        cout<<cnt<<endl;
    }
    cerr<<"\nExecution time: "<<tick()<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define pb push_back
#define fl(i,a,n) for(ll i=a;i<n;i++)
#define rl(i,a,n) for(ll i=a;i>=n;i--)
#define fc(i,a) for(auto& i: a)
#define all(a) a.begin(), a.end()
#define gcd(a,b) __gcd(a,b)
#define ll long long
#define VLL vector<ll>
#define VVLL vector<vector<ll>>

double tick(){
    static clock_t oldt,newt=clock();
    double diff=1.0* (newt-oldt)/CLOCKS_PER_SEC;
    oldt=newt;
    return diff;
}

ll mod(ll n,ll m=1000000007){
    if(n<0){
        return (m-abs(n)%m)%m;
    }
    else return n%m;
}

vector<bool> prime(10000001,true);

void sieve(){
    prime[0]=prime[1]=false;
    for(ll i=2;i<10000001;i++){
        if(prime[i]){
            for(ll j=i*i;j<10000001;j+=i)
                prime[j]=false;
        }
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif

    ll t=1;
    // cin>>t;
    while(t--){
        ll n;
        cin>>n;
        VVLL arr(1001,VLL(2,0));
        VLL digits;
        fl(i,0,n){
        	ll a,b;
        	cin>>a>>b;
        	arr[a][0]++;
        	arr[b][1]=a;
        	if(arr[a][0]==1) digits.pb(a);
        }
        ll cnt=n;
        fl(i,0,digits.size()){
        	if(arr[digits[i]][1]==digits[i]){
        		if(arr[digits[i]][0]>1){
	        		cnt-=arr[digits[i]][0];
        		}
        	}
        	else if(arr[arr[digits[i]][1]][0]){
        		cnt-=arr[digits[i]][0];
        	}
        		// cout<<cnt<<endl;
        }
        cout<<cnt<<endl;
    }
    cerr<<"\nExecution time: "<<tick()<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define pb push_back
#define fl(i,a,n) for(ll i=a;i<n;i++)
#define rl(i,a,n) for(ll i=a;i>=n;i--)
#define fc(i,a) for(auto& i: a)
#define all(a) a.begin(), a.end()
#define gcd(a,b) __gcd(a,b)
#define ll long long
#define VLL vector<ll>
#define VVLL vector<vector<ll>>

double tick(){
    static clock_t oldt,newt=clock();
    double diff=1.0* (newt-oldt)/CLOCKS_PER_SEC;
    oldt=newt;
    return diff;
}

ll mod(ll n,ll m=1000000007){
    if(n<0){
        return (m-abs(n)%m)%m;
    }
    else return n%m;
}

vector<bool> prime(10000001,true);

void sieve(){
    prime[0]=prime[1]=false;
    for(ll i=2;i<10000001;i++){
        if(prime[i]){
            for(ll j=i*i;j<10000001;j+=i)
                prime[j]=false;
        }
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif

    ll t=1;
    // cin>>t;
    while(t--){
        ll n;
        cin>>n;
        VVLL arr(1001,VLL(2));
        VLL digits;
        fl(i,0,n){
        	ll a,b;
        	cin>>a>>b;
        	arr[b][0]++;
        	arr[b][1]=a;
        	if(arr[b][0]==1) digits.pb(b);
        }
        ll cnt=n;
        fl(i,0,digits.size()){
        		// cout<<cnt<<endl;
        	if(arr[digits[i]][1]==digits[i]){
        		if(arr[digits[i]][0]>1){
	        		cnt-=arr[digits[i]][0];
        		}
        	}
        	else if(arr[arr[digits[i]][1]][0]>=1){
        		cnt-=arr[digits[i]][0];
        	}
        }
        cout<<cnt<<endl;
    }
    cerr<<"\nExecution time: "<<tick()<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define pb push_back
#define fl(i,a,n) for(ll i=a;i<n;i++)
#define rl(i,a,n) for(ll i=a;i>=n;i--)
#define fc(i,a) for(auto& i: a)
#define all(a) a.begin(), a.end()
#define gcd(a,b) __gcd(a,b)
#define ll long long
#define VLL vector<ll>
#define VVLL vector<vector<ll>>

double tick(){
    static clock_t oldt,newt=clock();
    double diff=1.0* (newt-oldt)/CLOCKS_PER_SEC;
    oldt=newt;
    return diff;
}

ll mod(ll n,ll m=1000000007){
    if(n<0){
        return (m-abs(n)%m)%m;
    }
    else return n%m;
}

vector<bool> prime(10000001,true);

void sieve(){
    prime[0]=prime[1]=false;
    for(ll i=2;i<10000001;i++){
        if(prime[i]){
            for(ll j=i*i;j<10000001;j+=i)
                prime[j]=false;
        }
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif

    ll t=1;
    // cin>>t;
    while(t--){
        ll n;
        cin>>n;
        VVLL arr(1001,VLL(2));
        VLL digits;
        fl(i,0,n){
        	ll a,b;
        	cin>>a>>b;
        	arr[a][0]++;
        	arr[a][1]=b;
        	if(arr[a][0]==1) digits.pb(a);
        }
        ll cnt=n;
        fl(i,0,digits.size()){
        		// cout<<cnt<<endl;
        	if(arr[digits[i]][1]==digits[i]){
        		if(arr[digits[i]][0]>1){
	        		cnt-=arr[digits[i]][0];
        		}
        	}
        	else if(arr[arr[digits[i]][1]][1]>=1){
        		cnt-=arr[digits[i]][0];
        	}
        }
        cout<<cnt<<endl;
    }
    cerr<<"\nExecution time: "<<tick()<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

const int INF = numeric_limits<int>::max();
const ll INF_LL = numeric_limits<ll>::max();

void solve() {
    int n;
    cin >> n;
    vector<int> a(n + 1), b(n + 1);
    for (int i = 0; i < n; i++) {
        cin >> a[i] >> b[i];
    }
    int k = 0;
     for (int i = 0; i < n; i++) {
         for (int j = 0; j < n; j++) {
             if(i != j && a[i] == b[j]) {
                 k++;
                 break;
             }
         }
     }

    cout << n - k;

}

int main() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);

    solve();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

const int INF = numeric_limits<int>::max();
const ll INF_LL = numeric_limits<ll>::max();

void solve() {
    ll n;
    cin >> n;

    vector<vector<bool>> can_open(1001, vector<bool>(1001, false));

    vector<int> bottles(n);
    for (int i = 0; i < n; i++) {
        int a, b;
        cin >> a >> b;
        bottles[i] = a;
        can_open[a][b] = true;
    }

    int cnt = 0;
    for (int bottle : bottles) {
        bool res = false;
        for (int i = 1; !res && i <= 1000; i++) {
            if (bottle != i && can_open[i][bottle])
                res = true;
        }
        if (!res)
            cnt++;
    }

    cout << cnt;

}

int main() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);

    solve();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

const int INF = numeric_limits<int>::max();
const ll INF_LL = numeric_limits<ll>::max();

void solve() {
    int n;
    cin >> n;
    vector<vector<int>> mt(1 + n);
    for (int i = 0; i < n; i++) {
        int a, b;
        cin >> a >> b;
        mt[a].push_back(b);
    }

    queue<int> q;
    vector<bool> opened(1 + n, false);
    for (int i = 1; i <= n; i++) {
        vector<bool> visited(1 + n, false);
        q.push(i);

        while (!q.empty()) {
            int cur = q.front();
            q.pop();
            visited[cur] = true;

            for (int dest : mt[cur]) {
                if (!visited[dest]) {
                    opened[dest] = true;
                    q.push(dest);
                }
            }
        }
    }

    int cnt = 0;
    for (int i = 0; i < opened.size(); i++)
        cnt += !opened[i];
    cout << cnt - 1;

}

int main() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);

    solve();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

const int INF = numeric_limits<int>::max();
const ll INF_LL = numeric_limits<ll>::max();

void solve() {
    ll n;
    cin >> n;
    vector<bool> opens(1010, false);
    vector<ll> count(1010, 0);

    vector<ll> additional_check;

    for (ll i = 0; i < n; i++) {
        ll a, b;
        cin >> a >> b;
        count[a]++;

        if (a == b)
            additional_check.push_back(a);
        else
            opens[b] = true;
    }

//    for (ll a : additional_check)
//        if (count[a] > 1)
//            opens[a] = true;

    ll cnt = 0;
    for (ll i = 1; i <= 1000; i++)
        if (count[i] > 0 && !opens[i])
            cnt += count[i];

    cout << cnt;

}

int main() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);

    solve();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

const int INF = numeric_limits<int>::max();
const ll INF_LL = numeric_limits<ll>::max();

void solve() {
    int n;
    cin >> n;
    vector<int> opens(1001, false);
    vector<int> count(1001, 0);
    for (int i = 0; i < n; i++) {
        int a, b;
        cin >> a >> b;
        count[a]++;

        if (a == b && count[a] > 1)
            opens[b] = true;
        if (a != b)
            opens[b] = true;
    }

    int cnt = 0;
    for (int i = 1; i <= 1000; i++)
        if (count[i] > 0 && !opens[i])
            cnt += count[i];

    cout << cnt;

}

int main() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);

    solve();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

const int INF = numeric_limits<int>::max();
const ll INF_LL = numeric_limits<ll>::max();

void solve() {
    ll n;
    cin >> n;
    vector<bool> opens(1010, false);
    vector<ll> count(1010, 0);

    vector<ll> additional_check;

    for (ll i = 0; i < n; i++) {
        ll a, b;
        cin >> a >> b;
        count[a]++;

        if (a == b)
            additional_check.push_back(a);
        else
            opens[b] = true;
    }

    for (ll a : additional_check)
        if (count[a] > 1)
            opens[a] = true;

    ll cnt = 0;
    for (ll i = 1; i <= 1000; i++)
        if (count[i] > 0 && !opens[i])
            cnt += count[i];

    cout << cnt;

}

int main() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);

    solve();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

const int INF = numeric_limits<int>::max();
const ll INF_LL = numeric_limits<ll>::max();

void solve() {
    int n;
    cin >> n;
    vector<int> opens(1001, false);
    vector<int> count(1001, 0);

    vector<int> additional_check;

    for (int i = 0; i < n; i++) {
        int a, b;
        cin >> a >> b;
        count[a]++;

        if (a == b)
            additional_check.push_back(a);
        if (a != b)
            opens[b] = true;
    }

    for (int a : additional_check)
        if (count[a] > 1)
            opens[a] = true;

    int cnt = 0;
    for (int i = 1; i <= 1000; i++)
        if (count[i] > 0 && !opens[i])
            cnt += count[i];

    cout << cnt;

}

int main() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);

    solve();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

const int INF = numeric_limits<int>::max();
const ll INF_LL = numeric_limits<ll>::max();

void solve() {
    ll n;
    cin >> n;

    vector<vector<bool>> can_open(1001, vector<bool>(1001, false));
    vector<int> bottles(1001, 0);
    for (int i = 0; i < n; i++) {
        int a, b;
        cin >> a >> b;
        can_open[a][b] = true;
        bottles[a]++;
    }

    int cnt = 0;
    for (int i =  1; i <= 1000; i++) {
        bool res = false;
        if (bottles[i] == 0)
            continue;
        for (int j = 1; !res && j <= 1000; j++)
            if (i == j && bottles[i] > 1 && can_open[i][i])
                res = true;
            else if (i != j && can_open[i][j] && bottles[j] > 0)
                res = true;

        if (!res)
            cnt++;
    }

    cout << cnt;

}

int main() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);

    solve();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

const int INF = numeric_limits<int>::max();
const ll INF_LL = numeric_limits<ll>::max();

void solve() {
    int n;
    cin >> n;
    vector<vector<int>> mt(1 + n);
    for (int i = 0; i < n; i++) {
        int a, b;
        cin >> a >> b;
        mt[a].push_back(b);
    }

    queue<int> q;
    vector<bool> opened(1001, false);
    for (int i = 1; i <= n; i++) {
        vector<bool> visited(1 + n, false);
        q.push(i);

        while (!q.empty()) {
            int cur = q.front();
            q.pop();
            visited[cur] = true;

            for (int dest : mt[cur]) {
                if (!visited[dest]) {
                    opened[dest] = true;
                    q.push(dest);
                }
            }
        }
    }

    int cnt = 0;
    for (int i = 0; i < opened.size(); i++)
        cnt += !opened[i];
    cout << cnt - 1;

}

int main() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);

    solve();
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    long long q(1);//cin>>q;
    while(q--){
        long long n,a,b;set<long long >se;cin>>n;
        for(int i=0;i<n;i++){cin>>a>>b;if(a!=b){se.insert(b);}}
        cout<<n-se.size()<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    long long q(1);//cin>>q;
    while(q--){
        long long n,a,b,tot(0);cin>>n;
        map<long long ,long long>mp,gin;long long arr[n];
        for(int i=0;i<n;i++){cin>>arr[i]>>b;gin[arr[i]]++;if(arr[i]==b)continue;mp[b]++;}
        for(auto &x :arr){if(mp[x]==0 and gin[x]<2)tot++;}cout<<tot<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    long long q(1);//cin>>q;
    while(q--){
        long long n,a,b,tot(0);cin>>n;
        map<long long ,long long>mp,gin;long long arr[n];
        for(int i=0;i<n;i++){cin>>arr[i]>>b;gin[b]++;if(arr[i]==b)continue;mp[b]++;}
        for(auto &x :arr){if(mp[x]==0 and gin[x]<2)tot++;}cout<<tot<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    long long q(1);//cin>>q;
    while(q--){
        long long n,tot(0);cin>>n;
        long long arr[n],b[n],c[n]={0};
        for(int i=0;i<n;i++){cin>>arr[i]>>b[i];}
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                if(arr[i]==b[j] and i-j) c[i]++;
            }
        }
        for(auto &x : c){if(x==0)tot++;}
        cout<<tot<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    long long q(1);//cin>>q;
    while(q--){
        long long n,a,b,tot(0);cin>>n;
        map<long long ,long long >mp,same,cnt;long long arr[n];
        for(int i=0;i<n;i++){
            cin>>arr[i]>>b;cnt[arr[i]]++;
            if(arr[i]==b){same[i]++;}
            else{mp[b]++;}
        }
        for(auto &x :arr){
            if(mp[x]==0){tot++;}
            else continue;
            if(same[x]!=0 and cnt[x]>1) tot--;
        }
        cout<<tot<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    long long q(1);//cin>>q;
    while(q--){
        long long n,a,b,tot(0);cin>>n;
        map<long long ,long long>mp,gin,same;long long arr[n];
        for(int i=0;i<n;i++){cin>>arr[i]>>b;gin[arr[i]]++;if(arr[i]==b){same[a]++;continue;};mp[b]++;}
        for(auto &x :arr){if(mp[x]==0 or ( gin[x]<2 and same[x]!=0))tot++;}cout<<tot<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    long long q(1);//cin>>q;
    while(q--){
        long long n,a,b,tot(0);cin>>n;
        map<long long ,long long>mp;long long arr[n];
        for(int i=0;i<n;i++){cin>>arr[i]>>b;if(arr[i]==b)continue;mp[b]++;}
        for(auto &x :arr){if(mp[x]==0)tot++;}cout<<tot<<endl;
    }
}


#include<stdio.h>
int main (){
    int ara[101];
    int i,n,a,b,c=0;
    scanf("%d",&n);
    for(i=1;i<=n;i++){
        scanf("%d %d",&a,&b);
        if(a!=b){
            c++;
        }
    }
    printf("%d\n",n-c);
    return 0;
}


#include<stdio.h>
int ara[1001];
int counter[1001];
int main (){
    int i,n,a,b,co=0;

    scanf("%d",&n);
    for(i=0;i<n;i++){
        scanf("%d %d",&a,&b);
        ara[b]=a;
        counter[b]++;
    }
    for(i=1;i<1001;i++){
        if(ara[ara[i]]>0){
            if(i==ara[i]){
                    if(counter[ara[i]]>1){
                        co++;
                    }
            }
            else co++;
        }
    }
    printf("%d\n",n-co);
    return 0;
}


#include<stdio.h>
int main (){
    int ara[101];
    int i,n,a,b,c=0;
    scanf("%d",&n);
    for(i=1;i<=n;i++){
        scanf("%d %d",&a,&b);
        ara[a]=b;
    }
    for(i=1;i<=n;i++){
        if(ara[i]!=i){
            c++;
        }
    }
    printf("%d\n",n-c);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    cin >> n;

    map<int,int> ma, mb;

    int a, b;
    for(int i=0;i<n;++i)
    {
        cin >> a >> b;
        if(a != b or ma[a]) mb[b]++;
        ma[a]++;
    }

    for(auto x: mb)
        ma[x.first] = 0;

    int counter = 0;
    for(auto x: ma)
       counter += x.second;

    cout << counter << '\n';

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    cin >> n;

    map<int,int> ma, mb;

    int a, b;
    for(int i=0;i<n;++i)
    {
        cin >> a >> b;
        mb[b]++;
        ma[a]++;
    }

    for(auto x: mb)
    {
        if(x.second == 1 and ma[x.first] == 1) continue;
        ma[x.first] = 0;
    }

    int counter = 0;
    for(auto x: ma)
       counter += x.second;

    cout << counter << '\n';

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    cin >> n;

    vector<pair<int,int>> v(n);

    for(int i=0;i<n;++i)
        cin >> v[i].first >> v[i].second;

    int counter = 0;
    for(int i=0;i<n;++i)
        for(int j=0;j<n;++j)
            if(i!=j and v[j].second == v[i].first)
            {
                counter++;
                break;
            }

    cout << n - counter << '\n';

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    cin >> n;

    map<int,int> mp;
    int a, b;
    for(int i=0;i<n;++i)
    {
        cin >> a >> b;
        mp[b]++;
        if(a != b)
            mp[b] = -1e9;
    }

    int counter = 0;
    for(auto x: mp)
        counter += x.second > 0;

    cout << counter << '\n';

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    cin >> n;

    map<int,vector<int>> mp;
    map<int,bool> mb;

    int a, b;
    for(int i=0;i<n;++i)
    {
        cin >> a >> b;
        mp[a].push_back(b);
    }

    for(auto x: mp)
    {
        if(x.second.size() == 1 and x.first == x.second[0]) continue;
        for(auto y: x.second)
            mb[y] = true;
    }

    int counter = 0;
    for(auto x: mb)
    {
        // cout << x.first << " " << x.second << '\n';
        if(x.second)
            counter += mp[x.first].size();
    }

    cout << n - counter << '\n';

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    cin >> n;

    map<int,int> mp;
    int a, b;
    for(int i=0;i<n;++i)
    {
        cin >> a >> b;
        mp[a]++;
        if(a != b)
            mp[b] = -1e9;
    }

    int counter = 0;
    for(auto x: mp)
        counter += x.second > 0;

    cout << counter << '\n';

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    cin >> n;

    map<int,int> mp;
    vector<int> v;
    int a, b;

    for(int i=0;i<n;++i)
    {
        cin >> a >> b;
        if(a != b)
            v.push_back(a);
        mp[b]++;
    }

    for(int i=0;i<v.size();++i)
        mp[v[i]] = 0;

    int counter = 0;
    for(auto x: mp)
        counter += x.second > 0;

    cout << counter << '\n';

    return 0;
}


#include <iostream>
using namespace std;
int main()
{
    int n,c=0;
    std::cin>>n;
    int a[n],b[n],d[n]={0};
    for(int i=1;i<=n;i++){
    std::cin>>a[i]>>b[i];
    if((a[i]!=b[i])&&d[b[i]]==0)
    c++;
    d[b[i]]=1;
    }
    cout<<n-c;
}


#include <iostream>

using namespace std;

int main()
{
    int n;
    cin>>n;
    int d=n;
    int a[n],b[n];
    for(int i=0;i<n;i++)
        cin>>a[i]>>b[i];
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(i!=j&&(a[i]==b[j]||a[i]==a[j])){
                d--;
                break;
            }
        }
    }
    cout<<d;

    return 0;
}


#include <iostream>

using namespace std;

int main()
{
    int n;
    cin>>n;
    int d=n;
    int a[n],b[n];
    for(int i=0;i<n;i++)
        cin>>a[i]>>b[i];
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(i!=j){
                    if(a[i]==b[j]){
                d--;
                break;
                    }
                else if(a[i]==b[i]&&a[i]==a[j]){
                    d--;
                break;
                }
            }
        }
    }
    cout<<d;

    return 0;
}