#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <map>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <string.h>
#include <cstdio>
#include <tuple>
#include <numeric>
using namespace std; using ll = long long; using ld = long double;  using pll = pair<ll, ll>;
using vl = vector<ll>; using vll = vector<vl>; using vpll = vector<pll>;
using vs = vector<string>; using tll = tuple<ll, ll, ll>; using vtll = vector<tll>;
const ld PI = 3.1415926535897932;
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define co(i) cout << i << endl;
#define co2(i,j) cout << i << " " << j << endl;
#define co3(i) cout << i << " ";
#define po(i) cout << i.first << " " << i.second << endl;
void in() {}
void debug_out() { cerr << endl; }
template<typename Head, typename... Tail>
void debug_out(Head h, Tail... t) {
	cerr << " " << h;
	if (sizeof...(t) > 0) cerr << " :";
	debug_out(t...);
}
template <typename T>
ostream& operator<<(ostream& os, vector<T> vec) {
	for (size_t i = 0; i < vec.size(); i++)os << vec[i] << (i + 1 == vec.size() ? "" : " ");
	return os;
}
ll ctoi(char c) {
	switch (c) {
	case '0': return 0; case '1': return 1; case '2': return 2;
	case '3': return 3; case '4': return 4; case '5': return 5;
	case '6': return 6; case '7': return 7; case '8': return 8;
	case '9': return 9;  default: return 0;
	}
}
bool pairCompare(const pll firstElof, pll secondElof)
{
	return firstElof.first > secondElof.first;
}
//**Snippetリスト**//
//rep, vin, all, iteLoop, bitSearch, bitList, nod, digitDP, treeDP//
//gcdlcm, isPrime, eratos, primeFactorize, Npow, combination, divisor, modinv, doubling//
//dfs, bfs, dijkstra, WarshallFloyd, BellmanFord, UnionFind, Kruskal, RMQ, LCA//
ll i, j, k, l; ll N, M, K, H, W, L, X, Y, Z, R, Q;
ll MOD = 1000000007, INF = 1LL << 60, ans = 0, z = 0, o = 1;
vl flag, D; vll path; vll A;
//***********//
int main() {
	vll Xr(200001), Xl(200001), Yu(200001), Yd(200001);
	// Z1 ↑← Z2 →↓ Z3 →↑ Z4 ↓←
	vll Vr(400001), Vl(400001), Vu(400001), Vd(400001);
	vll Wr(400001), Wl(400001), Wu(400001), Wd(400001);
	cin >> N;
	for (i = 0; i < N; i++) {
		ll x, y; char u;
		cin >> x >> y >> u;
		if (u == 'U') {
			Yu[x].push_back(y);
			Vu[x+y].push_back(x);
			Wu[y-x+200000].push_back(x);
		}
		if (u == 'D') {
			Yd[x].push_back(y);
			Vd[x+y].push_back(x);
			Wd[y - x + 200000].push_back(x);
		}
		if (u == 'R') {
			Xr[y].push_back(x);
			Vr[x+y].push_back(x);
			Wr[y - x + 200000].push_back(x);
		}
		if (u == 'L') {
			Xl[y].push_back(x);
			Vl[x+y].push_back(x);
			Wl[y - x + 200000].push_back(x);
		}
	}
	ll ans = INF;
	for (i = 0; i <= 200000; i++) {
		vl A = Xr[i], B = Xl[i];
		ll a = 0; ll b = 0;
		if (A.size() > 0 && B.size() > 0) {
			sort(A.begin(), A.end());
			sort(B.begin(), B.end());
			B.push_back(INF / 100);
			for (j = 0; j < A.size(); j++) {
				ans = min(ans, (*lower_bound(B.begin(), B.end(), A[j]) - A[j]) * 5);
			}
		}
		A = Yu[i], B = Yd[i];
		a = 0; b = 0;
		if (A.size() > 0 && B.size() > 0) {
			sort(A.begin(), A.end());
			sort(B.begin(), B.end());
			B.push_back(INF / 100);
			for (j = 0; j < A.size(); j++) {
				ans = min(ans, (*lower_bound(B.begin(), B.end(), A[j]) - A[j]) * 5);
			}
		}
	}
	for (i=0;i<=400000;i++){
		vl A = Vr[i], B = Vu[i];
		ll a = 0; ll b = 0;
		if (A.size() > 0 && B.size() > 0) {
			sort(A.begin(), A.end());
			sort(B.begin(), B.end());
			B.push_back(INF/100);
			for (j = 0; j < A.size(); j++) {
				ans = min(ans, (*lower_bound(B.begin(), B.end(), A[j]) - A[j]) * 10);
			}
		}
		A = Vd[i], B = Vl[i];
		a = 0; b = 0;
		if (A.size() > 0 && B.size() > 0) {
			sort(A.begin(), A.end());
			sort(B.begin(), B.end());
			B.push_back(INF/100);
			for (j = 0; j < A.size(); j++) {
				ans = min(ans, (*lower_bound(B.begin(), B.end(), A[j]) - A[j]) * 10);
			}
		}
		A = Wu[i], B = Wl[i];
		a = 0; b = 0;
		if (A.size() > 0 && B.size() > 0) {
			sort(A.begin(), A.end());
			sort(B.begin(), B.end());
			B.push_back(INF/100);
			for (j = 0; j < A.size(); j++) {
				ans = min(ans, (*lower_bound(B.begin(), B.end(), A[j]) - A[j]) * 10);
			}
		}
		A = Wr[i], B = Wd[i];
		a = 0; b = 0;
		if (A.size() > 0 && B.size() > 0) {
			sort(A.begin(), A.end());
			sort(B.begin(), B.end());
			B.push_back(INF/100);
			for (j = 0; j < A.size(); j++) {
				ans = min(ans, (*lower_bound(B.begin(), B.end(), A[j]) - A[j])*10);
			}
		}
	}
	if (ans >= 1000000000) cout << "SAFE";
	else cout << ans;
}

Measured memory_bytes: 115556