// BEGIN CUT HERE

// END CUT HERE
#line 5 "PublicTransitHard.cpp"
#include <iostream>
#include <sstream>
#include <set>
#include <map>
#include <bitset>
#include <algorithm>
#include <utility>
#include <numeric>
#include <functional>
#include <vector>
#include <queue>
#include <stack>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <cmath>
#include <cassert>
#include <climits>

#define forn(i, n) for (int i = 0; i < int(n); i++)
#define forl(i, n) for (int i = 1; i <= int(n); i++)
#define ford(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define fore(i, l, r) for (int i = int(l); i <= int(r); i++)
#define correct(x, y, n, m) (0 <= (x) && (x) < (n) && 0 <= (y) && (y) < (m))
#define all(a) (a).begin(), (a).end()
#define sz(a) int((a).size())
#define pb(a) push_back(a)
#define mp(x, y) make_pair((x), (y))
#define ft first
#define sc second
#define x first
#define y second
#define isnan(a) false
#define isinf(a) false

using namespace std;

typedef long long li;
typedef long double ld;
typedef pair<int, int> pt;

template<typename X> inline X abs(const X& a) { return a < 0? -a: a; }
template<typename X> inline X sqr(const X& a) { return a * a; }

const int INF = int(1e9);
const li INF64 = li(1e18);
const ld EPS = 1e-9, PI = 3.1415926535897932384626433832795;

class PublicTransitHard
{
	public:
	int countValidTeleporters(int N, vector <int> edges, int X);
};

struct value {
	pt r[3], d[2];
	value() {
		forn(i, 2) r[i] = d[i] = mp(0, -1);
		r[2] = mp(-1, -1);
	}
	void addr(pt rv) {
		if (r[0] < rv) r[2] = r[1], r[1] = r[0], r[0] = rv;
		else if (r[1] < rv) r[2] = r[1], r[1] = rv;
		else if (r[2] < rv) r[2] = rv;
	}
	void addd(pt rd) {
		if (d[0] < rd) d[1] = d[0], d[0] = rd;
		else if (d[1] < rd) d[1] = rd;
	}
	int getr(int i1 = -2, int i2 = -2) {
		forn(i, 3) if (r[i].y != i1 && r[i].y != i2) return r[i].x;
		throw;
	}
	int getd(int i1 = -2, int i2 = -2) {
		forn(i, 2) if (d[i].y != i1 && d[i].y != i2) return d[i].x;
		return 0;
	}
	int getdbyr(int i1) {
		int ans = 0, c = 2;
		forn(i, 3) if (r[i].y != i1 && c > 0) ans += r[i].x, c--;
		return ans;
	}
	int getid(int val) {
		forn(i, 3) if (r[i].x == val) return i;
		throw;
	}
};

const int N = 2000 + 3;

int n, x, ans;
vector<int> g[N];
value z[N][N];

void solve(int v, int p) {
	value& ans = z[v][p];
	if (ans.r[2].x != -1) return;
	ans.r[2] = mp(0, -1);

	forn(i, sz(g[v])) {
		int to = g[v][i];
		if (to == p) continue;
		solve(to, v);
		value& next = z[to][v];
		ans.addd(mp(ans.r[0].x + next.r[0].x + 1, v));
		ans.addr(mp(next.r[0].x + 1, to));
		ans.addd(mp(next.d[0].x, to));
	}
}

int w[N][N][3];

void dfs1(int rt, int j, int v, int p, int maxl, int d) {
	int d1 = z[rt][n].r[j].x;
	int d2 = z[v][p].r[0].x;
	if (p == n && d1 > x) maxl = -1;

	int cmaxl = maxl;
	if (p != n && d1 + d2 + d > x) cmaxl = min(cmaxl, x + d - d1 - d2);
	w[rt][v][j] = cmaxl;

	forn(i, sz(g[v])) {
		int to = g[v][i];
		if (to == p) continue;
		int nmaxl = maxl;
		d2 = z[v][p].getr(to);
		if (p != n && d1 + d2 + d > x) nmaxl = min(nmaxl, x + d - d1 - d2);
		dfs1(rt, j, to, v, nmaxl, d + 1);
	}
}

