#include <stdio.h>

enum { N_MAX = 100000 };

int
main(void)
{
    freopen("alternative.in", "rt", stdin);
    freopen("alternative.out", "wt", stdout);
    int i, k, n, v, lca, root = 0, p[N_MAX], t[N_MAX], c[N_MAX], u[N_MAX];
    int r = 0;

    scanf("%d %d", &n, &k);
    if (!k) {
        printf("0\n");
        return 0;
    }
    for (i = 0; i < n; ++i) {
        scanf("%d %d", p + i, t + i);
        if (!p[i]) {
            root = i;
        }
        --p[i];
        u[i] = 0;
    }
    for (i = 0; i < k + 1; ++i) {
        v = i;
        while (!u[v] && v != root) {
            r += t[v] - t[p[v]];
            ++u[v];
            c[p[v]] = v;
            v = p[v];
        }
        ++u[v];
    }

    lca = root;
    while (u[lca] == 1) {
        lca = c[lca];
    }
    r -= t[lca];

    printf("%d\n", r * 2);

    return 0;
}
