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 <stdio.h>
#include <assert.h>
#include <iostream>
#include <complex>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
using namespace std;
#define rep(i, n) for(int i=0; i<(int)(n); i++)
#define mp make_pair
typedef complex<double> P;
typedef vector<P> convex;
#define EPS (1e-9)

double cross(const P& a, const P& b) { return imag(conj(a)*b); }
double dot(const P& a, const P& b) { return real(conj(a)*b); }

int ccw(const P& a, P b, P c) {
    b -= a; c -= a;
    if(cross(b, c)>0) return 1;
    if(cross(b, c)<0) return -1;
    if(dot(b, c)<0) return 2;
    if(norm(b)<norm(c)) return -2;
    return 0;
}

P projection(const P& l0, const P& l1, const P& p) {
    const double t = dot(p-l0, l1-l0) / norm(l1-l0);
    return l0 + t*(l1-l0);
}

bool intersectSP(const P& s0, const P& s1, const P& p) {
    return abs(s0-p)+abs(s1-p)-abs(s1-s0)<EPS;
}

P crosspoint(const P& l0, const P& l1, const P& m0, const P& m1) {
    const double a = cross(l1-l0, m1-m0);
    const double b = cross(l1-l0, l1-m0);
    if(abs(a)<EPS && abs(b)<EPS) return m0;
//    if(abs(a)<EPS) throw 0;
    return m0 + b/a*(m1-m0);
}

convex convex_cut(const convex& v, const P& l0, const P& l1) {
    convex r;
    rep(i, v.size()) {
        const P& a(v[i]), b(v[(i+1)%v.size()]);
        if(ccw(l0, l1, a)!=-1) r.push_back(a);
        if(ccw(l0, l1, a)*ccw(l0, l1, b)<0) {
            r.push_back(crosspoint(l0, l1, a, b));
        }
    }
    return r;
}

bool contains(const convex& ps, const P& p) {
    bool in = false;
    rep(i, ps.size()) {
        const int j = (i+1)%ps.size();
        P a(ps[i]-p), b(ps[j]-p);
        if(imag(a) > imag(b)) swap(a, b);
        if(imag(a) <= 0 && 0 < imag(b) && cross(a, b) < 0) in = !in;
        if(cross(a, b)==0 && dot(a, b) <= 0) return true; // on edge
    }
    return in;
}

bool intersectCVCV(const convex& a, const convex& b) {
    rep(i, a.size()) if(contains(b, a[i])) return true;
    rep(i, b.size()) if(contains(a, b[i])) return true;
    return false;
}

// convex and hinges
// hinges are represented by (1 + the index of the connected segment)
typedef pair<convex, vector<int> > segment;

int index(const convex& v, const P& s0, const P& s1) {
    const P& mid(0.5*(s0+s1));
    rep(i, v.size()) {
        const int j = (i+1)%v.size();
        if(intersectSP(v[i], v[j], mid)) return i;
    }
    return -1;
}

vector<segment> turnover(const vector<segment>& vs, P p, P q) {
    // first, determine which segments to turn over
    queue<int> qu;
    vector<int> is(vs.size(), 0);
    // rule 1
    for(int i=(int)vs.size()-1; i>=0; i--) if(contains(vs[i].first, p)) {
        qu.push(i);
        is[i] = 1;
        break;
    }
    P m(0.5*(p+q));
    P dir((p-q)*P(0, 1));
    while(!qu.empty()) {
        const int ix = qu.front();
        qu.pop();
        const convex& cv = vs[ix].first;
        convex a(convex_cut(cv, m, m-dir));
        // rule 2
        rep(i, a.size()) {
            const int j = (i+1)%a.size();
            const int k = index(cv, a[i], a[j]);
            if(k==-1) continue;
            const int tx = vs[ix].second[k]-1;
            if(tx!=-1 && !is[tx]) {
                qu.push(tx);
                is[tx] = 1;
            }
        }
        // rule 3
        for(int i=ix+1; i<(int)vs.size(); i++) {
            if(!is[i] && intersectCVCV(a, vs[i].first)) {
                qu.push(i);
                is[i] = 1;
            }
        }
    }

    // second, turn over the segments
    // in this section, hinges are left untouched, except for:
    //   hinges to segment in `s` -> positive
    //   hinges to segment in `t` -> negative
    vector<pair<int, segment> > s, t;
    rep(k, vs.size()) {
        if(is[k]==0) s.push_back(mp(k+1, vs[k]));
        else {
            const convex& cv = vs[k].first;
            convex a(convex_cut(cv, m, m+dir));
            if(a.size()>0) {
                vector<int> hg;
                rep(i, a.size()) {
                    const int j = (i+1)%a.size();
                    const int ix = index(cv, a[i], a[j]);
                    hg.push_back(ix==-1 ? -(k+1) : vs[k].second[ix]);
                }
                s.push_back(mp(k+1, mp(a, hg)));
            }
            convex b(convex_cut(cv, m, m-dir));
            if(b.size()>0) {
                vector<int> hg;
                rep(i, b.size()) {
                    const int j = (i-1+b.size())%b.size();
                    const int ix = index(cv, b[i], b[j]);
                    const P to = 2.0*projection(m, m+dir, b[i])-b[i];
                    hg.push_back(ix==-1 ? k+1 : -vs[k].second[ix]);
                }
                rep(i, b.size()) b[i] = 2.0*projection(m, m+dir, b[i])-b[i];
                reverse(b.begin(), b.end());
                reverse(hg.begin(), hg.end());
                t.push_back(mp(k+1, mp(b, hg)));
            }
        }
    }

    // third, fix the hinges
    map<int, int> of;
    rep(i, s.size()) of[s[i].first] = i+1;
    rep(i, t.size()) of[-t[i].first] = s.size()+t.size()-i;
    vector<segment> r;
    rep(i, s.size()) r.push_back(s[i].second);
    rep(i, t.size()) r.push_back(t[t.size()-1-i].second);
    rep(i, r.size()) rep(j, r[i].second.size()) {
        r[i].second[j] = of[r[i].second[j]];
    }
    return r;
}

int N, Px[16], Py[16], Qx[16], Qy[16];
int Hx, Hy;

int main() {
    for(;;) {
        scanf("%d", &N);
        if(N==0) return 0;
        rep(i, N) scanf("%d%d%d%d", Px+i, Py+i, Qx+i, Qy+i);
        scanf("%d%d", &Hx, &Hy);
        convex v;
        v.push_back(P(0, 0)); v.push_back(P(100, 0));
        v.push_back(P(100, 100)); v.push_back(P(0, 100));
        vector<segment> vs;
        vs.push_back(mp(v, vector<int>(v.size())));
        rep(i, N) vs = turnover(vs, P(Px[i], Py[i]), P(Qx[i], Qy[i]));
        int ans = 0;
        rep(i, vs.size()) if(contains(vs[i].first, P(Hx, Hy))) ans++;
        printf("%d\n", ans);
    }
}