void dfs2(int rt, int v, int p, int d, int maxd, int maxl) {
	int j = z[v][n].getid(z[v][n].getr(p));
	int cmaxl = min(w[v][rt][j], maxl);
	int cmaxd = max(maxd, z[v][p].d[0].x);
	if (d <= cmaxl && cmaxd <= x) {
		ans += 1 + (d == 0);
	}

	forn(i, sz(g[v])) {
		int to = g[v][i];
		if (to == p) continue;
		int nmaxd = max(maxd, z[v][p].getd(v, to));
		nmaxd = max(nmaxd, z[v][p].getdbyr(to));
		j = z[v][n].getid(z[v][to].getr(p));
		int nmaxl = min(w[v][rt][j], maxl);
		dfs2(rt, to, v, d + 1, nmaxd, nmaxl);
	}
}

int PublicTransitHard::countValidTeleporters(int _n, vector <int> ed, int _x) {
	n = _n, x = _x, ans = 0;
	forn(i, n) g[i].clear();
	forn(i, sz(ed)) {
		g[i + 1].pb(ed[i]);
		g[ed[i]].pb(i + 1);
	}
	forn(i, n + 1) forn(j, n + 1) z[i][j] = value();

	forn(i, n) forn(j, sz(g[i])) solve(g[i][j], i);
	forn(i, n) solve(i, n);

	forn(i, n) forn(j, 3) dfs1(i, j, i, n, n - 1, 0);

	forn(i, n) dfs2(i, i, n, 0, 0, n - 1);

	assert(ans % 2 == 0);
	ans >>= 1;
	return ans;
}

// BEGIN CUT HERE
namespace moj_harness {
	int run_test_case(int);
	void run_test(int casenum = -1, bool quiet = false) {
		if (casenum != -1) {
			if (run_test_case(casenum) == -1 && !quiet) {
				cerr << "Illegal input! Test case " << casenum << " does not exist." << endl;
			}
			return;
		}
		
		int correct = 0, total = 0;
		for (int i=0;; ++i) {
			int x = run_test_case(i);
			if (x == -1) {
				if (i >= 100) break;
				continue;
			}
			correct += x;
			++total;
		}
		
		if (total == 0) {
			cerr << "No test cases run." << endl;
		} else if (correct < total) {
			cerr << "Some cases FAILED (passed " << correct << " of " << total << ")." << endl;
		} else {
			cerr << "All " << total << " tests passed!" << endl;
		}
	}
	
	int verify_case(int casenum, const int &expected, const int &received, clock_t elapsed) { 
		cerr << "Example " << casenum << "... "; 
		
		string verdict;
		vector<string> info;
		char buf[100];
		
		if (elapsed > CLOCKS_PER_SEC / 200) {
			sprintf(buf, "time %.2fs", elapsed * (1.0/CLOCKS_PER_SEC));
			info.push_back(buf);
		}
		
		if (expected == received) {
			verdict = "PASSED";
		} else {
			verdict = "FAILED";
		}
		
		cerr << verdict;
		if (!info.empty()) {
			cerr << " (";
			for (int i=0; i<(int)info.size(); ++i) {
				if (i > 0) cerr << ", ";
				cerr << info[i];
			}
			cerr << ")";
		}
		cerr << endl;
		
		if (verdict == "FAILED") {
			cerr << "    Expected: " << expected << endl; 
			cerr << "    Received: " << received << endl; 
		}
		
		return verdict == "PASSED";
	}

