#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define PB push_back
constexpr int kN = int(2E5 + 10), kInf = int(1E9 + 10);
int x[kN], y[kN], dx[kN], dy[kN];
string u[kN];
vector<int> vx[kN], vy[kN], va[kN << 1], vb[kN << 1];
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	vector<int> A, B, C, D;
	int n, ans = kInf, pos, sz;
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> x[i] >> y[i] >> u[i];
	for (int i = 1; i <= n; i++)
		if (u[i] == "U") dx[i] = 0, dy[i] = 1;
		else if (u[i] == "R") dx[i] = 1, dy[i] = 0;
		else if (u[i] == "D") dx[i] = 0, dy[i] = -1;
		else dx[i] = -1, dy[i] = 0;
	for (int i = 1; i <= n; i++) vx[x[i]].PB(i);
	for (int i = 1; i <= n; i++) vy[y[i]].PB(i);
	for (int i = 0; i < kN; i++) if (!vx[i].empty()) {
		A.clear(), B.clear();
		for (int j : vx[i]) if (dx[j] == 0) {
			if (dy[j] == 1) A.PB(y[j]);
			else B.PB(y[j]);
		}
		sort(A.begin(), A.end());
		sort(B.begin(), B.end());
		pos = 0, sz = int(B.size());
		for (int j : A) {
			while (pos < sz && B[pos] < j) pos++;
			if (pos < sz && B[pos] > j) ans = min(ans, B[pos] - j);
		}
	}
	for (int i = 0; i < kN; i++) if (!vy[i].empty()) {
		A.clear(), B.clear();
		for (int j : vy[i]) if (dy[j] == 0) {
			if (dx[j] == 1) A.PB(x[j]);
			else B.PB(x[j]);
		}
		sort(A.begin(), A.end());
		sort(B.begin(), B.end());
		pos = 0, sz = int(B.size());
		for (int j : A) {
			while (pos < sz && B[pos] < j) pos++;
			if (pos < sz && B[pos] > j) ans = min(ans, B[pos] - j);
		}
	}
	for (int i = 1; i <= n; i++) va[x[i] + y[i]].PB(i);
	for (int i = 1; i <= n; i++) vb[x[i] - y[i] + kN].PB(i);
	for (int i = 0; i < (kN << 1); i++) if (!va[i].empty()) {
		A.clear(), B.clear(), C.clear(), D.clear();
		for (int j : va[i]) if (dx[j] == 1) A.PB(x[j]);
		else if (dx[j] == -1) B.PB(x[j]);
		else if (dy[j] == 1) C.PB(x[j]);
		else D.PB(x[j]);
		sort(B.begin(), B.end());
		sort(D.begin(), D.end());
		pos = 0, sz = int(B.size());
		for (int j : D) {
			while (pos < sz && B[pos] < j) pos++;
			if (pos < sz && B[pos] > j) ans = min(ans, (B[pos] - j) << 1);
		}
		sort(A.begin(), A.end());
		sort(C.begin(), C.end());
		pos = 0, sz = int(C.size());
		for (int j : A) {
			while (pos < sz && C[pos] < j) pos++;
			if (pos < sz && C[pos] > j) ans = min(ans, (C[pos] - j) << 1);
		}
	}
	for (int i = 0; i < (kN << 1); i++) if (!vb[i].empty()) {
		A.clear(), B.clear(), C.clear(), D.clear();
		for (int j : vb[i]) if (dx[j] == 1) A.PB(x[j]);
		else if (dx[j] == -1) B.PB(x[j]);
		else if (dy[j] == 1) C.PB(x[j]);
		else D.PB(x[j]);
		sort(A.begin(), A.end());
		sort(D.begin(), D.end());
		pos = 0, sz = int(D.size());
		for (int j : A) {
			while (pos < sz && D[pos] < j) pos++;
			if (pos < sz && D[pos] > j) ans = min(ans, (D[pos] - j) << 1);
		}
		sort(B.begin(), B.end());
		sort(C.begin(), C.end());
		pos = 0, sz = int(B.size());
		for (int j : C) {
			while (pos < sz && B[pos] < j) pos++;
			if (pos < sz && B[pos] > j) ans = min(ans, (B[pos] - j) << 1);
		}
	}
	if (ans == kInf) printf("SAFE\n");
	else printf("%d\n", ans * 5);

	/*
	 * x[i] + dx[i] * t = x[j] + dx[j] * t
	 * y[i] + dy[i] * t = y[j] + dy[j] * t
	 * case 1 : dx[i] = dx[j]
	 * 	dx[i] = 1  X
	 * 	dx[i] = -1 X
	 * 	dx[i] = 0  -> dy[i] = -dy[j] and y[i] == y[j]
	 * 
	 */

}


Measured memory_bytes: 61220