Problem: Origami is the traditional Japanese art of paper folding. One day, Professor Egami found the message board decorated with some pieces of origami works pinned on it, and became interested in the pinholes on the origami paper. Your mission is to simulate paper folding and pin punching on the folded sheet, and calculate the number of pinholes on the original sheet when unfolded.

A sequence of folding instructions for a flat and square piece of paper and a single pinhole position are specified. As a folding instruction, two points P and Q are given. The paper should be folded so that P touches Q from above (Figure 4). To make a fold, we first divide the sheet into two segments by creasing the sheet along the folding line, i.e., the perpendicular bisector of the line segment PQ, and then turn over the segment containing P onto the other. You can ignore the thickness of the paper.

<image>


Figure 4: Simple case of paper folding

The original flat square piece of paper is folded into a structure consisting of layered paper segments, which are connected by linear hinges. For each instruction, we fold one or more paper segments along the specified folding line, dividing the original segments into new smaller ones. The folding operation turns over some of the paper segments (not only the new smaller segments but also some other segments that have no intersection with the folding line) to the reflective position against the folding line. That is, for a paper segment that intersects with the folding line, one of the two new segments made by dividing the original is turned over; for a paper segment that does not intersect with the folding line, the whole segment is simply turned over.

The folding operation is carried out repeatedly applying the following rules, until we have no segment to turn over.

* Rule 1: The uppermost segment that contains P must be turned over.
* Rule 2: If a hinge of a segment is moved to the other side of the folding line by the operation, any segment that shares the same hinge must be turned over.
* Rule 3: If two paper segments overlap and the lower segment is turned over, the upper segment must be turned over too.



In the examples shown in Figure 5, (a) and (c) show cases where only Rule 1 is applied. (b) shows a case where Rule 1 and 2 are applied to turn over two paper segments connected by a hinge, and (d) shows a case where Rule 1, 3 and 2 are applied to turn over three paper segments.

<image>


Figure 5: Different cases of folding

After processing all the folding instructions, the pinhole goes through all the layered segments of paper at that position. In the case of Figure 6, there are three pinholes on the unfolded sheet of paper.

<image>


Figure 6: Number of pinholes on the unfolded sheet



Input

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

Each dataset is formatted as follows.

k
px1 py1 qx1 qy1
.
.
.
pxk pyk qxk qyk
hx hy

For all datasets, the size of the initial sheet is 100 mm square, and, using mm as the coordinate unit, the corners of the sheet are located at the coordinates (0, 0), (100, 0), (100, 100) and (0, 100). The integer k is the number of folding instructions and 1 ≤ k ≤ 10. Each of the following k lines represents a single folding instruction and consists of four integers pxi, pyi, qxi, and qyi, delimited by a space. The positions of point P and Q for the i-th instruction are given by (pxi, pyi) and (qxi, qyi), respectively. You can assume that P ≠ Q. You must carry out these instructions in the given order. The last line of a dataset contains two integers hx and hy delimited by a space, and (hx, hy ) represents the position of the pinhole.

You can assume the following properties:

1. The points P and Q of the folding instructions are placed on some paper segments at the folding time, and P is at least 0.01 mm distant from any borders of the paper segments.
2. The position of the pinhole also is at least 0.01 mm distant from any borders of the paper segments at the punching time.
3. Every folding line, when infinitely extended to both directions, is at least 0.01 mm distant from any corners of the paper segments before the folding along that folding line.
4. When two paper segments have any overlap, the overlapping area cannot be placed between any two parallel lines with 0.01 mm distance. When two paper segments do not overlap, any points on one segment are at least 0.01 mm distant from any points on the other segment.



For example, Figure 5 (a), (b), (c) and (d) correspond to the first four datasets of the sample input.

Output

For each dataset, output a single line containing the number of the pinholes on the sheet of paper, when unfolded. No extra characters should appear in the output.

Example

Input

2
90 90 80 20
80 20 75 50
50 35
2
90 90 80 20
75 50 80 20
55 20
3
5 90 15 70
95 90 85 75
20 67 20 73
20 75
3
5 90 15 70
5 10 15 55
20 67 20 73
75 80
8
1 48 1 50
10 73 10 75
31 87 31 89
91 94 91 96
63 97 62 96
63 80 61 82
39 97 41 95
62 89 62 90
41 93
5
2 1 1 1
-95 1 -96 1
-190 1 -191 1
-283 1 -284 1
-373 1 -374 1
-450 1
2
77 17 89 8
103 13 85 10
53 36
0


Output

3
4
3
2
32
1
0

Solution (in CPP):

Solution:
#include <iostream>
#include <cstdio>
#include <iomanip>
#include <complex>
#include <vector>
#include <algorithm>
#include <cmath>
#include <array>
using namespace std;
const double EPS = 1e-8;
const double INF = 1e12;
#define EQ(n,m) (abs((n)-(m)) < EPS)
#define X real()
#define Y imag()
typedef complex<double> P;
typedef vector<P> VP;
struct L : array<P, 2>{
    L(const P& a, const P& b){ at(0)=a; at(1)=b; }
    L(){}
};

struct poly_connect{
    VP v;
    vector<int> c;
};

double dot(P a, P b){
    return (conj(a)*b).X;
}
double cross(P a, P b){
    return (conj(a)*b).Y;
}
int ccw(P a, P b, P c){
    b -= a;
    c -= a;
    if(cross(b,c) > EPS) return +1; //ccw
    if(cross(b,c) < -EPS) return -1; //cw
    if(dot(b,c) < -EPS) return +2; //c-a-b
    if(abs(c)-abs(b) > EPS) return -2; //a-b-c
    return 0; //a-c-b
}

