Medicine faculty of Berland State University has just finished their admission campaign. As usual, about 80\% of applicants are girls and majority of them are going to live in the university dormitory for the next 4 (hopefully) years.

The dormitory consists of n rooms and a single mouse! Girls decided to set mouse traps in some rooms to get rid of the horrible monster. Setting a trap in room number i costs c_i burles. Rooms are numbered from 1 to n.

Mouse doesn't sit in place all the time, it constantly runs. If it is in room i in second t then it will run to room a_i in second t + 1 without visiting any other rooms inbetween (i = a_i means that mouse won't leave room i). It's second 0 in the start. If the mouse is in some room with a mouse trap in it, then the mouse get caught into this trap.

That would have been so easy if the girls actually knew where the mouse at. Unfortunately, that's not the case, mouse can be in any room from 1 to n at second 0.

What it the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from?

Input

The first line contains as single integers n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of rooms in the dormitory.

The second line contains n integers c_1, c_2, ..., c_n (1 ≤ c_i ≤ 10^4) — c_i is the cost of setting the trap in room number i.

The third line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n) — a_i is the room the mouse will run to the next second after being in room i.

Output

Print a single integer — the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from.

Examples

Input

5
1 2 3 2 10
1 3 4 3 3


Output

3


Input

4
1 10 2 10
2 4 2 2


Output

10


Input

7
1 1 1 1 1 1 1
2 2 2 3 6 7 6


Output

2

Note

In the first example it is enough to set mouse trap in rooms 1 and 4. If mouse starts in room 1 then it gets caught immideately. If mouse starts in any other room then it eventually comes to room 4.

In the second example it is enough to set mouse trap in room 2. If mouse starts in room 2 then it gets caught immideately. If mouse starts in any other room then it runs to room 2 in second 1.

Here are the paths of the mouse from different starts from the third example:

  * 1 → 2 → 2 → ...; 
  * 2 → 2 → ...; 
  * 3 → 2 → 2 → ...; 
  * 4 → 3 → 2 → 2 → ...; 
  * 5 → 6 → 7 → 6 → ...; 
  * 6 → 7 → 6 → ...; 
  * 7 → 6 → 7 → ...; 



So it's enough to set traps in rooms 2 and 6.
3
1 2 4
2 2 1
 5
5 4 2 6 10
2 4 3 3 2
 4
2 1 2 2
2 3 3 2
 8
1 2 2 9 7 5 6 5
8 4 1 5 5 1 1 8
 3
76 74 84
3 2 1
 3
1 2 4
2 3 3
 7
2 1 64 8 32 4 16
5 7 2 4 2 4 6
 3
1 2 4
3 2 3
 5
1 1 10 10 10
2 3 4 5 3
 4
1 12 2 3
2 3 4 2
 4
99 71 47 82
1 3 4 2
 5
1 2 3 4 5
3 4 5 3 2
 4
90 5 66 84
3 1 2 4
 3
9 5 6
3 3 1
 8
8 8 1 10 6 9 3 1
5 2 7 1 6 8 4 3
 3
80 6 67
1 2 3
 10
10 10 10 10 1 10 10 10 10 10
2 3 4 5 3 2 6 9 5 8
 10
6870 3547 3337 9426 3866 1873 4908 4628 7800 6716
2 3 4 5 1 4 6 2 8 9
 4
97 27 70 99
1 2 3 4
 4
2 58 52 89
3 1 4 2
 3
1 2 4
1 3 1
 3
1 2 4
3 3 3
 4
81 69 80 19
4 3 1 2
 4
4 95 20 62
2 1 4 3
 2
62 49
1 2
 4
1 2 4 8
2 3 3 2
 8
1 8 32 4 16 64 128 2
6 4 1 5 4 4 1 3
 2
21 28
2 1
 10
5620 2478 4051 7060 6327 2170 6912 2031 1781 7454
1 1 1 1 1 1 1 1 1 1
 6
10 7 1 1 8 2
5 5 2 3 3 5
 2
1 2
2 2
 10
6 9 1 1 1 10 2 4 9 6
5 3 4 2 6 8 9 1 10 7
 3
15 52 14
1 3 2
 1
12
1
 5
1 16 2 4 8
5 3 1 5 4
 4
88 78 39 46
4 3 2 1
 3
1 2 4
3 3 1
 3
1 2 4
3 2 2
 4
37 86 1 88
4 2 1 3
 10
6724 5003 9029 376 7529 5554 1407 388 2632 981
2 3 4 5 1 4 6 7 8 9
 10
