//#pragma warning(disable:4996)
#include <iostream>
#include <vector>
#include <limits.h>
#include <algorithm>
#include <string>
#include <math.h>
#include <limits.h>
#include <queue>
#include <map>
#include <set>
#include <iomanip>
#include <bitset>
#include <cassert>
#include <random>
#include <functional>
#include <stack>
#include <iomanip>
#include <cassert>
//#include <boost/multiprecision/cpp_int.hpp>
#include <complex>
#include <cstdio>
#include <list>
#include <bitset>
//#include <stdio.h>

//< in.txt > out.txt
using namespace std;
//std::ios::sync_with_stdio(false);
//std::cin.tie(0);
const long long MOD = 1e9 + 7;
const long long INF = 1e15;
typedef long long LL;
typedef long double LD;
//typedef boost::multiprecision::cpp_int bigint;
typedef pair<LL, LL> PLL;
typedef pair<int, int> PI;
typedef pair<LD, LL> pdl;
typedef pair<LD, LD> pdd;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef unsigned long long ULL;

int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	int N;
	cin >> N;
	vector<vector<PI>> flys;
	flys.resize(4);
	for (int n = 0; n < N; n++) {
		int X, Y;
		string D;
		cin >> X >> Y >> D;
		if (D == string("U"))flys[1].push_back(PI(X, Y));
		else if (D == string("R"))flys[0].push_back(PI(X, Y));
		else if (D == string("D"))flys[3].push_back(PI(X, Y));
		else flys[2].push_back(PI(X, Y));
	}
	int ans = 1e9;
	for (int v = 0; v < 4; v++) {
		sort(flys[(v+1)%4].begin(), flys[(v + 1) % 4].end(), [](PI a, PI b) {
			if (a.first + a.second != b.first + b.second) {
				return a.first + a.second < b.first + b.second;
			}
			else {
				return a.first < b.first;
			}
		});
		sort(flys[(v + 2) % 4].begin(), flys[(v + 2) % 4].end(), [](PI a, PI b) {
			if (a.second != b.second)return a.second < b.second;
			else return a.first < b.first;
		});
		sort(flys[(v + 3) % 4].begin(), flys[(v + 3) % 4].end(), [](PI a, PI b) {
			if (a.first - a.second != b.first - b.second) {
				return a.first - a.second < b.first - b.second;
			}
			else {
				return a.first < b.first;
			}
		});
		for (PI pl : flys[(v + 0) % 4]) {
			int x = pl.first;
			int y = pl.second;
			//s:x'+y' < x+y or x'+y' == x+y&&x' <= x
			int s = -1, e = flys[(v + 1) % 4].size();
			while (e - s > 1) {
				int m = (e + s) / 2;
				int xx = flys[(v + 1) % 4][m].first;
				int yy = flys[(v + 1) % 4][m].second;
				if (xx + yy < x + y)s = m;
				else if (xx + yy == x + y && xx <= x)s = m;
				else e = m;
			}
			if (e != flys[(v + 1) % 4].size()) {
				if (flys[(v + 1) % 4][e].first + flys[(v + 1) % 4][e].second == x + y) {
					ans = min(ans, 10 * (flys[(v + 1) % 4][e].first - x));
				}
			}
			//s:y' < y or y == y'&&x' <= x
			s = -1, e = flys[(v + 2) % 4].size();
			while (e - s > 1) {
				int m = (e + s) / 2;
				int xx = flys[(v + 2) % 4][m].first;
				int yy = flys[(v + 2) % 4][m].second;
				if (yy < y)s = m;
				else if (yy == y && xx <= x)s = m;
				else e = m;
			}
			if (e != flys[(v + 2) % 4].size()) {
				if (flys[(v + 2) % 4][e].second == y) {
					ans = min(ans, 5 * (flys[(v + 2) % 4][e].first - x));
				}
			}

			//s:x'-y' < x - y or (x'-y' == x - y && x' <= x)
			s = -1, e = flys[(v + 3) % 4].size();
			while (e - s > 1) {
				int m = (e + s) / 2;
				int xx = flys[(v + 3) % 4][m].first;
				int yy = flys[(v + 3) % 4][m].second;
				if (xx - yy < x - y)s = m;
				else if (xx - yy == x - y && xx <= x)s = m;
				else e = m;
			}
			if (e != flys[(v + 3) % 4].size()) {
				if (flys[(v + 3) % 4][e].first - flys[(v + 3) % 4][e].second == x - y) {
					ans = min(ans, 10 * (flys[(v + 3) % 4][e].second - y));
				}
			}
		}
		for (int vv = 0; vv < 4; vv++) {
			for (PI& p : flys[vv]) {
				int x = p.first;
				int y = p.second;
				p = PI(y, -x);
			}
		}
	}
	if (ans != 1e9)cout << ans << "\n";
	else cout << "SAFE\n";
	return 0;
}

Measured memory_bytes: 5244