#include <bits/stdc++.h>
using namespace std;
struct rev_vector {
	const int magic = 123456;
	vector<int> pk;
	int l, r;
	rev_vector(){
		pk.resize(3 * magic);
		l = magic;
		r = l;
	}
	void shift_right(){
		l--;
		pk[l] = 0;
	}
	int& operator [](int pos){
		while(pos + l <= r) pk[r++] = 0;
		return pk[pos + l];
	}
	void clear(){
		l = magic;
		r = l;
	}
};
const int maxn = 100100;
int ans, k, vis[maxn];
vector<int> e[maxn];
int dfs(int u){
	int dep = 0;
	for(int v : e[u]) {
		int mv = dfs(v);
		if(mv == k - 1 && u){
			ans++;
		} else {
			dep = max(dep, mv + 1);
		}
	}
	return dep;
}
int main(){
	int n; cin >> n >> k;
	vector<int> a(n);
	for(int i = 0; i < n; i++) cin >> a[i], a[i]--;
	if(a[0] != 0){
		a[0] = 0;
		ans++;
	}
	for(int i = 0; i < n; i++){
		if(i != a[i]){
			e[a[i]].push_back(i);
		}
	}
	dfs(0);
	cout << ans << endl;
	return 0;
}

#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <sstream>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#define rep(i, n) for(int i = 0; i < n; i ++)
#define COUNT(i, n) for(int i = 1; i <= n; i ++)
#define ITER(c) __typeof((c).begin())
#define each(c, it) for(ITER(c) it =(c).begin(); it!=(c).end(); it++)
#define ALL(c) c.begin(), c.end()
#define mp make_pair
#define pb push_back
#define MEMSET(v, h) memset((v), h, sizeof(v))
 
using namespace std; 

typedef pair<int, int> pii;
typedef long long ll;
typedef vector<int> vi;
typedef vector<string> vs;

const int INF = 1 << 24;
 
int main(void){
    for(int n, m, s; s = 0, cin >> n >> m, n; cout << s << endl){
        priority_queue<int> p;
        rep(i, n){
            int in; cin >> in;
            p.push(in);
        }
        COUNT(i, n){
            if(i % m) s += p.top();
            p.pop();
        }
    }
 
    return 0;
}