stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
//2018.9.29 by ljz

#include<bits/stdc++.h>

using namespace std;

#define res register int

#define LL long long

#define inf 0x3f3f3f3f

#define eps 1e-15

inline int read(){

    res s=0;

    bool w=0;

    char ch=getchar();

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

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

    return w?-s:s;

}

inline void _swap(res &x,res &y){

    x^=y^=x^=y;

}

inline int _abs(const res &x){

    return x>0?x:-x;

}

inline int _max(const res &x,const res &y){

    return x>y?x:y;

}

inline int _min(const res &x,const res &y){

    return x<y?x:y;

}

const int N=1e5+10;

namespace MAIN{

    int n,k;

    int fa[N];

    struct E{

        int next,to;

        E() {}

        E(res next,res to):next(next),to(to) {}

    }edge[N];

    int head[N],cnt;

    inline void addedge(const res &u,const res &v){

        edge[++cnt]=E(head[u],v),head[u]=cnt;

    }

    int ans;

    int dfs(const res &x,const res &depx){

        res ret=depx;

        for(res i=head[x];~i;i=edge[i].next){

            res tox=edge[i].to;

            ret=_max(ret,dfs(tox,depx+1));

        }

        if(fa[x]!=1&&ret-depx==k-1){ans++;return 0;}

        return ret;

    }

    inline void MAIN(){

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

        n=read(),k=read();

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

        for(res i=2;i<=n;i++)addedge(fa[i],i);

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

        fa[1]=1;

        dfs(1,0);

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

    }

}

int main(){

    MAIN::MAIN();

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.4036756731667892x):
#include <bits/stdc++.h>

using namespace std;

const int maxn=100000+10;

int n,k,a[maxn],f[maxn],head[maxn],to[maxn],nxt[maxn],tot,ans;



void add(int x,int y){

	to[++tot]=y;

	nxt[tot]=head[x];

	head[x]=tot;

}



void dfs(int x){

	for(int i=head[x],y;i;i=nxt[i]){

		y=to[i];

		dfs(y);

		f[x]=max(f[x],f[y]+1);

	}

	if(f[x]==k-1&&a[x]!=1){

		f[x]=-1;

		ans++;

	}

}



int main()

{

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

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

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

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

		add(a[i],i);

	}

	if(a[1]!=1){

		ans++;

		a[1]=1;

	}

	dfs(1);

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

	return 0;

}
// Slower C++ solution:
#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;

}


// Faster C++ solution for the same problem (measured speedup: 3.778766190301769x):
#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;

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 3.621765521896657x):
#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;

}
// Slower C++ solution:
#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;

}


// Faster C++ solution for the same problem (measured speedup: 3.5389662686746557x):
#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);

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 1.4885909596384324x):
#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;

}
// Slower C++ solution:
#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();

}

// Faster C++ solution for the same problem (measured speedup: 1.1103736324079054x):
#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();

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 1.909046041169727x):
#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;

}
// Slower C++ solution:
#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));

}

// Faster C++ solution for the same problem (measured speedup: 3.568707647953654x):
#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;

}
// Slower C++ solution:
#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));

}

// Faster C++ solution for the same problem (measured speedup: 3.5568803525570893x):
#include <bits/stdc++.h>



using namespace std;



#ifdef Lin1043

    #define DEBUG(msg, ...) printf("(%s #%d) " msg, __FUNCTION__, __LINE__, __VA_ARGS__)

    #define massert(...) assert(...)

#else

    #define DEBUG(...)

    #define massert(...)

#endif



#define rep(i , l , r) for(int i = (l) , ___ = (r) ; i <= ___ ; ++i )

#define per(i , r , l) for(int i = (r) , ___ = (l) ; i >= ___ ; --i )



struct iopener

{

    iopener()

    {

#ifdef Lin1043

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

#endif

    }

} iop;



typedef long long LL;



template<typename T>inline bool chkmin(T &x , const T &y) { return x > y ? (x = y , 1) : 0; }

