#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
const int N=550;
typedef long long LL;
LL g[N][N];
LL dist[N];
bool st[N];
LL n,m,res;
void prim()
{
memset(dist,0x3f,sizeof dist);
dist[1]=0;
for(int i=0;i<n;i++)
{
int t=-1;
for(int j=1;j<=n;j++)
{
if(!st[j]&&(t==-1||dist[j]<dist[t])) t=j;
}
st[t]=true;
res+=dist[t];
for(int j=1;j<=n;j++)
{
if(!st[j]&&dist[j]>g[t][j])
{
dist[j]=g[t][j];
}
}
}
}
int main()
{
scanf("%lld",&n);
m=2*n-1;
memset(g,0x3f,sizeof g);
while(m--)
{
LL a,b,c,d;
scanf("%lld%lld%lld%lld",&a,&b,&c,&d);
if(d==1)
{
g[a][b]=g[b][a]=0;
}
else
{
g[a][b]=g[b][a]=c;
}
}
prim();
printf("%lld",res);
return 0;
}