Problem: A laser beam generator, a target object and some mirrors are placed on a plane. The mirrors stand upright on the plane, and both sides of the mirrors are flat and can reflect beams. To point the beam at the target, you may set the beam to several directions because of different reflections. Your job is to find the shortest beam path from the generator to the target and answer the length of the path.

Figure G-1 shows examples of possible beam paths, where the bold line represents the shortest one.

<image>

Figure G-1: Examples of possible paths

Input

The input consists of a number of datasets. The end of the input is indicated by a line containing a zero.

Each dataset is formatted as follows. Each value in the dataset except n is an integer no less than 0 and no more than 100.

> n
>  PX1 PY1 QX1 QY1
>  ...
>  PXn PYn QXn QYn
>  TX TY
>  LX LY
>

The first line of a dataset contains an integer n (1 ≤ n ≤ 5), representing the number of mirrors. The following n lines list the arrangement of mirrors on the plane. The coordinates (PXi, PYi) and (QXi, QYi) represent the positions of both ends of the mirror. Mirrors are apart from each other. The last two lines represent the target position (TX, TY) and the position of the beam generator (LX, LY). The positions of the target and the generator are apart from each other, and these positions are also apart from mirrors.

The sizes of the target object and the beam generator are small enough to be ignored. You can also ignore the thicknesses of mirrors.

In addition, you can assume the following conditions on the given datasets.

* There is at least one possible path from the beam generator to the target.
* The number of reflections along the shortest path is less than 6.
* The shortest path does not cross or touch a plane containing a mirror surface at any points within 0.001 unit distance from either end of the mirror.
* Even if the beam could arbitrarily select reflection or passage when it reaches each plane containing a mirror surface at a point within 0.001 unit distance from one of the mirror ends, any paths from the generator to the target would not be shorter than the shortest path.
* When the beam is shot from the generator in an arbitrary direction and it reflects on a mirror or passes the mirror within 0.001 unit distance from the mirror, the angle θ formed by the beam and the mirror satisfies sin(θ) > 0.1, until the beam reaches the 6th reflection point (exclusive).



Figure G-1 corresponds to the first dataset of the Sample Input below. Figure G-2 shows the shortest paths for the subsequent datasets of the Sample Input.

<image>

Figure G-2: Examples of the shortest paths

Output

For each dataset, output a single line containing the length of the shortest path from the beam generator to the target. The value should not have an error greater than 0.001. No extra characters should appear in the output.

Sample Input


2
30 10 30 75
60 30 60 95
90 0
0 100
1
20 81 90 90
10 90
90 10
2
10 0 10 58
20 20 20 58
0 70
30 0
4
8 0 8 60
16 16 16 48
16 10 28 30
16 52 28 34
24 0
24 64
5
8 0 8 60
16 16 16 48
16 10 28 30
16 52 28 34
100 0 100 50
24 0
24 64
0


Output for the Sample Input


180.27756377319946
113.13708498984761
98.99494936611666
90.50966799187809
90.50966799187809






Example

Input

2
30 10 30 75
60 30 60 95
90 0
0 100
1
20 81 90 90
10 90
90 10
2
10 0 10 58
20 20 20 58
0 70
30 0
4
8 0 8 60
16 16 16 48
16 10 28 30
16 52 28 34
24 0
24 64
5
8 0 8 60
16 16 16 48
16 10 28 30
16 52 28 34
100 0 100 50
24 0
24 64
0


Output

180.27756377319946
113.13708498984761
98.99494936611666
90.50966799187809
90.50966799187809

Solution (in CPP):

