stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;



#define int long long

#define INF ((int)1e17)

#define MAX 110000



int N, K;

int a[MAX], hoge[MAX], fuga[MAX];

vector<int> node[MAX];





signed main(){

  int ans = 0, temp = 0;

  stack<pair<int,pair<int,int> > > st;



  cin>>N>>K;



  for(int i = 0; i < N; i++){

    hoge[i] = -1;

  }



  for(int i = 0; i < N; i++){

    cin>>a[i];

    a[i]--;

    if(i) node[a[i]].push_back(i);

  }



  st.push(make_pair(0,make_pair(0,0)));



  while(!st.empty()){

    pair<int,pair<int,int> > p = st.top(), t; st.pop(); 

    int num = p.first;



    if(p.second.second >= node[num].size()){      

      if(num == 0) break;

      else {

	t = st.top(); st.pop();

	if(p.second.first+1 == K && a[num]){

	  ans++;

	  p.second.first = -1;

	}

	t.second.first = max(t.second.first,p.second.first+1);

      }

      st.push(t);

    } else {

      st.push(make_pair(num,make_pair(p.second.first,p.second.second+1)));

      st.push(make_pair(node[num][p.second.second],make_pair(0,0)));

    }



  }



  if(a[0] != 0) ans++;



  

  cout<<ans<<endl;



  



  return 0;

}


// Optimized solution to the same problem (faster):
#include<iostream>

#include<vector>

#include<algorithm>

#include<string>

using namespace std;



#define int long long



int ans, K, X;



int dfs(vector<vector<int>> & tree, int depth, int now, int per){

  int maximum = depth;



  for(int next : tree[now]){

    maximum = max(maximum, dfs(tree, depth+1, next, now));

    // cout<<now<<" -> "<<next<<endl;

  }

  // cout<<endl;

  //cout<<"K = "<<K<<" per "<<per<<endl;

  //cout<<"now "<<now<<" depth "<<depth<<" maximum "<<maximum<<" <> "<<maximum-depth<<" ans "<<ans<<endl;



  if(now == 0){



  } else if(per == 0){

    if(maximum - depth >= K){

      ans++;

      maximum = depth-1;

    }

  }else{

    if(maximum - depth >= K-1){

      ans++;

      maximum = depth-1;

    }

  }



  //  cout<<"now "<<now<<" depth "<<depth<<" maximum "<<maximum<<" <> "<<maximum-depth<<" ans "<<ans<<endl;



  return maximum;

}



