#include <bits/stdc++.h>
#define maxn 200666
using namespace std;
vector<int> X[maxn], Y[maxn], XY[maxn * 2], YX[maxn * 2];
int x[maxn], y[maxn];
char u[maxn];
int main() {
	int n;
	scanf("%d", &n);
	for(int i = 1; i <= n; ++i) scanf("%d%d %c", x + i, y + i, u + i);
	for(int i = 1; i <= n; ++i) {
		X[x[i]].push_back(i);
		Y[y[i]].push_back(i);
		XY[x[i] + y[i]].push_back(i);
		YX[x[i] - y[i] + maxn].push_back(i);
	}
	for(int i = 0; i < maxn; ++i) {
		sort(X[i].begin(), X[i].end(), [&](int a, int b){return y[a] < y[b];});
		sort(Y[i].begin(), Y[i].end(), [&](int a, int b){return x[a] < x[b];});
	}
	for(int i = 0; i < 2 * maxn; ++i) {
		sort(XY[i].begin(), XY[i].end(), [&](int a, int b){return x[a] < x[b];});
		sort(YX[i].begin(), YX[i].end(), [&](int a, int b){return x[a] < x[b];});
	}
	int Ans = 0x3F3F3F3F;
	for(int i = 0; i < maxn; ++i) {
		if(X[i].size() >= 2) {
			int lasU = -1;
			for(int o : X[i]) {
				if(u[o] == 'U') lasU = o;
				else if(u[o] == 'D') {
					if(lasU != -1) {
						Ans = min(Ans, 5 * (y[o] - y[lasU]));
					}
				}
			}
		}
	}
	for(int i = 0; i < maxn; ++i) {
		if(Y[i].size() >= 2) {
			int lasR = -1;
			for(int o : Y[i]) {
				if(u[o] == 'R') lasR = o;
				else if(u[o] == 'L') {
					if(lasR != -1) {
						Ans = min(Ans, 5 * (x[o] - x[lasR]));
					}
				}
			}
		}
	}
	for(int i = 0; i < 2 * maxn; ++i) {
		if(XY[i].size() >= 2) {
			int lasR = -1, lasD = -1;
			for(int o : XY[i]) {
				if(u[o] == 'R') lasR = o;
				else if(u[o] == 'D') lasD = o;
				else if(u[o] == 'U') {
					if(lasR != -1) {
						Ans = min(Ans, 10 * (x[o] - x[lasR]));
					}
				} else if(u[o] == 'L') {
					if(lasD != -1) {
						Ans = min(Ans, 10 * (x[o] - x[lasD]));
					}
				}
			}
		}
		if(YX[i].size() >= 2) {
			int lasR = -1, lasU = -1;
			for(int o : YX[i]) {
				if(u[o] == 'R') lasR = o;
				else if(u[o] == 'U') lasU = o;
				else if(u[o] == 'D') {
					if(lasR != -1) {
						Ans = min(Ans, 10 * (x[o] - x[lasR]));
					}
				} else if(u[o] == 'L') {
					if(lasU != -1) {
						Ans = min(Ans, 10 * (x[o] - x[lasU]));
					}
				}
			}
		}
	}
	if(Ans == 0x3F3F3F3F) puts("SAFE");
	else printf("%d", Ans);
	return 0;
}