#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <cstdio>
#include <map>
#include <unordered_map>
#include <string>
#include <iomanip>
#include <vector>
#include <memory.h>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <algorithm>
#include <math.h>
#include <sstream>
#include <functional>
#include <bitset>
using namespace std;
#define mems(A, val) memset(A, val, sizeof(A))
#define mp(a, b) make_pair(a, b)
#define all(B) (B).begin(), (B).end()
#define forn(it, from, to) for(int it = from; it < to; ++it)
#define forit (it, coll) for(auto it = coll.begin(); it != coll.end(); ++it)
#define sz(a) (int)a.size()
#define pb push_back
const int MAXN = 2 * 1000 * 1000 + 10;
const double EPS = 1e-9;
typedef long long LL;
const LL MOD = 1000 * 1000 * 1000 + 7;

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

#ifdef _DEBUG
	freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#else
	//freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
	//freopen("numbers.in", "r", stdin); freopen("numbers.out", "w", stdout);
#endif

	ios::sync_with_stdio(0);

	int n;
	cin >> n;
	vector<int> p(n);
	forn(i, 0, n) {
		cin >> p[i]; p[i]--;
	}

	vector<int> b(n);
	int sum_b = 0;
	forn(i, 0, n) {
		cin >> b[i];
		sum_b += b[i];
	}

	int ans = 0;

	ans += (sum_b + 1) % 2;

	vector<int> self;
	for (int i = 0; i < n; ++i) {
		if (p[i] == i) self.push_back(i);
	}

	for (int i = 0; i < self.size(); ++i) {
		p[self[(i + 1) % (int)self.size()]] = self[i];
		ans++;
	}

	vector<char> used(n);
	for (int i = 0; i < n; ++i) {
		if (used[i]) continue;

		ans+=2;
		int v = i;
		while (!used[v]) {
			used[v] = true;
			v = p[v];
		}
	}

	ans -= 2;
	cout << ans;

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <cstdio>
#include <map>
#include <unordered_map>
#include <string>
#include <iomanip>
#include <vector>
#include <memory.h>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <algorithm>
#include <math.h>
#include <sstream>
#include <functional>
#include <bitset>
using namespace std;
#define mems(A, val) memset(A, val, sizeof(A))
#define mp(a, b) make_pair(a, b)
#define all(B) (B).begin(), (B).end()
#define forn(it, from, to) for(int it = from; it < to; ++it)
#define forit (it, coll) for(auto it = coll.begin(); it != coll.end(); ++it)
#define sz(a) (int)a.size()
#define pb push_back
const int MAXN = 2 * 1000 * 1000 + 10;
const double EPS = 1e-9;
typedef long long LL;
const LL MOD = 1000 * 1000 * 1000 + 7;

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

#ifdef _DEBUG
	freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#else
	//freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
	//freopen("numbers.in", "r", stdin); freopen("numbers.out", "w", stdout);
#endif

	ios::sync_with_stdio(0);

	int n;
	cin >> n;
	vector<int> p(n);
	forn(i, 0, n) {
		cin >> p[i]; p[i]--;
	}

	vector<int> b(n);
	int sum_b = 0;
	forn(i, 0, n) {
		cin >> b[i];
		sum_b += b[i];
	}

	if (n == 1) {
		if (b[0] == 1) {
			cout << 0;
		}
		else {
			cout << 1;
		}
		return 0;
	}

	int ans = 0;

	ans += (sum_b + 1) % 2;

	int count = 0;
	vector<char> used(n);
	for (int i = 0; i < n; ++i) {
		if (used[i]) continue;

		ans++;
		count++;
		int v = i;
		while (!used[v]) {
			used[v] = true;
			v = p[v];
		}
	}

	if (count == 1) ans--;

	cout << ans;

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <cstdio>
#include <map>
#include <unordered_map>
#include <string>
#include <iomanip>
#include <vector>
#include <memory.h>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <algorithm>
#include <math.h>
#include <sstream>
#include <functional>
#include <bitset>
using namespace std;
#define mems(A, val) memset(A, val, sizeof(A))
#define mp(a, b) make_pair(a, b)
#define all(B) (B).begin(), (B).end()
#define forn(it, from, to) for(int it = from; it < to; ++it)
#define forit (it, coll) for(auto it = coll.begin(); it != coll.end(); ++it)
#define sz(a) (int)a.size()
#define pb push_back
const int MAXN = 2 * 1000 * 1000 + 10;
const double EPS = 1e-9;
typedef long long LL;
const LL MOD = 1000 * 1000 * 1000 + 7;

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

#ifdef _DEBUG
	freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#else
	//freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
	//freopen("numbers.in", "r", stdin); freopen("numbers.out", "w", stdout);
#endif

	ios::sync_with_stdio(0);

	int n;
	cin >> n;
	vector<int> p(n);
	forn(i, 0, n) {
		cin >> p[i]; p[i]--;
	}

	vector<int> b(n);
	int sum_b = 0;
	forn(i, 0, n) {
		cin >> b[i];
		sum_b += b[i];
	}

	if (n == 1) {
		if (b[0] == 1) {
			cout << 0;
		}
		else {
			cout << 1;
		}
		return 0;
	}

	int ans = 0;

	ans += (sum_b + 1) % 2;

	vector<int> self;
	for (int i = 0; i < n; ++i) {
		if (p[i] == i) self.push_back(i);
	}

	if (self.size() != 1) {
		for (int i = 0; i < self.size(); ++i) {
			p[self[(i + 1) % (int)self.size()]] = self[i];
			ans++;
		}
	}

	vector<char> used(n);
	for (int i = 0; i < n; ++i) {
		if (used[i]) continue;

		// only if self.size() == 1 ans n > 1
		if (p[i] == i) continue;

		ans+=2;
		int v = i;
		while (!used[v]) {
			used[v] = true;
			v = p[v];
		}
	}

	if (self.size() == 1) {
		ans += 2;
	}

	ans -= 2;
	cout << ans;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

vector <int> a;
bool vis[200100];
void dfs (int u)
{
	vis[u] = 1;
	if (!vis[a[u]]) dfs(a[u]);
}

int main()
{
	int n;
	cin >> n;
	a = vector <int> (n);
	for (int &t : a)
	{
		cin >> t;
		t--;
	}
	int x = 0, anw = 0;
	for (int i = 0; i < n; i++)
	{
		int t;
		cin >> t;
		x += t;
	}
	if (!(x&1)) anw++;
	int c = 0;
	for (int i = 0; i < n; i++)
	{
		if (!vis[i]){
			dfs(i);
			c++;
		}
	}
	if (c != 1) anw += c;
	cout << anw << endl;
}


#include <bits/stdc++.h>

using namespace std;

vector <int> a;
bool vis[110];
void dfs (int u)
{
	vis[u] = 1;
	if (!vis[a[u]]) dfs(a[u]);
}

int main()
{
	int n;
	cin >> n;
	a = vector <int> (n);
	for (int &t : a)
	{
		cin >> t;
		t--;
	}
	int x = 0, anw = 0;
	for (int i = 0; i < n; i++)
	{
		int t;
		cin >> t;
		x += t;
	}
	if (!(x&1)) anw++;
	int c = 0;
	for (int i = 0; i < n; i++)
	{
		if (!vis[i]){
			dfs(i);
			c++;
		}
	}
	if (c != 1) anw += c;
	cout << anw << endl;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  vector<int> perm(N);
  for (int &p : perm) {
    cin >> p;
    --p;
  }
  bool was[2] = {false, false};
  for (int i = 0, b; i < N; ++i) {
    cin >> b;
    was[b] = true;
  }
  int res = (!was[1]);
  vector<bool> vis(N, false);
  int cycs = 0;
  for (int i = 0; i < N; ++i) {
    if (vis[i])continue;
    ++cycs;
    for (int at = i; !vis[at]; at = perm[at])
      vis[at] = true;
  }
  if (cycs > 1)
    res += cycs;
  cout << res << endl;
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  vector<int> perm(N);
  for (int &p : perm) {
    cin >> p;
    --p;
  }
  bool was[2] = {false, false};
  int odd = 0;
  for (int i = 0, b; i < N; ++i) {
    cin >> b;
    was[b] = true;
    if (b == 1)
      odd ^= 1;
  }
  int res = (!was[1]) + odd;
  vector<bool> vis(N, false);
  int cycs = 0;
  for (int i = 0; i < N; ++i) {
    if (vis[i])continue;
    ++cycs;
    for (int at = i; !vis[at]; at = perm[at])
      vis[at] = true;
  }
  if (cycs > 1)
    res += cycs;
  cout << res << endl;
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  vector<int> perm(N);
  for (int &p : perm) {
    cin >> p;
    --p;
  }
  bool was[2] = {false, false};
  for (int i = 0, b; i < N; ++i) {
    cin >> b;
    was[b] = true;
  }
  int res = (!was[0] || !was[1]);
  vector<bool> vis(N, false);
  int cycs = 0;
  for (int i = 0; i < N; ++i) {
    if (vis[i])continue;
    ++cycs;
    for (int at = i; !vis[at]; at = perm[at])
      vis[at] = true;
  }
  if (cycs > 1)
    res += cycs;
  cout << res << endl;
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  vector<int> perm(N);
  for (int &p : perm) {
    cin >> p;
    --p;
  }
  int odd = 0;
  for (int i = 0, b; i < N; ++i) {
    cin >> b;
    if (b == 1)
      odd ^= 1;
  }
  int res = (!odd);
  vector<bool> vis(N, false);
  int cycs = 0;
  for (int i = 0; i < N; ++i) {
    if (vis[i])continue;
    ++cycs;
    for (int at = i; !vis[at]; at = perm[at])
      vis[at] = true;
  }
  if (cycs > 1)
    res += cycs;
  cout << res << endl;
  return 0;
}


#include <stdio.h>
#include <bits/stdc++.h>
#include <x86intrin.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
//																														tree<key, mapped = null_type, cmp = less<key>, rb_tree_tag, tree_order_statistics_node_update> name; order_of_key find_by_order

using namespace std;

#define pb push_back
#define ppb pop_back
#define mp make_pair
#define fs first
#define sc second
#define abs(a) ((a) < 0 ? -(a) : (a))
#define sqr(a) ((a) * (a))

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;

inline pair<pair<bool, bool>, pair<string, bool> > useinout();

void initialization() {
#ifdef SOL
	if (useinout().fs.fs)
		freopen("input.txt", "r", stdin);
	if (useinout().fs.sc)
		freopen("output.txt", "w", stdout);
#else
	srand(__rdtsc());
	const string file = useinout().sc.fs;
	if (!file.empty()) {
		freopen((file + ".in").c_str(), "r", stdin);
		freopen((file + ".out").c_str(), "w", stdout);
	}
	if(useinout().sc.sc) {
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	}
#endif
}

int solution();

void finish(int exitval) {
	fflush(stdout);
#ifdef SOL
	fprintf(stderr, "\nexit value: %d\ntime: %.3lf ms\n", exitval, 1000. * clock() / CLOCKS_PER_SEC);
#endif
}

int main() {
	initialization();
	finish(solution());
	return (0);
}

const double eps = 1e-9;
const int mod = (int) 1e+9 + 7;
const double pi = acos(-1.);
const int maxn = 100100;

int a[maxn], b[maxn];
bool pm[maxn];

int solution() {

	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		scanf("%d", &a[i]);
		a[i]--;
	}
	int c = 0;
	for (int i = 0; i < n; i++) {
		scanf("%d", &b[i]);
		c += b[i];
	}
	int ans = -2;
	ans += !(c & 1);
	for (int i = 0; i < n; i++) {
		bool ok = 1;
		int j = i;
		while (!pm[j]) {
			ok = 0;
			pm[j] = 1;
			j = a[j];
		}
		if (!ok) {
			ans += 2;
		}
	}
	printf("%d", ans);

	return (0);
}

inline pair<pair<bool, bool>, pair<string, bool> > useinout() {
	return (mp(mp(0, 0), mp("", 0)));
}

//by Andrey Kim


#include <stdio.h>
#include <bits/stdc++.h>
#include <x86intrin.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
//																														tree<key, mapped = null_type, cmp = less<key>, rb_tree_tag, tree_order_statistics_node_update> name; order_of_key find_by_order

using namespace std;

#define pb push_back
#define ppb pop_back
#define mp make_pair
#define fs first
#define sc second
#define abs(a) ((a) < 0 ? -(a) : (a))
#define sqr(a) ((a) * (a))

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;

inline pair<pair<bool, bool>, pair<string, bool> > useinout();

void initialization() {
#ifdef SOL
	if (useinout().fs.fs)
		freopen("input.txt", "r", stdin);
	if (useinout().fs.sc)
		freopen("output.txt", "w", stdout);
#else
	srand(__rdtsc());
	const string file = useinout().sc.fs;
	if (!file.empty()) {
		freopen((file + ".in").c_str(), "r", stdin);
		freopen((file + ".out").c_str(), "w", stdout);
	}
	if(useinout().sc.sc) {
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	}
#endif
}

int solution();

void finish(int exitval) {
	fflush(stdout);
#ifdef SOL
	fprintf(stderr, "\nexit value: %d\ntime: %.3lf ms\n", exitval, 1000. * clock() / CLOCKS_PER_SEC);
#endif
}

int main() {
	initialization();
	finish(solution());
	return (0);
}

const double eps = 1e-9;
const int mod = (int) 1e+9 + 7;
const double pi = acos(-1.);
const int maxn = 200100;

int a[maxn], b[maxn];
bool pm[maxn];

int solution() {

	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		scanf("%d", &a[i]);
		a[i]--;
	}
	int c = 0;
	for (int i = 0; i < n; i++) {
		scanf("%d", &b[i]);
		c += b[i];
	}
	int ans = -2;
	ans += !(c & 1);
	for (int i = 0; i < n; i++) {
		bool ok = 1;
		int j = i;
		while (!pm[j]) {
			ok = 0;
			pm[j] = 1;
			j = a[j];
		}
		if (!ok) {
			ans += 2;
		}
	}
	printf("%d", ans);

	return (0);
}

