The Association for Cutting Machinery (ACM) has just announced a new portable laser capable of slicing through six-inch sheet metal like a hot knife through Jello (or butter, if you are a traditionalist). The laser is mounted on a small motorized vehicle that is programmed to drive over the surface being cut. The vehicle has two operations: it can move forward in a straight line, cutting the surface beneath it as it goes, or it can pivot in place to face a different direction.
Trouble arises if the laser cuts a closed hole in the surface: the material inside the hole drops out, and the vehicle falls in. Your task is to take a sequence of instructions and decide whether they cause the vehicle to cut a hole -- that is, whether the path it cuts ever intersects itself. Assume the surface is infinite and that the laser makes a cut of zero width.
The laser starts at (0, 0) facing the positive Y direction. Instructions always alternate between a turn and a move. Every instruction is relative: a turn rotates the vehicle by a given number of degrees counterclockwise from its current heading (a negative value turns clockwise), and a move drives it forward a given distance along its current heading.
For example, consider this sequence:
TURN -90
MOVE 10
TURN 90
MOVE 5
TURN 135
MOVE 10
TURN -90
MOVE 5
The vehicle behaves as follows:
The input is a series of at most 20 data sets. Each data set begins with a line containing an integer $N$, the number of turn/move pairs to execute ($1 \le N \le 100$). A line containing 0 marks the end of the input.
The next $N$ lines each contain two integers $T$ and $M$: $T$ is the number of degrees to turn ($-179 \le T \le 179$), and $M$ is the distance to move after turning ($1 \le M \le 100$).
Although $T$ and $M$ are integers, the laser's position is generally not integral. The data is chosen so that any two cuts that intersect do so well away from their endpoints, and any two cuts that do not intersect stay clearly separated.
For each data set, output the number of the first move that creates a hole (for example, output 3 if the third move creates the hole). Once a hole is created, the remaining instructions are irrelevant, but they must still be read from the input.
If the entire sequence can be carried out without creating a hole, output the word SAFE.