#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
const int MAXN = (1 << 20);
const int MAXLOG = 20;
template <class T>
struct fenwick
{
int sz;
T tr[MAXN];
void init(int n)
{
sz = n + 1;
memset(tr, 0, sizeof(tr));
}
T query(int idx)
{
T ans = 0;
for(; idx >= 1; idx -= (idx & -idx))
ans += tr[idx];
return ans;
}
void update(int idx, T val)
{
if(idx <= 0) return;
for(; idx <= sz; idx += (idx & -idx))
tr[idx] += val;
}
T query(int l, int r) { return query(r) - query(l - 1); }
};
int n, h;
vector<int> G[MAXN];
void read()
{
cin >> n >> h;
for(int i = 1; i < n; i++)
{
int p;
cin >> p;
G[p].push_back(i);
G[i].push_back(p);
}
}
int lgn, st[MAXN], en[MAXN], depth[MAXN], dfs_time = 0;
int par[MAXN][MAXLOG], root = 0;
vector<int> li[MAXN];
void dfs_lca(int u, int d = 0)
{
li[d].push_back(u);
depth[u] = d;
int sz = G[u].size(), v;
st[u] = ++dfs_time;
for(int i = 1; i < MAXLOG; i++)
par[u][i] = par[par[u][i - 1]][i - 1];
for(int i = 0; i < sz; i++)
{
v = G[u][i];
if(v != par[u][0])
{
par[v][0] = u;
dfs_lca(v, d + 1);
}
}
en[u] = dfs_time;
}
inline bool upper(int u, int v) { return st[u] <= st[v] && en[v] <= en[u]; }
int lca(int u, int v)
{
if(upper(u, v)) return u;
if(upper(v, u)) return v;
int a = u;
for(int i = MAXLOG - 1; i >= 0; i--)
if(!upper(par[a][i], v))
a = par[a][i];
return par[a][0];
}
int parent(int u, int up)
{
int a = u;
for(int i = MAXLOG - 1; i >= 0; i--)
if(up & (1 << i))
a = par[a][i];
return a;
}
void lca_precompute(int _root)
{
root = _root;
for(int i = 0; i < MAXLOG; i++) par[root][i] = root;
dfs_time = 0;
dfs_lca(root);
}
fenwick<int64_t> anst, t;
int get_par(int u, int k)
{
if(t.query(st[u], en[u]) >= k) return u;
for(int l = MAXLOG - 1; l >= 0; l--)
if(t.query(st[par[u][l]], en[par[u][l]]) < k)
u = par[u][l];
u = par[u][0];
if(t.query(st[u], en[u]) < k) return -1;
return u;
}
int get_real_par(int u)
{
for(int l = MAXLOG - 1; l >= 0; l--)
if(!anst.query(st[par[u][l]], en[par[u][l]]))
u = par[u][l];
return u;
}
int64_t solve(int l, int k)
{
int64_t ret = 0;
vector<int> to_rem;
for(int u: li[l])
{
int up, p = get_par(u, k);
if(p == -1) continue;
if(anst.query(st[p], en[p]))
continue;
up = get_real_par(p);
ret += depth[p] - depth[up] + 1;
anst.update(st[p], 1);
to_rem.push_back(p);
}
for(int u: to_rem)
anst.update(st[u], -1);
return ret;
}
void solve()
{
lca_precompute(0);
anst.init(n + 2);
t.init(n + 2);
int64_t ans = 0;
for(int i = 0; i <= h; i++)
{
for(int u: li[i]) t.update(st[u], 1);
int k;
cin >> k;
if(k != 0) ans += 1ll * solve(i, k);
else ans += 1ll * n;
for(int u: li[i]) t.update(st[u], -1);
}
cout << ans << endl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
read();
solve();
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgZW5kbCAnXG4nCgp1c2luZyBuYW1lc3BhY2Ugc3RkOwpjb25zdCBpbnQgTUFYTiA9ICgxIDw8IDIwKTsKY29uc3QgaW50IE1BWExPRyA9IDIwOwoKdGVtcGxhdGUgPGNsYXNzIFQ+CnN0cnVjdCBmZW53aWNrCnsKCWludCBzejsKCVQgdHJbTUFYTl07CgoJdm9pZCBpbml0KGludCBuKQoJewoJCXN6ID0gbiArIDE7CgkJbWVtc2V0KHRyLCAwLCBzaXplb2YodHIpKTsKCX0KCglUIHF1ZXJ5KGludCBpZHgpCgl7CgkJVCBhbnMgPSAwOwoJCWZvcig7IGlkeCA+PSAxOyBpZHggLT0gKGlkeCAmIC1pZHgpKQoJCQlhbnMgKz0gdHJbaWR4XTsKCQlyZXR1cm4gYW5zOwoJfQoKCXZvaWQgdXBkYXRlKGludCBpZHgsIFQgdmFsKQoJew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