// AOJ 1127: Building a Space Station
// 2017.9.19 bal4u@uu
// 2017.11.23

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#define R 10
long long x[101], y[101], z[101], r[101];
char f[101];

char buf[100], *p;

long long dbl2int()
{
	int n = 0;
	while (*p >= '0') n = (n<<3) + (n<<1) + (*p++ & 0xf);
	if (*p == '.') p++;
	while (*p >= '0') n = (n<<3) + (n<<1) + (*p++ & 0xf);
	return n << R;
}

long long dist(long long dx, long long dy, long long dz)
{
	return (long long)sqrt((double)(dx*dx + dy*dy + dz*dz));
}

int main()
{
    int i, j, n, cnt, nx;
	long long ans, min, d;

    while (fgets(buf, 10, stdin) && *buf != '0') {
		n = atoi(buf);
		memset(f, 0, n);
		for (i = 0; i < n; i++) {
			fgets(p=buf, 100, stdin);
			x[i] = dbl2int(), p++, y[i] = dbl2int(), p++, z[i] = dbl2int(), p++, r[i] = dbl2int();
		}
        ans = 0, f[1] = 1, cnt = 1;
        while (cnt < n) {
            for (nx = -1, min = 1LL<<60, i = 0; i < n; i++) {
                if (f[i] == 0) continue;
                for (j = 0; j < n; j++) {
                    if (f[j]) continue;
					d = dist(x[i]-x[j], y[i]-y[j], z[i]-z[j]) - (r[i]+r[j]);
					if (nx < 0 || d < min) min = d, nx = j;
                }
            }
            f[nx] = 1, cnt++; if (min > 0) ans += min;
        }
		ans = (ans+512) >> R;
        printf("%lld.%03lld\n", ans/1000, ans%1000);
    }
    return 0;
}