#include<bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using pi = pair<int,int>;
typedef long long ll;
#define debug(x) cout << #x << " = " << x << "\n";
#define vdebug(a) cout << #a << " = "; for(auto x: a) cout << x << " "; cout << "\n";
const int MOD = 1e9+7;
const int N = 1e6;
vi in, ot;
vector<ll> profit, cost, a;
vector<vi>adjL;
int cnt;
void dfs(int pos) {
in[pos] = cnt++;
for(int adj: adjL[pos]) {
profit[adj] = profit[pos] + a[adj];
cost[adj] = min(cost[pos], profit[adj]);
dfs(adj);
}
ot[pos] = cnt++;
}
void solve() {
int n;
ll s;
cin >> n >> s;
profit.assign(n+1, 0);
cost.assign(n+1, 0);
a.assign(n+1, 0);
adjL.resize(n+1);
in.resize(n+1);
ot.resize(n+1);
for(int i=1; i<=n; ++i) {
int p;
cin >> a[i] >> p;
adjL[p].push_back(i);
}
cnt = 0;
dfs(0);
ll ans = s, Ans = s;
priority_queue<pair<ll, int>>pq;
vector<pair<ll,int>>vec(n);
for(int i=1; i<=n; ++i) {
vec[i-1] = {cost[i], i};
}
sort(vec.rbegin(), vec.rend());
set<int>st;
for(int i=0; i<n; ++i) {
int j = i;
while(j<n && vec[j].first + ans >= 0) {
int tmp = vec[j].second;
pq.push({profit[tmp], tmp});
++j;
}
if(pq.empty()) break;
int ind = pq.top().second;
pq.pop();
if(profit[ind] < 0) break;
auto it = st.lower_bound(in[ind]);
if(it == st.end() or (*it) > ot[ind]) {
ans += profit[ind];
st.insert(in[ind]);
}
i = j-1;
Ans = max(Ans, ans);
}
while(!pq.empty()) {
int ind = pq.top().second;
pq.pop();
if(profit[ind] < 0) break;
auto it = st.lower_bound(in[ind]);
if(it == st.end() or (*it) > ot[ind]) {
ans += profit[ind];
st.insert(in[ind]);
}
Ans = max(ans, Ans);
}
cout << Ans-s << endl;
}
int main(){
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int tc = 1;
while(tc--) solve();
}