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.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Iterator;
import java.util.Stack;
											public class Solution implements Runnable
											{
												static final long MAX = 1000000007L;
												static class InputReader
												{
													private InputStream stream;
													private byte[] buf = new byte[1024];
													private int curChar;
													private int numChars;
													private SpaceCharFilter filter;
													private BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
											 
													public InputReader(InputStream stream)
													{
														this.stream = stream;
													}
													
													public int read()
													{
														if (numChars==-1) 
															throw new InputMismatchException();
														
														if (curChar >= numChars)
														{
															curChar = 0;
															try 
															{
																numChars = stream.read(buf);
															}
															catch (IOException e)
															{
																throw new InputMismatchException();
															}
															
															if(numChars <= 0)				
																return -1;
														}
														return buf[curChar++];
													}
												 
													public String nextLine()
													{
														String str = "";
											            try
											            {
											                str = br.readLine();
											            }
											            catch (IOException e)
											            {
											                e.printStackTrace();
											            }
											            return str;
													}
													public int nextInt()
													{
														int c = read();
														
														while(isSpaceChar(c)) 
															c = read();
														
														int sgn = 1;
														
														if (c == '-') 
														{
															sgn = -1;
															c = read();
														}
														
														int res = 0;
														do 
														{
															if(c<'0'||c>'9') 
																throw new InputMismatchException();
															res *= 10;
															res += c - '0';
															c = read();
														}
														while (!isSpaceChar(c)); 
														
														return res * sgn;
													}
													
													public long nextLong() 
													{
														int c = read();
														while (isSpaceChar(c))
															c = read();
														int sgn = 1;
														if (c == '-') 
														{
															sgn = -1;
															c = read();
														}
														long res = 0;
														
														do 
														{
															if (c < '0' || c > '9')
																throw new InputMismatchException();
															res *= 10;
															res += c - '0';
															c = read();
														}
														while (!isSpaceChar(c));
															return res * sgn;
													}
													
													public double nextDouble() 
													{
														int c = read();
														while (isSpaceChar(c))
															c = read();
														int sgn = 1;
														if (c == '-') 
														{
															sgn = -1;
															c = read();
														}
														double res = 0;
														while (!isSpaceChar(c) && c != '.') 
														{
															if (c == 'e' || c == 'E')
																return res * Math.pow(10, nextInt());
															if (c < '0' || c > '9')
																throw new InputMismatchException();
															res *= 10;
															res += c - '0';
															c = read();
														}
														if (c == '.') 
														{
															c = read();
															double m = 1;
															while (!isSpaceChar(c)) 
															{
																if (c == 'e' || c == 'E')
																	return res * Math.pow(10, nextInt());
																if (c < '0' || c > '9')
																	throw new InputMismatchException();
																m /= 10;
																res += (c - '0') * m;
																c = read();
															}
														}
														return res * sgn;
													}
													
													public String readString() 
													{
														int c = read();
														while (isSpaceChar(c))
															c = read();
														StringBuilder res = new StringBuilder();
														do 
														{
															res.appendCodePoint(c);
															c = read();
														} 
														while (!isSpaceChar(c));
														
														return res.toString();
													}
												 
													public boolean isSpaceChar(int c) 
													{
														if (filter != null)
															return filter.isSpaceChar(c);
														return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
													}
												 
													public String next() 
													{
														return readString();
													}
													
													public interface SpaceCharFilter 
													{
														public boolean isSpaceChar(int ch);
													}
												}
											 	
												public static void main(String args[]) throws Exception
												{
													new Thread(null, new Solution(),"Solution",1<<26).start();
												}
												ArrayList<Integer> adj[];
												long fans = 0;
												long[] cost;
												public void run() 
												{
													InputReader sc= new InputReader(System.in);
													PrintWriter w= new PrintWriter(System.out);
													int n = sc.nextInt();
													cost = new long[n];
													for(int i = 0;i < n;i++) {
														cost[i] = sc.nextLong();
													}
													adj = new ArrayList[n];
													for(int i = 0;i < n;i++) {
														adj[i] = new ArrayList();
													}
													for(int i = 0;i < n;i++) {
														int x = sc.nextInt()-1;
														adj[i].add(x);
													}
													topo();
													visited = new boolean[n];
													visited1 = new int[n];
													int c = 0;
													cycle = new ArrayList();
													while(!st.isEmpty()) {
														int x = st.pop();
														if(visited1[x] == 0) {
															c++;
															dfs1(x,c);
														}
													}
													visited = new boolean[n];
													for(int i = 0;i < cycle.size();i++) {
														min = Integer.MAX_VALUE;
														fdfs(cycle.get(i));
														fans += min;
													}
													w.println(fans);
													w.close();
												}
												long min;
												void fdfs(int a) {
													min = Math.min(min,cost[a]);
													visited[a] = true;
													Iterator<Integer> it = adj[a].iterator();
													while(it.hasNext()) {
														int x = it.next();
														if(!visited[x]) {
															fdfs(x);
														}
													}
												}
												boolean[] visited;
												int[] visited1;
												ArrayList<Integer> cycle;
												void dfs1(int a,int c) {
													visited1[a] = c;
													Iterator<Integer> it = adj[a].iterator();
													while(it.hasNext()) {
														int x = it.next();
														if(visited1[x] == 0) {
															dfs1(x,c);
														}else if(visited1[x] == c) {
															cycle.add(x);
														}
													}
												}
												void topo() {
													visited = new boolean[adj.length];
													for(int i = 0;i < adj.length;i++) {
														if(!visited[i]) {
															dfs(i);
														}
													}
												}
												Stack<Integer> st = new Stack();
												void dfs(int a) {
													visited[a] = true;
													Iterator<Integer> it = adj[a].iterator();
													while(it.hasNext()) {
														int x = it.next();
														if(!visited[x]) {
															dfs(x);
														}
													}
													st.add(a);
												}
												long ans = Integer.MAX_VALUE;
												void generate(int[] p, int L, int R) {
													  if (L == R) { // all numbers are set
													    // do something with permutation in array p[]
														  if(ans > lis(p,p.length) + lds(p,p.length)) {
															  ans = lis(p,p.length) + lds(p,p.length);
															  System.out.println(Arrays.toString(p));
															  System.out.println(ans);
														  }
														  
													  } else { // numbers at positions [0, L-1] are set, try to set L-th position
													    for (int i = L; i <= R; i++) {
													      int tmp = p[L]; p[L] = p[i]; p[i] = tmp;
													      generate(p, L+1, R);
													      tmp = p[L]; p[L] = p[i]; p[i] = tmp;
													    }
													  }
													}
											    static int lis(int arr[],int n)
											    {
											          int lis[] = new int[n];
											          int i,j,max = 0;
											 
											          /* Initialize LIS values for all indexes */
											           for ( i = 0; i < n; i++ )
											              lis[i] = 1;
											 
											           /* Compute optimized LIS values in bottom up manner */
											           for ( i = 1; i < n; i++ )
											              for ( j = 0; j < i; j++ ) 
											                         if ( arr[i] > arr[j] && lis[i] < lis[j] + 1)
											                    lis[i] = lis[j] + 1;
											 
											           /* Pick maximum of all LIS values */
											           for ( i = 0; i < n; i++ )
											              if ( max < lis[i] )
											                 max = lis[i];
											 
											            return max;
											    }
											    static int lds(int arr[],int n)
											    {
											          int lis[] = new int[n];
											          int i,j,max = 0;
											 
											          /* Initialize LIS values for all indexes */
											           for ( i = 0; i < n; i++ )
											              lis[i] = 1;
											 
											           /* Compute optimized LIS values in bottom up manner */
											           for ( i = 1; i < n; i++ )
											              for ( j = 0; j < i; j++ ) 
											                         if ( arr[i] < arr[j] && lis[i] < lis[j] + 1)
											                    lis[i] = lis[j] + 1;
											 
											           /* Pick maximum of all LIS values */
											           for ( i = 0; i < n; i++ )
											              if ( max < lis[i] )
											                 max = lis[i];
											 
											            return max;
											    }

												// modifies c to next permutation or returns null if such permutation does not exist
												private static Comparable[] nextPermutation( final Comparable[] c ) {
													// 1. finds the largest k, that c[k] < c[k+1]
													int first = getFirst( c );
													if ( first == -1 ) return null; // no greater permutation
													// 2. find last index toSwap, that c[k] < c[toSwap]
													int toSwap = c.length - 1;
													while ( c[ first ].compareTo( c[ toSwap ] ) >= 0 )
														--toSwap;
													// 3. swap elements with indexes first and last
													swap( c, first++, toSwap );
													// 4. reverse sequence from k+1 to n (inclusive) 
													toSwap = c.length - 1;
													while ( first < toSwap )
														swap( c, first++, toSwap-- );
													return c;
												}

												// finds the largest k, that c[k] < c[k+1]
												// if no such k exists (there is not greater permutation), return -1
												private static int getFirst( final Comparable[] c ) {
													for ( int i = c.length - 2; i >= 0; --i )
														if ( c[ i ].compareTo( c[ i + 1 ] ) < 0 )
															return i;
													return -1;
												}

												// swaps two elements (with indexes i and j) in array 
												private static void swap( final Comparable[] c, final int i, final int j ) {
													final Comparable tmp = c[ i ];
													c[ i ] = c[ j ];
													c[ j ] = tmp;
												}
												long gcd(long a, long b)
												{
												    if (b == 0)
												        return a;
												    return gcd(b, a % b);
												}
												
												
											    long lcm(long a, long b) {
											        return a * b / gcd(a, b);
											    }
											}