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.

import java.util.*;
import java.io.*;
 
 
 public class gfg  {
 
    static PrintWriter out = new PrintWriter(System.out);
    
    public static void main(String[] args) throws IOException{
        //Reader.init(System.in);
        
        Reader.init(System.in);
        int t = 1;//Reader.nextInt();
        while(t-->0){
           solve();
        }
        out.close();
    }
    
    static ArrayList<Integer>[] graph; 
    static int n=8;
    static int m=8;
    static int k =0;
    static long mod =1000000007;
    static int MOD = 1000000007;
    static int[] dx = {0,0,1,-1};
    static int[] dy = {1,-1,0,0};
    static int[] cost;
    static int[] col; //0 ,1,2
    static int[] par;
    static long ans=0;
    static ArrayList<Integer>[] list;

    
    static void solve()throws IOException{
        n = Reader.nextInt();
        graph = new ArrayList[n+1];
        cost = new int[n+1];
        col = new int[n+1];
        par = new int[n+1];
        for(int i=1;i<=n;i++){
            graph[i] = new ArrayList<Integer>();
            cost[i] = Reader.nextInt();
        }
        for(int i=1;i<=n;i++)
            graph[i].add(Reader.nextInt());

        for(int i=1;i<=n;i++){
            if(col[i]==0  ){
                dfs(i);
            }
        }
        out.println(ans);
        // for(int i=1;i<=n;i++)
        //     out.print(col[i]+" ");
    }
    
    static void dfs(int x){
        col[x]=1;
        for(int v:graph[x]){
            par[v]=x;
            if(v==x){
                ans+=cost[x];
                col[v]=2;
                return;
            }
            else if(col[v]==2)
                continue;
            else if(col[v]==0)
                dfs(v);
            else if(col[v]==1 ){
                int temp = v;
                int min = cost[v];
                while(par[temp]!=v ){
                    temp = par[temp];
                    min = Math.min(cost[temp],min);
                }
                ans+=min;
            }
        }
        col[x]=2;
    }
    
   
    // ggratest common divisor println
    static int gcd(int a , int b){
        if(b==0)
            return a;
        else
            return gcd(b,a%b);
    }
 
    // least common multiple
    static int lcm(int a, int b){
        return (a*b)/gcd(a,b);
    }
    
    // a^b
    static long fastpow(long a, long b){
        long res = 1;a=a%mod;
        while(b>0){
            if(b%2==1)
                res = (res*a)%mod;
            a = (a*a)%mod;
            b = b>>1;
        }
        return res;
    }

    // segment tree
   /* static void update(int ind, int l, int h, int pos, int v){
        if(l==h){
            seg[ind] = v;
            return;
        }
        int mid = (l+h)/2;
        if(pos<=mid)
            update(2*ind,l,mid,pos,v);
        else
            update(2*ind+1,mid+1,h,pos,v);
        seg[ind] = seg[2*ind]^seg[2*ind+1];
    }
    static int query(int ind, int l , int h, int ll , int hh){
        if(ll>hh)
            return 0;
        if(l==ll && h==hh)
            return seg[ind];
        int mid = (l+h)/2;
        return query(2*ind,l,mid,ll,Math.min(mid,hh)) ^ query(2*ind+1,mid+1,h,Math.max(mid+1,ll),hh);
    }*/
    // priority queue with comparator
    // PriorityQueue<Interval> pq = new PriorityQueue<>(new Comparator<Interval>() {
    //             public int compare(Interval a, Interval b){
    //                 int diff1 = a.r - a.l;
    //                 int diff2 = b.r - b.l;
 
    //                 if(diff1 == diff2){
    //                     return a.l - b.l;
    //                 }
 
    //                 return diff2 - diff1;
    //             }
    // });
}
 
class trienode{
        int v;
        trienode[] child ;
        boolean leaf;
        int cnt;
        int p=0;
        trienode(int vv){
            child = new trienode[2];
            leaf=false;
            cnt=0;
            v=vv;
        }
        trienode(){
            child = new trienode[2];
            leaf=false;
            cnt=0;
            v=0;
        }
    }
class Reader {
    static BufferedReader reader;
    static StringTokenizer tokenizer;
 
    /** call this method to initialize reader for InputStream */
    static void init(InputStream input) {
        reader = new BufferedReader(
                     new InputStreamReader(input) );
        tokenizer = new StringTokenizer("");
    }
 
    /** get next word */
    static String next() throws IOException {
        while ( ! tokenizer.hasMoreTokens() ) {
            //TODO add check for eof if necessary
            tokenizer = new StringTokenizer(
                   reader.readLine() );
        }
        return tokenizer.nextToken();
    }
 
    static int nextInt() throws IOException {
        return Integer.parseInt( next() );
    }
    static long nextLong() throws IOException {
        return Long.parseLong( next() );
    }
    
    static double nextDouble() throws IOException {
        return Double.parseDouble( next() );
    }
}
class newcomparator implements Comparator<Integer>{
    //@Override
    public int compare(Integer a, Integer  b){
        return a<=b?1:-1;
    }
 }
class node {
    int f;
    int s;// cost to rreach\
    node(int ff,int ss){
        f=ff;
        s=ss;
    }
}
class mergesort{
    static void sort(int l, int h, int[] arr){
        if(l<h){
            int mid = (l+h)/2;
            
            sort(l,mid,arr);
            sort(mid+1,h,arr);
            merge(l,mid,h,arr);
        }
    }
    static void merge(int l, int m , int h , int [] arr){

        int[] left = new int[m-l+1];
        int[] right = new int[h-m];
        for(int i= 0 ;i< m-l+1;i++){
            left[i] = arr[l+i];
        }
        for(int i=0;i<h-m;i++){
            right[i] = arr[m+1+i];
        }
        //now left and right arrays are assumed to be sorted and we have tp merge them together 
        // int the original aaray.
        int i=l;
        int lindex = 0;
        int rindex =  0;
        
        while(lindex<m-l+1 && rindex<h-m){
            if(left[lindex]<=right[rindex]){
                arr[i]=left[lindex];
                lindex++;
                i++;
            }
            else{
                arr[i]=right[rindex];
                rindex++;
                i++;
            }
        }
        while(lindex<m-l+1){
            arr[i]=left[lindex];
            lindex++;
            i++;
        }
        while(rindex<h-m){
            arr[i]=right[rindex];
            rindex++;
            i++;
        }
    }
    
}
 
 