inline pair<pair<bool, bool>, pair<string, bool> > useinout() {
	return (mp(mp(0, 0), mp("", 0)));
}

//by Andrey Kim


#include <stdio.h>
#include <bits/stdc++.h>
#include <x86intrin.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
//																														tree<key, mapped = null_type, cmp = less<key>, rb_tree_tag, tree_order_statistics_node_update> name; order_of_key find_by_order

using namespace std;

#define pb push_back
#define ppb pop_back
#define mp make_pair
#define fs first
#define sc second
#define abs(a) ((a) < 0 ? -(a) : (a))
#define sqr(a) ((a) * (a))

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;

inline pair<pair<bool, bool>, pair<string, bool> > useinout();

void initialization() {
#ifdef SOL
	if (useinout().fs.fs)
		freopen("input.txt", "r", stdin);
	if (useinout().fs.sc)
		freopen("output.txt", "w", stdout);
#else
	srand(__rdtsc());
	const string file = useinout().sc.fs;
	if (!file.empty()) {
		freopen((file + ".in").c_str(), "r", stdin);
		freopen((file + ".out").c_str(), "w", stdout);
	}
	if(useinout().sc.sc) {
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	}
#endif
}

int solution();

void finish(int exitval) {
	fflush(stdout);
#ifdef SOL
	fprintf(stderr, "\nexit value: %d\ntime: %.3lf ms\n", exitval, 1000. * clock() / CLOCKS_PER_SEC);
#endif
}

int main() {
	initialization();
	finish(solution());
	return (0);
}

const double eps = 1e-9;
const int mod = (int) 1e+9 + 7;
const double pi = acos(-1.);
const int maxn = 200100;

int a[maxn], b[maxn];
bool pm[maxn];

int solution() {

	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		scanf("%d", &a[i]);
		a[i]--;
	}
	int c = 0;
	for (int i = 0; i < n; i++) {
		scanf("%d", &b[i]);
		c += b[i];
	}
	int ans = 0, cnt = 0;
	ans += !(c & 1);
	for (int i = 0; i < n; i++) {
		bool ok = 1;
		int j = i;
		while (!pm[j]) {
			ok = 0;
			pm[j] = 1;
			j = a[j];
		}
		if (!ok) {
			ans += 1;
			cnt += 1;
		}
	}
	ans -= cnt == 1;
	printf("%d", ans);

	return (0);
}

inline pair<pair<bool, bool>, pair<string, bool> > useinout() {
	return (mp(mp(0, 0), mp("", 0)));
}

//by Andrey Kim


#include <bits/stdc++.h>

#define x first
#define y second

using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
typedef pair<ll,ll> pll;

const int mod=1000000000+7;

int addm(int& a,int b) {return (a+=b)<mod?a:a-=mod;}

template<class T,class U> bool smin(T& a,U b) {return a>b?(a=b,1):0;}
template<class T,class U> bool smax(T& a,U b) {return a<b?(a=b,1):0;}

int n,p[200000],b[200000];
bool vis[200000];
int ct;

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	cin >> n;
	for (int i=0;i<n;i++) cin >> p[i];
	for (int i=0;i<n;i++) {
		cin >> b[i];
		p[i]--;
	}

	for (int i=0;i<n;i++) if (!vis[i]) {
		vis[i]=1;
		ct++;
		int j=p[i];
		while (!vis[j]) {
			vis[j]=1;
			j=p[j];
		}
	}

	cout << (ct==1?0:ct)+(accumulate(b,b+n,0)%2==0) << endl;
}


#include <bits/stdc++.h>

#define x first
#define y second

using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
typedef pair<ll,ll> pll;

const int mod=1000000000+7;

int addm(int& a,int b) {return (a+=b)<mod?a:a-=mod;}

template<class T,class U> bool smin(T& a,U b) {return a>b?(a=b,1):0;}
template<class T,class U> bool smax(T& a,U b) {return a<b?(a=b,1):0;}

int n,p[200000],b[200000];
bool vis[200000];
int ct;

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	cin >> n;
	for (int i=0;i<n;i++) cin >> p[i];
	for (int i=0;i<n;i++) {
		cin >> b[i];
		p[i]--;
	}

	for (int i=0;i<n;i++) if (!vis[i]) {
		vis[i]=1;
		ct++;
		int j=p[i];
		while (!vis[j]) {
			vis[j]=1;
			j=p[j];
		}
	}

	cout << 2*(ct-1)+(accumulate(b,b+n,0)%2==0) << endl;
}


/*
ID: espr1t
TASK:
KEYWORDS:
*/

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>

using namespace std;
FILE *in; FILE *out;

const int MAX = 200002;

int n;
bool vis[MAX];
int a[MAX], b[MAX];

void recurse(int node) {
    if (vis[node])
        return;
    vis[node] = true;
    recurse(a[node]);
}

int main(void) {
	in = stdin; out = stdout;
//	in = fopen("A.in", "rt"); // out = fopen("file.out", "wt");

	fscanf(in, "%d", &n);
	for (int i = 0; i < n; i++) {
	   fscanf(in, "%d", &a[i]);
	   a[i]--;
    }
	int cnt = 0;
	for (int i = 0; i < n; i++) {
	   fscanf(in, "%d", &b[i]);
	   cnt += b[i];
    }

	int ans = 0;
	for (int i = 0; i < n; i++) {
	    if (!vis[i]) {
	        ans++;
	        recurse(i);
        }
    }
    ans = (ans - 1) * 2 + !(cnt % 2);
	fprintf(out, "%d\n", ans);

	return 0;
}


/*
ID: espr1t
TASK:
KEYWORDS:
*/

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>

using namespace std;
FILE *in; FILE *out;

const int MAX = 200002;

int n;
bool vis[MAX];
int a[MAX], b[MAX];

int recurse(int node) {
    if (vis[node])
        return 0;
    vis[node] = true;
    return 1 + recurse(a[node]);
}

int main(void) {
	in = stdin; out = stdout;
//	in = fopen("A.in", "rt"); // out = fopen("file.out", "wt");

	fscanf(in, "%d", &n);
	for (int i = 0; i < n; i++) {
	   fscanf(in, "%d", &a[i]);
	   a[i]--;
    }
	int cnt = 0;
	for (int i = 0; i < n; i++) {
	   fscanf(in, "%d", &b[i]);
	   cnt += b[i];
    }

	int ans = 0;
	for (int i = 0; i < n; i++) {
	    if (!vis[i]) {
	        int size = recurse(i);
	        ans++;
	        recurse(i);
        }
    }
    if (ans == 1)
        ans = 0;
    ans += !(cnt % 2);
	fprintf(out, "%d\n", ans);

	return 0;
}


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

#define ms(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORd(i, a, b) for (int i = (a) - 1; i >= (b); i--)
#define FORall(it, a) for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
#define FORalld(it, a) for (__typeof((a).rbegin()) it = (a).rbegin(); it != (a).rend(); it++)
#define sz(a) int((a).size())
#define present(t, x) (t.find(x) != t.end())
#define all(a) (a).begin(), (a).end()
#define uni(a) (a).erase(unique(all(a)), (a).end())
#define pb push_back
#define pf push_front
#define mp make_pair
#define fi first
#define se second
#define prec(n) fixed<<setprecision(n)
#define bit(n, i) (((n) >> (i)) & 1)
#define bitcount(n) __builtin_popcountll(n)
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pi> vii;
const int MOD = (int) 1e9 + 7;
const int INF = (int) 1e9;
const ll LINF = (ll) 1e18;
const ld PI = acos((ld) -1);
const ld EPS = 1e-9;
inline ll gcd(ll a, ll b) {ll r; while (b) {r = a % b; a = b; b = r;} return a;}
inline ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
inline ll fpow(ll n, ll k, int p = MOD) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = r * n % p; n = n * n % p;} return r;}
template<class T> inline int chkmin(T& a, const T& val) {return val < a ? a = val, 1 : 0;}
template<class T> inline int chkmax(T& a, const T& val) {return a < val ? a = val, 1 : 0;}
template<class T> inline T isqrt(T k) {T r = sqrt(k) + 1; while (r * r > k) r--; return r;}
template<class T> inline T icbrt(T k) {T r = cbrt(k) + 1; while (r * r * r > k) r--; return r;}
inline void addmod(int& a, int val, int p = MOD) {if ((a = (a + val)) >= p) a -= p;}
inline void submod(int& a, int val, int p = MOD) {if ((a = (a - val)) < 0) a += p;}
inline int mult(int a, int b, int p = MOD) {return (ll) a * b % p;}
inline int inv(int a, int p = MOD) {return fpow(a, p - 2, p);}
inline int sign(ld x) {return x < -EPS ? -1 : x > +EPS;}
inline int sign(ld x, ld y) {return sign(x - y);}

const int maxn = 200000 + 10;
int n;
int p[maxn];
int b[maxn];
int vis[maxn];

void solve() {
	cin >> n;
	FOR(i, 0, n) cin >> p[i], p[i]--;
	FOR(i, 0, n) cin >> b[i];
	vi v;
	FOR(i, 0, n) if (!vis[i]) {
		int t = i, cnt = 0;
		while (!vis[t]) {
			vis[t] = 1;
			t = p[t];
			cnt++;
		}
		v.pb(cnt);
	}
	int ans = 0;
	if (sz(v) > 1) ans = sz(v);
	int sum = 0;
	FOR(i, 0, n) sum += b[i];
	if (!(sum & 1)) ans++;
	cout << ans << "\n";
}

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef _LOCAL_
	freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout);
#endif
	solve();
#ifdef _LOCAL_
	//printf("\nTime elapsed: %dms", 1000 * clock() / CLOCKS_PER_SEC);
#endif
	return 0;
}


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

#define ms(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORd(i, a, b) for (int i = (a) - 1; i >= (b); i--)
#define FORall(it, a) for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
#define FORalld(it, a) for (__typeof((a).rbegin()) it = (a).rbegin(); it != (a).rend(); it++)
#define sz(a) int((a).size())
#define present(t, x) (t.find(x) != t.end())
#define all(a) (a).begin(), (a).end()
#define uni(a) (a).erase(unique(all(a)), (a).end())
#define pb push_back
#define pf push_front
#define mp make_pair
#define fi first
#define se second
#define prec(n) fixed<<setprecision(n)
#define bit(n, i) (((n) >> (i)) & 1)
#define bitcount(n) __builtin_popcountll(n)
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pi> vii;
const int MOD = (int) 1e9 + 7;
const int INF = (int) 1e9;
const ll LINF = (ll) 1e18;
const ld PI = acos((ld) -1);
const ld EPS = 1e-9;
inline ll gcd(ll a, ll b) {ll r; while (b) {r = a % b; a = b; b = r;} return a;}
inline ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
inline ll fpow(ll n, ll k, int p = MOD) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = r * n % p; n = n * n % p;} return r;}
template<class T> inline int chkmin(T& a, const T& val) {return val < a ? a = val, 1 : 0;}
template<class T> inline int chkmax(T& a, const T& val) {return a < val ? a = val, 1 : 0;}
template<class T> inline T isqrt(T k) {T r = sqrt(k) + 1; while (r * r > k) r--; return r;}
template<class T> inline T icbrt(T k) {T r = cbrt(k) + 1; while (r * r * r > k) r--; return r;}
inline void addmod(int& a, int val, int p = MOD) {if ((a = (a + val)) >= p) a -= p;}
inline void submod(int& a, int val, int p = MOD) {if ((a = (a - val)) < 0) a += p;}
inline int mult(int a, int b, int p = MOD) {return (ll) a * b % p;}
inline int inv(int a, int p = MOD) {return fpow(a, p - 2, p);}
inline int sign(ld x) {return x < -EPS ? -1 : x > +EPS;}
inline int sign(ld x, ld y) {return sign(x - y);}

const int maxn = 200000 + 10;
int n;
int p[maxn];
int b[maxn];
int vis[maxn];

void solve() {
	cin >> n;
	FOR(i, 0, n) cin >> p[i], p[i]--;
	FOR(i, 0, n) cin >> b[i];
	int ans = 0;
	FOR(i, 0, n) if (!vis[i]) {
		int t = i;
		while (!vis[t]) {
			vis[t] = 1;
			t = p[t];
		}
		ans++;
	}
	ans = (ans - 1) * 2;
	int sum = 0;
	FOR(i, 0, n) sum += b[i];
	if (!(sum & 1)) ans++;
	cout << ans << "\n";
}

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef _LOCAL_
	freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout);
#endif
	solve();
#ifdef _LOCAL_
	//printf("\nTime elapsed: %dms", 1000 * clock() / CLOCKS_PER_SEC);
#endif
	return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define F first
#define S second
using namespace std;
const int maxn = 2e5+5;
int n, p[maxn];
int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n;
	for (int i=1;i<=n;i++) cin >> p[i];
	int cyc = 0;
	for (int i=1;i<=n;i++) {
		if (p[i] == -1) continue;
		int u = i;
		while (u != -1) {
			int v = p[u];
			p[u] = -1;
			u = v;
		}
		cyc++;
	}
	int ans = 0;
	for (int i=0;i<n;i++) {
		int x; cin >> x; ans |= x;
	}
	ans = ans^1;
	// cout << ans << " " << cyc << " ";
	if (cyc > 1) ans += cyc;
	cout << ans << endl;
	return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define F first
#define S second
using namespace std;
const int maxn = 2e5+5;
int n, p[maxn];
int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n;
	for (int i=1;i<=n;i++) cin >> p[i];
	int cyc = 0;
	for (int i=1;i<=n;i++) {
		if (p[i] == -1) continue;
		int u = i;
		while (u != -1) {
			int v = p[u];
			p[u] = -1;
			u = v;
		}
		cyc++;
	}
	int ans = 0;
	for (int i=0;i<n;i++) {
		int x; cin >> x; ans ^= x;
	}
	ans = ans^1;
	// cout << ans << " " << cyc << " ";
	if (cyc > 1) ans += cyc;
	cout << ans << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int f[200020];
