#include <stdio.h>
#include <stdlib.h>
int father[101];
struct road
{
int u,v,len;
};
int compare(const void*a,const void*b);
int find(int x);
void Union(int x,int y);
int main()
{
struct road path[5100];
int m,n,i,pv,pu,s;
while(scanf("%d",&n)!=EOF&&n!=0)
{
m=n*(n-1)/2;
for(i=1;i<=n;i++)
father[i]=i;
for(i=0;i<m;i++)
{
scanf("%d %d %d",&path[i].u,&path[i].v,&path[i].len);
}
qsort(path,m,sizeof(path[0]),compare);
for(s=i=0;i<m;i++)
{
pv=find(path[i].u);
pu=find(path[i].v);
if(pv!=pu)
{
s=s+path[i].len;
Union(pv,pu);
}
}
printf("%d\n",s);
}
return 0;
}
int find(int x)
{
if(x!=father[x])
father[x]=find(father[x]);
return father[x];
}
void Union(int x,int y)
{
int a=find(x);
int b=find(y);
if(a!=b)
father[b]=a;
}
int compare(const void*a,const void*b)
{
return *(*int)a-*(int*)b;
}