template<typename T>inline bool chkmax(T &x , const T &y) { return x < y ? (x = y , 1) : 0; }



template<typename T>inline T read(T &f)

{

    f = 0; int x = 1 ; char c = getchar();

    while(!isdigit(c)) x = (c == '-' ? -1 : 1) , c = getchar();

    while(isdigit(c)) (f *= 10) += c & 15 , c = getchar();

    return f = x * f;

}



const int N = 100000 + 5;



int n , m , u , v , Ans , A[N] , dep[N];



struct Edge

{

    int to ; Edge *nxt;

} E[N << 1] , *pre[N];



void addedge(int u , int v)

{

    static int tot = 0;

    E[++tot] = (Edge){v , pre[u]} , pre[u] = &E[tot];

    E[++tot] = (Edge){u , pre[v]} , pre[v] = &E[tot];

}



void dfs(int u , int fa)

{

    dep[u] = 1;

    for(Edge *i = pre[u] ; i ; i = i->nxt)

    {

        int v = i->to;

        if(v == fa) continue ;

        dfs(v , u); //  , u);

        chkmax(dep[u] , dep[v] + 1);

    }

    if(dep[u] >= m && A[u] != 1)

    {

        Ans++ , dep[u] = 0;

    }

}



int main()

{

    read(n) , read(m);

    rep(i , 1 , n)

    {

        read(v) , u = i ;

        A[i] = v;

        if(i != 1) addedge(v , u);

    }

    if(A[1] != 1) Ans++ , A[1] = 1;

    dfs(1 , 0);

    cout << Ans << endl;

    return 0;

}
// Slower C++ solution:
#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));

}

// Faster C++ solution for the same problem (measured speedup: 3.5577232645771066x):
#include <iostream>

#include <cstdio>

#include <cstring>

#include <algorithm>

#include <stack>

using namespace std;

struct edge{

	int pre,to;

	#define pre(i) ed[i].pre

	#define to(i) ed[i].to

}ed[100010];

int cnt,head[100010];

void merge(int a,int b){

	++cnt;

	to(cnt)=b;

	pre(cnt)=head[a];

	head[a]=cnt;

}

int n,k,ans;

int a[100010],seg[100010];

void dfs(int x,int pre){

	for(int t=head[x];t;t=pre(t)){

		int l=to(t);

		dfs(l,x);

		seg[x]=max(seg[x],seg[l]+1);

	}

	if(seg[x]>=k-1&&x!=1&&pre!=1){

		ans++;

		seg[x]=-1;

	}

}

int main(){

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

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

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

		if(t!=1)merge(a[t],t);

	}

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

	dfs(1,0);

	printf("%d",ans);

}
// Slower C++ solution:
#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;

}




// Faster C++ solution for the same problem (measured speedup: 3.5223011806227666x):
#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;

}
// Slower C++ solution:
#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;

}


// Faster C++ solution for the same problem (measured speedup: 3.4085754623639257x):
#include <bits/stdc++.h>

using namespace std;

const int N=100000+10;

struct EDGEE

{

	int nxt, to;

}edge[N<<1];

int head[N], tot = 0;

void add(int x,int y)

{

	edge[tot]=(EDGEE){head[x], y};

	head[x]=tot++;

}

int n,m;

int ans=0;

int dfs(int x, int fa)

{

	int ret=0;

	for(int i=head[x];i;i=edge[i].nxt)

		if(edge[i].to!=fa)

		{

			int tmp=dfs(edge[i].to,x);

			if(tmp==m-1) ans += (x != 1);

			else ret=max(ret,tmp+1);

		}

	return ret;

}

int main()

{

	ios::sync_with_stdio(false);

	cin>>n>>m;

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

	{

		int x;

		cin>>x;

		if(i==1) ans+=(x!=1);

		else add(i,x), add(x,i);

	}

	dfs(1,0);

	cout<<ans<<endl;

	return 0;

}
// Slower C++ solution:
#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;

}


// Faster C++ solution for the same problem (measured speedup: 3.4876278157539167x):
#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;

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 3.5630494569861586x):
#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;

}