#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]; vector leafPaths[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++; } 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].push_back({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){ //cout << "NEXT " << *i << endl; //FOR(k,0,N){ // cout << " " << k << " " << leafPaths[k].size() << ":"; // FOR(l,0,leafPaths[k].size()) cout << " " << leafPaths[k][l].first; // cout << endl; //} // remove longest leaf path int L = 0; FOR(j,0,leafPaths[*i].size()) if (leafPaths[*i][L].first < leafPaths[*i][j].first) L = j; pii largest = leafPaths[*i][L]; leafPaths[*i].erase(leafPaths[*i].begin() + L); if (*i == 0) continue; int bn; int d = 1 + branch_depth(pre[*i],bn); leafPaths[bn].push_back({d + largest.first,largest.second}); if (leafPaths[bn].size() == nc[bn]) newNext.push_back(bn); } next = newNext; } vector toTake; FOR(i,0,N) FORIT(j,leafPaths[i]) toTake.push_back(*j); sort(all(toTake)); FORIT(t,toTake){ if (t->first > K){ cout << R << endl; return 0; } K-=t->first; R--; } cout << R << endl; return 0; }