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.
   //    package com.company;
        import java.io.BufferedReader;
        import java.io.IOException;
        import java.io.InputStreamReader;
        import java.lang.reflect.Array;
        import java.sql.SQLOutput;
        import java.util.*;
        public class Main {
 
            public static void main(String[] args) throws IOException {
                Scanner s = new Scanner(System.in);
                //BufferedReader s=new BufferedReader(new InputStreamReader(System.in));
                 StringBuilder sb1 = new StringBuilder();//int n=Integer.parseInt(s.readLine());
     int n=s.nextInt();
     int[] c=new int[n+1];
     int[] a=new int[n+1];
     for(int i=1;i<=n;i++){
         c[i]=s.nextInt();
     }
     for(int i=1;i<=n;i++){
         a[i]=s.nextInt();
     }int [] vis=new int[n+1];long ans=0;
     for(int i=1;i<=n;i++)
     {
         if(vis[i]==0){
              min=Integer.MAX_VALUE;
             dfs(i,vis,new HashMap<Integer,Integer> (),a,c,0);
           if(min!=Integer.MAX_VALUE)  ans+=min;
         }
     }
                System.out.println(ans);            }static int min=Integer.MAX_VALUE;static long ans=0;static long count=0;
            static void dfs(int i,int[] vis,HashMap<Integer,Integer> h,int [] a,int[] c,int ts){
              //if(vis[i]!= vis1[i]) return;
                if(vis[i]>0&&(!h.containsKey(i)||h.get(i)!=vis[i])) return;
 
                if(vis[i]==2) return;
                if(h.containsKey(i)&&h.get(i)==2) return;
                if(h.containsKey(i))  h.put(i,h.get(i)+1);
                else h.put(i,1);
                vis[i]+=1;
                //vis1[i]+=1;
                if(h.get(i)==2) min=Math.min(min,c[i])    ;
            //    if(vis[i]==1) min=c[i];
                dfs(a[i],vis,h,a,c,ts);
            }
            static  void computeLPSArray(String pat, int M, int lps[])
            {
            int len = 0;
                int i = 1;
                lps[0] = 0;
                while (i < M) {
                    if (pat.charAt(i) == pat.charAt(len)) {
                        len++;
                        lps[i] = len;
                        i++;
                    }
                    else // (pat[i] != pat[len])
                    {
                        if (len != 0) {
                            len = lps[len - 1];
                        }
                        else // if (len == 0)
                        {
                            lps[i] = len;
                            i++;
                        }
                    }
                }
            }
            static boolean isPrime(int n)
            {
                if (n <= 1) return false;
                if (n <= 3) return true;
 
                if (n % 2 == 0 || n % 3 == 0) return false;
 
                for (int i = 5; i * i <= n; i = i + 6)
                    if (n % i == 0 || n % (i + 2) == 0)
                        return false;
 
                return true;
            }
 
            static double power(double x, long y, int p) {
                double res = 1;
                x = x % p;
                while (y > 0) {
                    if ((y & 1) == 1)
                        res = (res * x) % p;
                    y = y >> 1;
                    x = (x * x) % p;
                }
                return res;
            }
            public static void fracion(double x) {
                String a = "" + x;
                String spilts[] = a.split("\\."); // split using decimal
                int b = spilts[1].length(); // find the decimal length
                int denominator = (int) Math.pow(10, b); // calculate the denominator
                int numerator = (int) (x * denominator); // calculate the nerumrator Ex
                // 1.2*10 = 12
                int gcd = (int)gcd((long)numerator, denominator); // Find the greatest common
                // divisor bw them
                String fraction = "" + numerator / gcd + "/" + denominator / gcd;
                // System.out.println((denominator/gcd));
                long x1=modInverse(denominator / gcd,998244353  );
                //  System.out.println(x1);
                System.out.println((((numerator / gcd )%998244353 *(x1%998244353  ))%998244353  ) );
            }static int anst=Integer.MAX_VALUE;
          static StringBuilder sb1=new StringBuilder();
            public static int bfs(int i1, ArrayList<Integer>[] h, int[] vis, int n,int val1) {
                Queue<Integer> q = new LinkedList<Integer>();
                q.add(i1);Queue<Integer> aq=new LinkedList<Integer>();
                aq.add(0);
                while(!q.isEmpty()){
                    int i=q.poll();
                    int val=aq.poll();
                    if(i==n){
                        return val;
                    }
                    if(h[i]!=null){
                        for(Integer j:h[i]){
                           if(vis[j]==0){
                            q.add(j);vis[j]=1;
                               aq.add(val+1);}
                        }
                    }
                }return -1;
            }
            static int i(String a) {
                return Integer.parseInt(a);
            }
            static long l(String a) {
                return Long.parseLong(a);
            }
            static String[] inp() throws IOException {
                BufferedReader s = new BufferedReader(new InputStreamReader(System.in));
                return s.readLine().trim().split("\\s+");
            }
 
 
            static long gcd(long a, long b) {
                if (b == 0)
                    return a;
                return gcd(b, a % b);
            }
            static long modInverse(int a, int m)
            {
 
 
                {
                    // If a and m are relatively prime, then modulo inverse
                    // is a^(m-2) mode m
                    return (power(a, m - 2, m));
                }
            }
 
            // To compute x^y under modulo m
            static long power(int x, int y, int m)
            {
                if (y == 0)
                    return 1;
 
                long p = power(x, y / 2, m) % m;
                p = (p * p) % m;
 
                if (y % 2 == 0)
                    return p;
                else
                    return (x * p) % m;
            }
 
            // Function to return gcd of a and b
 
        }
        class Student
        {
            int l;int r;
            public Student(int l, int r) {
                this.l = l;
                this.r = r;
            }
            // Constructor
 
 
            // Used to print student details in main()
            public String toString()
            {
                return this.l+" ";
            }
        }
 
        class Sortbyroll implements Comparator<Student>
        {
            // Used for sorting in ascending order of
            // roll number
            public int compare(Student a, Student b){
                return a.l-b.l;  }
        } class Sortbyroll1 implements Comparator<Student>
        {
            // Used for sorting in ascending order of
            // roll number
            public int compare(Student a, Student b){
               if(a.r==b.r) return a.l-b.l;
                return a.r-b.r;
            }
        }
