/**********************
*@Name:
*
*@Author: Nervending
*@Describtion:
*@DateTime: 2017-10-16 19:48:25
***********************/
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
const int maxn=1e2+10;
const int maxm=1e4+1000;
int root[maxn];
struct node {
	int from,to;
	double cost;
}e[maxm];
bool cmp (node &a,node &b){
	return a.cost<b.cost;
}
int find (int a){
	if(root[a]==a)return a;
	else return root[a]=find(root[a]);
}
#define same(a,b) (find(a)==find(b))
void unite(int a,int b){
	a=find(a);
	b=find(b);
	root[a]=b;
}
double x[maxn],y[maxn],z[maxn],r[maxn];
int n,m,nume;
double ans;
void init(){
	memset(e,0,sizeof e);
	for(int i=0;i<maxn;i++){
		root[i]=i;
	}
	ans=0;
	nume=0;
}
void kruskal(){
	for(int i=0;i<nume;i++){
		int a=e[i].from,b=e[i].to;
		if(!same(a,b)){
			unite(a,b);
			ans+=e[i].cost;
		}
	}
}
double dst(int a,int b){
	double dx=(x[a]-x[b]);
	double dy=(y[a]-y[b]);
	double dz=(z[a]-z[b]);
	double d=sqrt(dx*dx+dy*dy+dz*dz);
	double dr=r[a]+r[b];
	if(d>=dr)return d-dr;
	else return -1;
}
int main(){
//	freopen("in.txt","r",stdin);
	while(~scanf("%d",&n)&&n){
		init();
		for(int i=0;i<n;i++){
			scanf("%lf%lf%lf%lf",&x[i],&y[i],&z[i],&r[i]);
		}
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				double cost=dst(i,j);
				if(cost>0){
					node t={i,j,cost};
					e[nume++]=t;
				}
				else {
					node t={i,j,0};
					e[nume++]=t;
				}
			}
		}
		sort(e,e+nume,cmp);
		kruskal();
		printf("%.3lf\n",ans);
	}
}