int p[200020];
int ans,ttl,i,n,b;

void go(int u)
 {
  if (f[u]) return;
  f[u]=1;
  go(p[u]);
 }

int main() {

 cin>>n;
 for (i=1;i<=n;i++)
  scanf("%d",&p[i]);

 for (i=1;i<=n;i++)
   if (!f[i]) { ttl++; go(i); }

  for (i=1;i<=n;i++)
  {
    scanf("%d",&b);
    if (b) ans=1-ans;
  }

   ttl--;
   ttl*=2;
   ttl+=1-ans;
   cout<<ttl<<endl;

}


#include <bits/stdc++.h>

using namespace std;

int f[200020];
int p[200020];
int ans,ttl,i,n,b;

void go(int u)
 {
  if (f[u]) return;
  f[u]=1;
  go(p[u]);
 }

int main() {

 cin>>n;
 for (i=1;i<=n;i++)
  scanf("%d",&p[i]);

 for (i=1;i<=n;i++)
   if (!f[i]) { ttl++; go(i); }

  for (i=1;i<=n;i++)
   scanf("%d",&b),ans^=b;

   ttl--;
   ttl*=2;
   ttl+=ans^1;
   cout<<ttl<<endl;

}


#include <bits/stdc++.h>

using namespace std;

int f[200020];
int p[200020];
int ans,ttl,i,n,b;

void go(int u)
 {
  if (f[u]) return;
  f[u]=1;
  go(p[u]);
 }

int main() {

 cin>>n;
 for (i=1;i<=n;i++)
  scanf("%d",&p[i]);

 for (i=1;i<=n;i++)
   if (!f[i]) { ttl++; go(i); }

  for (i=1;i<=n;i++)
  {
    scanf("%d",&b);
    if (b) ans=1-ans;
  }

   if (ttl == 1) ttl--;
   ttl+=1-ans;
   cout<<ttl<<endl;

}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fst first
#define snd second
#define fore(i,a,b) for(int i=a,ThxDem=b;i<ThxDem;++i)
using namespace std;
typedef long long ll;

int p[200005];
bool vis[200005];
int n;

