#include <bits/stdc++.h>
using namespace std;
#define EPS 0.0001

typedef struct {
  double x;
  double y;
  double z;
  double r;
} Cell;

bool eq(double a, double b){
  return abs(a-b) < EPS;
}

double cellLength( Cell a, Cell b){
  double l = sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2) + pow(a.z - b.z, 2)) - (a.r + b.r);
  if(l <= 0) return 0;
  else return l;
}

int main(void){
  int n;
  double btw[101][101];
  Cell cell[101];
  while(cin >> n, n){
    for(int i=0; i < n; i++){
      double x, y, z, r;
      cin >> x >> y >> z >> r;
      Cell tmp = {x, y, z, r};
      cell[i] = tmp;
    }

    for(int p=0; p < n; p++){
      for(int q=0; q < n; q++){
        btw[p][q] = cellLength(cell[p], cell[q]);
      }
    }

    //prim
    double mincost[101];
    bool used[101];
    for(int i=0; i < n; i++){
      mincost[i] = INT_MAX-1;
      used[i] = false;
    }
    mincost[0] = 0;
    double res = 0;

    while(true){
      int v = -1;
      for(int u=0; u < n; u++){
        if(!used[u] && (v == -1 || mincost[u] < mincost[v])) v = u;
      }

      if(v == -1) break;
      used[v] = true;
      res += mincost[v];

      for(int u=0; u < n; u++){
        mincost[u] = min(mincost[u], btw[v][u]);
      }
    }
    printf("%.3f\n", res);
  }
  return 0;
}