Starlight Falls

Given two observers' angular-direction and distance-range observations, decide whether a consistent star placement exists and find the maximum number of stars.

Medium7GeometryIntervalsGreedySortingNo attempts yetTime limit2sMemory limit128 MB

Problem

Coders and High are astronomers who live in the two dimensional coordinate plane. Coders stands still at the point (xC,0)(x_C, 0) and High stands still at the point (xH,0)(x_H, 0), and both of them watch the stars floating in the sky, that is, the part of the plane where y>0y > 0.

Each astronomer takes their own position as the reference point and writes the coordinates of a point in the sky as (r,θ)(r, \theta). Here r>0r > 0 is the straight line distance between the observer and the point, and 0<θ<1800^\circ < \theta < 180^\circ is the size of the angle of the ray from the observer to the point, measured counterclockwise from the positive direction of the xx axis.

A star at distance 5 and angle 65 degrees from the observer

From the observer's reference point, the star in the picture above is at (5,65)(5, 65^\circ).

The reason the two write positions in such an awkward way is a limit of their equipment. It reports the direction θ\theta of a star exactly, but it cannot report the straight line distance rr to the star. Instead it analyzes the waveform of the light and reports only a range of distances where a star can be. A single observation therefore tells the observer one fact: in the measured direction, inside the measured range of distances, there is at least one star.

An observation that fixes the direction exactly and the distance only as a range

An observer measures only the direction of a star and a range of distances, both relative to their own position.

The two know this limit well, so they decided to work together and make up for the error in their measurements. Each of them observed every light source in the sky and recorded the result, and now they want to combine the two records and recover the exact positions of the stars. They treat every position where a star may or may not be as a position where a star is floating, so that not a single star is missed. At most one star sits at the same coordinates.

Write a program that decides whether the positions of the stars in the sky can be recovered so that both records are satisfied, and if they can, finds the largest number of stars the two can recover.

Input

The first line contains two integers xCx_C and xHx_H (0xC<xH50000 \le x_C < x_H \le 5000), separated by a space, giving the positions where Coders and High stand. Coders stands still at (xC,0)(x_C, 0) and High stands still at (xH,0)(x_H, 0).

The second line contains the number of observations nn (1n1000001 \le n \le 100000) made by Coders.

Each of the next nn lines contains one observation of Coders, given as four integers θxC\theta x_C, θyC\theta y_C, sCs_C, eCe_C (5000<θxC<5000-5000 < \theta x_C < 5000, 0<θyC<100000 < \theta y_C < 10000, 0<sCeC<50000 < s_C \le e_C < 5000) separated by spaces. It means that the direction vector of the ray from Coders toward a star is (θxC,θyC)(\theta x_C, \theta y_C), and that the straight line distance between the star and Coders is at least sCs_C and at most eCe_C.

The next line contains the number of observations mm (1m1000001 \le m \le 100000) made by High.

Each of the next mm lines contains one observation of High, given as four integers θxH\theta x_H, θyH\theta y_H, sHs_H, eHe_H (5000<θxH<5000-5000 < \theta x_H < 5000, 0<θyH<100000 < \theta y_H < 10000, 0<sHeH<50000 < s_H \le e_H < 5000) separated by spaces, with the same meaning as for Coders.

A single observation is the segment of the ray from the observer whose distance from the observer is at least ss and at most ee. The observations made by Coders never touch or overlap each other. The observations made by High never touch or overlap each other.

Output

If the positions of the stars in the sky can be recovered so that both records are satisfied, print on the first line the largest number of stars the two can recover. If they cannot be recovered, print -1.