// Arup Guha
// 6/29/2013
// Solution to 2012 Mid-Central Regional Contest Problem C: Any Way You Slice It

// Note: This is a pretty ugly solution. I was being lazy and never made a point or line segment class.
//       I just stored line segments in arrays of size 4, which is pretty awful, style-wise. Basically,
//       we just need to detect line segment intersection.

import java.util.*;

public class c {

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);
		int n = stdin.nextInt();

		// Go through each case.
		while (n != 0) {

			double[][] linesegs = new double[n][4];
			double x = 0, y = 0, angle = Math.PI/2;
			boolean safe = true;
			int answer = -1;

			// Go through each line segment.
			for (int i=0; i<n; i++) {

				// Update the new angle.
				double dAngle = stdin.nextInt();
				angle = angle + dAngle*Math.PI/180;
				int dist = stdin.nextInt();

				// Store the previous position.
				linesegs[i][0] = x;
				linesegs[i][1] = y;

				// Calculate the new position.
				x = x + Math.cos(angle)*dist;
				y = y + Math.sin(angle)*dist;

				linesegs[i][2] = x;
				linesegs[i][3] = y;

				// See if this new segment intersects with previous ones.
				boolean intersects = getIntersections(linesegs, i);

				// Mark the first hole created.
				if (safe && intersects) {
					answer = i+1;
					safe = false;
				}
			}

			// Output final result.
			if (safe)
				System.out.println("SAFE");
			else
				System.out.println(answer);

			// Go to the next case.
			n = stdin.nextInt();
		}

	}

	// Returns true iff segment n intersects with at least one of segments 0 through n-1.
	public static boolean getIntersections(double[][] linesegs, int n) {

		// See if any of these cross.
		for (int i=0; i<n-1; i++)
			if (crosses(linesegs[i], linesegs[n]))
				return true;

		// Nothing crossed.
		return false;
	}

	public static boolean crosses(double[] seg1, double[] seg2) {

		// Get movement vectors.
		double dx1 = seg1[2] - seg1[0];
		double dy1 = seg1[3] - seg1[1];
		double dx2 = seg2[2] - seg2[0];
		double dy2 = seg2[3] - seg2[1];

		// Use Cramer's to solve thee system of 2 equations.
		double den = det(dx1, -dx2, dy1, -dy2);

		// Singular system.
		if (Math.abs(den) < 1e-9) {

			// Lines are same so check for overlap.
			if (Math.abs(dy1*(seg2[0]-seg1[0]) - (seg2[1]-seg1[1])*dx1) < 1e-9)
				return crossOnLine(seg1, seg2);

			// Lines don't intersect, so the segments don't either.
			else
				return false;
		}

		// Regular system of equations.
		else {

			double num1 = det(seg2[0]-seg1[0], -dx2, seg2[1]-seg1[1], -dy2);
			double num2 = det(dx1, seg2[0]-seg1[0] , dy1, seg2[1]-seg1[1]);

			double alpha = num1/den;
			double beta = num2/den;

			// Intersection criteria.
			if (0 <= alpha && alpha <=1 && 0 <= beta && beta <= 1)
				return true;
			else
				return false;
		}


	}

	// Returns true if these two segments (which are on the same line), intersect.
	public static boolean crossOnLine(double[] seg1, double[] seg2) {
		return onSeg(seg1, seg2[0], seg2[1]) || onSeg(seg1, seg2[2], seg2[3]) ||
			   onSeg(seg2, seg1[0], seg1[1]) || onSeg(seg2, seg1[2], seg1[3]);
	}

	// Returns true iff the point (x,y) on the line seg, is also on the segment seg.
	public static boolean onSeg(double[] seg, double x, double y) {

		double minx = Math.min(seg[0], seg[2]);
		double maxx = Math.max(seg[0], seg[2]);
		double miny = Math.min(seg[1], seg[3]);
		double maxy = Math.max(seg[1], seg[3]);

		return minx <= x && x <= maxx && miny <= y && y <= maxy;
	}

	public static double det(double a, double b, double c, double d) {
		return a*d - b*c;
	}
}