var
a:array[0..100,0..100] of boolean;
c,d,e:array[0..100] of longint;
b:array[0..100,1..3] of longint;
i,j,k,ans,l,n,f:longint;
t:boolean;
begin
readln(n);
for i:=1to n-1do
readln(b[i,1],b[i,2],b[i,3]);
l:=0;
fillchar(a,sizeof(a),true);
whilenot eoln dobegin
inc(l);
readln(e[l]);
for j:=1to l-1dobegin
a[e[j],e[l]]:=false;
a[e[l],e[j]]:=false;
end;
end;
for i:=1to n-1dofor j:=i+1to n doif b[i,3]<b[j,3] thenbegin
k:=b[i,1];b[i,1]:=b[j,1];b[j,1]:=k;
k:=b[i,2];b[i,2]:=b[j,2];b[j,2]:=k;
k:=b[i,3];b[i,3]:=b[j,3];b[j,3]:=k;
end;
for i:=0to n-1do
c[i]:=i;
for i:=1to n-1dobegin
d:=c;
t:=true;
if (d[b[i,1]]<>d[b[i,2]]) thenbegin
f:=d[b[i,2]];
for j:=0to n-1doif d[j]=f then
d[j]:=d[b[i,1]];
end;
for k:=1to l dofor j:=1to l doif k<>j thenif (d[e[k]]=d[e[j]])and(a[e[k],e[j]]=false) then t:=false;
ifnot t then
ans:=ans+b[i,3]
else c:=d;
end;
writeln(ans);
end.