int main(){
	scanf("%d",&n);
	fore(i,0,n)scanf("%d",p+i),p[i]--;
	int nb=0;
	fore(i,0,n){
		int b;
		scanf("%d",&b);
		if(b)nb++;
	}
	int nc=0;
	fore(i,0,n){
		if(!vis[i]){
			nc++;
			for(int j=i;!vis[j];j=p[j])vis[j]=true;
		}
	}
	int r=2*(nc-1);
	if(nb%2==0)r++;
	printf("%d\n",r);
	return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fst first
#define snd second
#define fore(i,a,b) for(int i=a,ThxDem=b;i<ThxDem;++i)
using namespace std;
typedef long long ll;

int p[200005];
bool vis[200005];
int n;

int main(){
	scanf("%d",&n);
	fore(i,0,n)scanf("%d",p+i),p[i]--;
	int nb=0;
	fore(i,0,n){
		int b;
		scanf("%d",&b);
		if(b)nb++;
	}
	int nc=0;
	fore(i,0,n){
		if(!vis[i]){
			nc++;
			for(int j=i;!vis[j];j=p[j])vis[j]=true;
		}
	}
	int r=0;
	if(nc>1)r+=nc;
	if(nb%2==0)r++;
	printf("%d\n",r);
	return 0;
}


// RandomUsername (Nikola Jovanovic)
// 8VC 2017 Final Round
// A

#include <bits/stdc++.h>
#define DBG false
#define debug(x) if(DBG) printf("(ln %d) %s = %d\n", __LINE__, #x, x);
#define lld long long
#define ff(i,a,b) for(int i=a; i<=b; i++)
#define fb(i,a,b) for(int i=a; i>=b; i--)
#define par pair<int, int>
#define fi first
#define se second
#define mid (l+r)/2
#define INF 1000000000
#define MAXN 200005

using namespace std;

int n;
int a[MAXN];
int b[MAXN];
int cnt = 0;
int ret = 0;
bool vis[MAXN];
int comps = 0;

int main()
{
    scanf("%d", &n);
    ff(i, 1, n)
    {
        scanf("%d", &a[i]);
    }
    ff(i, 1, n)
    {
        scanf("%d", &b[i]);
        cnt += b[i];
    }
    if(cnt % 2 == 0)
    {
        ret = 1;
    }
    ff(i, 1, n)
    {
        if(!vis[i])
        {
            comps++;
        }
        int curr = i;
        while(!vis[curr])
        {
            vis[curr] = true;
            curr = a[curr];
        }
    }
    ret += 2 * (comps - 1);
    printf("%d\n", ret);
    return 0;
}


// RandomUsername (Nikola Jovanovic)
// 8VC 2017 Final Round
// A

#include <bits/stdc++.h>
#define DBG false
#define debug(x) if(DBG) printf("(ln %d) %s = %d\n", __LINE__, #x, x);
#define lld long long
#define ff(i,a,b) for(int i=a; i<=b; i++)
#define fb(i,a,b) for(int i=a; i>=b; i--)
#define par pair<int, int>
#define fi first
#define se second
#define mid (l+r)/2
#define INF 1000000000
#define MAXN 200005

using namespace std;

int n;
int a[MAXN];
int b[MAXN];
int cnt = 0;
int ret = 0;
bool vis[MAXN];
int comps = 0;

int main()
{
    scanf("%d", &n);
    ff(i, 1, n)
    {
        scanf("%d", &a[i]);
    }
    ff(i, 1, n)
    {
        scanf("%d", &b[i]);
        cnt += b[i];
    }
    if(cnt % 2 == 0)
    {
        ret = 1;
    }
    ff(i, 1, n)
    {
        if(!vis[i])
        {
            comps++;
        }
        int curr = i;
        while(!vis[curr])
        {
            vis[curr] = true;
            curr = a[curr];
        }
    }
    if(comps > 1)
    ret += comps;
    printf("%d\n", ret);
    return 0;
}


// RandomUsername (Nikola Jovanovic)
// 8VC 2017 Final Round
// A

#include <bits/stdc++.h>
#define DBG false
#define debug(x) if(DBG) printf("(ln %d) %s = %d\n", __LINE__, #x, x);
#define lld long long
#define ff(i,a,b) for(int i=a; i<=b; i++)
#define fb(i,a,b) for(int i=a; i>=b; i--)
#define par pair<int, int>
#define fi first
#define se second
#define mid (l+r)/2
#define INF 1000000000
#define MAXN 200005

using namespace std;

int n;
int a[MAXN];
int b[MAXN];
int cnt = 0;
int ret = 0;
bool vis[MAXN];
int comps = 0;

int main()
{
    scanf("%d", &n);
    ff(i, 1, n)
    {
        scanf("%d", &a[i]);
    }
    ff(i, 1, n)
    {
        scanf("%d", &b[i]);
        cnt += b[i];
    }
    if(cnt % 2 == 0)
    {
        ret = 1;
    }
    int ones = 0;
    ff(i, 1, n)
    {
        if(a[i] == i)
        {
            ones++;
        }
        if(!vis[i])
        {
            comps++;
        }
        int curr = i;
        while(!vis[curr])
        {
            vis[curr] = true;
            curr = a[curr];
        }
    }
    ret += 2 * (comps - 1);
    if(ones > comps - 2)
        ones = comps - 2;
    if(ones > 0)
        ret -= ones;
    printf("%d\n", ret);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=2*100000+5;
int rfl[maxn],b[maxn];
bool vis[maxn];
int n;
int main()
{
	//freopen("2.in","r",stdin);
	//freopen("1.out","w",stdout);
	scanf("%d",&n);
	//printf("n=%d\n",n);
	for(int i=1;i<=n;i++) scanf("%d",&rfl[i]);
	bool flag=false;
	for(int i=1;i<=n;i++) {scanf("%d",&b[i]);flag|=(b[i]==1);}
	//for(int i=1;i<=n;i++) printf("b[%d]=%d\n",i,b[i]);
	//printf("flag=%d\n",flag);
	int Ans=0;
	if(!flag) Ans++;
	set<int > points;
	for(int i=1;i<=n;i++) points.insert(i);
	for(set<int >::iterator ite=points.begin();ite!=points.end();++ite){
		int tmp=*ite,st=rfl[*ite];
		Ans++;
		while(st!=tmp){
			points.erase(st);
			st=rfl[st];
		}
		points.erase(tmp);
	}
	printf("%d\n",Ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=2*100000+5;
int rfl[maxn],b[maxn];
int n;
int main()
{
	//freopen("2.in","r",stdin);
	//freopen("1.out","w",stdout);
	scanf("%d",&n);
	//printf("n=%d\n",n);
	for(int i=1;i<=n;i++) scanf("%d",&rfl[i]);
	int bnum=0;
	for(int i=1;i<=n;i++) {scanf("%d",&b[i]);bnum+=(b[i]==1);}
	//for(int i=1;i<=n;i++) printf("b[%d]=%d\n",i,b[i]);
	//printf("flag=%d\n",flag);
	int Ans=0;
	if(bnum%2==0) Ans++;
	set<int > points;
	for(int i=1;i<=n;i++) points.insert(i);
	int tim=0;
	for(set<int >::iterator ite=points.begin();ite!=points.end();){
		int tmp=*ite,st=rfl[*ite];
		++tim;
		while(st!=tmp){
			points.erase(st);
			st=rfl[st];
		}
		++ite;
		points.erase(tmp);
	}
	Ans+=tim==1?0:tim;
	printf("%d\n",Ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=2*100000+5;
int rfl[maxn],b[maxn];
bool vis[maxn];
int n;
int main()
{
	//freopen("7.in","r",stdin);
	//freopen("1.out","w",stdout);
	scanf("%d",&n);
	//printf("n=%d\n",n);
	for(int i=1;i<=n;i++) scanf("%d",&rfl[i]);
	bool flag=false;
	for(int i=1;i<=n;i++) {scanf("%d",&b[i]);flag|=(b[i]==1);}
	//for(int i=1;i<=n;i++) printf("b[%d]=%d\n",i,b[i]);
	//printf("flag=%d\n",flag);
	int Ans=0;
	if(!flag) Ans++;
	set<int > points;
	for(int i=1;i<=n;i++) points.insert(i);
	int tim=0;
	for(set<int >::iterator ite=points.begin();ite!=points.end();){
		int tmp=*ite,st=rfl[*ite];
		++tim;
		while(st!=tmp){
			points.erase(st);
			st=rfl[st];
		}
		++ite;
		points.erase(tmp);
	}
	Ans+=tim==1?0:tim;
	printf("%d\n",Ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=2*100000+5;
int rfl[maxn],b[maxn];
bool vis[maxn];
int n;
int main()
{
//	freopen("2.in","r",stdin);
	//freopen("1.out","w",stdout);
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",&rfl[i]);
	bool flag=false;
	for(int i=1;i<=n;i++) {scanf("%d",&b[i]);flag|=(b[i]==1);}
	int Ans=0;
	if(!flag) Ans++;
	for(int i=1;i<=n;i++){
		if(!vis[i] && rfl[rfl[i]]==i) {Ans+=1;vis[i]=true;vis[rfl[i]]=true;}
	}
	printf("%d\n",Ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=2*100000+5;
int rfl[maxn],b[maxn];
bool vis[maxn];
int n;
int main()
{
//	freopen("2.in","r",stdin);
	//freopen("1.out","w",stdout);
	scanf("%d",&n);
	//printf("n=%d\n",n);
	for(int i=1;i<=n;i++) scanf("%d",&rfl[i]);
	bool flag=false;
	for(int i=1;i<=n;i++) {scanf("%d",&b[i]);flag|=(b[i]==1);}
	//for(int i=1;i<=n;i++) printf("b[%d]=%d\n",i,b[i]);
	//printf("flag=%d\n",flag);
	int Ans=0;
	if(!flag) Ans++;
	set<int > points;
	for(int i=1;i<=n;i++) points.insert(i);
	int tim=0;
	for(set<int >::iterator ite=points.begin();ite!=points.end();){
		int tmp=*ite,st=rfl[*ite];
		Ans++;++tim;
		while(st!=tmp){
			points.erase(st);
			st=rfl[st];
		}
		++ite;
		points.erase(tmp);
	}
	if(tim==1) Ans--;
	printf("%d\n",Ans);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef  long long ll;
typedef unsigned long long ull;
int inf_int=1e8;
ll inf_ll=1e15;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define pb push_back
const double pi=3.1415926535898;
#define dout if(debug) cout
#define fi first
#define se second
#define sp setprecision
#define sz size()
#define mp make_pair
bool debug=0;
const int MAXN=2e5+1;
int mod=1e9+7;

int p[MAXN];
int b[MAXN];
int parent[MAXN];
int mask[MAXN];
int find_parent(int v)
{
    if(v==parent[v])
        return v;

    return parent[v]=find_parent(parent[v]);
}
void union_set(int a,int b)
{
    a=find_parent(a);
    b=find_parent(b);
    if(a!=b)
    {
        if(rand()%2)
            swap(a,b);
        parent[b]=a;
        mask[a]|=mask[b];

    }
}
void solve()
{
    int n;
    cin >> n;
    int zero=0,one=0;
    for(int i=1;i<=n;++i)
    {
        cin >> p[i];
    }
    for(int i=1;i<=n;++i)
    {
        cin >> b[i];
    }

    int mask=0;
    int cnt2=0;
    for(int i=1;i<=n;++i)
    {
        parent[i]=i;
        if(b[i])
            cnt2++;
    }

    for(int i=1;i<=n;++i)
    {
        union_set(i,p[i]);
    }

    int cnt=0;
    int msk=0;
    int ans=0;
    for(int i=1;i<=n;++i)
    {
        if(parent[i]==i)
        {
            cnt++;
        }
    }
    if(cnt==1)
    {
        cnt--;
    }
    if(cnt2%2==0)
    {
        cnt++;
    }
    cout << cnt;

}

#define FILE "stupid_rmq"
int main()
{
        #ifdef zxc
            freopen("input.txt","r",stdin);
       //    freopen("output.txt","w",stdout);
        #endif // zxc

     //   freopen("input.txt","r",stdin);
        //   freopen("output.txt","w",stdout);

       if(!debug)
       {
         //   ios_base::sync_with_stdio(0);
        //    cin.tie(0);
        //    cout.tie(0);
       }

        int t=1;
       // cin >> t;
        while(t--)
           solve();

        return 0;

}


#include <bits/stdc++.h>

using namespace std;
typedef  long long ll;
typedef unsigned long long ull;
int inf_int=1e8;
ll inf_ll=1e15;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define pb push_back
const double pi=3.1415926535898;
#define dout if(debug) cout
#define fi first
#define se second
#define sp setprecision
#define sz size()
#define mp make_pair
bool debug=0;
const int MAXN=1e4+1;
int mod=1e9+7;

int p[MAXN];
int b[MAXN];
int parent[MAXN];
int mask[MAXN];
int find_parent(int v)
{
    if(v==parent[v])
        return v;

    return parent[v]=find_parent(parent[v]);
}
void union_set(int a,int b)
{
    a=find_parent(a);
    b=find_parent(b);
    if(a!=b)
    {
        if(rand()%2)
            swap(a,b);
        parent[b]=a;
        mask[a]|=mask[b];

    }
}
void solve()
{
    int n;
    cin >> n;
    int zero=0,one=0;
    for(int i=1;i<=n;++i)
    {
        cin >> p[i];
    }
    for(int i=1;i<=n;++i)
    {
        cin >> b[i];
    }

    for(int i=1;i<=n;++i)
    {
        parent[i]=i;
        mask[i]=1<<b[i];
    }

    for(int i=1;i<=n;++i)
    {
        union_set(i,p[i]);
    }

    int cnt=0;
    int msk=0;
    int ans=0;
    for(int i=1;i<=n;++i)
    {
        if(p[i]==i)
        {
            cnt++;
            msk|=mask[i];
        }
        else if(find_parent(i)==i)
        {
            if(mask[i]==1 || mask[i]==2)
            {
                ans++;
            }
        }
    }
    ans+=cnt;
    if(cnt==1)
    {
        ans++;
    }
    else
    {
        if(msk==1 || msk==2)
            ans++;
    }
    cout << ans;
}

#define FILE "stupid_rmq"
int main()
{
        #ifdef zxc
            freopen("input.txt","r",stdin);
       //    freopen("output.txt","w",stdout);
        #endif // zxc

     //   freopen("input.txt","r",stdin);
        //   freopen("output.txt","w",stdout);

       if(!debug)
       {
         //   ios_base::sync_with_stdio(0);
        //    cin.tie(0);
        //    cout.tie(0);
       }

        int t=1;
       // cin >> t;
        while(t--)
           solve();

        return 0;

}


#include<bits/stdc++.h>
using namespace std;
vector<int>v[100005];
bool vis[100005];
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr);
    int n;
    cin>>n;
    int p[n];
    for(int i=0;i<n;i++){
        cin>>p[i];
        p[i]--;
    }
    int b[n],cnt=0;
    for(int i=0;i<n;i++){
        cin>>b[i];
        cnt+=b[i];
    }
    int ii=0,nn=0;
    for(int i=0;i<n;i++){
        if(!vis[i]){
            ii=i;
            while(!vis[ii]){
                vis[ii]=1;
                v[nn].push_back(ii);
                ii=p[ii];
            }
            nn++;
        }
    }
    if(nn==1)nn=0;
    if(!(cnt&1))cnt=1;
    else cnt=0;
    cout<<nn+cnt<<"\n";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
bool vis[200005];
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr);
    int n;
    cin>>n;
    int p[n+3];
    for(int i=0;i<n;i++){
        cin>>p[i];
        p[i]--;
    }
    int b[n+3],cnt=0;
    for(int i=0;i<n;i++){
        cin>>b[i];
        cnt+=b[i];
    }
    int ii=0,nn=0;
    for(int i=0;i<n;i++){
        if(!vis[i]){
            ii=i;
            while(!vis[ii]){
                vis[ii]=1;
                ii=p[ii];
            }
            nn++;
        }
    }
    if(nn==1)nn=0;
    if(!(cnt&1))cnt=1;
    else cnt=0;
    cout<<nn+cnt<<"\n";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
vector<int>v[100005];
bool vis[100005];
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr);
    int n;
    cin>>n;
    int p[n];
    for(int i=0;i<n;i++){
        cin>>p[i];
        p[i]--;
    }
    int b[n],cnt=0;
    for(int i=0;i<n;i++){
        cin>>b[i];
        cnt+=b[i];
    }
    int ii=0,nn=0;
    for(int i=0;i<n;i++){
        if(!vis[i]){
            ii=i;
            while(!vis[ii]){
                vis[ii]=1;
                v[nn].push_back(ii);
                ii=p[ii];
            }
            nn++;
        }
    }
    nn--;
    if(!(cnt&1))cnt=1;
    else cnt=0;
    cout<<nn*2+cnt<<"\n";
    return 0;
}


#include <bits/stdc++.h>

#define FOR(i,b,e) for(int i=(b); i <= (e); ++i)
#define FORD(i,b,e) for(int i=(b); i >= (e); --i)
#define SIZE(c) (int) (c).size()
#define FORE(i,c) FOR(i,0,SIZE(c)-1)
#define FORDE(i,c) FORD(i,SIZE(c)-1,0)

#define pb push_back
#define mp make_pair
#define st first
#define nd second

using namespace std;

typedef long long ll;
typedef pair <int,int> pii;
typedef pair <ll,ll> pll;

typedef vector <int> VI;
typedef vector <bool> VB;
typedef vector <pii> VP;
typedef vector <ll> VL;
typedef vector <pll> VPL;

typedef vector <VI> VVI;
typedef vector <VL> VVL;
typedef vector <VB> VVB;
typedef vector <VP> VVP;

const int MOD = 1000000007;
const int INF = 1000000001;
const ll LINF = 1000000000000000001LL;

struct FAU {
    VI p, r;

    FAU(int n): p(n,-1), r(n,0) {}

    int Find(int x) {
        if (p[x] == -1) return x;
        return p[x] = Find(p[x]);
    }

    bool Together(int x, int y) {
        return (Find(x) == Find(y));
    }

    void Union(int x, int y) {
        x = Find(x); y = Find(y);
        if (x == y) return ;

        if (r[x] > r[y]) p[y] = x;
        else p[x] = y;

        if (r[x] == r[y]) ++r[y];
    }
};

/*************************************************************************/

int main() {
    ios_base::sync_with_stdio(0);

    int n;
    cin >> n;

    VI p(n);
    FOR(i,0,n-1) {
        cin >> p[i]; p[i]--;
    }

    VI b(n);
    FOR(i,0,n-1) {
        cin >> b[i];
    }

    FAU fau(n);
    FOR(i,0,n-1) {
        fau.Union(i, p[i]);
    }

    int comp = 0;
    FOR(i,0,n-1) {
        comp += (fau.Find(i) == i);
    }

    int ans;

    if (comp > 1) ans = comp;
    else ans = 0;

    int bSum = 0;
    FOR(i,0,n-1) bSum += b[i];

    if (bSum % 2 == 0) {
        ans++;
    }

    cout << ans;

    return 0;
}

/*************************************************************************/


#include <bits/stdc++.h>

#define FOR(i,b,e) for(int i=(b); i <= (e); ++i)
#define FORD(i,b,e) for(int i=(b); i >= (e); --i)
#define SIZE(c) (int) (c).size()
#define FORE(i,c) FOR(i,0,SIZE(c)-1)
#define FORDE(i,c) FORD(i,SIZE(c)-1,0)

#define pb push_back
#define mp make_pair
#define st first
#define nd second

using namespace std;

typedef long long ll;
typedef pair <int,int> pii;
typedef pair <ll,ll> pll;

typedef vector <int> VI;
typedef vector <bool> VB;
typedef vector <pii> VP;
typedef vector <ll> VL;
typedef vector <pll> VPL;

typedef vector <VI> VVI;
typedef vector <VL> VVL;
typedef vector <VB> VVB;
typedef vector <VP> VVP;

const int MOD = 1000000007;
const int INF = 1000000001;
const ll LINF = 1000000000000000001LL;

struct FAU {
    VI p, r;

    FAU(int n): p(n,-1), r(n,0) {}

    int Find(int x) {
        if (p[x] == -1) return x;
        return p[x] = Find(p[x]);
    }

    bool Together(int x, int y) {
        return (Find(x) == Find(y));
    }

    void Union(int x, int y) {
        x = Find(x); y = Find(y);
        if (x == y) return ;

        if (r[x] > r[y]) p[y] = x;
        else p[x] = y;

        if (r[x] == r[y]) ++r[y];
    }
};

/*************************************************************************/

int main() {
    ios_base::sync_with_stdio(0);

    int n;
    cin >> n;

    VI p(n);
    FOR(i,0,n-1) {
        cin >> p[i]; p[i]--;
    }

    VI b(n);
    FOR(i,0,n-1) {
        cin >> b[i];
    }

    FAU fau(n);
    FOR(i,0,n-1) {
        fau.Union(i, p[i]);
    }

    int comp = 0;
    FOR(i,0,n-1) {
        comp += (fau.Find(i) == i);
    }

    int ans = 2 * (comp - 1);

    int bSum = 0;
    FOR(i,0,n-1) bSum += b[i];

    if (bSum % 2 == 0) {
        ans++;
    }

    cout << ans;

    return 0;
}

/*************************************************************************/


#include<bits/stdc++.h>

#define F first
#define S second
#define ll long long
#define ull unsigned long long
#define pb push_back

using namespace std;
const int N = 300000;
vector < int > k;
int used[N], b[N], p[N], n;
void dfs(int v)
{
    used[v] = 1;
    if (!used[p[v]]) dfs(p[v]);
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> p[i];
    int t = 0;
    for (int j = 1; j <= n; j++){
        cin >> b[j];
        if (b[j] != 0) t++;
    }
    int ans = 0, c = 0;
    if (t % 2 == 0) ans++;
    for (int i = 1; i <= n; i++){
        if (!used[i]){
            dfs(i);
            c++;
        }
    }
    if (c != 1) ans += c;
    cout << ans;
}


#include<bits/stdc++.h>

#define F first
#define S second
#define ll long long
#define ull unsigned long long
#define pb push_back

using namespace std;
const int N = 300000;
vector < int > k;
int used[N], b[N], p[N], n;
void dfs(int v)
{
    used[v] = 1;
    k.pb(v);
    if (!used[p[v]]) dfs(p[v]);
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> p[i];
    int t = 0;
    for (int j = 1; j <= n; j++){
        cin >> b[j];
        if (b[j] != 0) t = 1;
    }
    int ans = 0, c = 0;
    if (!t) ans++;
    for (int i = 1; i <= n; i++){
        if (!used[i]){
            dfs(i);
            c++;
        }
    }
    if (c != 0) ans += 2 * (c - 1);
    cout << ans;
}


#include<bits/stdc++.h>

#define F first
#define S second
#define ll long long
#define ull unsigned long long
#define pb push_back

using namespace std;
const int N = 300000;
vector < int > k;
int used[N], b[N], p[N], n;
void dfs(int v)
{
    used[v] = 1;
    k.pb(v);
    if (!used[p[v]]) dfs(p[v]);
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> p[i];
    int t = 0;
    for (int j = 1; j <= n; j++){
        cin >> b[j];
        if (b[j] != 0) t = 1;
    }
    int ans = 0, c = 0;
    if (!t) ans++;
    for (int i = 1; i <= n; i++){
        if (!used[i]){
            dfs(i);
            c++;
        }
    }
    if (c != 1) ans += c;
    cout << ans;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define FORD(i,a,b) for(int i=(a);i>=(b);--i)
#define foreach(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define all(c) (c).begin(),(c).end()
#define scanf(...) scanf(__VA_ARGS__)?:0
#define e1 first
#define e2 second
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
int n,b,x,s,p[300001],o[300001];
int main()
{
	scanf("%d",&n);
	FOR(i,1,n) scanf("%d",&p[i]);
	FOR(i,1,n) scanf("%d",&b),x^=b;
	x^=1;
	FOR(i,1,n) if (!o[i])
	{
		b=i; x++; s++;
		while (!o[b]) o[b]=1,b=p[b];
	}
	if (s==1) x--;
	printf("%d",x);
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define FORD(i,a,b) for(int i=(a);i>=(b);--i)
#define foreach(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define all(c) (c).begin(),(c).end()
#define scanf(...) scanf(__VA_ARGS__)?:0
#define e1 first
#define e2 second
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
int n,b,x,p[200001],o[200001];
int main()
{
	scanf("%d",&n);
	FOR(i,1,n) scanf("%d",&p[i]);
	FOR(i,1,n) scanf("%d",&b),x|=b;
	x^=1;
	FOR(i,1,n) if (!o[i])
	{
		x+=2; b=i;
		while (!o[b]) o[b]=1,b=p[b];
	}
	printf("%d",x-2);
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define FORD(i,a,b) for(int i=(a);i>=(b);--i)
#define foreach(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define all(c) (c).begin(),(c).end()
#define scanf(...) scanf(__VA_ARGS__)?:0
#define e1 first
#define e2 second
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
int n,b,x,s,p[200001],o[200001];
int main()
{
	scanf("%d",&n);
	FOR(i,1,n) scanf("%d",&p[i]);
	FOR(i,1,n) scanf("%d",&b),x|=b;
	x^=1;
	FOR(i,1,n) if (!o[i])
	{
		b=i; x++; s++;
		while (!o[b]) o[b]=1,b=p[b];
	}
	if (s==1) x--;
	printf("%d",x);
}


#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
// #include <boost/multiprecision/cpp_int.hpp>

#define gc getchar//_unlocked
#define pc putchar//_unlocked
#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define pp pair<int,int>
#define ppl pair<ll,ll>
#define bigint boost::multiprecision::cpp_int
#define finp ios_base::sync_with_stdio(0);cin.tie(0);
#define bc __builtin_popcountll
#define afor(i,a,b) for(int i=a;i<=b;++i)
#define bfor(i,a,b) for(int i=a;i>=b;--i)
#define vi vector<int>
#define vpp vector<pp>
#define vll vector<ll>

using namespace std;
using namespace __gnu_pbds;

char putnb[20];
void putn(ll n) {if(!n)pc('0');if(n<0)pc('-'),n=0-n;int pi=0;while(n)putnb[pi++]=(n%10)+'0',n/=10;while(pi)pc(putnb[--pi]);}
void sci(int *x) {register char c = gc();*x = 0;for(; (c<48)||(c>57);c = gc());for(; (c>47)&&(c<58);c = gc())*x = (int)((((*x)<<1) + ((*x)<<3)) + c - 48);}
void scll(ll *x)  {register char c = gc();*x = 0;for(; (c<48)||(c>57);c = gc());for(; (c>47)&&(c<58);c = gc())*x =  (ll)((((*x)<<1) + ((*x)<<3)) + c - 48);}
ll fp(ll a,ll b,ll c) {if(b==0)return 1%c; if(b==1)return a%c; ll ret=fp(a,b/2,c); ret=(ret*ret)%c; if(b&1)ret=(ret*a)%c; return ret;}

const ll mod=1e9 +7;
const ll mod2=1999999973;
const ll inf=1e18;
const int infs=1e9 + 1000;
const int N=200000;
const long double PI = acos(-1);

template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

ll n;
ll p[N+5];
ll b[N+5];
ll ans;
bool vis[N+5];

int main()
{
  finp;
  cin>>n;
  afor(i,1,n)cin>>p[i];

  bool f = false;
  afor(i,1,n)
  {
  	cin>>b[i];
  	if(b[i])f=true;
  }
  if(!f)++ans;

  ll ncyc = 0;

  afor(i,1,n)
  {
  	if(vis[i])continue;
  	++ncyc;
  	ll curr = i;
  	while(1)
  	{
  		if(vis[curr])break;
  		vis[curr] = true;
  		curr = p[curr];
  	}
  }

  ans+= 2*(ncyc - 1);
  cout<<ans;

return 0;
}


#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
// #include <boost/multiprecision/cpp_int.hpp>

#define gc getchar//_unlocked
#define pc putchar//_unlocked
#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define pp pair<int,int>
#define ppl pair<ll,ll>
#define bigint boost::multiprecision::cpp_int
#define finp ios_base::sync_with_stdio(0);cin.tie(0);
#define bc __builtin_popcountll
#define afor(i,a,b) for(int i=a;i<=b;++i)
#define bfor(i,a,b) for(int i=a;i>=b;--i)
#define vi vector<int>
#define vpp vector<pp>
#define vll vector<ll>

using namespace std;
using namespace __gnu_pbds;

char putnb[20];
void putn(ll n) {if(!n)pc('0');if(n<0)pc('-'),n=0-n;int pi=0;while(n)putnb[pi++]=(n%10)+'0',n/=10;while(pi)pc(putnb[--pi]);}
void sci(int *x) {register char c = gc();*x = 0;for(; (c<48)||(c>57);c = gc());for(; (c>47)&&(c<58);c = gc())*x = (int)((((*x)<<1) + ((*x)<<3)) + c - 48);}
void scll(ll *x)  {register char c = gc();*x = 0;for(; (c<48)||(c>57);c = gc());for(; (c>47)&&(c<58);c = gc())*x =  (ll)((((*x)<<1) + ((*x)<<3)) + c - 48);}
ll fp(ll a,ll b,ll c) {if(b==0)return 1%c; if(b==1)return a%c; ll ret=fp(a,b/2,c); ret=(ret*ret)%c; if(b&1)ret=(ret*a)%c; return ret;}

const ll mod=1e9 +7;
const ll mod2=1999999973;
const ll inf=1e18;
const int infs=1e9 + 1000;
const int N=200000;
const long double PI = acos(-1);

template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

ll n;
ll p[N+5];
ll b[N+5];
ll ans;
bool vis[N+5];

int main()
{
  finp;
  cin>>n;
  afor(i,1,n)cin>>p[i];

  ll par = 0;
  afor(i,1,n)
  {
  	cin>>b[i];
  	par^= b[i];
  }
  ans = par^1;

  ll ncyc = 0;

  afor(i,1,n)
  {
  	if(vis[i])continue;
  	++ncyc;
  	ll curr = i;
  	while(1)
  	{
  		if(vis[curr])break;
  		vis[curr] = true;
  		curr = p[curr];
  	}
  }

  ans+= 2*(ncyc - 1);
  cout<<ans;

return 0;
}


#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
// #include <boost/multiprecision/cpp_int.hpp>

#define gc getchar//_unlocked
#define pc putchar//_unlocked
#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define pp pair<int,int>
#define ppl pair<ll,ll>
#define bigint boost::multiprecision::cpp_int
#define finp ios_base::sync_with_stdio(0);cin.tie(0);
#define bc __builtin_popcountll
#define afor(i,a,b) for(int i=a;i<=b;++i)
#define bfor(i,a,b) for(int i=a;i>=b;--i)
#define vi vector<int>
#define vpp vector<pp>
#define vll vector<ll>

using namespace std;
using namespace __gnu_pbds;

char putnb[20];
void putn(ll n) {if(!n)pc('0');if(n<0)pc('-'),n=0-n;int pi=0;while(n)putnb[pi++]=(n%10)+'0',n/=10;while(pi)pc(putnb[--pi]);}
void sci(int *x) {register char c = gc();*x = 0;for(; (c<48)||(c>57);c = gc());for(; (c>47)&&(c<58);c = gc())*x = (int)((((*x)<<1) + ((*x)<<3)) + c - 48);}
void scll(ll *x)  {register char c = gc();*x = 0;for(; (c<48)||(c>57);c = gc());for(; (c>47)&&(c<58);c = gc())*x =  (ll)((((*x)<<1) + ((*x)<<3)) + c - 48);}
ll fp(ll a,ll b,ll c) {if(b==0)return 1%c; if(b==1)return a%c; ll ret=fp(a,b/2,c); ret=(ret*ret)%c; if(b&1)ret=(ret*a)%c; return ret;}

const ll mod=1e9 +7;
const ll mod2=1999999973;
const ll inf=1e18;
const int infs=1e9 + 1000;
const int N=200000;
const long double PI = acos(-1);

template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

ll n;
ll p[N+5];
ll b[N+5];
ll ans;
bool vis[N+5];

int main()
{
  finp;
  cin>>n;
  afor(i,1,n)cin>>p[i];

  ll par = 0;
  afor(i,1,n)
  {
  	cin>>b[i];
  	par^= b[i];
  }
  ans = par^1;

  ll ncyc = 0;

  afor(i,1,n)
  {
  	if(vis[i])continue;
  	++ncyc;
  	ll curr = i;
  	while(1)
  	{
  		if(vis[curr])break;
  		vis[curr] = true;
  		curr = p[curr];
  	}
  }

  if(ncyc > 1)ans+=ncyc;
  cout<<ans;

return 0;
}


#include <stdio.h>

int p[200005];
int vs[200005];

void dfs(int x){
    if(vs[x]) return;
    vs[x] = 1;
    dfs(p[x]);
    return;
}

int main(){
    int n;
    scanf("%d", &n);
    for(int i=1; i<=n; i++) scanf("%d", &p[i]);
    int res=1;
    for(int i=1; i<=n; i++){
        int b;
        scanf("%d", &b);
        res ^= b;
    }
    int cnt=0;
    for(int i=1; i<=n; i++) if(!vs[i]) dfs(i), cnt++;
    printf("%d", res+cnt-(cnt==1));
    return 0;
}


#include <stdio.h>

int p[200005];
int vs[200005];
int mrk[200005];

int main(){
    int n;
    scanf("%d", &n);
    for(int i=1; i<=n; i++) scanf("%d", &p[i]), mrk[p[i]] = i;
    int res=1;
    for(int i=1; i<=n; i++){
        int b;
        scanf("%d", &b);
        res ^= b;
    }
    int ptr=1, cur=1;
    for(int i=1; i<n; i++){
        vs[cur] = 1;
        while(vs[ptr]) ptr++;
        if(vs[p[cur]]){
            p[cur] = ptr;
            p[mrk[ptr]] = p[cur];
            res += 2;
        }
        cur = p[cur];
    }
    printf("%d", res);
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define M_PI 3.14159265358979323846
typedef long long ll;
using namespace std;
const int N = 2e5;
int n;
int p[N];
int b[N];
bool was[N];
void dfs(int i) {
	was[i] = true;
	if (was[p[i]]) return;
	dfs(p[i]);
}
int main() {
	srand(time(NULL));
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> p[i];
		p[i]--;
	}
	int col = 0;
	for (int i = 0; i < n; i++) {
		cin >> b[i];
		col += b[i];
	}
	fill(was, was + n, false);
	int ans = 0;
	for (int i = 0; i < n; i++) if (!was[i]) {
		ans++;
		dfs(i);
	}
	cout << (ans - 1) * 2 + (1 - col % 2) << endl;
	return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define M_PI 3.14159265358979323846
typedef long long ll;
using namespace std;
const int N = 2e5;
int n;
int p[N];
int b[N];
bool was[N];
void dfs(int i) {
	was[i] = true;
	if (was[p[i]]) return;
	dfs(p[i]);
}
int main() {
	srand(time(NULL));
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> p[i];
		p[i]--;
	}
	int col = 0;
	for (int i = 0; i < n; i++) {
		cin >> b[i];
		col += b[i];
	}
	fill(was, was + n, false);
	int ans = 0;
	for (int i = 0; i < n; i++) if (!was[i]) {
		ans++;
		dfs(i);
	}
	if (ans == 1) ans = 0;
	cout <<  ans + (1 - col % 2) << endl;
	return 0;
}


#include <bits/stdc++.h>

#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define files(name) freopen(name".in","r",stdin); freopen(name".out","w",stdout);
#define all(a) a.begin(),a.end()
#define elif else if
#define mp make_pair
#define pb push_back
#define fir first
#define sec second

using namespace std;
#define int long long

typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long double ld;
typedef long long ll;

const int maxint=numeric_limits<int>::max();
const ll maxll=numeric_limits<ll>::max();
const int arr=2e5+10;
const int ar=2e3+10;
const ld pi=acos(-1);
const ld eps=1e-10;
const ll md=1e9+7;

///---program start---///

vi reb[arr];
bool use[arr];

void dfs(int now)
{
    use[now]=true;
    for (auto wh:reb[now])
        if (!use[wh])
            dfs(wh);
}

main()
{
    int n;
    cin>>n;
    for (int i=1;i<=n;i++)
    {
        int u;
        cin>>u;
        reb[i].pb(u);
        reb[u].pb(i);
    }
    int sum=0;
    for (int i=1;i<=n;i++)
    {
        int a;
        cin>>a;
        sum+=a;
    }
    int cnt=0;
    for (int i=1;i<=n;i++)
        if (!use[i])
            cnt++,
            dfs(i);
    cnt=(cnt-1)*2;
    if (n%2==0&&sum%2==0) cnt++;
    if (n%2==1&&sum%2==0) cnt++;
    cout<<cnt;
}


#include <bits/stdc++.h>

#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define files(name) freopen(name".in","r",stdin); freopen(name".out","w",stdout);
#define all(a) a.begin(),a.end()
#define elif else if
#define mp make_pair
#define pb push_back
#define fir first
#define sec second

using namespace std;
#define int long long

typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long double ld;
typedef long long ll;

const int maxint=numeric_limits<int>::max();
const ll maxll=numeric_limits<ll>::max();
const int arr=2e5+10;
const int ar=2e3+10;
const ld pi=acos(-1);
const ld eps=1e-10;
const ll md=1e9+7;

///---program start---///

vi reb[arr];
bool use[arr];

void dfs(int now)
{
    use[now]=true;
    for (auto wh:reb[now])
        if (!use[wh])
            dfs(wh);
}

main()
{
    int n;
    cin>>n;
    for (int i=1;i<=n;i++)
    {
        int u;
        cin>>u;
        reb[i].pb(u);
        reb[u].pb(i);
    }
    int sum=0;
    for (int i=1;i<=n;i++)
    {
        int a;
        cin>>a;
        sum+=a;
    }
    int cnt=0;
    for (int i=1;i<=n;i++)
        if (!use[i])
            cnt++,
            dfs(i);
    if (n%2==0&&sum%2==0) cnt++;
    if (n%2==1&&sum%2==1) cnt++;
    cout<<cnt;
}


#include <bits/stdc++.h>

#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define files(name) freopen(name".in","r",stdin); freopen(name".out","w",stdout);
#define all(a) a.begin(),a.end()
#define elif else if
#define mp make_pair
#define pb push_back
#define fir first
#define sec second

using namespace std;
#define int long long

typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long double ld;
typedef long long ll;

const int maxint=numeric_limits<int>::max();
const ll maxll=numeric_limits<ll>::max();
const int arr=2e5+10;
const int ar=2e3+10;
const ld pi=acos(-1);
const ld eps=1e-10;
const ll md=1e9+7;

///---program start---///

vi reb[arr];
bool use[arr];

void dfs(int now)
{
    use[now]=true;
    for (auto wh:reb[now])
        if (!use[wh])
            dfs(wh);
}

main()
{
    int n;
    cin>>n;
    for (int i=1;i<=n;i++)
    {
        int u;
        cin>>u;
        reb[i].pb(u);
        reb[u].pb(i);
    }
    int sum=0;
    for (int i=1;i<=n;i++)
    {
        int a;
        cin>>a;
        sum+=a;
    }
    int cnt=0;
    for (int i=1;i<=n;i++)
        if (!use[i])
            cnt++,
            dfs(i);
    cnt--;
    if (n%2==0&&sum%2==1) cnt++;
    if (n%2==1&&sum%2==0) cnt++;
    cout<<cnt;
}


#include <bits/stdc++.h>

#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define files(name) freopen(name".in","r",stdin); freopen(name".out","w",stdout);
#define all(a) a.begin(),a.end()
#define elif else if
#define mp make_pair
#define pb push_back
#define fir first
#define sec second

using namespace std;
#define int long long

typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long double ld;
typedef long long ll;

const int maxint=numeric_limits<int>::max();
const ll maxll=numeric_limits<ll>::max();
const int arr=2e5+10;
const int ar=2e3+10;
const ld pi=acos(-1);
const ld eps=1e-10;
const ll md=1e9+7;

///---program start---///

vi reb[arr];
bool use[arr];

void dfs(int now)
{
    use[now]=true;
    for (auto wh:reb[now])
        if (!use[wh])
            dfs(wh);
}

main()
{
    int n;
    cin>>n;
    for (int i=1;i<=n;i++)
    {
        int u;
        cin>>u;
        reb[i].pb(u);
        reb[u].pb(i);
    }
    int sum=0;
    for (int i=1;i<=n;i++)
    {
        int a;
        cin>>a;
        sum+=a;
    }
    int cnt=0;
    for (int i=1;i<=n;i++)
        if (!use[i])
            cnt++,
            dfs(i);
    if (cnt==1)
        cnt=0;
    if (n%2==0&&sum%2==0) cnt++;
    if (n%2==1&&sum%2==0) cnt++;
    cout<<cnt;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

typedef long long ll;
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;

template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define all(x) x.begin(), x.end()
#define f(i,a,b) for(int i = (a); i <= (b); i++)
#define fd(i,a,b) for(int i = (a); i >= (b); i--)
#define mp make_pair
#define faster_io() ios_base::sync_with_stdio(false)
#define pb push_back
#define pii pair<int,int>
#define SZ(x) ((int)x.size())
#define vii vector<pair<int,int>>

const int INF = 2000000005;
const ll INFLL = 1000000000000000002ll;
const ll MOD = 1000000007;

inline ll min(ll a, ll b, ll c){return min(min(a,b),c);}
inline ll min(ll a, ll b, ll c, ll d){return min(min(min(a,b),c),d);}
inline ll max(ll a, ll b, ll c){return max(max(a,b),c);}
inline ll max(ll a, ll b, ll c, ll d){return max(max(max(a,b),c),d);}

// -------------------------------------------------------------------------------------------------------------

bool U[400005];
int N, P[400005], B[400005];

int main()
{
    cin >> N;
    f(i,1,N) scanf("%d", &P[i]);
    f(i,1,N) scanf("%d", &B[i]);
    int cycles = 0;
    f(i,1,N) if(!U[i])
    {
        int n = i;
        U[i] = true;
        do
        {
            n = P[n];
            U[n] = true;
        }while(n != i);
        cycles++;
    }
    int x = 0;
    f(i,1,N) x += B[i];
    int ans = cycles == 1 ? 0 : cycles;
    if(x&1) cout << ans;
    else cout << ans+1;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

typedef long long ll;
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;

template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define all(x) x.begin(), x.end()
#define f(i,a,b) for(int i = (a); i <= (b); i++)
#define fd(i,a,b) for(int i = (a); i >= (b); i--)
#define mp make_pair
#define faster_io() ios_base::sync_with_stdio(false)
#define pb push_back
#define pii pair<int,int>
#define SZ(x) ((int)x.size())
#define vii vector<pair<int,int>>

const int INF = 2000000005;
const ll INFLL = 1000000000000000002ll;
const ll MOD = 1000000007;

inline ll min(ll a, ll b, ll c){return min(min(a,b),c);}
inline ll min(ll a, ll b, ll c, ll d){return min(min(min(a,b),c),d);}
inline ll max(ll a, ll b, ll c){return max(max(a,b),c);}
inline ll max(ll a, ll b, ll c, ll d){return max(max(max(a,b),c),d);}

// -------------------------------------------------------------------------------------------------------------

bool U[400005];
int N, P[400005], B[400005];

int main()
{
    cin >> N;
    f(i,1,N) scanf("%d", &P[i]);
    f(i,1,N) scanf("%d", &B[i]);
    f(i,1,N)
    {
        int j = P[i];
        if(B[j]) P[i] = j+N, P[i+N] = j;
        else P[i+N] = j+N;
    }
    int cycles = 0;
    f(i,1,2*N) if(!U[i])
    {
        int n = i;
        U[i] = true;
        do
        {
            n = P[n];
            U[n] = true;
        }while(n != i);
        cycles++;
    }
    cout << cycles-1;
}


//84104971101048411497 - Can you guess what does this mean?
using namespace std;
#include <bits/stdc++.h>
#define mapii map<int, int>
#define debug(a) cout << #a << ": " << a << endl
#define debuga1(a, l, r) fto(i, l, r) cout << a[i] << " "; cout << endl
#define fdto(i, r, l) for(int i = (r); i >= (l); --i)
#define fto(i, l, r) for(int i = (l); i <= (r); ++i)
#define forit(it, var) for(__typeof(var.begin()) it = var.begin(); it != var.end(); it++)
#define forrit(rit, var) for(__typeof(var.rbegin()) rit = var.rbegin(); rit != var.rend(); rit++)
#define ii pair<int, int>
#define iii pair<int, ii>
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define ll long long
#define maxN 200005
#define maxK 20
#define oo 1000000007

template <class T>
T min(T a, T b, T c) {
    return min(a, min(b, c));
}

template <class T>
T max(T a, T b, T c) {
    return max(a, max(b, c));
}

template <class T>
void read(vector<T> &v) {
    T x;
    cin >> x;
    v.pb(x);
}

int n, p[maxN], b[maxN], verCnt, ccCnt, OneCnt;
bool visited[maxN];

void DFS(int u) {
    if (!visited[u]) {
        ++verCnt;
        visited[u] = true;
        DFS(p[u]);
    }
}

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

    scanf("%d", &n);
    fto(i, 1, n) scanf("%d", &p[i]);
    fto(i, 1, n) scanf("%d", &b[i]);

    fto(i, 1, n) {
        if (!visited[i]) {
            verCnt = 0;
            DFS(i);
            ++ccCnt;
            if (verCnt == 1) ++OneCnt;
        }
    }
//    debug(ccCnt);
//    debug(OneCnt);

    int sum = accumulate(b+1, b+n+1, 0);
    if (OneCnt <= 1) cout << (ccCnt-1)*2 + (1-sum%2);
    else if (OneCnt == ccCnt) cout << OneCnt + (1-sum%2);
    else cout << (ccCnt-OneCnt)*2 + OneCnt - 1 + (1-sum%2);

    return 0;
}


//84104971101048411497 - Can you guess what does this mean?
using namespace std;
#include <bits/stdc++.h>
#define mapii map<int, int>
#define debug(a) cout << #a << ": " << a << endl
#define debuga1(a, l, r) fto(i, l, r) cout << a[i] << " "; cout << endl
#define fdto(i, r, l) for(int i = (r); i >= (l); --i)
#define fto(i, l, r) for(int i = (l); i <= (r); ++i)
#define forit(it, var) for(__typeof(var.begin()) it = var.begin(); it != var.end(); it++)
#define forrit(rit, var) for(__typeof(var.rbegin()) rit = var.rbegin(); rit != var.rend(); rit++)
#define ii pair<int, int>
#define iii pair<int, ii>
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define ll long long
#define maxN 200005
#define maxK 20
#define oo 1000000007

template <class T>
T min(T a, T b, T c) {
    return min(a, min(b, c));
}

template <class T>
T max(T a, T b, T c) {
    return max(a, max(b, c));
}

template <class T>
void read(vector<T> &v) {
    T x;
    cin >> x;
    v.pb(x);
}

int n, p[maxN], b[maxN], ccCnt;
bool visited[maxN];

void DFS(int u) {
    if (!visited[u]) {
        visited[u] = true;
        DFS(p[u]);
    }
}

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

    scanf("%d", &n);
    fto(i, 1, n) scanf("%d", &p[i]);
    fto(i, 1, n) scanf("%d", &b[i]);

    fto(i, 1, n) {
        if (!visited[i]) {
            DFS(i);
            ++ccCnt;
        }
    }

    int sum = accumulate(b+1, b+n+1, 0);
    cout << (ccCnt-1)*2 + (1-sum%2);

    return 0;
}


//84104971101048411497 - Can you guess what does this mean?
using namespace std;
#include <bits/stdc++.h>
#define mapii map<int, int>
#define debug(a) cout << #a << ": " << a << endl
#define debuga1(a, l, r) fto(i, l, r) cout << a[i] << " "; cout << endl
#define fdto(i, r, l) for(int i = (r); i >= (l); --i)
#define fto(i, l, r) for(int i = (l); i <= (r); ++i)
#define forit(it, var) for(__typeof(var.begin()) it = var.begin(); it != var.end(); it++)
#define forrit(rit, var) for(__typeof(var.rbegin()) rit = var.rbegin(); rit != var.rend(); rit++)
#define ii pair<int, int>
#define iii pair<int, ii>
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define ll long long
#define maxN 200005
#define maxK 20
#define oo 1000000007

template <class T>
T min(T a, T b, T c) {
    return min(a, min(b, c));
}

template <class T>
T max(T a, T b, T c) {
    return max(a, max(b, c));
}

template <class T>
void read(vector<T> &v) {
    T x;
    cin >> x;
    v.pb(x);
}

int n, p[maxN], b[maxN], verCnt, ccCnt, OneCnt;
bool visited[maxN];

void DFS(int u) {
    if (!visited[u]) {
        ++verCnt;
        visited[u] = true;
        DFS(p[u]);
    }
}

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

    scanf("%d", &n);
    fto(i, 1, n) scanf("%d", &p[i]);
    fto(i, 1, n) scanf("%d", &b[i]);

    fto(i, 1, n) {
        if (!visited[i]) {
            verCnt = 0;
            DFS(i);
            ++ccCnt;
            if (verCnt == 1) ++OneCnt;
        }
    }
//    debug(ccCnt);
//    debug(OneCnt);

    int sum = accumulate(b+1, b+n+1, 0);
    if (OneCnt <= 1) cout << (ccCnt-1)*2 + (1-sum%2);
    else if (OneCnt == ccCnt) cout << OneCnt + (1-sum%2);
    else cout << (ccCnt-OneCnt)*2 + OneCnt + (1-sum%2);

    return 0;
}


//84104971101048411497 - Can you guess what does this mean?
using namespace std;
#include <bits/stdc++.h>
#define mapii map<int, int>
#define debug(a) cout << #a << ": " << a << endl
#define debuga1(a, l, r) fto(i, l, r) cout << a[i] << " "; cout << endl
#define fdto(i, r, l) for(int i = (r); i >= (l); --i)
#define fto(i, l, r) for(int i = (l); i <= (r); ++i)
#define forit(it, var) for(__typeof(var.begin()) it = var.begin(); it != var.end(); it++)
#define forrit(rit, var) for(__typeof(var.rbegin()) rit = var.rbegin(); rit != var.rend(); rit++)
#define ii pair<int, int>
#define iii pair<int, ii>
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define ll long long
#define maxN 200005
#define maxK 20
#define oo 1000000007

template <class T>
T min(T a, T b, T c) {
    return min(a, min(b, c));
}

template <class T>
T max(T a, T b, T c) {
    return max(a, max(b, c));
}

template <class T>
void read(vector<T> &v) {
    T x;
    cin >> x;
    v.pb(x);
}

int n, p[maxN], b[maxN], verCnt, ccCnt, OneCnt;
bool visited[maxN];

void DFS(int u) {
    if (!visited[u]) {
        ++verCnt;
        visited[u] = true;
        DFS(p[u]);
    }
}

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

    scanf("%d", &n);
    fto(i, 1, n) scanf("%d", &p[i]);
    fto(i, 1, n) scanf("%d", &b[i]);

    int ans = n;
    fto(i, 1, n) {
        if (!visited[i]) {
            verCnt = 0;
            DFS(i);
            ans -= max(0, verCnt-1);
            ++ccCnt;
//            if (verCnt == 1) ++OneCnt;
        }
    }
//    debug(ccCnt);
//    debug(OneCnt);
    if (ccCnt == 1) ans = 0;
    int sum = accumulate(b+1, b+n+1, 0);
    ans += 1-sum%2;
    cout << ans;
//    if (OneCnt <= 1) cout << (ccCnt-1)*2 + (1-sum%2);
//    else if (OneCnt == ccCnt) cout << OneCnt + (1-sum%2);
//    else cout << (ccCnt-OneCnt)*2 + OneCnt - 1 + (1-sum%2);

    return 0;
}


//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define mod 1000000007
#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pil pair<int,ll>
#define pii pair<int,int>
#define cd complex<double>
#define ull unsigned long long
#define base 1000000000000000000
#define fio ios::sync_with_stdio(false);cin.tie(0)

using namespace std;

const double g=10.0,eps=1e-12;
const int N=200000+10,maxn=1200000+10,inf=0x3f3f3f3f,INF=0x3f3f3f3f3f3f3f3f;

int p[N];
bool vis[N];
int main()
{
    int n,ans=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",&p[i]);
    for(int i=1;i<=n;i++)
    {
        if(!vis[i])
        {
            ans++;
            int now=i;
            while(!vis[now])
            {
                vis[now]=1;
                now=p[now];
            }
        }
    }
    if(ans==1)ans=0;
    int ok=0;
    for(int x,i=1;i<=n;i++)
    {
        scanf("%d",&x);
        if(x)ok++;
    }
    if(!(ok&1))ans++;
    printf("%d\n",ans);
    return 0;
}
/********************

********************/


//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define mod 1000000007
#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pil pair<int,ll>
#define pii pair<int,int>
#define cd complex<double>
#define ull unsigned long long
#define base 1000000000000000000
#define fio ios::sync_with_stdio(false);cin.tie(0)

using namespace std;

const double g=10.0,eps=1e-12;
const int N=200000+10,maxn=1200000+10,inf=0x3f3f3f3f,INF=0x3f3f3f3f3f3f3f3f;

int p[N];
bool vis[N];
int main()
{
    int n,ans=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",&p[i]);
    for(int i=1;i<=n;i++)
    {
        if(!vis[i])
        {
            ans++;
            int now=i;
            while(!vis[now])
            {
                vis[now]=1;
                now=p[now];
            }
        }
    }
    if(ans==1)ans=0;
    bool ok=0;
    for(int x,i=1;i<=n;i++)
    {
        scanf("%d",&x);
        if(x)ok=1;
    }
    if(!ok)ans++;
    printf("%d\n",ans);
    return 0;
}
/********************

********************/


#include <iostream>
#include <cstdio>
#include <string>
#include <string.h>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <cassert>
#include <bitset>
#include <fstream>
#include <stack>
#include <cstdlib>
#include <unordered_set>
#include <unordered_map>

using namespace std;

#define fst first
#define snd second
#define pb push_back
#define mp make_pair
#define ll long long
#define ull unsigned long long
#define ld long double

template < typename T>
T sqr(T x) {
	return x * x;
}

template < typename T>
T abs(T x) {
	return x > 0 ? x : -x;
}

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

int main() {
	//srand(time(NULL));
	#ifdef LOCAL
		//gen();
		freopen("a.in", "r", stdin);
		//freopen("a.out", "w", stdout);
		cout << endl << endl;
	#else
		//freopen("whats.in", "r", stdin);
		//freopen("whats.out", "w", stdout);
	#endif
	//check();

	ios_base::sync_with_stdio(false);

	int n;
	cin >> n;
	vector < int > a(n + 1);
	vector < bool > used(n + 1);
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	int cnt = 0;
	for (int i = 1; i <= n; i++) {
		if (!used[i]) {
			while (!used[i]) {
				used[i] = true;
				i = a[i];
			}
			cnt++;
		}
	}
	if (cnt == 1) {
		cnt = 0;
	}
	int x = 0;
	for (int i = 0; i < n; i++) {
		int y;
		cin >> y;
		x ^= y;
	}
	cout << cnt + (x^1) << "\n";

	return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <string.h>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <cassert>
#include <bitset>
#include <fstream>
#include <stack>
#include <cstdlib>
#include <unordered_set>
#include <unordered_map>

using namespace std;

#define fst first
#define snd second
#define pb push_back
#define mp make_pair
#define ll long long
#define ull unsigned long long
#define ld long double

template < typename T>
T sqr(T x) {
	return x * x;
}

template < typename T>
T abs(T x) {
	return x > 0 ? x : -x;
}

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

int main() {
	//srand(time(NULL));
	#ifdef LOCAL
		//gen();
		freopen("a.in", "r", stdin);
		//freopen("a.out", "w", stdout);
		cout << endl << endl;
	#else
		//freopen("whats.in", "r", stdin);
		//freopen("whats.out", "w", stdout);
	#endif
	//check();

	ios_base::sync_with_stdio(false);

	int n;
	cin >> n;
	vector < int > a(n + 1);
	vector < bool > used(n + 1);
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	int cnt = -1;
	for (int i = 1; i <= n; i++) {
		if (!used[i]) {
			while (!used[i]) {
				used[i] = true;
				i = a[i];
			}
			cnt++;
		}
	}
	int x = 0;
	for (int i = 0; i < n; i++) {
		int y;
		cin >> y;
		x ^= y;
	}
	cout << 2 * cnt + (x^1) << "\n";

	return 0;
}


#include <bits/stdc++.h>
#define ld long double
#define endl "\n"
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb(x) push_back(x)
#define mp(a,b) make_pair(a,b)
#define ms(v,x) memset(v,x,sizeof(v))
#define all(v) v.begin(),v.end()
#define ff first
#define ss second
#define rep(i, a, b) for(int i = a; i < (b); ++i)
//#define per(i, a, b) for(int i = b-1; i>=a ; i--)
#define trav(a, x) for(auto& a : x)
#define allin(a , x) for(auto a : x)
#define td(v) v.begin(),v.end()
#define sz(v) (int)v.size()
//#define M   1000000007 // 1e9 + 7
#define int long long
using namespace std;
typedef vector<int> vi;
#define y1 abacaba
#define left sefude
typedef long long ll;
typedef pair<int,int> pii;
inline ll mod(ll n, ll m ){ ll ret = n%m; if(ret < 0) ret += m; return ret; }
ll gcd(ll a, ll b){return (b == 0LL ? a : gcd(b, a%b));}
ll exp(ll a,ll b,ll m){
    if(b==0LL) return 1LL;
    if(b==1LL) return mod(a,m);
    ll k = mod(exp(a,b/2,m),m);
    if(b&1LL){
        return mod(a*mod(k*k,m),m);
    }
    else return mod(k*k,m);
}

const int N = 200100;
int pai[N],p[N];
int comp=0;

int f(int x){
  return (x==pai[x]?x:pai[x] = f(pai[x]));
}
void join(int a,int b){
  a = f(a);
  b = f(b);
  if(a==b)return;
  comp--;
  pai[a] = b;
}
int b[N];

int32_t main(){
  fastio;
  int n;
  cin >> n;
  comp = n;
  for(int i=1;i<=n;i++){
    cin >> p[i];
    pai[i] = i;
  }
  for(int i=1;i<=n;i++){
    join(p[i],i);
  }
  int qtd=0;
  for(int i=1;i<=n;i++){
    cin >> b[i];
    if(b[i] == 1)qtd++;
  }

  int res = 2* (comp - 1) + (qtd%2==1 ? 0 : 1);
  cout << res << endl;

  // Math -> gcd it all
  // Did u check N=1? Did you switch N,M?
}


/*  ^^ ====== ^^
ID: meixiuxiu
PROG: test
LANG: C++11
*/
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <climits>
#include <string>
#include <vector>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <sstream>
#include <cctype>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int ,int> pii;
#define MEM(a,b) memset(a,b,sizeof a)
#define CLR(a) memset(a,0,sizeof a);
#define pi acos(-1.0)
#define maxn 40000
#define maxv 100005
const ll inf = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int x,y;
int n,m;
ll dis[1005][1005];
bool vis[1005];
ll ans[1005];
int t[1005];
int c[1005];
struct node
{
    int v;
    ll w;
    node(int v,ll w):v(v),w(w){}
};
std::vector<node> edge[1005];
void dij(int s){
    MEM(dis[s],inf);
    dis[s][s] = 0;
    queue<int> q;q.push(s);
    while(!q.empty()){
        int u = q.front();
        q.pop();vis[u] = false;
        for(int i=0;i<edge[u].size();i++){
            int v = edge[u][i].v;
            ll w = edge[u][i].w;
            if(dis[s][v] > dis[s][u] + w){
                dis[s][v] = dis[s][u]+w;
                if(!vis[v]){
                    vis[v] = true;
                    q.push(v);
                }
            }
        }
    }
}
void solve(int s){
    MEM(ans,inf);
    MEM(vis,0);
    ans[s] = 0;
    queue<int> q;q.push(s);
    while(!q.empty()){
        int u = q.front();
        q.pop();vis[u] = false;
        for(int i=0;i<edge[u].size();i++){
            int v = edge[u][i].v;
            ll w = edge[u][i].w;
            if(w > t[u])continue;
            if(ans[v] > ans[u] + c[u]){
                ans[v] = ans[u]+c[u];
                if(!vis[v]){
                    vis[v] = true;
                    q.push(v);
                }
            }

        }
    }
}
int main()
{
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
//	freopen("out.txt","w",stdout);
#endif
    cin >> n >>m >> x >> y;
    MEM(dis,inf);
    for(int i=1;i<=m;i++){
        int u,v;ll w;cin >> u >>v >> w;
        dis[u][v] = min(dis[u][v],w);
        dis[v][u] = min(dis[v][u],w);
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(dis[i][j]!=inf){
                edge[i].push_back(node(j,dis[i][j]));
            }
        }
    }
    for(int i=1;i<=n;i++){
        dij(i);
    }
    for(int i=1;i<=n;i++){
        edge[i].clear();
        cin >> t[i] >> c[i];
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(i==j || dis[i][j]==inf)continue;
            edge[i].push_back(node(j,dis[i][j]));
        }
    }
    solve(x);
    if(ans[y]<inf){
        cout << ans[y] <<endl;
    }
    else cout <<-1 <<endl;
	return 0;
}


/*  ^^ ====== ^^
ID: meixiuxiu
PROG: test
LANG: C++11
*/
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <climits>
#include <string>
#include <vector>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <sstream>
#include <cctype>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int ,int> pii;
#define MEM(a,b) memset(a,b,sizeof a)
#define CLR(a) memset(a,0,sizeof a);
#define pi acos(-1.0)
#define maxn 40000
#define maxv 100005
const ll inf = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int x,y;
int n,m;
ll dis[1005][1005];
bool vis[1005];
ll ans[1005];
int t[1005];
int c[1005];
struct node
{
    int v;
    ll w;
    node(int v,ll w):v(v),w(w){}
    bool operator < (const node &r)const{
        return w > r.w;
    }
};
std::vector<node> edge[1005];
void dij(int s){
    MEM(dis[s],inf);
    dis[s][s] = 0;
    MEM(vis,0);
    priority_queue<node> q;q.push(node(s,0));
    while(!q.empty()){
        node x = q.top();q.pop();
        ll u = x.v;;
        if(vis[u])continue;vis[u] = 1;
        for(int i=0;i<edge[u].size();i++){
            int v = edge[u][i].v;
            ll w = edge[u][i].w;
            if(dis[s][v] > dis[s][u] + w){
                dis[s][v] = dis[s][u]+w;
                if(!vis[v]){
                    q.push(node(v,dis[s][v]));
                }
            }
        }
    }
}
void solve(int s){
    MEM(ans,inf);
    MEM(vis,0);
    ans[s] = 0;
    priority_queue<node> q;q.push(node(s,0));
    while(!q.empty()){
        node x = q.top();q.pop();
        ll u = x.v;;
        if(vis[u])continue;vis[u] = 1;
        for(int i=0;i<edge[u].size();i++){
            int v = edge[u][i].v;
            ll w = edge[u][i].w;
            if(w > t[u])continue;
            if(ans[v] > ans[u] + c[u]){
                ans[v] = ans[u]+c[u];
                if(!vis[v]){
                    q.push(node(v,ans[v]));
                }
            }

        }
    }
}
int main()
{
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
//	freopen("out.txt","w",stdout);
#endif
    cin >> n >>m >> x >> y;
    MEM(dis,inf);
    for(int i=1;i<=m;i++){
        int u,v, w;scanf("%d%d%d",&u,&v,&w);
        dis[u][v] = min(dis[u][v],1ll*w);
        dis[v][u] = min(dis[v][u],1ll*w);
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(dis[i][j]!=inf){
                edge[i].push_back(node(j,dis[i][j]));
            }
        }
    }
    for(int i=1;i<=n;i++){
        dij(i);
    }
    for(int i=1;i<=n;i++){
        edge[i].clear();
        scanf("%d%d",&t[i],&c[i]);
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(i==j || dis[i][j]>t[i])continue;
            edge[i].push_back(node(j,dis[i][j]));
        }
    }
    solve(x);
    if(ans[y]<inf){
        cout << ans[y] <<endl;
    }
    else cout <<-1 <<endl;
	return 0;
}


/*  ^^ ====== ^^
ID: meixiuxiu
PROG: test
LANG: C++11
*/
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <climits>
#include <string>
#include <vector>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <sstream>
#include <cctype>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int ,int> pii;
#define MEM(a,b) memset(a,b,sizeof a)
#define CLR(a) memset(a,0,sizeof a);
#define pi acos(-1.0)
#define maxn 40000
#define maxv 100005
const ll inf = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int x,y;
int n,m;
ll dis[1005][1005];
bool vis[1005];
ll ans[1005];
int t[1005];
int c[1005];
struct node
{
    int v;
    ll w;
    node(int v,ll w):v(v),w(w){}
    bool operator < (const node &r)const{
        return w > r.w;
    }
};
std::vector<node> edge[1005];
void dij(int s){
    MEM(dis[s],inf);
    dis[s][s] = 0;
    MEM(vis,0);
    priority_queue<node> q;q.push(node(s,0));
    while(!q.empty()){
        node x = q.top();q.pop();
        ll u = x.v;;
        if(vis[u])continue;vis[u] = 1;
        for(int i=0;i<edge[u].size();i++){
            int v = edge[u][i].v;
            ll w = edge[u][i].w;
            if(dis[s][v] > dis[s][u] + w){
                dis[s][v] = dis[s][u]+w;
                if(!vis[v]){
                    q.push(node(v,dis[s][v]));
                }
            }
        }
    }
}
void solve(int s){
    MEM(ans,inf);
    MEM(vis,0);
    ans[s] = 0;
    priority_queue<node> q;q.push(node(s,0));
    while(!q.empty()){
        node x = q.top();q.pop();
        ll u = x.v;;
        if(vis[u])continue;vis[u] = 1;
        for(int i=0;i<edge[u].size();i++){
            int v = edge[u][i].v;
            ll w = edge[u][i].w;
            if(w > t[u])continue;
            if(ans[v] > ans[u] + c[u]){
                ans[v] = ans[u]+c[u];
                if(!vis[v]){
                    q.push(node(v,ans[v]));
                }
            }

        }
    }
}
int main()
{
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
//	freopen("out.txt","w",stdout);
#endif
    cin >> n >>m >> x >> y;
    MEM(dis,inf);
    for(int i=1;i<=m;i++){
        int u,v;ll w;cin >> u >>v >> w;
        dis[u][v] = min(dis[u][v],w);
        dis[v][u] = min(dis[v][u],w);
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(dis[i][j]!=inf){
                edge[i].push_back(node(j,dis[i][j]));
            }
        }
    }
    for(int i=1;i<=n;i++){
        dij(i);
    }
    for(int i=1;i<=n;i++){
        edge[i].clear();
        cin >> t[i] >> c[i];
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(i==j || dis[i][j]==inf)continue;
            edge[i].push_back(node(j,dis[i][j]));
        }
    }
    solve(x);
    if(ans[y]<inf){
        cout << ans[y] <<endl;
    }
    else cout <<-1 <<endl;
	return 0;
}


/*  ^^ ====== ^^
ID: meixiuxiu
PROG: test
LANG: C++11
*/
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <climits>
#include <string>
#include <vector>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <sstream>
#include <cctype>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int ,int> pii;
#define MEM(a,b) memset(a,b,sizeof a)
#define CLR(a) memset(a,0,sizeof a);
#define pi acos(-1.0)
#define maxn 40000
#define maxv 100005
const ll inf = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int x,y;
int n,m;
ll dis[1005][1005];
bool vis[1005];
ll ans[1005];
int t[1005];
int c[1005];
struct node
{
    int v;
    ll w;
    node(int v,ll w):v(v),w(w){}
    bool operator < (const node &r)const{
        return w > r.w;
    }
};
std::vector<node> edge[1005];
void dij(int s){
    MEM(dis[s],inf);
    dis[s][s] = 0;
    MEM(vis,0);
    priority_queue<node> q;q.push(node(s,0));
    while(!q.empty()){
        node x = q.top();q.pop();
        ll u = x.v;;
        if(vis[u])continue;vis[u] = 1;
        for(int i=0;i<edge[u].size();i++){
            int v = edge[u][i].v;
            ll w = edge[u][i].w;
            if(dis[s][v] > dis[s][u] + w){
                dis[s][v] = dis[s][u]+w;
                if(!vis[v]){
                    q.push(node(v,dis[s][v]));
                }
            }
        }
    }
}
void solve(int s){
    MEM(ans,inf);
    MEM(vis,0);
    ans[s] = 0;
    priority_queue<node> q;q.push(node(s,0));
    while(!q.empty()){
        node x = q.top();q.pop();
        ll u = x.v;;
        if(vis[u])continue;vis[u] = 1;
        for(int i=0;i<edge[u].size();i++){
            int v = edge[u][i].v;
            ll w = edge[u][i].w;
            if(w > t[u])continue;
            if(ans[v] > ans[u] + c[u]){
                ans[v] = ans[u]+c[u];
                if(!vis[v]){
                    vis[v] = true;
                    q.push(node(v,ans[v]));
                }
            }

        }
    }
}
int main()
{
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
//	freopen("out.txt","w",stdout);
#endif
    cin >> n >>m >> x >> y;
    MEM(dis,inf);
    for(int i=1;i<=m;i++){
        int u,v;ll w;cin >> u >>v >> w;
        dis[u][v] = min(dis[u][v],w);
        dis[v][u] = min(dis[v][u],w);
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(dis[i][j]!=inf){
                edge[i].push_back(node(j,dis[i][j]));
            }
        }
    }
    for(int i=1;i<=n;i++){
        dij(i);
    }
    for(int i=1;i<=n;i++){
        edge[i].clear();
        cin >> t[i] >> c[i];
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(i==j || dis[i][j]==inf)continue;
            edge[i].push_back(node(j,dis[i][j]));
        }
    }
    solve(x);
    if(ans[y]<inf){
        cout << ans[y] <<endl;
    }
    else cout <<-1 <<endl;
	return 0;
}


/*  ^^ ====== ^^
ID: meixiuxiu
PROG: test
LANG: C++11
*/
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <climits>
#include <string>
#include <vector>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <sstream>
#include <cctype>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int ,int> pii;
#define MEM(a,b) memset(a,b,sizeof a)
#define CLR(a) memset(a,0,sizeof a);
#define pi acos(-1.0)
#define maxn 40000
#define maxv 100005
const ll inf = 0x3f3f3f3f3f3f3f;
const int MOD = 1e9 + 7;
int x,y;
int n,m;
ll dis[1005];
bool vis[1005];
ll ans[1005];
int t[1005];
int c[1005];
struct node
{
    int v;
    ll w;
    node(int v,ll w):v(v),w(w){}
    bool operator < (const node &r)const{
        return w > r.w;
    }
};
std::vector<node> edge[1005];
std::vector<node> v[1005];
void dij(int s,vector<node> *edge){
    for(int i=1;i<=n;i++)dis[i] = inf;
    dis[s] = 0;
    MEM(vis,0);
    priority_queue<node> q;q.push(node(s,0));
    while(!q.empty()){
        node x = q.top();q.pop();
        ll u = x.v;;
        if(vis[u])continue;vis[u] = 1;
        for(int i=0;i<edge[u].size();i++){
            int v = edge[u][i].v;
            ll w = edge[u][i].w;
            if(dis[v] > dis[u] + w && !vis[v]){
                dis[v] = dis[u]+w;
                q.push(node(v,dis[v]));
            }
        }
    }
}
int main()
{
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
//	freopen("out.txt","w",stdout);
#endif
    cin >> n >>m >> x >> y;
    for(int i=1;i<=m;i++){
        int u,v, w;scanf("%d%d%d",&u,&v,&w);
        edge[u].push_back(node(v,w));
        edge[v].push_back(node(u,w));
    }
    for(int i=1;i<=n;i++){
        scanf("%d%d",&t[i],&c[i]);
    }
    for(int i=1;i<=n;i++){
        dij(i,edge);
        for(int j=1;j<=n;j++){
            if(i==j || dis[j]>t[i])continue;
            v[i].push_back(node(j,c[i]));
        }
    }
    dij(x,v);
    if(dis[y]<inf){
        cout << dis[y] <<endl;
    }
    else cout <<-1 <<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define pb push_back
#define mp make_pair
#define sl short int
#define ll long long int
#define ull unsigned long long int
#define FOR(i,begin,end) for(ll i=begin;i<=end;++i)
typedef pair< ll,ll> pii;
const ll max_=1000 + 10;
vector < pii > a[max_];
ll n,m,k;
ll d[1000][1000];
vector < pii > g[max_];
pii taxi[max_];
int dijkstra()
{
	for(int str=1;str<=n;str++)
	{
		fill(d[str],d[str]+n,1e18);
    	d[str][str]=0;
    	ll u=0;
    	set < pii >s;
    	s.insert(mp(d[str][str],str));
    	while(!s.empty())
    	{
        	u=s.begin()->second;
        	s.erase(s.begin());
        	for(ll i=0;i<a[u].size();i++)
        	{
            	pii p=a[u][i];
            	if(d[str][p.first]>d[str][u]+p.second)
            	{
                	s.erase(mp(d[str][p.first],p.first));
                	d[str][p.first]=d[str][u]+p.second;
                	s.insert(mp(d[str][p.first],p.first));
            	}
        	}
    	}
    	s.clear();
    }
}
ll dis[max_];
int dijkstra2(int str)
{
    fill(dis,dis+n+10,1e18);
    dis[str]=0;
    ll u=0;
    set < pii >s1;
    s1.insert(mp(dis[str],str));
    while (!s1.empty())
    {
        u=s1.begin()->second;
        s1.erase(s1.begin());
        for(ll i=0;i<g[u].size();i++)
        {
            pii p=g[u][i];
            if(dis[p.first]>dis[u]+p.second)
            {
                s1.erase(mp(dis[p.first],p.first));
                dis[p.first]=dis[u]+p.second;
                //f[p.first]=u; //balayish to masir
                s1.insert(mp(dis[p.first],p.first));
            }
        }
    }
}

int main()
{
	int x,y;
	cin>>n>>m>>x>>y;
	int s,e,w;
	for(int i=1;i<=m;i++)
	{
		cin>>s>>e>>w;
		a[s].pb(mp(e,w));
        a[e].pb(mp(s,w));
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			d[i][j]=1e18;
		}
	}
	dijkstra();
	int di,mo;
 	for(int i=1;i<=n;i++)
    {
        cin>>di>>mo;
        for(int j=1;j<=n;j++)
        {
            if(d[i][j]<=di)
            {
                g[i].pb(mp(j,mo));
            }
        }
    }
	dijkstra2(x);
	if(dis[y]==1e18)
		dis[y]=-1;
	cout<<dis[y]<<endl;
}


#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define pb push_back
#define mp make_pair
#define sl short int
#define ll long long int
#define ull unsigned long long int
#define FOR(i,begin,end) for(ll i=begin;i<=end;++i)
typedef pair< ll,ll> pii;
const ll max_=1000000 + 10000;
vector < pii > a[max_];
ll n,m,k;
ll d[10000][10000];
vector < pii > g[max_];
pii taxi[max_];
int dijkstra()
{
	for(int str=1;str<=n;str++)
	{
		fill(d[str],d[str]+n,1e18);
    	d[str][str]=0;
    	ll u=0;
    	set < pii >s;
    	s.insert(mp(d[str][str],str));
    	while(!s.empty())
    	{
        	u=s.begin()->second;
        	s.erase(s.begin());
        	for(ll i=0;i<a[u].size();i++)
        	{
            	pii p=a[u][i];
            	if(d[str][p.first]>d[str][u]+p.second)
            	{
                	s.erase(mp(d[str][p.first],p.first));
                	d[str][p.first]=d[str][u]+p.second;
                	s.insert(mp(d[str][p.first],p.first));
            	}
        	}
    	}
    	s.clear();
    }
}

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

ll dis[max_];
int dijkstra2(int str)
{
    fill(dis,dis+n+10,1e18);
    dis[str]=0;
    ll u=0;
    set < pii >s1;
    s1.insert(mp(dis[str],str));
    while (!s1.empty())
    {
        u=s1.begin()->second;
        s1.erase(s1.begin());
        for(ll i=0;i<g[u].size();i++)
        {
            pii p=g[u][i];
            if(dis[p.first]>dis[u]+p.second)
            {
                s1.erase(mp(dis[p.first],p.first));
                dis[p.first]=dis[u]+p.second;
                //f[p.first]=u; //balayish to masir
                s1.insert(mp(dis[p.first],p.first));
            }
        }
    }
}

int main()
{
	int x,y;
	cin>>n>>m>>x>>y;
	for(int i=1;i<=m;i++)
	{
		int s,e,w;
		cin>>s>>e>>w;
		a[s].pb(mp(e,w));
        a[e].pb(mp(s,w));
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			d[i][j]=1e18;
		}
	}
	dijkstra();
	/*
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			if(d[i][j]==0 && i!=j)
			{
				d[i][j]=1e18;
			}
			cout<<d[i][j]<<" ";
		}
		cout<<endl;
	}
*/
	int di,mo;
 	for(int i=1;i<=n;i++)
    {
        cin>>di>>mo;
        for(int j=1;j<=n;j++)
        {
            if(d[i][j]<=di)
            {
                g[i].pb(mp(j,mo));
            }
        }
    }
	dijkstra2(x);
	cout<<dis[y]<<endl;
	/*
	for(int i=1;i<=n;i++)
	{
		cout<<dis[i]<<" ";
	}
	*/
}


