#include using namespace std; const int oo = 0x3f3f3f3f; const double eps = 1e-9; const double PI = 2.0 * acos(0.0); typedef long long ll; typedef unsigned long long ull; typedef pair pii; typedef vector vi; typedef vector vs; #define sz(c) int((c).size()) #define all(c) (c).begin(), (c).end() #define FOR(i,a,b) for (int i = (a); i < (b); i++) #define FORS(i,a,b,s) for (int i = (a); i < (b); i=i+(s)) #define FORD(i,a,b) for (int i = int(b)-1; i >= (a); i--) #define FORIT(i,c) for (__typeof__((c).begin()) i = (c).begin(); i != (c).end(); i++) int pre[1000*1000]; int nc[1000*1000]; int branch_depth(int i, int & bn){ if (i == 0) {bn = i; return 0;} if (nc[i] > 1) {bn = i; return 0;} return 1 + branch_depth(pre[i], bn); } int main(){ //cout << 1 << endl; //return 0; int N,K; cin >> N >> K; FOR(i,1,N){ cin >> pre[i]; nc[pre[i]]++; } int R = 0; FOR(i,0,N){ if (nc[i]) continue; // only do this for leafs R++; } map> leafPaths; vi next; FOR(i,1,N){ if (nc[i]) continue; // only do this for leafs int bn; int d = branch_depth(i,bn); leafPaths[bn].insert({d,i}); if (leafPaths[bn].size() == nc[bn]) next.push_back(bn); //cout << i << " " << d << " " << bn << endl; //toTake.insert({d,{i,bn}}); } while (next.size()){ vi newNext; FORIT(i,next){ // remove longest leaf path auto it = leafPaths[*i].end(); it--; pii largest = *it; leafPaths[*i].erase(it); if (*i == 0) continue; int bn; int d = 1 + branch_depth(pre[*i],bn); leafPaths[bn].insert({d + largest.first,largest.second}); if (leafPaths[bn].size() == nc[bn]) newNext.push_back(bn); } next = newNext; } set toTake; FOR(i,0,N) FORIT(j,leafPaths[i]) toTake.insert(*j); FORIT(t,toTake){ if (t->first > K){ cout << R << endl; return 0; } K-=t->first; R--; } cout << R << endl; return 0; }