#pragma comment(linker, "/STACK:60000000")
#define _CRT_SECURE_NO_WARNINGS

#include <cstdio>
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <ctime>
#include <cstring>
#include <cassert>
#include <sstream>
#include <iomanip>
#include <complex>
#include <queue>
#include <functional>

using namespace std;

#define forn(i, n) for(int i = 0; i < (int)(n); i++)
#define ford(i, n) for(int i = (int)(n) - 1; i >= 0; i--)
#define pb push_back
#define mp make_pair
#define fs first
#define sc second
#define last(a) int(a.size() - 1)
#define all(a) a.begin(), a.end()
#define seta(a,x) memset (a, x, sizeof (a))
#define I (int)
#define norm(a) sort(all(a)), a.erase(unique(all(a)), a.end())
#define num(a,x) (int)(lower_bound(all(a), x) - a.begin())

typedef long long int64;
typedef pair <int, int> pii;
typedef long double ldb;

const long double eps = 1e-7;
const int inf = (1 << 30) - 1;
const int64 inf64 = ((int64)1 << 62) - 1;
const long double pi = 3.1415926535897932384626433832795;
const string task = "alternative";

template <class T> T sqr (T x) {return x * x;}

const int nmax = 100100;

int n, k, res, root;
int p[nmax], t[nmax];
vector<int> g[nmax];
bool used[nmax];

pii go(int v){
	used[v] = 1;
	pii res (0, 0);
	if (v <= k)
		res.sc ++;
	if (g[v].size() == 0)
		return res;
	forn(i, g[v].size())
		if (!used[g[v][i]])	{
			pii now = go(g[v][i]);
			if (now.sc == 0) continue;
			res.sc += now.sc;
			res.fs += 2 * abs(t[g[v][i]] - t[v]) + now.fs;
		}
	return res;
}

void build(){
	forn(i,n){
		p[i] --;
		if (p[i] >= 0){
			g[p[i]].pb (i);
			g[i].pb(p[i]);
		}
	}
}		

void solve(){
	seta(used, 0);
	build();
	pii now = go(0);
	res = now.fs;
}

void read(){
	scanf("%d %d", &n, &k);
	forn(i, n)
		scanf("%d %d", &p[i], &t[i]);
}			

void write(){
	cout << res << endl;
}

int main ()
{
	freopen ((task + ".in").data(), "r", stdin);
	freopen ((task + ".out").data(), "w", stdout);

	read();
	solve();
	write();

	return 0;
}
