{$R+,Q+}
{$APPTYPE CONSOLE}

const
  NN=100000;

var
  n,m,k,i,root,min:integer;
  p,t,l,km:array[1..NN] of integer;
  res:array[1..NN] of int64;
  b,e,pr:array[1..NN] of integer;

procedure dfs(v:integer);
var r:integer;
begin
  km[v]:=ord(v<=k+1);
  res[v]:=0;
  r:=l[v];
  while r<>0 do
  begin
    dfs(e[r]);
    res[v]:=res[v]+res[e[r]];
    km[v]:=km[v]+km[e[r]];
    if km[e[r]]>0 then res[v]:=res[v]+2*(t[e[r]]-t[v]);
    r:=pr[r];
  end;
  if km[v]=k+1 then
  begin
    writeln(res[v]);
    halt;
  end;
end;

begin
  reset(input, 'alternative.in');
  rewrite(output, 'alternative.out');
  read(n,k);
  min:=maxint;
  m:=0;
  fillchar(l,sizeof(l),0);
  for i:=1 to n do
  begin
    read(p[i],t[i]);
    if p[i]=0 then root:=i else
    begin
      inc(m);
      b[m]:=p[i];
      e[m]:=i;
      pr[m]:=l[b[m]];
      l[b[m]]:=m;      
    end; 
  end;
  dfs(root);
  
end.
