eolymp
bolt
Try our new interface for solving problems
Problems

Ray of Light

Ray of Light

\textit{Light my mirror! Tell} \textit{Yes, the whole truth reported} \textit{Tell the me a secret,} \textit{Set the source of light?} Conversation of a student of the Institute of Precision Optics and Magic with a magic mirror On the coordinate plate there is a field limited square with vertices (\textbf{0}, \textbf{0}), (\textbf{N}, \textbf{0}), (\textbf{N}, \textbf{N}), (\textbf{0}, \textbf{N}). In some interior points of the field with integer coordinates two-way mirrors are established and placed at an angle of \textbf{45°} to the coordinate axis and reflect light (at each point may be no more than one mirror, which is installed so that its centre coincided with the corresponding period). At the border of mirrors there. Mirrors come in two types: a mirror of the first type parallel to the line \textbf{x} + \textbf{y} = \textbf{0}, and the second type - direct \textbf{x} -- \textbf{y} = \textbf{0}. All mirrors have a negligible thickness. Examples of reflection ray mirrors of different types are shown in the figures. \includegraphics{https://static.e-olymp.com/content/d2/d28942435a0adec842955da224f9e079e1a10d00.jpg} Mirror the second type Mirror of the first type In addition, at the right boundary at the point with coordinates (\textbf{N}, \textbf{T}) is the target that you want to get a beam. Write a program that finds a point with integer coordinates on the boundary, where you can locate the source of light so that light passing through the field, and perhaps reflected by the multiple mirror belonging to a given target. The light source can not be located at a single point on a target and should be installed so that it emits a beam was perpendicular to the border at this point. \InputFile The first line contains three integers: the size of the field \textbf{N}, the coordinate point with the target \textbf{T} and the number of mirrors on the field of \textbf{M} (\textbf{0} < \textbf{T} < \textbf{N} < \textbf{500}, \textbf{0} <= \textbf{M} <= (\textbf{N}-\textbf{1})\textbf{^2}). This is followed by \textbf{M} lines, each of which describes one of the mirror three integers \textbf{x}, \textbf{y} and \textbf{a}, where (\textbf{x}, \textbf{y}) - coordinates of the point at which the mirror is located (\textbf{0} < \textbf{x}, \textbf{y} < \textbf{N}), \textbf{a} - type of mirror (\textbf{1} or \textbf{2}). \OutputFile In the first line, print out two integers - the coordinates of points on the boundary of the field, which should be a source of light. In the second line print out an integer specifying how many times the beam will be reflected in the mirrors along the path from source to target. If such a point does not exist, output a single number \textbf{-1}. If there are a few points, you can choose any of them.
Time limit 1 second
Memory limit 64 MiB
Input example #1
5 4 4
1 1 1
3 1 2
1 3 2
3 4 2
Output example #1
5 3
4
Author Янушкевич В.А.