Problem:
Score : 800 points

Problem Statement

There are N towns in Snuke Kingdom, conveniently numbered 1 through N.
Town 1 is the capital.

Each town in the kingdom has a Teleporter, a facility that instantly transports a person to another place.
The destination of the Teleporter of town i is town a_i (1≤a_i≤N).
It is guaranteed that one can get to the capital from any town by using the Teleporters some number of times.

King Snuke loves the integer K.
The selfish king wants to change the destination of the Teleporters so that the following holds:

Starting from any town, one will be at the capital after using the Teleporters exactly K times in total.

Find the minimum number of the Teleporters whose destinations need to be changed in order to satisfy the king's desire.

Constraints

2≤N≤10^5

1≤a_i≤N

One can get to the capital from any town by using the Teleporters some number of times.

1≤K≤10^9

Input

The input is given from Standard Input in the following format:

N K
a_1 a_2 ... a_N

Output

Print the minimum number of the Teleporters whose destinations need to be changed in order to satisfy King Snuke's desire.

Sample Input 1

3 1
2 3 1

Sample Output 1

2

Change the destinations of the Teleporters to a = (1,1,1).

Sample Input 2

4 2
1 1 2 2

Sample Output 2

0

There is no need to change the destinations of the Teleporters, since the king's desire is already satisfied.

Sample Input 3

8 2
4 1 2 3 1 2 3 4

Sample Output 3

3

For example, change the destinations of the Teleporters to a = (1,1,2,1,1,2,2,4).

Solution in C++14 (GCC 5.4.1) (judge verdict: Time Limit Exceeded)

#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
#include <map>
#include <iterator>
#include <functional>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <fstream>
#include <iomanip>
#include <unordered_map>
#include <unordered_set>
#include <numeric>
#include <cmath>
#include <list>
#include <sstream>
#include <tuple>
#include <complex>
#include <stdio.h>
using namespace std;
#pragma GCC optimize("O3")
#pragma GCC target("sse4")

typedef double LD;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LD, LD> PDD;
typedef pair<LL, LL> PLL;
typedef vector<int> VI;
typedef vector<LL> VLL;
typedef vector<char> VCH;
typedef vector<LD> VLD;
typedef vector<VI> VVI;
typedef vector<VLL> VVLL;
typedef vector<VCH> VVCH;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;
typedef vector<PDD> VPDD;
#define MP make_pair
#define PB push_back
#define X first
#define Y second
#define next fake_next
#define prev fake_prev
#define left fake_left
#define right fake_right

#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 LD PI = acos(-1.0);
const LL mod = 1000000007;
const LL INF = 1e9;
const LL MAXN = 100001;

VI a;
int n;
int k;
VVI g;
VI lvl;
VI ord;

int comp(int x, int y)
{
	return lvl[x] < lvl[y];
}

void dfs(int v)
{
	for (auto i : g[v])
		lvl[i] = lvl[v] + 1, dfs(i);
}

int DP[MAXN][20];

int getAncestor(int v, int x)
{
	if (x == 0)
		return v;

	if (x == 1)
		return a[v];

	RFOR(i, 20, 0)
		if ((1 << i) <= x)
			return getAncestor(DP[v][i], x - (1 << i));
}

VCH ok;

void zanuly(int v)
{
	ok[v] = 1;
	for (auto i : g[v])
		zanuly(i);
}

int main()
{
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	//freopen("In.txt", "r", stdin);

	cin >> n >> k;
	a.assign(n + 1, 0);
	FOR(i, 0, n)
		cin >> a[i + 1];

	int ans = 0;

	if (k == 1)
	{
		FOR(i, 1, n + 1)
			if (a[i] != 1)
				++ans;
		cout << ans;
		return 0;
	}

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

	g.assign(n + 1, VI());
	FOR(i, 2, n + 1)
		g[a[i]].push_back(i);

	lvl.assign(n + 1, 0);
	dfs(1);

	FOR(i, 2, n + 1)
		DP[i][0] = a[i];

	FOR(i, 2, n + 1)
		ord.push_back(i);

	sort(ALL(ord), comp);
	FOR(i, 0, SZ(ord))
	{
		FOR(j, 1, 20)
			DP[ord[i]][j] = DP[DP[ord[i]][j - 1]][j - 1];
	}

	reverse(ALL(ord));
	ok.assign(n + 1, 0);
	FOR(j, 0, SZ(ord))
	{
		int i = ord[j];
		if (lvl[i] > k && ok[i] == 0)
		{
			int anc = getAncestor(i, k - 1);
			if (ok[anc] == 0)
			{
				a[anc] = 1;
				++ans;
				zanuly(anc);
			}
		}
	}

	cout << ans;
	//cin >> n;
	return 0;
}