#include<stdio.h>
#include<math.h>
#define INF 1e+10

int n;
double x[100],y[100],z[100],r[100];
double dist[100][100];

double max(double a,double b){
	return a<b?b:a;
}
double min(double a,double b){
	return a<b?a:b;
}
double square(double x){
	return x*x;
}
double distance(int a,int b){
	double d = sqrt(square(x[a]-x[b])+square(y[a]-y[b])+square(z[a]-z[b]));
	return max(0.0 ,d-r[a]-r[b]);
}
double prime(){
	int i,cur=0,count=1;
	int used[100];
	double res=0,mind[100], mi;
	for(i=0;i<n;i++)mind[i]=INF;
	for(i=0;i<n;i++)used[i]=0;
	used[cur]=1;
	while(count<n){
		for(i=0;i<n;i++)mind[i] = min(mind[i], dist[cur][i]);
		mi = INF;
		for(i=0;i<n;i++){
			if(!used[i] && mind[i]<mi){
				mi = mind[i];
				cur = i;
			}
		}
		res += mi;
		used[cur] = 1;
		count++;
	}
	return res;
} 
int main(){
	int i,j;
	while(scanf("%d",&n)==1&&n){
		for(i=0;i<n;i++)
			scanf("%lf %lf %lf %lf",x+i,y+i,z+i,r+i);
		for(i=0;i<n;i++){
			for(j=i+1;j<n;j++)dist[i][j]=dist[j][i]=distance(i,j);
		}
		printf("%.3f\n",prime());
	}
}