eolymp
bolt
Try our new interface for solving problems
Problems

Billiard

Billiard

Let \textbf{G} be a convex polygon and \textbf{p} a point lying strictly outside of the polygon. There are two tangents from \textbf{p} to \textbf{G}. Let's take the "right" one (related to \textbf{p}) and assume that \textbf{q} is the tangent point and \textbf{r} is the point symmetrical to \textbf{p} if \textbf{q }is the center of symmetry. We say \textbf{r = T(p)} and call \textbf{T} a \textit{billiard transform}. \includegraphics{https://static.e-olymp.com/content/a0/a01d6fe5baf86fd274dfc5600a30f76cc672dd3c.jpg} \includegraphics{https://static.e-olymp.com/content/2c/2c13416e5c2e45c051648c0f1387a6a65f76974b.jpg} \includegraphics{https://static.e-olymp.com/content/a5/a5cf45fecc599d199cc90508960b832cd6cdd640.jpg} \includegraphics{https://static.e-olymp.com/content/49/49270e28ebc371c9e6e548fe5edf6353526f0ecb.jpg} A tangent \textbf{pq} from point \textbf{p} to a convex polygon \textbf{G}, where \textbf{q} is the tangent point, is called right if for every point \textbf{uG }the counter-clockwise rotation from vector to vector is in the interval \[\textbf{0}, ). Here is an example which may help you better understand what \textbf{T} is. Suppose \textbf{G} is a triangle with vertices (\textbf{0}, \textbf{0}), (\textbf{2}, \textbf{0}), (\textbf{1}, \textbf{1}) and \textbf{p} has coordinates (\textbf{-1}, \textbf{-1}). Then the "right" tangent will pass through vertex (\textbf{2}, \textbf{0}), so \textbf{T(p)} = (\textbf{5}, \textbf{1}). Similarly, \textbf{T((3, 0)) = (-1, 2)}, \textbf{T((3, 2)) = (-1, 0)}, \textbf{T((3, 4)) = (-3, -4)}. In some cases the "right" tangent may touch the polygon not in the vertex, but by the side. If this happens, \textbf{T(p)} is undefined. For example, for the former \textbf{G} the values \textbf{T((3, 3))}, \textbf{T((-1, 0))}, \textbf{T((4, -2))} are undefined. Consider the sequence \textbf{S(p) = (p, T(p), T(T(p)), ...)}. Depending on the behavior of this sequence, the set of all points \textbf{p} outside of the polygon may be divided into three classes: \begin{enumerate} \item The sequence \textbf{S(p)} is finite (that is, \textbf{T(v)} is undefined for its last term \textbf{v}). \item The sequence \textbf{S(p)} is infinite and periodic, maybe with some preperiod. \item The sequence \textbf{S(p)} is infinite and non-periodic. \end{enumerate} In this problem, the "billiard table", or the figure \textbf{G}, is the parallelogram \textbf{OABC}, where \textbf{O = (0, 0)}. You are given the coordinates of some "table" and a point strictly outside of it. Find the type of this sequence. If this sequence is periodic (of type \textbf{2}), find the period. \InputFile The first line of input contains one integer \textbf{T} (\textbf{1} ≤ \textbf{T} ≤ \textbf{3·10^5}) which is the number of test cases. Each of the next \textbf{T}lines contains six integers \textbf{x_A}, \textbf{y_A}, \textbf{x_C}, \textbf{y_C}, \textbf{x_p}, \textbf{y_p} not exceeding \textbf{10^9} by absolute value: the coordinates of points \textbf{A}, \textbf{C }and \textbf{p}. It is guaranteed that \textbf{OABC} is a non-degenerate parallelogram and that \textbf{p} lies strictly outside of it. \OutputFile Print one number per test case. This number should be: \begin{itemize} \item \textbf{-1} if the sequence is finite, \item Period length if the sequence is periodic, \item \textbf{-3} if the sequence is infinite and non-periodic. \end{itemize} Each number must be printed on a separate line.
Time limit 1 second
Memory limit 128 MiB
Input example #1
2
0 2 2 0 1 -1
1 0 0 1 -1 1
Output example #1
4
-1
Source 2013 Petrozavodsk, MIPT contest, August 25, Problem D