signed main(){



  int N;

  vector<int> a;

  vector<vector<int>> tree;



  cin>>N>>K;



  a.resize(N);

  tree.resize(N);



  for(int i = 0; i < N; i++){

    cin>>a[i];



    a[i]--;



    if(!i) {

      if(a[i] != 0) ans++;

      continue;

    }



    tree[a[i]].push_back(i);

    //tree[i].push_back(a[i]);



  }



  dfs(tree, 0, 0, -1);



  cout<<ans<<endl;



  return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <stdio.h>

#include <bits/stdc++.h>

#define pb push_back

#define pp pop_back

#define mp make_pair

#define f first

#define s second

#define all(x) x.begin(), x.end()



typedef long long ll;



using namespace std;



const ll INF = 1e18;

const double pi = acos(-1.0), eps = 1e-9;

const int N = 1e5 + 111, inf = 1e9, mod = 1e9 + 7;



int n, k, a[N], ans, dp[N];

bool used[N], ok[N];

vector <int> g[N], ord;



bool cmp(int x, int y){

	return dp[x] < dp[y];

}



void dfsa(int v){

	ok[v] = 1;

	for(auto to : g[v]){

		if(ok[to])

			continue;

		dp[to] = dp[v] + 1;

		dfsa(to);

	}

}



void dfsaa(int v){

	for(auto to : g[v]){

		dp[to] = dp[v] + 1;

		dfsaa(to);

	}

}



int main(){

	srand(time(NULL));



	cin.tie(0);

	cout.tie(0);

	ios_base::sync_with_stdio(0);



	cout << setprecision(10) << fixed;



	cin >> n >> k;

	

	for(int i = 1; i <= n; ++i)

		cin >> a[i];



	if(a[1] != 1)

		a[1] = 1, ans = 1;



	for(int i = 2; i <= n; ++i)

		g[a[i]].pb(i);



	dfsaa(1);



	for(int i = 1; i <= n; ++i)

		ord.pb(i);



	sort(all(ord), &cmp);



	

	for(int i = 0; i < n; ++i){

		int x = ord[n - i - 1];

		if(!ok[x] && dp[x] > k){

			int root = x;

			for(int j = 2; j <= k; ++j)

				root = a[root];

			a[root] = 1;

			ans++;

			dp[root] = 1;

			dfsa(root);	

		}

	}



	cout << ans;



	//cerr << (clock() + 0.0) / CLOCKS_PER_SEC;

	return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<iostream>

#include<cstring>

#define RG register

using namespace std;

const int N=1e5+10;

struct dabt{

	int next,to;

}e[N];

int n,m,head[N],k,fa[N],ans;

inline void add(int from,int to){e[++k]=(dabt){head[from],to}; head[from]=k;}

template<typename I> inline void read(I &ot){

	I ch=getchar(), x=0, f=1;

	while(ch<'0' || ch>'9'){if(ch=='-') f=-1; ch=getchar();	}

	while(ch>='0' && ch<='9'){x=x*10+ch-'0'; ch=getchar();	}

	ot=x*f;}

template<typename I, typename... U> inline void read(I &x,U&... y){read(x); read(y...);}

template<typename I>inline I mx(const I&a,const I&b){return a>b ? a : b;}

int dfs(int st,int dep)

{

	int tt=dep;

	for(int i=head[st];i;i=e[i].next)

	{

		int v=e[i].to;

		tt=mx(tt,dfs(v,dep+1));

	}

	if(tt-dep+1==m && fa[st]!=1) 

	{

		ans++;

		fa[st]=1;

		return 0;

	}

	return tt;

}

int main()

{

//	freopen("Teleporter.in","r",stdin);

	//freopen(".out","w",stdout);

	read(n,m);

	for(int i=1;i<=n;i++) read(fa[i]);

	for(int i=2;i<=n;i++) add(fa[i],i);

	if(fa[1]!=1) ans++;

	fa[1]=1;

	dfs(1,0);

	printf("%d\n",ans);

	//fclose(stdin); fclose(stdout);

	return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;



const int MaxN(100003);

int fa[MaxN], n, K, ans;

vector<int> G[MaxN];



int dfs(int u)

{

	int d = 0;

	for(auto v : G[u])

		d = max(d, dfs(v));

	if(d == K - 1)

		ans += (fa[u] != 1), d = -1;

	return d + 1;

}



int main()

{

	scanf("%d%d", &n, &K);

	for(int i = 1; i <= n; i++)

		scanf("%d", &fa[i]);

	ans = (fa[1] != 1), fa[1] = 1;

	for(int i = 2; i <= n; i++)

		G[fa[i]].push_back(i);

	dfs(1);

	printf("%d\n", ans);

	return 0;

}


// Optimized solution to the same problem (faster):
#include<iostream>

#include<cstdio>

using namespace std;



int n,k,ans=0;

int tot=0,h[100005],a[100005];

struct Edge{

    int x,next;

}e[200005];



inline void add_edge(int x,int y){

    e[++tot].x=y;

    e[tot].next=h[x],h[x]=tot;

}



int dfs(int x,int deep){

    int ret=deep;

    for(int i=h[x];i;i=e[i].next)

        ret=max(ret,dfs(e[i].x,deep+1));

    if(a[x]!=1&&ret-deep==k-1)return ans++,0;

    else return ret;

}



int main(){

    scanf("%d%d",&n,&k);

    for(int i=1;i<=n;i++)scanf("%d",a+i);

    if(a[1]!=1)ans=a[1]=1;

    for(int i=2;i<=n;i++)add_edge(a[i],i);

    dfs(1,0),printf("%d",ans);

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

#include<vector>

#include<algorithm>

using namespace std;

int n, k, a[200000], cnt; vector<int>x[200000];

int dfs(int pos) {

	int maxn = 0;

	for (int i = 0; i < x[pos].size(); i++) {

		maxn = max(maxn, dfs(x[pos][i]) + 1);

	}

	if (maxn == k - 1 && pos != 1 && a[pos] != 1) { maxn = -1; cnt++; }

	return maxn;

}

int main() {

	cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> a[i]; if (i >= 2) { x[a[i]].push_back(i); } }

	if (a[1] != 1) { a[1] = 1; cnt++; }

	dfs(1);

	cout << cnt << endl;

	return 0;

}

// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

using namespace std;

int n,k,a[1<<17],cnt;vector<int>x[1<<17];

int D(int p) {

	int M=0;

	for(int i=0;i<x[p].size();i++){

		M=max(M,D(x[p][i])+1);

	}

	if(M==k-1&&p!=1&&a[p]!=1){M=-1;cnt++;}

	return M;

}

main(){

	cin>>n>>k;for(int i=1;i<=n;i++){cin>>a[i];if(i>=2){x[a[i]].push_back(i);}}

	if(a[1]!=1){a[1]=1;cnt++;}

	D(1);

	cout<<cnt<<endl;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>



using namespace std;



const int INF = 1 << 30;



int N, K, A[100000];

int succ[36][101010];

vector< pair< int, int > > d;

vector< int > g[100000];

int dist[100000];



void dfs(int idx)

{

  for(int to : g[idx]) {

    d[to] = {d[idx].first + 1, to};

    dfs(to);

  }

}



bool cut(int idx)

{

  int now = idx;



  if(dist[now] != -1) return (false);

  for(int i = 0; i < K - 1; i++) now = A[now];



  queue< int > que;

  dist[now] = 0;

  que.emplace(now);

  while(!que.empty()) {

    int p = que.front();

    que.pop();

    for(int to : g[p]) {

      if(dist[to] == -1) {

        dist[to] = dist[p] + 1;

        if(dist[to] < K - 1) que.push(to);

      }

    }

  }

  return (true);

}



void Calculation()

{

  cin >> N >> K;



  for(int i = 0; i < N; i++) {

    cin >> A[i];

    --A[i];

    succ[0][i] = A[i];

  }



  int ret = 0;

  if(A[0] != 0) {

    ++ret;

    A[0] = 0;

  }



  d.assign(N, {0, 0});

  for(int i = 1; i < N; i++) g[A[i]].push_back(i);

  dfs(0);

  sort(begin(d), end(d));

  reverse(begin(d), end(d));

  memset(dist, -1, sizeof(dist));

  for(int i = 0; i < N; i++) {

    if(d[i].first <= K) break;

    ret += cut(d[i].second);

  }

  cout << ret << endl;

}



int main()

{

  Calculation();

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>



using namespace std;



int N, K, A[100000];

vector< int > g[100000];

int depth[100000];

int rec(int idx)

{

  int ret = 0;

  for(int to : g[idx]) {

    ret += rec(to);

    depth[idx] = max(depth[idx], depth[to] + 1);

  }

  if(depth[idx] == K - 1 && idx != 0 && A[idx] != 0) {

    depth[idx] = -1;

    ++ret;

  }

  return(ret);

}



void Calculation()

{

  cin >> N >> K;

  for(int i = 0; i < N; i++) {

    cin >> A[i];

    --A[i];

    if(i) g[A[i]].push_back(i);

  }

  cout << rec(0) + (A[0] != 0) << endl;

}



int main()

{

  cin.tie(0);

  ios::sync_with_stdio(false);

  Calculation();

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

#include<vector>

using namespace std;

vector<int>G[1<<17];

int n,K,a[1<<17];

vector<int>dk[1<<17];

int pk[1<<17];

void dfs(int u,int k)

{

	pk[k]=u;

	if(k>=K)

	{

		dk[pk[k-K]].push_back(u);

	}

	for(int i=0;i<G[u].size();i++)dfs(G[u][i],k+1);

}

int dp[1<<17];

int get(int,int);

int bfs(int u)

{

	dp[u]=1e9;

	int s=0;

	for(int i=0;i<G[u].size();i++)

	{

		s+=bfs(G[u][i]);

	}

	//dp[u]=min(dp[u],s+(int)G[u].size());

	s=0;

	for(int i=0;i<dk[u].size();i++)s+=dp[dk[u][i]];

	dp[u]=min(dp[u],s+(int)dk[u].size());

	if(1L*n*K<1e8)dp[u]=get(u,K-1);

	return dp[u];

}

int get(int u,int k)

{

	if(k<0)return dp[u]+1;

	int ans=dp[u]+1;

	int sum=0;

	for(int i=0;i<G[u].size();i++)

	{

		sum+=get(G[u][i],k-1);

	}

	return min(ans,sum);

}

main()

{

	cin>>n>>K;

	for(int i=0;i<n;i++)

	{

		cin>>a[i];

		a[i]--;

		if(i)G[a[i]].push_back(i);

	}

	dfs(0,0);

	bfs(0);

	cout<<get(0,K)+!!a[0]<<endl;

}

// Optimized solution to the same problem (faster):
#include<iostream>

#include<vector>

using namespace std;

vector<int>G[1<<17];

int n,K,a[1<<17];

int cnt;

int dfs(int u,int p)

{

	int d=0;

	for(int i=0;i<G[u].size();i++)

	{

		d=max(d,dfs(G[u][i],u));

	}

	if(d==K-1&&p!=0)cnt++,d=-1;

	return d+1;

}

main()

{

	cin>>n>>K;

	for(int i=0;i<n;i++)

	{

		cin>>a[i];

		a[i]--;

		if(i)G[a[i]].push_back(i);

	}

	dfs(0,0);

	cout<<cnt+!!a[0]<<endl;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

#include<cstdio>

#include<vector>



using namespace std;



int n,k,f[100001],ans,d[100001];

vector<int> point[100001];



void dfs(int u){

	d[u]=1;for(int v:point[u])dfs(v),d[u]=max(d[u],d[v]+1);

	if(d[u]>=k&&f[u]!=1)d[u]=0,++ans;

}



int main(){

	scanf("%d%d",&n,&k);for(int i=1;i<=n;++i){scanf("%d",f+i);if(i!=1)point[f[i]].push_back(i);}

	for(int v:point[1])dfs(v);

	printf("%d",ans+(f[1]!=1));

}

// Optimized solution to the same problem (faster):
#include<iostream>

#include<cstdio>

#include<cstdlib>

#include<cstring>

#include<cmath>

#include<algorithm>

#include<vector>

using namespace std;

#define MAX 100100

#define ll long long

inline int read()

{

	int x=0;bool t=false;char ch=getchar();

	while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();

	if(ch=='-')t=true,ch=getchar();

	while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();

	return t?-x:x;

}

struct Line{int v,next;}e[MAX];

int h[MAX],cnt=1;

inline void Add(int u,int v){e[cnt]=(Line){v,h[u]};h[u]=cnt++;}

int n,K,ans,fa[MAX],dep[MAX];

int dfs(int u,int dep)

{

	int ret=dep;

	for(int i=h[u];i;i=e[i].next)

		ret=max(ret,dfs(e[i].v,dep+1));

	if(fa[u]!=1&&ret-dep==K-1){++ans;return 0;}

	return ret;

}

int main()

{

	n=read();K=read();

	for(int i=1;i<=n;++i)fa[i]=read();

	for(int i=2;i<=n;++i)Add(fa[i],i);

	if(fa[1]!=1)++ans;fa[1]=1;

	dfs(1,0);

	printf("%d\n",ans);

	return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;



int n, k;

int a;

vector<int> g[100001];



int ans = 0;



int dfs(int v = 0, int t = 0)

{

  int d = 0;

  for (int i = 0; i < g[v].size(); i++){

    d = max(d, dfs(g[v][i], v) + 1);

  }



  if (d == k - 1 && t != 0){

    d = -1;

    ans++;

  }



  return d;

}



int main()

{

  cin >> n >> k;

  cin >> a;



  for (int i = 1; i < n; i++){

    int v;

    cin >> v;

    g[v - 1].push_back(i);

  }



  dfs();

  cout << (a != 1) + ans << endl;

}




// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;



int n, k;

vector<int> a;



int ans;

vector<vector<int>> G;



int dfs(int v = 0) {

  int res = 0;

  for (auto &to : G[v]) {

    res = max(res, dfs(to));

  }



  if (a[v] && res == k - 1) {

    res = -1;

    ans++;

  }

  return res + 1;

}



int main() {

  cin >> n >> k;



  a.resize(n);

  for (auto &i : a) cin >> i, i--;



  if (a[0] != 0) {

    a[0] = 0;

    ans++;

  }



  G.resize(n);

  for (int i = 1; i < n; ++i) {

    G[a[i]].push_back(i);

  }



  dfs();



  cout << ans << endl;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<cstdio>

#include<vector>

using namespace std;

const int MAXN=100005;



int N,K;

int nxt[MAXN],dep[MAXN];

vector<int> radj[MAXN];

int ans=0;



void dfs(int u)

{

	for(auto v:radj[u])

	{

		dfs(v);

		dep[u]=max(dep[v]+1,dep[u]);

	}

	if(dep[u]==K-1&&nxt[u]!=1)

	{

		ans++;

		dep[u]=-1;

	}

}



int main()

{

	scanf("%d%d",&N,&K);

	for(int i=1;i<=N;i++)

	{

		scanf("%d",&nxt[i]);

		if(i==1&&nxt[i]!=1)

		{

			nxt[i]=1;

			ans++;

			continue;

		}

		if(i!=1)

			radj[nxt[i]].push_back(i);

	}

	dfs(1);

	printf("%d\n",ans);

	

	return 0;

}


// Optimized solution to the same problem (faster):
#include <map>

#include <list>

#include <cmath>

#include <cstdio>

#include <cstring>

#include <iostream>

#include <algorithm>

#define LL long long

using namespace std;



int to[100005],nxt[100005],hed[100005] = {0},ec = 0;

void add_edge(int f,int t){

	ec ++; to[ec] = t; nxt[ec] = hed[f]; hed[f] = ec;

}



int n,k,a[100005],ans = 0;



int dfs(int v,int dep){

	int tmp = dep;

	for(int i = hed[v];i;i = nxt[i]){

		tmp = max(tmp,dfs(to[i],dep + 1));

	}

	if(a[v] != 1 && tmp - dep == k - 1){

		a[v] = 1;

		ans ++;

		return -1;

	}

	return tmp;

}



int main(){

	scanf("%d %d",&n,&k);

	scanf("%d",&a[1]); if(a[1] != 1) ans ++; a[1] = 1;

	for(int i = 2;i <= n;i ++){

		scanf("%d",&a[i]);

		add_edge(a[i],i);

	}

	dfs(1,0);

	printf("%d\n",ans);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;



const int N = 1e5 + 10;



vector <int> tr[N];

int a[N];

int ans, k;



int dfs(int x, int fa){

	int rec = -1;

	for (int i = 0; i < tr[x].size(); i++){

		if (tr[x][i] == fa) continue;

		rec = max(rec, dfs(tr[x][i], x));

	}

	rec++;

	if (x != 1 && rec == k - 1 && fa != 1){

		ans++;

		return -1;

	}

	return rec;

}



int main(){

	int n;

	scanf("%d %d",&n,&k);

	for (int i = 1; i <= n; i++) scanf("%d",&a[i]);

	if (a[1] != 1) ans = 1;

	else ans = 0;

	for (int i = 2; i <= n; i++){

		tr[i].push_back(a[i]);

		tr[a[i]].push_back(i);

	}

	dfs(1, 0);

	printf("%d\n", ans);

	return 0;

}

// Optimized solution to the same problem (faster):
#include <map>

#include <list>

#include <cmath>

#include <cstdio>

#include <cstring>

#include <iostream>

#include <algorithm>

#define LL long long

using namespace std;



int to[100005],nxt[100005],hed[100005] = {0},ec = 0;

void add_edge(int f,int t){

	ec ++; to[ec] = t; nxt[ec] = hed[f]; hed[f] = ec;

}



int n,k,a[100005],ans = 0;



int dfs(int v,int dep){

	int tmp = dep;

	for(int i = hed[v];i;i = nxt[i]){

		tmp = max(tmp,dfs(to[i],dep + 1));

	}

	if(a[v] != 1 && tmp - dep == k - 1){

		a[v] = 1;

		ans ++;

		return -1;

	}

	return tmp;

}



int main(){

	scanf("%d %d",&n,&k);

	scanf("%d",&a[1]); if(a[1] != 1) ans ++; a[1] = 1;

	for(int i = 2;i <= n;i ++){

		scanf("%d",&a[i]);

		add_edge(a[i],i);

	}

	dfs(1,0);

	printf("%d\n",ans);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#define _CRT_SECURE_NO_WARNINGS

#include <vector>

#include <iostream>

#include <string.h>

#include <set>

#include <algorithm>

#include <cmath>

#include <queue>

using namespace std;



typedef double LD;

typedef long long LL;

typedef pair<int, int> PII;

#define MP make_pair

#define PB push_back

#define FOR(i,a,b) for(int i = (a); i < (b); ++i)

#define RFOR(i,b,a) for(int i = (b) - 1; i >= (a); --i)

#define REP(i, t) FOR(i,0,t)

#define ALL(a) a.begin(), a.end()

#define SZ(a) (int)((a).size())



const LL MOD = 1000000007;

const int INF = 1e9;

const LL LINF = 1LL * INF * INF;

const int MAXN = 100007;

const LD EPS = 1e-7;



int A[MAXN], D[MAXN], Dep[MAXN], n, k;

vector <int> G[MAXN], G1[MAXN];

queue <int> Q;

bool used[MAXN];



void dfs(int v)

{

	used[v] = true;

	REP(i, SZ(G1[v]))

	{

		int to = G1[v][i];

		if (!used[to])

		{

			dfs(to);

			Dep[v] = max(Dep[v], (Dep[to] + 1) % k);

		}

	}

}



int main()

{

	ios_base::sync_with_stdio(0);

	//freopen("In.txt", "r", stdin);

	int ans = 0, e = 0;

	cin >> n >> k;

	REP(i, n)

	{

		cin >> A[i];

		--A[i];

	}

	if (A[0] != 0)

	{

		A[0] = 0;

		ans = 1;

	}

	FOR(i, 1, n)

		G[A[i]].push_back(i);

	used[0] = true;

	D[0] = 0;

	Q.push(0);

	while(!Q.empty())

	{

		int v = Q.front();

		Q.pop();

		e = max(e, D[v]);

		REP(i, SZ(G[v]))

		{

			int to = G[v][i];

			if (!used[to])

			{

				used[to] = true;

				D[to] = D[v] + 1;

				G1[v].push_back(to);

				Q.push(to);

			}

		}

	}

	memset(used, 0, MAXN);

	dfs(0);

	REP(i, n)

	{

		if (D[i] > 1 && Dep[i] == k - 1)

			++ans;

	}

	cout << ans;

	//cout << endl; system("pause");

	return 0;

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;

struct edge

{

	int v,p;

}e[100010];

int n,k,a[100010],top,head[100010],cnt;

void addedge(int u,int v)

{

	e[++top]=(edge){v,head[u]};

	head[u]=top;	

}

int dfs(int u,int dep)

{

	int ret=dep;

	for(int i=head[u];i;i=e[i].p)

	{

		ret=max(ret,dfs(e[i].v,dep+1));

	}

	if(a[u]!=1&&ret-dep==k-1)

	{

		cnt++;

		return 0;

	}

	return ret;

}

int main()

{

	scanf("%d%d",&n,&k);

	for(int i=1;i<=n;i++)

	{

		scanf("%d",a+i);

	}

	if(a[1]!=1)cnt++;

	a[1]=1;

	for(int i=2;i<=n;i++)

	{

		addedge(a[i],i);

	}

	dfs(1,0);

	cout<<cnt<<endl;

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
  #include<bits/stdc++.h>

  using namespace std;

  #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

  #define endl "\n"

  #define int long long

  #define all(v) v.begin(),v.end()

  #define rep(i,a,b) for(int i = a; i <=b; i++)

  #define f first

  #define s second

  #define PB push_back

  #define MP make_pair

  #pragma comment(linker, "/STACK:2000000")

  #pragma comment(linker, "/HEAP:2000000")

  #define db long double

  #define trace1(x)                cerr<<#x<<": "<<x<<endl

  #define trace2(x, y)             cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl

  #define trace3(x, y, z)          cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl

  #define trace4(a, b, c, d)       cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl

  #define trace5(a, b, c, d, e)    cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl

  #define trace6(a, b, c, d, e, f) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl

  #define cases int testcases;cin>>testcases; while(testcases--)

  #define MOD 1000000007

  int deg[200005]={0};

  vector<int> adj[200005];

  bool vis[200005];

  vector<int>v; 

  int32_t main()

  {        

    IOS



    int a,b;

    cin>>a>>b;

    

    if(a==b)

    {

      cout<<"1";

    }

    else if((a+b)%2==0)

    {

      cout<<(a+b)/2;

    }

    else

    {

      cout<<"IMPOSSIBLE";

    }

    return 0;

} 

// Optimized solution to the same problem (faster):
  #include<bits/stdc++.h>

  using namespace std;

  #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

  #define endl "\n"

  #define int long long

  #define all(v) v.begin(),v.end()

  #define rep(i,a,b) for(int i = a; i <=b; i++)

  #define f first

  #define s second

  #define PB push_back

  #define MP make_pair

  #pragma comment(linker, "/STACK:2000000")

  #pragma comment(linker, "/HEAP:2000000")

  #define db long double

  #define trace1(x)                cerr<<#x<<": "<<x<<endl

  #define trace2(x, y)             cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl

  #define trace3(x, y, z)          cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl

  #define trace4(a, b, c, d)       cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl

  #define trace5(a, b, c, d, e)    cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl

  #define trace6(a, b, c, d, e, f) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl

  #define cases int testcases;cin>>testcases; while(testcases--)

  #define MOD 1000000007

  int32_t main()

  {        

    IOS

    int a,b;

    cin>>a>>b;

    if(a==b)

    {

      cout<<"1";

    }

    else if((a+b)%2==0)

    {

      cout<<(a+b)/2;

    }

    else

    {

      cout<<"IMPOSSIBLE";

    }

    return 0;

} 

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

int main ()





{



    long long int a,b,i,j,k,c,temp;



    cin>>a>>b;

    if(a>b)



    {

        temp=a;

        a=b;

        b=temp;

    }







    for(i=a; i<=b; i++)



    {





        if(abs(a-i)==abs(b-i))

        {



            c=1;

            break;

        }

        else

            c=0;



    }



    if(c==0)

        cout<<"IMPOSSIBLE"<<endl;

    else if(c==1)

        cout<<i<<endl;











    return 0;

}


// Optimized solution to the same problem (faster):
#include<stdio.h>

#include<math.h>

int main()

{

    int a,b,k;

    int c=0;

    scanf("%d%d",&a,&b);



 if((a+b)%2==0)



      {

           k=(a+b)/2;

          printf("%d",k);

      }

 else

       {

           printf("IMPOSSIBLE");

       }





return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

using namespace std;

int main()

{

 int a,b,c,d;

 cin >> a >> b;

 c=a+b;

 d=c/2;

 if(c%2!=0){

    cout << "IMPOSSIBLE";

 }

 else {

    cout << d;

 }

 return 0;

}


// Optimized solution to the same problem (faster):
#include<stdio.h>

#include<math.h>

int main()

{

    int a,b,k;

    int c=0;

    scanf("%d%d",&a,&b);



 if((a+b)%2==0)



      {

           k=(a+b)/2;

          printf("%d",k);

      }

 else

       {

           printf("IMPOSSIBLE");

       }





return 0;

}


// Fastest known solution for this problem: