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
import java.io.*;
import java.util.*;
import java.math.BigInteger; 

public class test1 {
    
    static int[][] dir={{0,1},{1,0},{-1,0},{0,-1}};
   static class sort implements Comparator<int[]>
   {
        public int compare(int[] a,int[] b)
        {
            if(a[0] == b[0]) return a[1]-b[1];
           return a[0]-b[0];
        }
   }
   public static void pri(PrintWriter out,Object o)
   {
      
    out.println(o.toString());
    
   }
   public static int intIn(String st)
   {
      return Integer.parseInt(st);
   }
   
   
    
    public static void pr(Object o)
    {
        System.out.println(o.toString());
    }
    public static void prW(Object o)
    {
        System.out.print(o.toString());
    }
    
   
    public static int inInt(String s)
    {
        return Integer.parseInt(s);
    }
    public static long in(String s)
    {
        return Long.parseLong(s);
    }

    static long power(long x, long y, long p)
      {
        long res = 1; // Initialize result
     
        x = x % p; // Update x if it is more than or
        // equal to p
     
        if (x == 0)
          return 0l; // In case x is divisible by p;
     
        while (y > 0)
        {
     
          // If y is odd, multiply x with result
          if ((y & 1) != 0)
            res = (res * x) % p;
     
          // y must be even now
          y = y >> 1; // y = y/2
          x = (x * x) % p;
        }
        return res;
      }
      static long gcd(long a, long b)
        {
          if (b == 0)
            return a;
          return gcd(b, a % b); 
        }
        static int gcd(int a, int b,int o)
        {
          if (b == 0)
            return a;
          return gcd(b, a % b,o); 
        }
    
      
    
    
    static int[] toIntArray(String[] m) 
    { 
        int[] p=new int[m.length];
        for(int o=0;o<m.length;o++)
        {
            p[o]= inInt(m[o]);
        }
        return p;
    }
    static long[] toLArray(String[] m) 
    { 
       long[] p=new long[m.length];
        for(int o=0;o<m.length;o++)
        {
            p[o]= in(m[o]);
        }
        return p;
    }
    public static long F(long[] arr)
    {
        //int len=arr.length;
        
       // Arrays.sort(arr);
        long med;
        if((arr.length%2) == 0)
        {
            med = (arr[arr.length/2] + arr[(arr.length/2)-1])/(2);
        }
        else
        {
            med = (arr[arr.length/2]);
        }
        long dis = 0l;

        for(long x : arr)
        {
            dis += (Math.abs(x-med));
           // prW(x+" ");
        }
        //prW(med);
       // pr("");
       // arr=tem;
        return dis;


    }
    static int[][] pre;// = new int[n][27];
    static int[][] fin;//
    public static String[] F(BufferedReader bf) throws Exception
    {
        return (bf.readLine().split(" "));
    }
    public static int F(int x,int y)
    {
        int[] preX = pre[x];
        int[] finX = fin[x];
        int[] preY = pre[y];int n=preX.length;int res=0;
        HashMap<Integer,Integer> t1,t2;
        t1=new HashMap<>();
        t2=new HashMap<>();
        for(int o=0;o<n;o++)
        {
            if(!t1.containsKey(preX[o])) t1.put(preX[o],o);
        }
        for(int o=preX.length-1;o>=0;o--)
        {
            if(!t2.containsKey(finX[o])) t2.put(finX[o],o);
        }
        for(int c=0;c<n;c++)
        {
            if(t1.containsKey(c) && t2.containsKey(c))
            {

                int coun;
                int i1,i3;
                i1 = t1.get(c);i3=t2.get(c);
                if(i1>=i3) continue;
                coun = preY[i3] - preY[i1];
                if(coun<0) coun=0;
                res = Math.max(res,2*c + coun);
            }
        }
        //pr(x+" "+y+" "+(res));;
        return res;
    }
    static long mod = 998244353l;
    static int[][] arr = {{0,0},{0,1},{1,0},{1,1}};
    public static long F(int i,int y,int c,Long[][][] dp,int n,int m)
    {
        if(i>=n)
        {
            if(c==m)
            {
                return 1l;
            }
            return 0l;
        }
        if(c>m) return 0l;
        if((m-c) > (2*(n-i))) return 0l;
        long sum=0l;
        if(dp[i][c][y] != null)  return dp[i][c][y];
        int u,j;u=(y%2);j=(y/2);
        for(int x=0;x<4;x++)
        {
            int a,b;a=(x%2);b=(x/2);
            if(u==a && j==b)
            {
                sum = (sum%mod + F(i+1,x,c,dp,n,m)%mod)%mod;
            }
            else if(u==a && j!=b)
            {
                if(a==b){
                 sum = (sum%mod + F(i+1,x,c,dp,n,m)%mod)%mod;}
                 else
                 {
                    sum = (sum%mod + F(i+1,x,c+1,dp,n,m)%mod)%mod;
                 }
                 //sum = (sum%mod + F(i+1,a,b,c+1,dp,n,m)%mod)%mod;
            }
             else if(u!=a && j==b)
            {
                 if(a==b){
                 sum = (sum%mod + F(i+1,x,c,dp,n,m)%mod)%mod;}
                 else
                 {
                    sum = (sum%mod + F(i+1,x,c+1,dp,n,m)%mod)%mod;
                 }
            }
            else if(u!=a && j!=b)
            {
                 if(a==b){
                 sum = (sum%mod + F(i+1,x,c+1,dp,n,m)%mod)%mod;}
                 else
                 {
                    sum = (sum%mod + F(i+1,x,c+2,dp,n,m)%mod)%mod;
                 }
            }
        }
        dp[i][c][y] = sum%mod;return dp[i][c][y];

    }
    public static void main (String[] args) throws Exception {
        
        BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(System.out);;;
        
        //int[] map=new int[1000001];
      
        int y=1;//Integer.parseInt(bf.readLine());
        
        for(int h=0;h<y;h++)
        {

            
    
            String[] xlp = bf.readLine().split(" ");
            
            int n,k,t;//boolean bol=false;
            n=intIn(xlp[0]);
            int[] cost;int[] arr;
            cost = toIntArray(F(bf));
            arr = toIntArray(F(bf));
            for(int o=0;o<n;o++) arr[o]--;
            //int[][] p = new int[n][2];
            int curr=0;
            int[] over=new int[n];int i=0;
            long res=0l;
            int[] index=new int[n];
            List<Integer> list = new ArrayList<>();
            for(int o=0;o<n;o++)
            {
                if(over[o] != 0) continue;
                list.clear();
                int next=o;;//over[o]=true;
                int min=1000000;
                boolean bol=true;boolean hit = false;
               int cycle = o;
               int last=o;
                while((next != o || bol))
                {
                   bol=false;
                   over[next]=1;
                   int neig = arr[next];
                   index[next] = list.size();list.add(next);
                   if(over[neig] == 0)
                   {
                        next=neig;
                   }
                   else if(over[neig] == 1)
                   {
                        cycle = neig;
                        break;
                   }
                   else
                   {
                      hit=true;break;
                   }

                }
               // min=cost[o];
               // pr(hit+" "+cycle);
                //pr(list);
                if(hit)
                {
                    min=0;
                }
                else
                {
                    for(int u=index[cycle];u<list.size();u++)
                    {
                        min=Math.min(min,cost[list.get(u)]);
                    }
                }
                for(int u=0;u<list.size();u++)
                    {
                       over[list.get(u)]=2;
                    }
                
                //pr("min "+min);
                res += (min);
            }
            pri(out,res);
          
           
            

            

            
            
            
        }
        out.close();

    }
}
/*
5 10
3 4
10 11
12 13
14 16
18 20

static class SegmentTreeRMQ 
    { 
    int st[]; 
    int minVal(int x, int y) { 
        return (x > y) ? x : y; 
    } 
  
    
    int getMid(int s, int e) { 
        return s + (e - s) / 2; 
    } 
  
    
    int RMQUtil(int ss, int se, int qs, int qe, int index) 
    { 
        
        if (qs <= ss && qe >= se) 
            return st[index]; 
  
        // If segment of this node is outside the given range 
        if (se < qs || ss > qe) 
            return Integer.MIN_VALUE; 
  
        // If a part of this segment overlaps with the given range 
        int mid = getMid(ss, se); 
        return minVal(RMQUtil(ss, mid, qs, qe, 2 * index + 1), 
                RMQUtil(mid + 1, se, qs, qe, 2 * index + 2)); 
    } 
  
    // Return minimum of elements in range from index qs (query 
    // start) to qe (query end).  It mainly uses RMQUtil() 
    int RMQ(int n, int qs, int qe) 
    { 
        // Check for erroneous input values 
        
  
        return RMQUtil(0, n - 1, qs, qe, 0); 
    } 
  
    // A recursive function that constructs Segment Tree for 
    // array[ss..se]. si is index of current node in segment tree st 
    int constructSTUtil(int arr[], int ss, int se, int si) 
    { 
        // If there is one element in array, store it in current 
        //  node of segment tree and return 
        if (ss == se) { 
            st[si] = arr[ss]; 
            return arr[ss]; 
        } 
  
        // If there are more than one elements, then recur for left and 
        // right subtrees and store the minimum of two values in this node 
        int mid = getMid(ss, se); 
        st[si] = minVal(constructSTUtil(arr, ss, mid, si * 2 + 1), 
                constructSTUtil(arr, mid + 1, se, si * 2 + 2)); 
        return st[si]; 
    } 
  
    
    void con(int arr[]) 
    { 
        // Allocate memory for segment tree 
  
        //Height of segment tree 
        int n = (arr.length);
        int x = (int) (Math.ceil(Math.log(n) / Math.log(2))); 
  
        //Maximum size of segment tree 
        int max_size = 2 * (int) Math.pow(2, x) - 1; 
        st = new int[max_size]; // allocate memory 
  
        // Fill the allocated memory st 
        constructSTUtil(arr, 0, n - 1, 0); 
    }
    }
     static class DSU {
    
    int[] p;int[] sz;int op;int c;;
    int[] last;
    public void G(int n)
    {
        last=new int[n];
        p=new int[n];
        sz=new int[n];c=n;
        op=n;
        for(int h=0;h<n;h++)
        {
            sz[h]=1;p[h]=h;
            last[h]=h;
        }
    }
    public int find(int x)
    {
        int y=x;
        while(x!=p[x]) x=p[x];
        while(y!=p[y])
        {
            int tem=p[y];
            p[y]=x;y=tem;
        }
        return p[y];
    }
    public void union(int a,int b)
    {
        int x,y;
        x=find(a);y=find(b);
        if(x==y) return;
        if(sz[x]>sz[y])
        {
            p[y] = x;
            sz[x]+=sz[y];
            last[x]=Math.max(last[x],last[y]);
        }
        else
        {
          p[x]=y;sz[y]+=sz[x];
            last[y]=Math.max(last[y],last[x]);
        }
        c--;
        
    }}
*/