#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define pb push_back
#define mp make_pair
#define sl short int
#define ll long long int
#define ull unsigned long long int
#define FOR(i,begin,end) for(ll i=begin;i<=end;++i)
typedef pair< ll,ll> pii;
const ll max_=1000 + 10;
vector < pii > a[max_];
ll n,m,k;
ll d[1000][1000];
vector < pii > g[max_];
pii taxi[max_];
int dijkstra()
{
	for(int str=1;str<=n;str++)
	{
		fill(d[str],d[str]+n,1e18);
    	d[str][str]=0;
    	ll u=0;
    	set < pii >s;
    	s.insert(mp(d[str][str],str));
    	while(!s.empty())
    	{
        	u=s.begin()->second;
        	s.erase(s.begin());
        	for(ll i=0;i<a[u].size();i++)
        	{
            	pii p=a[u][i];
            	if(d[str][p.first]>d[str][u]+p.second)
            	{
                	s.erase(mp(d[str][p.first],p.first));
                	d[str][p.first]=d[str][u]+p.second;
                	s.insert(mp(d[str][p.first],p.first));
            	}
        	}
    	}
    	s.clear();
    }
}
ll dis[max_];
int dijkstra2(int str)
{
    fill(dis,dis+n+10,1e18);
    dis[str]=0;
    ll u=0;
    set < pii >s1;
    s1.insert(mp(dis[str],str));
    while (!s1.empty())
    {
        u=s1.begin()->second;
        s1.erase(s1.begin());
        for(ll i=0;i<g[u].size();i++)
        {
            pii p=g[u][i];
            if(dis[p.first]>dis[u]+p.second)
            {
                s1.erase(mp(dis[p.first],p.first));
                dis[p.first]=dis[u]+p.second;
                //f[p.first]=u; //balayish to masir
                s1.insert(mp(dis[p.first],p.first));
            }
        }
    }
}

int main()
{
	int x,y;
	cin>>n>>m>>x>>y;
	int s,e,w;
	for(int i=1;i<=m;i++)
	{
		cin>>s>>e>>w;
		a[s].pb(mp(e,w));
        a[e].pb(mp(s,w));
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			d[i][j]=1e18;
		}
	}
	dijkstra();
	int di,mo;
 	for(int i=1;i<=n;i++)
    {
        cin>>di>>mo;
        for(int j=1;j<=n;j++)
        {
            if(d[i][j]<=di)
            {
                g[i].pb(mp(j,mo));
            }
        }
    }
	dijkstra2(x);
	cout<<dis[y]<<endl;
}