#include<iostream>
#include<vector>
#include<queue>
#include<cmath>
#include<algorithm>
#include<cstdio>

using namespace std;

const int MAX = 101;
bool used[MAX];

struct data{double x,y,z,r;};
struct State{
  data d;
  double t;
  int n;
  State(){}
  State(data d, double t, int n):d(d),t(t),n(n){}
  bool operator < (const State& s) const {return t > s.t;}
};

int N;
vector<data> V;

void init(){
  V.clear();
  V.resize(N);
  fill(used,used+MAX,false);
}

void input(){
  for(int i = 0; i < N; i++)
    cin >> V[i].x >> V[i].y >> V[i].z >> V[i].r;
}


double getDis(data d1, data d2){
  return sqrt(pow(d1.x-d2.x,2.0)+pow(d1.y-d2.y,2.0)+pow(d1.z-d2.z,2.0));
}
void solve(){
  priority_queue<State> Q;
  Q.push(State(V[0],0,0));
  
  double ans = 0;
  while(!Q.empty()){
    State now = Q.top();
    Q.pop();
    
    if(used[now.n]) continue;
    used[now.n] = true;
  
    ans += now.t;
    
    for(int i = 0; i < N; i++){
      double dis = max(getDis(now.d,V[i])-now.d.r-V[i].r,0.0);
      if(!used[i]) Q.push(State(V[i],dis,i));
    }
  }
  printf("%.3f\n",ans);
}

int main(){
  while(cin >> N && N){
    init();
    input();
    solve();
  }
  return 0;
}