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.
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline T gi() {
  T x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
  return f * x;
}
const int INF = 0x3f3f3f3f, N = 200003, M = N << 1;
int n, m, a[N];
int tot, head[N], ver[M], nxt[M];
int dfn[N], low[N], tim, stk[N], tp, scc_cnt, mn[N], id[N];
bool in_stk[N];
int du[N];
inline void add(int u, int v) {
  ver[++tot] = v, nxt[tot] = head[u], head[u] = tot;
}
void Tarjan(int u) {
  dfn[u] = low[u] = ++tim, stk[++tp] = u, in_stk[u] = true;
  for (int i = head[u]; i; i = nxt[i]) {
    int v = ver[i];
    if (!dfn[v]) {
      Tarjan(v);
      low[u] = min(low[u], low[v]);
    } else if (in_stk[i])
      low[u] = min(low[u], dfn[v]);
  }
  if (low[u] == dfn[u]) {
    ++scc_cnt;
    int y = -1;
    mn[scc_cnt] = 1000000;
    do {
      y = stk[tp--];
      in_stk[y] = false;
      id[y] = scc_cnt;
      mn[scc_cnt] = min(mn[scc_cnt], a[y]);
    } while (y != u);
  }
}
int main() {
  n = gi<int>();
  for (int i = 1; i <= n; i += 1) a[i] = gi<int>();
  for (int i = 1; i <= n; i += 1) {
    int x = gi<int>();
    add(i, x);
  }
  for (int i = 1; i <= n; i += 1)
    if (!dfn[i]) Tarjan(i);
  for (int i = 1; i <= n; i += 1)
    for (int j = head[i]; j; j = nxt[j]) {
      int v = ver[j];
      if (id[i] != id[v]) ++du[id[i]];
    }
  set<int> s;
  for (int i = 1; i <= n; i += 1) s.insert(id[i]);
  int sum = 0;
  for (auto x : s)
    if (du[x] == 0) sum += mn[x];
  printf("%d\n", sum);
  return 0;
}
