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<iostream>
#include<vector>
#include<complex>
#include<algorithm>
#include<iterator>

using namespace std;

const double eps=1e-9;
const double PI=acos(0)*2;
typedef complex<double> P;

double cross(P a,P b){
  return (conj(a)*b).imag();
}

bool intersects(P a1,P a2,P b1,P b2){
  P a=a1-a2;
  P b=b1-b2;
  return cross(b1-a2,a)*cross(b2-a2,a)<=0&&cross(a1-b2,b)*cross(a2-b2,b)<=0;
}

P intersection(P a1,P a2,P b1,P b2){
  P a=a2-a1;
  P b=b2-b1;
  return a1+a*cross(b,b1-a1)/cross(b,a);
}
  
P rotate(P d,P s,double a){
  return (d-s)*polar(1.,a)+s;
}

P mirror_image(P r,P a,P b){
  return rotate(r,a,2*(arg(b-a)-arg(r-a)));
}

int n;
vector<P> PXY,QXY;
P T,L;


bool over_mirror(P p,int x,P d){
  if(x<0)return true;
  P is=intersection(p,d,PXY[x],QXY[x]);
  return abs(is-p)<abs(d-p);
}

int closest_mirror(P p,int cm,double a){
  int cx=-1;
  for(int i=0;i<n;i++){
    if(i==cm)continue;
    P fp=p+polar(999.,a);
    if(intersects(p,fp,PXY[i],QXY[i])){
      P cp=intersection(p,fp,PXY[i],QXY[i]);
      if(over_mirror(p,cm,cp)){
	bool f;
	if(cx<0){
	  f=true;
	}else{
	  P ncp=intersection(p,fp,PXY[cx],QXY[cx]);
	  f=abs(cp-p)<abs(ncp-p);
	}
	if(f){
	  cx=i;
	}
      }
    }
  }
  return cx;
}

double mirror_angle(double a,int x){
  double ba=arg(QXY[x]-PXY[x]);
  double ra=-(a-ba)+ba;
  while(ra<-2*acos(0)){
    ra+=4*acos(0);
  }
  while(ra>2*acos(0)){
    ra-=4*acos(0);
  }
  return ra;
}

double dfs(P p,int mx,double f,double l,int d);

double rdfs(P p,int cx,double f,double l,int d){
  P mp=mirror_image(p,PXY[cx],QXY[cx]);
  double nf=mirror_angle(l,cx);
  double nl=mirror_angle(f,cx);
  if(nf<nl){
    return dfs(mp,cx,nf,nl,d+1);
  }else{
    return min(dfs(mp,cx,nf,2*acos(0),d+1),dfs(mp,cx,-2*acos(0),nl,d+1));
  }
}

double dfs(P p,int mx,double f,double l,int d){
  // cout<<p<<'\t'<<mx<<'\t'<<f<<'\t'<<l<<endl;
  // cout<<abs(T-p)<<'\t'<<arg(T-p)<<endl;
  auto valid=[&](P d){
    return mx<0||intersects(p,d,PXY[mx],QXY[mx]);
  };
  double ta=arg(T-p);
  bool ft=ta<f||l<ta||!valid(T);
  for(int i=0;i<n;i++){
    if(mx==i)continue;
    if(intersects(p,T,PXY[i],QXY[i])){
      P is=intersection(p,T,PXY[i],QXY[i]);
      ft|=valid(is);
    }
  }
  if(!ft)return abs(T-p);
  if(d==5)return 1e99;
  int cx=closest_mirror(p,mx,f);
  vector<pair<P,int> > PQ;
  auto in_range=[&](P pp){
    double a=arg(pp-p);
    return valid(pp)&&eps+f<a&&a<l-eps;
  };
  for(int i=0;i<n;i++){
    if(in_range(PXY[i])){
      PQ.emplace_back(make_pair(PXY[i],i));
    }
    if(in_range(QXY[i])){
      PQ.emplace_back(make_pair(QXY[i],i));
    }
  }
  sort(begin(PQ),end(PQ),[&](pair<P,int> a,pair<P,int> b){
      return arg(a.first-p)<arg(b.first-p);
    });
  double ret=1e99;
  for(auto e:PQ){
    if(cx<0){
      cx=e.second;
      f=arg(e.first-p);
    }else if(cx==e.second){
      ret=min(ret,rdfs(p,cx,f,arg(e.first-p),d));
      f=arg(e.first-p);
      cx=closest_mirror(p,cx,f);
    }else if(valid(e.first)){
      double ang=arg(e.first-p);
      P cp=intersection(p,p+polar(999.,ang),PXY[cx],QXY[cx]);
      P dp=intersection(p,p+polar(999.,ang),PXY[e.second],QXY[e.second]);
      if(abs(dp-p)<abs(cp-p)){
	ret=min(ret,rdfs(p,cx,f,arg(e.first-p),d));
	cx=e.second;
	f=arg(e.first-p);
      }
    }
  }
  if(cx>=0){
    ret=min(ret,rdfs(p,cx,f,l,d));
  }
  return ret;
}

int main(){
  while(cin>>n,n){
    PXY.clear();
    QXY.clear();
    for(int i=0;i<n;i++){
      int px,py,qx,qy;
      cin>>px>>py>>qx>>qy;
      PXY.emplace_back(px,py);
      QXY.emplace_back(qx,qy);
    }
    int tx,ty,lx,ly;
    cin>>tx>>ty>>lx>>ly;
    T=P(tx,ty);
    L=P(lx,ly);
    cout<<fixed<<dfs(L,-1,-2*acos(0),2*acos(0),0)<<endl;
  }
}