var
o,v:array[0..1600] of boolean;
f,s,d,dis:array[0..1600] of longint;
next,p,c,w:array[-100000..100000] of longint;
i,j,k,l,y,t,tt,ft,n,ff,st,sf,ans,imp,new,flow:longint;
a:array[0..301,0..301]of longint;
tot:array[0..301]of longint;
procedure link(i,j,k,l:longint);
begin
inc(t);
next[t]:=d[i]; d[i]:=t; p[t]:=j; c[t]:=k; w[t]:=l;
next[-t]:=d[j]; d[j]:=-t; p[-t]:=i; w[-t]:=-l;
end;
function dfs(i,flow:longint):longint;
var j,k,l,min:longint;
begin
if i=tt then
begin
inc(ans,dis[i]*flow); exit(flow);
end;
k:=s[i]; j:=p[k]; dfs:=0;
o[i]:=true; v[i]:=true;
while k<>0 do
begin
l:=dis[i]+w[k]-dis[j]; min:=flow;
if c[k]<min then min:=c[k];
if(min>0)and(l<f[j])then f[j]:=l;
if(min>0)and(l=0)and(not o[j])then
begin
l:=dfs(j,min);
inc(dfs,l); dec(flow,l);
dec(c[k],l); inc(c[-k],l);
end;
if flow=0 then break;
s[i]:=next[s[i]];
k:=s[i]; j:=p[k];
end;
o[i]:=false;
end;
begin
readln(n); tt:=2*n+1;
for i:=1 to n do
for j:=1 to n do a[i,j]:=maxlongint div 2;
for i:=1 to n do
begin
read(k);
for j:=1 to k do
begin
read(l,y);
a[i,l]:=y; inc(tot[l]);
link(i,2*n+1,1,y);
end;
end;
for k:=1 to n do
for i:=1 to n do
for j:=1 to n do
if a[i,k]+a[k,j]<a[i,j] then a[i,j]:=a[i,k]+a[k,j];
for i:=1 to n do
for j:=1 to n do
if(i<>j)and(a[i,j]<maxlongint div 2)then
link(i+n,j,1 shl 20,a[i,j]);
link(0,1+n,1 shl 20,0);
for i:=1 to n do link(0,i+n,tot[i],0);
for i:=1 to n do link(i+n,i,1 shl 20,0);
repeat
for i:=0 to tt do s[i]:=d[i];
fillchar(v,sizeof(v),false);
fillchar(f,sizeof(f),1);
inc(flow,dfs(0,1 shl 20));
imp:=1 shl 20;
for i:=0 to tt do
if(not v[i])and(f[i]<imp)then imp:=f[i];
for i:=0 to tt do if not v[i] then inc(dis[i],imp);
until imp=1 shl 20;
writeln(ans);
end.