#P3234. The Kylin Den

The Kylin Den

Description

As more and more people come to visit her in the den and got lost in it, Kylin Orange had started a rebuild project – turn all the walls into glass so that the dizzy visitors will clearly take in the spot they are heading to. And it was just finished. Contrary to her wish, however, things turns out to be more complex as the cheesy glass walls always reflect much of the light rather than let it all pass through. Thus the walls are actually transparent mirrors. As a result, when someone wants to locate a place in the den, he will see a bundle of it and gets lost even easier.

Anyway, to change it back can be another torturing labor which the exhausted Kylin does not want. She has to think about setting up some signposts. In the first place, she needs to know how many images (including the real one) of an object one can see from a particular place. There are two important rules of counting:

  1. Any light that is reflected by the same wall twice will be ignored as it is getting too faint to recognize.
  2. Light passes through exactly the fringes of some walls does not vanish.

Input

The input contains exactly one test case given as specified below:

Line 1Four integers xm, ym, xd and yd, suggesting the locations of the observer (xm, ym) and the object (xd, yd). The two locations can coincide with each other.
Line 2An integer N (0 < N < 9), the number of walls.
Line 3 – N + 2Four integers x1, y1, x2 and y2 on each line, giving the two ends of a wall (x1, y1) and (x2, y2). The wall satisfies exactly one of four conditions:x1 = x2y1 = y2x1 + y1 = x2 + y2x1 − y1 = x2 − y2

The walls does not intersect or touch each other. All coordinates are in the range (−104, 104) × (−104, 104).

Output

Output the number of images that are seen.

0 0 0 0
2
-1 0 0 1
0 -1 1 0
5

Source

POJ Monthly--2007.06.03, Jia, You