#include <bits/stdc++.h>
using namespace std;
mt19937 timmy_loves_gambling(73);
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define readv(v) do { for(auto &i: v) cin >> i; } while(0)
#define writev(v) do { for(auto i: v) cout << i << " "; } while(0)
#define _ << " " <<
#define lf "\n"
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<long long, long long>
#define fi first
#define se second
template<typename... Args>
using vec = vector<Args...>;
#ifndef MARCO
bool dbg = 0;
#define infor(str, ...)
#define infof(str, ...)
#else
bool dbg = 1;
#define infor(str, ...) do { fprintf(stderr, str, ##__VA_ARGS__); } while(0)
#define infof(str, ...) do { fprintf(stderr, str "\n", ##__VA_ARGS__); } while(0)
#endif
vec<ll> P, X;
vec<vec<int>> G;
vec<priority_queue<array<ll, 3>>> pq;
ll s;
int N;
void dfs(int v) {
for(auto &u: G[v]) {
dfs(u);
}
for(auto &u: G[v]) {
while(!pq[u].empty()) {
pq[v].push({pq[u].top()[0], pq[u].top()[1], u});
pq[u].pop();
}
}
ll x = X[v];
ll need = max(0LL, -X[v]);
while(!pq[v].empty()) {
auto [nd, g, u] = pq[v].top();
nd *= -1;
if(x >= 0) {
if(nd > x && nd > need) break;
pq[v].pop();
x += g;
}
else {
need = max(need, nd - x);
pq[v].pop();
x += g;
}
if(!pq[u].empty()) {
pq[v].push({pq[u].top()[0], pq[u].top()[1], u});
pq[u].pop();
}
}
if(x <= 0) return;
pq[v].push({-need, x, v});
int bigc = v;
for(auto &u: G[v]) {
if(pq[u].size() > pq[bigc].size()) bigc = u;
}
if(bigc != v) swap(pq[bigc], pq[v]);
for(auto &u: G[v]) {
while(!pq[u].empty()) {
pq[v].emplace(pq[u].top());
pq[u].pop();
}
}
#ifdef MARCO
priority_queue<array<ll, 3>> pq2 = pq[v];
infof("- pq contents for %d:", v);
while(!pq2.empty()) {
auto [a, b, c] = pq2.top();
pq2.pop();
infof("[%lld %lld]", -a, b);
}
#endif
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> N;
P = X = vec<ll>(N+1);
G.resize(N+1);
pq.resize(N+1);
cin >> X[0];
for(int i=1; i<N+1; i++) {
cin >> X[i] >> P[i];
}
for(int i=1; i<=N; i++) {
G[P[i]].push_back(i);
}
dfs(0);
cout << pq[0].top()[1]-X[0] << "\n";
#ifdef MARCO
infof("- pq final contents:");
while(!pq[0].empty()) {
auto [a, b, c] = pq[0].top();
pq[0].pop();
infof("[%lld %lld]", -a, b);
}
#endif
}