bool intersectSP(const L& s, const P &p){
    return abs(cross(s[0]-p, s[1]-p))<EPS  && dot(s[0]-p, s[1]-p)<EPS;
}
P projection(const L& l, const P& p) {
  double t = dot(p-l[0], l[0]-l[1]) / norm(l[0]-l[1]);
  return l[0] + t*(l[0]-l[1]);
}
P reflection(const L& l, const P& p) {
  return p + 2.0*(projection(l, p) -p);
}
P crosspointLL(const L &l, const L &m) {
  double A = cross(l[1]-l[0], m[1]-m[0]);
  double B = cross(l[1]-l[0], l[1]-m[0]);
  return m[0] + B/A *(m[1]-m[0]);
}
int in_poly(const P &p, const VP &poly){
    int n = poly.size();
    int ret = -1;
    for(int i=0; i<n; i++){
        P a = poly[i]-p;
        P b = poly[(i+1)%n]-p;
        if(a.Y > b.Y) swap(a,b);
        if(intersectSP(L(a,b), P(0,0))) return 0;
        if(a.Y<=0 && b.Y>0 && cross(a,b)<0) ret = -ret;
    }
    return ret;
}
bool convex_overlap(const VP &a, const VP &b){
    for(P p : a){
        if(in_poly(p, b) > 0) return true;
    }
    for(P p : b){
        if(in_poly(p, a) > 0) return true;
    }
    if(in_poly((a[0] +a[1] +a[2])/3.0, b) > 0 ||
       in_poly((b[0] +b[1] +b[2])/3.0, a) > 0){
        return true;
    }
    return false;
}

poly_connect fold_convex(int numl, int idx, poly_connect pc, const L& l){
    poly_connect ret;
    int n = pc.v.size();
    for(int i=0; i<n; i++){
        P curr = pc.v[i];
        P next = pc.v[(i+1)%n];
        if(ccw(l[0], l[1], curr) != -1){
            ret.v.push_back(curr);
            ret.c.push_back(pc.c[i]);
        }
        if(ccw(l[0], l[1], curr) *ccw(l[0], l[1], next) == -1){
            ret.v.push_back(crosspointLL(L(curr, next), l));
            if(ccw(l[0], l[1], curr) != -1){
                ret.c.push_back(2*numl-1-idx);
            }else{
                ret.c.push_back(pc.c[i]);
            }
        }
    }
    return ret;
}

poly_connect reverse_layer(int numl, poly_connect pc, const L &l){
    int n = pc.v.size();
    if(n == 0) return pc;
    for(int i=0; i<n; i++){
        pc.v[i] = reflection(l, pc.v[i]);
        if(pc.c[i] != -1){
            pc.c[i] = 2*numl -1 -pc.c[i];
        }
    }
    reverse(pc.v.begin()+1, pc.v.end());
    reverse(pc.c.begin(), pc.c.end());
    return pc;
}

void recursive_fold(int numl, int idx, vector<poly_connect> &ulayer, vector<bool> &used){
    if(idx >= numl || used[idx]) return;
    used[idx] = true;
    for(int connect : ulayer[idx].c){
        if(connect != -1){
            recursive_fold(numl, connect, ulayer, used);
        }
    }
    for(int i=idx+1; i<numl; i++){
        if(!ulayer[i].v.empty() && convex_overlap(ulayer[idx].v, ulayer[i].v)){
            recursive_fold(numl, i, ulayer, used);
        }
    }
}

void fold(int numl, vector<poly_connect> &layer, const P &p, const P &q){
    vector<poly_connect> dlayer(512), ulayer(512);
    P mid = (p +q) /2.0;
    L bisec(mid, mid +(q -p) *P(0, -1)); //残す方
    L rbisec(bisec[1], bisec[0]);
    for(int i=0; i<numl; i++){
        dlayer[i] = fold_convex(numl, i, layer[i], bisec);
        ulayer[i] = fold_convex(numl, i, layer[i], rbisec);
    }
    vector<bool> used(512, false);
    for(int i=numl-1; i>=0; i--){
        //一番上にあるlayerから再帰的に始める
        if(in_poly(p, layer[i].v) > 0){
            recursive_fold(numl, i, ulayer, used);
            break;
        }
    }
    for(int i=0; i<numl; i++){
        if(used[i]){
            layer[i] = dlayer[i];
            layer[2*numl-1-i] = reverse_layer(numl, ulayer[i], bisec);
        }
    }
}

int main(){
    while(1){
        int n;
        cin >> n;
        if(n == 0) break;

        VP p(n), q(n);
        for(int i=0; i<n; i++){
            int px,py, qx,qy;
            cin >> px >> py >> qx >> qy;
            p[i] = P(px, py);
            q[i] = P(qx, qy);
        }
        int hx,hy;
        cin >> hx >> hy;
        P h(hx, hy);

        vector<poly_connect> layer(1024);
        layer[0].v = VP{P(0, 0), P(100, 0), P(100, 100), P(0, 100)};
        layer[0].c = vector<int>{-1, -1, -1, -1};
        for(int i=0; i<n; i++){ 
            fold(1<<i, layer, p[i], q[i]);
        }
        int ans = 0;
        for(int i=0; i<(1<<n); i++){
            if(in_poly(h, layer[i].v) > 0){
                ans++;
            }
        }
        cout << ans << endl;
    }
    return 0;
}