	int run_test_case(int casenum) {
		switch (casenum) {
		case 0: {
			int N                     = 4;
			int edges[]               = {0, 1, 2};
			int X                     = 1;
			int expected__            = 1;

			clock_t start__           = clock();
			int received__            = PublicTransitHard().countValidTeleporters(N, vector <int>(edges, edges + (sizeof edges / sizeof edges[0])), X);
			return verify_case(casenum, expected__, received__, clock()-start__);
		}
		case 1: {
			int N                     = 3;
			int edges[]               = {0, 0};
			int X                     = 2;
			int expected__            = 6;

			clock_t start__           = clock();
			int received__            = PublicTransitHard().countValidTeleporters(N, vector <int>(edges, edges + (sizeof edges / sizeof edges[0])), X);
			return verify_case(casenum, expected__, received__, clock()-start__);
		}
		case 2: {
			int N                     = 6;
			int edges[]               = {0, 0, 0, 1, 1};
			int X                     = 2;
			int expected__            = 1;

			clock_t start__           = clock();
			int received__            = PublicTransitHard().countValidTeleporters(N, vector <int>(edges, edges + (sizeof edges / sizeof edges[0])), X);
			return verify_case(casenum, expected__, received__, clock()-start__);
		}
		case 3: {
			int N                     = 7;
			int edges[]               = {0, 1, 0, 1, 2, 4};
			int X                     = 3;
			int expected__            = 0;

			clock_t start__           = clock();
			int received__            = PublicTransitHard().countValidTeleporters(N, vector <int>(edges, edges + (sizeof edges / sizeof edges[0])), X);
			return verify_case(casenum, expected__, received__, clock()-start__);
		}
		case 4: {
			int N                     = 16;
			int edges[]               = {0, 1, 0, 2, 0, 0, 4, 5, 8, 9, 10, 11, 8, 4, 6};
			int X                     = 7;
			int expected__            = 31;

			clock_t start__           = clock();
			int received__            = PublicTransitHard().countValidTeleporters(N, vector <int>(edges, edges + (sizeof edges / sizeof edges[0])), X);
			return verify_case(casenum, expected__, received__, clock()-start__);
		}
		case 5: {
			int N                     = 56;
			int edges[]               = {0, 1, 1, 3, 1, 5, 1, 0, 8, 8, 10, 10, 12, 10, 10, 8, 16, 16, 18, 19, 19, 21, 19, 19, 24, 25, 25, 27, 18, 0, 30, 30, 30, 33, 34, 34, 34, 30, 38, 39, 39, 38, 42, 43, 0, 45, 45, 45, 48, 45, 45, 51, 45, 53, 54};
			int X                     = 12;
			int expected__            = 610;

			clock_t start__           = clock();
			int received__            = PublicTransitHard().countValidTeleporters(N, vector <int>(edges, edges + (sizeof edges / sizeof edges[0])), X);
			return verify_case(casenum, expected__, received__, clock()-start__);
		}

		// custom cases

/*      case 6: {
			int N                     = ;
			int edges[]               = ;
			int X                     = ;
			int expected__            = ;

			clock_t start__           = clock();
			int received__            = PublicTransitHard().countValidTeleporters(N, vector <int>(edges, edges + (sizeof edges / sizeof edges[0])), X);
			return verify_case(casenum, expected__, received__, clock()-start__);
		}*/
/*      case 7: {
			int N                     = ;
			int edges[]               = ;
			int X                     = ;
			int expected__            = ;

			clock_t start__           = clock();
			int received__            = PublicTransitHard().countValidTeleporters(N, vector <int>(edges, edges + (sizeof edges / sizeof edges[0])), X);
			return verify_case(casenum, expected__, received__, clock()-start__);
		}*/
/*      case 8: {
			int N                     = ;
			int edges[]               = ;
			int X                     = ;
			int expected__            = ;

			clock_t start__           = clock();
			int received__            = PublicTransitHard().countValidTeleporters(N, vector <int>(edges, edges + (sizeof edges / sizeof edges[0])), X);
			return verify_case(casenum, expected__, received__, clock()-start__);
		}*/
		default:
			return -1;
		}
	}
}
 

int main(int argc, char *argv[]) {
	if (argc == 1) {
		moj_harness::run_test();
	} else {
		for (int i=1; i<argc; ++i)
			moj_harness::run_test(atoi(argv[i]));
	}
}
// END CUT HERE

