Origami Through-Hole

Time limit1sMemory limit128 MB

Statement

You must simulate folding a flat square sheet of paper and then punching a single pin through the folded stack, and report how many pinholes appear on the original sheet once it is unfolded.

You are given a sequence of folding instructions followed by one pinhole position. A folding instruction is a pair of points $P$ and $Q$. The paper is folded so that $P$ comes down onto $Q$ from above: the sheet is creased along the fold line — the perpendicular bisector of segment $PQ$ — and the part of the paper on the $P$ side is turned over onto the other side. The thickness of the paper is ignored.

The folded paper is a stack of flat paper segments joined along straight hinges (creases). Each fold divides some segments into smaller ones along the fold line and turns some of them over: a segment that crosses the fold line is split, and one of the two new pieces is turned over; a segment that does not cross the fold line is either turned over whole or left as is. Which segments are turned over is decided by repeatedly applying the following rules until no further segment needs turning:

  • Rule 1. The uppermost segment that contains $P$ must be turned over.
  • Rule 2. If turning a segment over moves one of its hinges to the other side of the fold line, then every segment sharing that hinge must also be turned over.
  • Rule 3. If two segments overlap and the lower one is turned over, the upper one must be turned over as well.

When a segment is turned over it is reflected across the fold line. After all folding instructions are carried out, the pin is pushed through every layer of paper at the pinhole position, so each layer covering that position receives one hole. Report the number of holes on the sheet when it is unfolded (equivalently, the number of paper layers covering the pinhole position in the final folded stack).

Input

The input is a sequence of datasets. The end of the input is a line containing a single zero. Each dataset has the form

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

For every dataset the initial sheet is a 100 mm square; using millimetres as the unit, its corners are at $(0,0)$, $(100,0)$, $(100,100)$ and $(0,100)$. The integer $k$ is the number of folding instructions, with $1 \le k \le 10$. Each of the next $k$ lines has four integers $px_i$, $py_i$, $qx_i$, $qy_i$ separated by spaces, giving the points $P = (px_i, py_i)$ and $Q = (qx_i, qy_i)$ of the $i$-th instruction; you may assume $P \ne Q$. The instructions must be carried out in the given order. The last line has two integers $hx$ and $hy$, the position $(hx, hy)$ of the pinhole.

You may assume the following:

  • At the time of each fold, $P$ and $Q$ lie on some paper segments, and $P$ is at least 0.01 mm away from every border of the paper segments.
  • At the time of punching, the pinhole is at least 0.01 mm away from every border of the paper segments.
  • Every fold line, extended infinitely in both directions, is at least 0.01 mm away from every corner of the paper segments present just before that fold.
  • Whenever two paper segments overlap, the overlapping region cannot be fit between two parallel lines 0.01 mm apart; and whenever two paper segments do not overlap, every point of one is at least 0.01 mm from every point of the other.

Output

For each dataset, output a single line containing the number of pinholes on the unfolded sheet. No other characters may appear in the output.