{$apptype console}

const
  NN=100000;

var
  i,k,n,v,lca,root:integer;
  r:int64;
  p,t,c:array[1..NN] of integer;
  u:array[1..NN] of integer;

begin
  reset(input, 'alternative.in');
  rewrite(output, 'alternative.out');
  read(n,k);
  if k=0 then
  begin
    writeln(0);
    halt;
  end;
  for i:=1 to n do
  begin
    read(p[i],t[i]);
    if p[i]=0 then root:=i;
  end;
  fillchar(u,sizeof(u),0);
  r:=0;
  for i:=1 to k+1 do
  begin
    v:=i;
    while (u[v]=0) and (v<>root) do
    begin
      r:=r+t[v]-t[p[v]];
      inc(u[v]);
      c[p[v]]:=v;
      v:=p[v];
    end;
    inc(u[v]);
  end;

  lca:=root;
  while u[lca]=1 do lca:=c[lca];
  r:=r-t[lca];

  writeln(r*2);
end.