Solution:
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<vector>
using namespace std;
const double EPS = 1e-8;
const double INF = 1e+10;
const double PI = acos(-1);
int sig(double r) { return (r < -EPS) ? -1 : (r > +EPS) ? +1 : 0; }
inline double ABS(double a){return max(a,-a);}
struct Pt {
	double x, y;
	Pt() {}
	Pt(double x, double y) : x(x), y(y) {}
	Pt operator+(const Pt &a) const { return Pt(x + a.x, y + a.y); }
	Pt operator-(const Pt &a) const { return Pt(x - a.x, y - a.y); }
	Pt operator*(const Pt &a) const { return Pt(x * a.x - y * a.y, x * a.y + y * a.x); }
	Pt operator-() const { return Pt(-x, -y); }
	Pt operator*(const double &k) const { return Pt(x * k, y * k); }
	Pt operator/(const double &k) const { return Pt(x / k, y / k); }
	double ABS() const { return sqrt(x * x + y * y); }
	double abs2() const { return x * x + y * y; }
	double arg() const { return atan2(y, x); }
	double dot(const Pt &a) const { return x * a.x + y * a.y; }
	double det(const Pt &a) const { return x * a.y - y * a.x; }
};
double tri(const Pt &a, const Pt &b, const Pt &c) { return (b - a).det(c - a); }
int iSP(Pt a, Pt b, Pt c) {
	int s = sig((b - a).det(c - a));
	if (s) return s;
	if (sig((b - a).dot(c - a)) < 0) return -2; // c-a-b
	if (sig((a - b).dot(c - b)) < 0) return +2; // a-b-c
	return 0;
}
double dLP(Pt a, Pt b, Pt c) {
	return ABS(tri(a, b, c)) / (b - a).ABS();
}
bool iSS(Pt a, Pt b, Pt c, Pt d) {
	return (sig(tri(a, b, c)) * sig(tri(a, b, d)) < 0 && sig(tri(c, d, a)) * sig(tri(c, d, b)) < 0);
}
Pt pLL(Pt a, Pt b, Pt c, Pt d) {
	b = b - a; d = d - c; return a + b * (c - a).det(d) / b.det(d);
}

Pt p[10];
Pt q[10];
Pt s;
Pt t;
double ret=0;
int use[10];
int n;
Pt u[10][10];
Pt v[10][10];
void move(int a){
	for(int i=0;i<n;i++){
		if(i==a)continue;
		double dis=dLP(p[a],q[a],p[i])*2;
		Pt vec;
		if(iSP(p[a],q[a],p[i])==-1){
			vec=((q[a]-p[a])*Pt(0,1));
		}else{
			vec=((p[a]-q[a])*Pt(0,1));
		}
		vec=vec/vec.ABS();
		p[i]=p[i]+vec*dis;
		dis=dLP(p[a],q[a],q[i])*2;
		if(iSP(p[a],q[a],q[i])==-1){
			vec=((q[a]-p[a])*Pt(0,1));
		}else{
			vec=((p[a]-q[a])*Pt(0,1));
		}
		vec=vec/vec.ABS();
		q[i]=q[i]+vec*dis;
	//	printf("%d: (%f, %f) (%f, %f)\n",i,p[i].x,p[i].y,q[i].x,q[i].y);
	}
	double dis=dLP(p[a],q[a],t)*2;
	Pt vec;
	if(iSP(p[a],q[a],t)==-1){
		vec=((q[a]-p[a])*Pt(0,1));
	}else{
		vec=((p[a]-q[a])*Pt(0,1));
	}
	vec=vec/vec.ABS();
	t=t+vec*dis;
	//printf("t: (%f, %f)\n",t.x,t.y);
}

void solve(int a,int b){
	bool ok=true;
	Pt at=s;
	for(int i=0;i<a;i++){
		Pt val=t;
		int nat=-1;
		for(int j=0;j<n;j++){
			if((i==0||use[i-1]!=j)&&iSS(at,val,u[i][j],v[i][j])){
				nat=j;
				val=pLL(at,val,u[i][j],v[i][j]);
			}
		}
		if(nat!=use[i]){ok=false;break;}
		at=val;
	}
	for(int j=0;j<n;j++){
		if(iSS(at,t,u[a][j],v[a][j]))ok=false;
	}
	if(ok){
		ret=min(ret,(t-s).ABS());
	}
	if(a<6){
		for(int i=0;i<n;i++){
			if(b==i)continue;
			use[a]=i;
			move(i);
			for(int j=0;j<n;j++){
				u[a+1][j]=p[j];
				v[a+1][j]=q[j];
			}
			solve(a+1,i);
			move(i);
		}
	}
}
int main(){
	int a;
	while(scanf("%d",&a),a){
		n=a;
		for(int i=0;i<a;i++){
			double X1,X2,Y1,Y2;
			scanf("%lf%lf%lf%lf",&X1,&Y1,&X2,&Y2);
			p[i]=Pt(X1,Y1);
			q[i]=Pt(X2,Y2);
		}
		double ix,iy;scanf("%lf%lf",&ix,&iy);
		for(int i=0;i<a;i++){
			u[0][i]=p[i];
			v[0][i]=q[i];
		}
		t=Pt(ix,iy);
		scanf("%lf%lf",&ix,&iy);
		s=Pt(ix,iy);
		ret=999999999;
		solve(0,-1);
		printf("%.12f\n",ret);
	}
}