1337 4359 5138 5246 7859 8111 1154 8455 3711 1737
1 1 2 3 4 1 6 7 8 9
 3
40 26 88
3 1 2
 10
7846 8354 1188 6898 8046 7561 8043 8588 8684 3699
1 1 2 3 4 5 6 7 8 9
 7
4 32 8 1 16 64 2
6 1 5 2 1 4 5
 3
30 98 37
2 3 1
 3
17 34 88
2 1 3
 10
9 19 19 1 3 9 1 12 10 8
7 1 6 3 4 8 5 10 2 9

2 2 2 12 150 4 8 6 10 2 146 2 89 6 9 153 1 3337 293 2 1 4 19 24 111 4 4 21 5620 1 2 4 29 12 4 85 1 2 87 376 1337 26 7846 1 30 105 1
        // "It does not matter how slowly you go as long as you do not stop." - Confucius
#include <bits/stdc++.h>

using namespace std;

/*
author : Roshan_Mehta
motto : Time Management,Consistency,Patience!!
*/

#define int long long

#ifdef Local_Debug

#define debug(x) cout<<#x <<" "<<x<<"  "
#define debug1(a) cout <<#a<<" " <<a << endl
#define debug2(a, b) cout <<#a<<" "<< a << " "<<#b<<" " << b << endl
#define debug3(a, b, c) cout <<#a<<" "<<a << " "<<#b<<" " << b << " "<<#c<<" " << c << endl
#define debug4(a, b, c, d) cout << #a<<" "<<a << " " <<#b<<" "<< b << " "<<#c<<" " << c << " "<<#d<<" "<< d << endl
void show(vector<int> &a)
{for (int i = 0; i < a.size(); i++)
{cout << a[i] << " ";}cout << endl;}
void show(vector<vector<int>> &a)
{
    for(int i=0;i<a.size();i++)
    {
        for(int j=0;j<a[i].size();j++)
        {
            cout<<a[i][j]<<" ";
        }
        cout<<endl;
    }
}
void printTime(clock_t begin)
{
    printf("%.3lf seconds\n", (double) (clock() - begin) / CLOCKS_PER_SEC);

}
#else
#define debug(x)
#define debug1(x)
#define debug2(x,y)
#define debug3(x,y,z)
#define debug4(x,y,z,a)
void show(vector<int> &a){return;}
void show(vector<vector<int>> &a){return;}
void printTime()
{return;}
#endif


#define M1 1000000007
#define M2 998244353
#define G(a,b) get<a>(b)
#define ll long long
#define pb push_back
#define mt make_tuple
#define fast() ios_base::sync_with_stdio(false);cin.tie(NULL);
#define mp make_pair
#define F first
#define S second
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rsort(x) sort(x,greater<int> ())
#define rall(x) rbegin(x), rend(x) //useful in sorting
#define endl "\n"
#define p0(a) cout << a << " "
#define p1(a) cout << a << endl
#define p2(a, b) cout << a << " " << b << endl
#define p3(a, b, c) cout << a << " " << b << " " << c << endl
#define p4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl
#define gcd __gcd
#define deci(n) fixed << setprecision(n)
#define test() int test_case;cin >> test_case;while (test_case--)
#define loop(i,a,n) for(int i=a;i<n;i++)
#define sp " "
#define popcount(x) __builtin_popcount(x)
#define popcountll(x) __builtin_popcountll(x)
#define clz(x) __builtin_clz(x)
#define clzll(x) __builtin_clzll(x)
#define ctz(x) __builtin_ctz(x)
#define ctzll(x) __builtin_ctzll(x)
#define max3(a,b,c) max(max(a,b),c)
#define max4(a,b,c,d) max(a,max3(b,c,d))
#define max5(a,b,c,d,e) max(max4(a,b,c,d),e)
#define getunique(v) {sort(all(v)); v.erase(unique(all(v)), v.end());}

typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
typedef pair<int,pi> tri;
typedef pair<pi,int> rtri;
typedef pair<ll, ll> pl;
typedef pair<double, double> pd;
typedef priority_queue<int, vector<int>, greater<int>> minpq;
typedef priority_queue<int> pq;
typedef unordered_map<int,int> umi;
typedef map<int,int> mi;
#define input(n,k) int n,k;cin>>n;cin>>k;vi arr(n,0);loop(i,0,n){cin>>arr[i];}

const int MOD = 1e9 + 7;
const int md=MOD;

int Power(int n, int x) {int ans = 1;while (x > 0) {if (x & 1) ans = (ans * n) % md;
n = (n * n) % md;x = x >> 1;}return ans;}

