#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(x) x.begin(),x.end()
#define dbg(x) cout<<#x<<":"<<x<<endl
typedef long long ll;
typedef pair<ll,ll> P;
typedef pair<ll,P> PP;

vector<int> g[111111];
int p[111111];
ll ans;
ll n,k;

int dfs(int n){
  int ma=0;
  for(int i=0;i<g[n].size();i++){
    ma=max(ma,dfs(g[n][i]));
  }
  if(n==0)return 0;
  if(ma==k-1&&p[n]!=0){
    ans++;
    return 0;
  }
  return ma+1;
}

int main(){
  cin>>n>>k;

  for(int i=0;i<n;i++){
    cin>>p[i];
    p[i]--;
    if(i==0&&p[i]!=0){
	 ans++;
	 p[i]=0;
    }
    if(i){
	 g[p[i]].pb(i);
    }
  }

  dfs(0);

  cout<<ans<<endl;
  
  return 0;
}



#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
using namespace std;
typedef long long ll;
int n,m;
ll x,sum,ans;
map<ll,int>mmp;
int main()
{
    scanf("%d%d",&n,&m);
    sum=0;
    ans=0;
    for(int i=0;i<n;i++)
    {
        scanf("%lld",&x);
        sum+=x;
        ans+=mmp[sum%m];
        if(sum%m==0) ans++;
        mmp[sum%m]++;
    }
    printf("%lld\n",ans);
    return 0;
}