// 1000 пъти аз по тебе

#include <iostream>
#include <algorithm>
#define int long long
using namespace std;
const int maxn = 1e5+10;

int w[maxn], last[maxn], n;
pair < int, int > p[maxn];

void solve() {

    int ans = 0;
    for (int i = 1 ; i <= n ; ++i) {

        w[i] = w[p[i].first];
        if (p[i].second == 0) last[i] = last[p[i].first];
        else last[i] = p[i].second;
        w[i] += (p[i].second == 0 ? -last[i] : last[i]);
        ans += w[i];

    }

    cout << ans << '\n';

}

void read() {

    cin >> n;
    for (int i = 1 ; i <= n ; ++i)
        cin >> p[i].first >> p[i].second;

}

void fast_io() {

    ios_base :: sync_with_stdio(0);
    cin.tie(nullptr);
    cout.tie(nullptr);

}

signed main () {

    fast_io();
    read();
    solve();

    return 0;

}
