#include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>

using namespace std;
static const double INF = 1e10;
static const double EPS = 1e-8;
inline int sign(double x){ return abs(x) < EPS ? 0 : (x > 0 ? 1 : -1); }

struct Point3D {
	double x, y, z;
	Point3D() : x(0), y(0), z(0) { }
	Point3D(double x, double y, double z) : x(x), y(y), z(z) { }

	Point3D operator-() const { return Point3D(-x, -y, -z); }
	Point3D operator+(const Point3D &p) const {
		return Point3D(x + p.x, y + p.y, z + p.z);
	}
	Point3D operator-(const Point3D &p) const {
		return Point3D(x - p.x, y - p.y, z - p.z);
	}
	Point3D operator*(const double s) const {
		return Point3D(x * s, y * s, z * s);
	}
	Point3D operator/(const double s) const {
		return Point3D(x / s, y / s, z / s);
	}
};

struct Line3D {
	Point3D a, b;
	Line3D() : a(), b() { }
	Line3D(const Point3D &a, const Point3D &b) : a(a), b(b) { }
};

struct Sphere {
	Point3D c;
	double r;
	Sphere() : c(), r(0) { }
	Sphere(const Point3D &c, double r) : c(c), r(r) { }
};

inline double dot(const Point3D &a, const Point3D &b){
	return a.x * b.x + a.y * b.y + a.z * b.z;
}
inline Point3D cross(const Point3D &a, const Point3D &b){
	return Point3D(
		a.y * b.z - a.z * b.y,
		a.z * b.x - a.x * b.z,
		a.x * b.y - a.y * b.x);
}
inline double norm(const Point3D &p){
	return p.x * p.x + p.y * p.y + p.z * p.z;
}
inline double abs(const Point3D &p){
	return sqrt(norm(p));
}
inline Point3D unit(const Point3D &p){
	return p / abs(p);
}
inline Point3D vec(const Line3D &l){
	return l.b - l.a;
}

inline Point3D projection(const Line3D &l, const Point3D &p){
	const Point3D v = vec(l);
	return l.a + v * (dot(p - l.a, v) / norm(v));
}
inline Point3D reflection(const Line3D &l, const Point3D &p){
	const Point3D q = projection(l, p);
	return q + q - p;
}

inline bool intersectLP(const Line3D &l, const Point3D &p){
	return !sign(norm(cross(p - l.a, vec(l))));
}
inline bool intersectSP(const Line3D &l, const Point3D &p){
	const Point3D a = p - l.a, b = vec(l);
	if(norm(a) < EPS || norm(a - b) < EPS){ return true; }
	if(norm(cross(a, b)) > EPS){ return false; }
	if(sign(a.x * b.x) < 0){ return false; }
	if(sign(a.y * b.y) < 0){ return false; }
	if(sign(a.z * b.z) < 0){ return false; }
	return norm(a) < norm(b);
}
inline double distanceLP(const Line3D &l, const Point3D &p){
	const Point3D q = projection(l, p);
	return abs(p - q);
}
inline double distanceSP(const Line3D &l, const Point3D &p){
	const Point3D u = p - l.a;
	const Point3D v = projection(Line3D(Point3D(), vec(l)), u);
	if(intersectSP(Line3D(Point3D(), vec(l)), v)){ return abs(u - v); }
	return sqrt(min(norm(p - l.a), norm(p - l.b)));
}
inline double distanceLL(const Line3D &s, const Line3D &t){
	const Point3D vs = unit(vec(s)), vt = unit(vec(t));
	const double f = 1.0 - pow(dot(vs, vt), 2);
	if(abs(f) < EPS){ return distanceLP(s, t.a); }
	const double a = dot(s.a - t.a, vt);
	const double b = dot(t.a - s.a, vs);
	const double c = dot(vs, vt);
	const double d = 1.0 / f;
	const double x = d * (b + c * a);
	const double y = d * (c * b + a);
	return abs((s.a + vs * x) - (t.a + vt * y));
}

vector<Point3D> crosspointCL(const Sphere &s, const Line3D &l){
	const Point3D &p = projection(l, s.c);
	const double d2 = norm(s.c - p);
	if(d2 > s.r * s.r + EPS){ return vector<Point3D>(); }
	if(d2 > s.r * s.r - EPS){ return vector<Point3D>(1, p); }
	const double sc = sqrt(s.r * s.r - d2);
	vector<Point3D> ret;
	ret.push_back(p + unit(vec(l)) * sc);
	ret.push_back(p - unit(vec(l)) * sc);
	return ret;
}
vector<Point3D> crosspointCS(const Sphere &s, const Line3D &l){
	const vector<Point3D> ps = crosspointCL(s, l);
	vector<Point3D> ret;
	for(int i = 0; i < ps.size(); ++i){
		if(intersectSP(l, ps[i])){ ret.push_back(ps[i]); }
	}
	return ret;
}

bool intersectCL(const Sphere &s, const Line3D &l){
	return distanceLP(l, s.c) < s.r + EPS;
}
bool intersectCS(const Sphere &s, const Line3D &l){
	return !crosspointCS(s, l).empty();
}

int main(){
	ios_base::sync_with_stdio(false);
	cout << setiosflags(ios::fixed) << setprecision(10);
	while(true){
		int n;
		cin >> n;
		if(n == 0){ break; }
		Point3D s, t;
		cin >> s.x >> s.y >> s.z;
		cin >> t.x >> t.y >> t.z;
		vector<Line3D> lines(n);
		for(int i = 0; i < n; ++i){
			double x1, y1, z1, x2, y2, z2;
			cin >> x1 >> y1 >> z1 >> x2 >> y2 >> z2;
			lines[i] = Line3D(Point3D(x1, y1, z1), Point3D(x2, y2, z2));
		}
		vector< vector<double> > mat(n, vector<double>(n, INF));
		for(int i = 0; i < n; ++i){
			mat[i][i] = 0.0;
			for(int j = 0; j < i; ++j){
				mat[i][j] = mat[j][i] = distanceLL(lines[i], lines[j]);
			}
		}
		for(int k = 0; k < n; ++k){
			for(int i = 0; i < n; ++i){
				for(int j = 0; j < n; ++j){
					mat[i][j] = min(mat[i][j], mat[i][k] + mat[k][j]);
				}
			}
		}
		double answer = abs(t - s);
		for(int i = 0; i < n; ++i){
			const double ds = distanceLP(lines[i], s);
			for(int j = 0; j < n; ++j){
				const double dt = distanceLP(lines[j], t);
				answer = min(answer, ds + dt + mat[i][j]);
			}
		}
		cout << answer << endl;
	}
	return 0;
}

Measured memory_bytes: 1412