#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, profit, cost, a;
vector<vi>adjL;
vector<bool>good;
int cnt;
void dfs(int pos) {
in[pos] = cnt++;
if(profit[pos] > 0) good[pos] = 1;
for(int adj: adjL[pos]) {
profit[adj] = profit[pos] + a[adj];
cost[adj] = min(cost[pos], profit[adj]);
dfs(adj);
good[pos] = good[adj] | good[pos];
}
ot[pos] = cnt++;
}
void solve() {
int n, 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);
good.assign(n+1, 0);
for(int i=1; i<=n; ++i) {
int p;
cin >> a[i] >> p;
adjL[p].push_back(i);
}
cnt = 0;
dfs(0);
int ans = s;
priority_queue<pi>pq;
for(int i=1; i<=n; ++i) {
if(good[i]) pq.push({cost[i], i});
}
set<int>st;
while(!pq.empty()) {
int ind = pq.top().second;
pq.pop();
if(cost[ind] + ans < 0) break;
auto it = st.lower_bound(in[ind]);
if(it == st.end() or (*it) > ot[ind]) {
ans += profit[ind];
st.insert(in[ind]);
}
}
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();
}