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.*;
import static java.lang.Math.*;
public class Main
{  
    static Reader scan;
    static PrintWriter pw;
    static int n,val[],scc[],mininscc[],to[];
    static ArrayList<Integer> adj[],radj[],adjscc[];
    static boolean vis[];
    static int cnt = 0;
    static Stack<Integer> st = new Stack();
    public static void main(String[] args) {
        new Thread(null,null,"_",1<<25)
        {
           public void run()
           {
               try
               {
                   solve();
               }
               catch(Exception e)
               {  
                   e.printStackTrace();
                   System.exit(1);
               }
           }
        }.start();
    }
    static void solve() throws IOException
    {
        scan = new Reader();
        pw = new PrintWriter(System.out,true);
        StringBuilder sb = new StringBuilder();
        n = ni();
        val = new int[n+1];
        to = new int[n+1];
        adj = new ArrayList[n+1];
        radj = new ArrayList[n+1];
        for(int i=1;i<=n;++i)
        {
            val[i] = ni();
            adj[i] = new ArrayList();
            radj[i] = new ArrayList();
        }
        for(int i=1;i<=n;++i)
        {  
            int mc = ni();
            to[i] = mc;
            adj[i].add(mc);
            radj[mc].add(i);
        }
        vis = new boolean[n+1];
        for(int i=1;i<=n;++i)
            if(!vis[i])
                dfs1(i);
        vis = new boolean[n+1];
        scc = new int[n+1];
        mininscc = new int[n+1];
        while(!st.isEmpty())
        {
            int x = st.pop();
            if(!vis[x])
            {
                dfs2(x);
                cnt++;
            }
        }
//        pl("no of sccs : "+cnt);
//        pl("scc of each vertex : ");
//        for(int i=1;i<=n;++i)
//            p(scc[i]);
//        pl();
//        pl("min in each scc : ");
//        for(int i=0;i<cnt;++i)
//            p(mininscc[i]);
//        pl();
        adjscc = new ArrayList[cnt];
        for(int i=0;i<cnt;++i)
            adjscc[i] = new ArrayList();
        int outdeg[] = new int[cnt];
        for(int i=1;i<=n;++i)
        {
            if(scc[i]!=scc[to[i]])
            {
                outdeg[scc[i]]++;
            }
        }
        long ans = 0;
        for(int i=0;i<cnt;++i)
        {
            if(outdeg[i]==0)
                ans+=mininscc[i];
        }
        pl(ans);
        pw.flush();
        pw.close();
    }
    static void dfs1(int x)
    {  
        vis[x] = true;
        for(int e:adj[x])
            if(!vis[e])
                dfs1(e);
        st.add(x);
    }
    static void dfs2(int x)
    {  
        vis[x] = true;
        scc[x] = cnt;
        if(mininscc[cnt]==0)
            mininscc[cnt] = val[x];
        else mininscc[cnt] = min(mininscc[cnt],val[x]);
        for(int e:radj[x])
            if(!vis[e])
                dfs2(e);
    }
    static int ni() throws IOException
    {
        return scan.nextInt();
    }
    static long nl() throws IOException
    {
        return scan.nextLong();
    }
    static double nd() throws IOException
    {
        return scan.nextDouble();
    }
    
    static void pl()
    {
        pw.println();
    }
    static void p(Object o)
    {
        pw.print(o+" ");
    }
    static void pl(Object o)
    {
        pw.println(o);
    }
    static void psb(StringBuilder sb)
    {
        pw.print(sb);
    }
   static class Reader {
    final private int BUFFER_SIZE = 1 << 16;
    private DataInputStream din;
    private byte[] buffer;
    private int bufferPointer, bytesRead;

    public Reader() {
        din = new DataInputStream(System.in);
        buffer = new byte[BUFFER_SIZE];
        bufferPointer = bytesRead = 0;
    }

    public Reader(String file_name) throws IOException {
        din = new DataInputStream(new FileInputStream(file_name));
        buffer = new byte[BUFFER_SIZE];
        bufferPointer = bytesRead = 0;
    }

    public String readLine() throws IOException {
        byte[] buf = new byte[64];
        int cnt = 0, c;
        while ((c = read()) != -1) {
            if (c == '\n') break;
            buf[cnt++] = (byte) c;
        }
        return new String(buf, 0, cnt);
    }

    public int nextInt() throws IOException {
        int ret = 0;
        byte c = read();
        while (c <= ' ') c = read();
        boolean neg = (c == '-');
        if (neg) c = read();
        do {
            ret = ret * 10 + c - '0';
        } while ((c = read()) >= '0' && c <= '9');
        if (neg) return -ret;
        return ret;
    }

    public long nextLong() throws IOException {
        long ret = 0;
        byte c = read();
        while (c <= ' ') c = read();
        boolean neg = (c == '-');
        if (neg) c = read();
        do {
            ret = ret * 10 + c - '0';
        } while ((c = read()) >= '0' && c <= '9');
        if (neg) return -ret;
        return ret;
    }

    public double nextDouble() throws IOException {
        double ret = 0, div = 1;
        byte c = read();
        while (c <= ' ') c = read();
        boolean neg = (c == '-');
        if (neg) c = read();
        do {
            ret = ret * 10 + c - '0';
        } while ((c = read()) >= '0' && c <= '9');
        if (c == '.') while ((c = read()) >= '0' && c <= '9') ret += (c - '0') / (div *= 10);
        if (neg) return -ret;
        return ret;
    }

    private void fillBuffer() throws IOException {
        bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
        if (bytesRead == -1) buffer[0] = -1;
    }

    private byte read() throws IOException {
        if (bufferPointer == bytesRead) fillBuffer();
        return buffer[bufferPointer++];
    }

    public void close() throws IOException {
        if (din == null) return;
        din.close();
    }
}
}