#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF 1e18
int n;
vector<int> p;
vector<vector<int>> adj;
vector<vector<pair<int, int>>> dp;
void dfs(int u) {
vector<pair<int, int>> c;
for (int v : adj[u]) {
dfs(v);
for (auto x : dp[v]) {
c.push_back(x);
}
}
sort(c.begin(), c.end());
int req = max(0LL, -p[u]), prof = p[u];
for (auto [cr, cp] : c) {
if (prof > 0) {
dp[u].push_back({req, prof});
req = cr;
prof = cp;
} else {
req = max(req, cr - prof);
prof += cp;
}
}
if (prof > 0) {
dp[u].push_back({req, prof});
}
}
void solve() {
cin >> n;
adj.resize(n + 1);
p.resize(n + 1); cin >> p[0];
for (int i = 1; i <= n; i++) {
int dep; cin >> p[i] >> dep;
adj[dep].push_back(i);
}
dp.resize(n + 1);
dfs(0);
int ans = 0;
for (auto [req, prof] : dp[0]) {
if (ans >= req) {
ans += prof;
}
}
cout << ans - p[0];
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}