vl fact, inv;

void inverse(ll n)
{if(n>=inv.size()){ll size=inv.size();size=size==0? 1:size;
inv.resize(n + 1);inv[0] = 1;
for (ll i = size; i <= n; i++)inv[i] = Power(fact[i], md - 2);}}

void factorial(ll n)
{if(n>=fact.size()){ll size=fact.size();size=size==0? 1:size;
fact.resize(n + 1);fact[0] = 1;for (ll i = size; i <= n; i++)
fact[i] = (fact[i - 1] * i) % md;
}}

ll ncr(ll n, ll r) { return (((fact[n] * inv[r]) % md) * inv[n - r]) % md; }

vl SieveOfEratosthenes(int n)
{
    bool prime[n+1];
    memset(prime, true, sizeof(prime));
    for (int p=2; p*p<=n; p++)
    {if (prime[p] == true){for (int i=p*p; i<=n; i += p)
    prime[i] = false;}}

    vl ans;for (int p=2; p<=n; p++)if (prime[p])ans.pb(p);return ans;
}
vi primeFactors(int n)
{
    vi ans;
    while (n % 2 == 0) {ans.pb(2);n = n / 2;}
    for (int i = 3; i <= sqrt(n); i = i + 2) {
        while (n % i == 0) {ans.pb(i);n = n / i;}}
    if (n > 2)ans.pb(n);return ans;
}

ll NcR(int n, int r)
{long long p = 1, k = 1;if (n - r < r)r = n - r;if (r != 0) {while (r) {p *= n;k *= r;
long long m = gcd(p, k);p /= m;k /= m;n--;r--;}
}else p = 1;return p;// cout << p << endl;
}
bool sortasc(const rtri a,const rtri b) //Ascending ... change the argument accordingwise;
{
    return a.second<b.second;
}
bool sortdesc(const int a,const int b) //Descending... change the argument accordingwise;a
{
    return a>b;
}
int myceil(int num,int den)
{
    return num%den == 0 ? num/den : num/den + 1;
}
int myfloor(int num,int den)
{
    return num%den == 0 ? num/den : num/den - 1;
}
// 2-D vector with both size--> vector<vector<int> > a(n, vector<int>(m));

#define pie 3.141592653589793238462643383279
const int N = 2e5 + 5;

void ipgraph(int n, int m);int dfs(int u, int par);int bfs(int start);void iptree(int n);
//********************THE END OF TEMPLATES*******************//

vi adj[N];
vb visited(N);



vi cost(N);
int32_t main()
{
    clock_t begin = clock();
    fast();
    #ifdef Local_Debug
	// freopen("input.txt", "r", stdin);
	// freopen("output.txt", "w", stdout);
    #endif
    // test()
    {
        int n;
        cin>>n;
        for (int i = 0; i < n; i++)
        {
            cin>>cost[i];
        }
        iptree(n);
        int ans=0;
        for (int i = 0; i < n; i++)
        {
            if(!visited[i])
            {
                int temp=bfs(i);
                if(temp!=-1)
                {
                    ans+=temp;
                }

            }
        }
        cout<<ans<<endl;


    }

    return 0;
}
void iptree(int n){  //n is number of node
    for (int i = 0; i < n; i++){             //0-based index
        int a;cin>>a;
        adj[i].pb(a-1);
        // adj[a-1].pb(i+1);
    }

}

void ipgraph(int n, int m){               //0-based index
	int i, u, v;
	while(m--){
		cin>>u>>v;
    u--, v--;
		adj[u].pb(v);
		adj[v].pb(u);
	}
}

int dfs(int u, int par){
    visited[u]=1;
	for(int v:adj[u]){
		if (v == par) continue;
		dfs(v, u);
	}
}

int bfs(int start){
    queue<int> Q;
    Q.push(start);
    visited[start]=1;
    set<int> roshan;
    int t=1;
    roshan.insert(start);
    vi mansi;
    mansi.pb(start);
    while(!Q.empty()){
        int v=Q.front();
        Q.pop();
        for (auto &&i : adj[v]){
            if(!visited[i])
            {
                visited[i]=1;
                roshan.insert(i);
                mansi.pb(i);
                Q.push(i);
            }
            else if(visited[i] and roshan.count(i))
            {
                auto index=lower_bound(all(mansi),i)-mansi.begin();
                int ans=INT_MAX;
                for (int j = index; j < sz(mansi); j++)
                {
                    ans=min(ans,cost[mansi[j]]);
                }
                return ans;
            }
            else
            {
                return -1;
            }

        }

    }
}

