#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef string str;

#define pb push_back
#define ppp pop_back
#define pii pair<int,int>
#define fi first
#define se second
#define stie std::tie
#define vec vector
#define	forn(i, l, r) for (int i=l; i<=r; i++)
#define emp empty
#define ins insert
#define cle clear
#define era erase
#define que queue
#define pque priority_queue
#define mset multiset
#define deq deque
#define con const
#define rsz resize
#define ass assign

template<class T> void mini(T& a, T b) { a = min(a, b); }
template<class T> void maxi(T& a, T b) { a = max(a, b); }
template<class T1, class T2> ostream& operator<<(ostream &out, pair<T1, T2> a) { return out << a.first << " " << a.second; }
template<class T> ostream& operator<<(ostream& out, vector<T> a) { for (auto& i : a) out << i << " "; return out; }
template<class T> ostream& operator<<(ostream& out, set<T> a) { for (auto& i : a) out << i << " "; return out; }
template<class T> ostream& operator<<(ostream& out, multiset<T> a) { for (auto& i : a) out << i << " "; return out; }
template<class T1, class T2> ostream& operator<<(ostream& out, map<T1, T2> a) { for (auto& i : a) out << i << "\n"; return out; }
template<class T1, class T2> ostream& operator<<(ostream& out, unordered_map<T1, T2> a) { for (auto& i : a) out << i << "\n"; return out; }
template<class T> ostream& operator<<(ostream& out, queue<T> a) { while (!a.empty()) { cout << a.front() << " "; a.pop(); } return out; }
template<class T> ostream& operator<<(ostream& out, deque<T> a) { while (!a.empty()) { cout << a.front() << " "; a.pop_front(); } return out; }
template<class T> ostream& operator<<(ostream& out, priority_queue<T> a) { while (!a.empty()) { cout << a.top() << "\n"; a.pop(); } return out; }
template<class T> void out(T a) { cout << a << endl; }
template<class T1, class T2> void out(T1 a, T2 b) { cout << a << " " << b << endl; }
template<class T1, class T2, class T3> void out(T1 a, T2 b, T3 c) { cout << a << " " << b << " " << c << endl; }
void out() { cout << "OK" << endl; }
template<class T> void sort(vector<T>& a) { sort(a.begin(), a.end()); }
template<class T> void uniq(vector<T>& a) { sort(a); a.erase(unique(a.begin(), a.end()), a.end()); }
template<class T> vector<T> set_int(vector<T> a, vector<T> b) { sort(a); sort(b); vector<T> res; set_intersection(a.begin(), a.end(), b.begin(), b.end(), back_inserter(res)); return res; }
#if __SIZEOF_INT128__ >= 16
typedef __int128 LL;
istream& operator>>(istream& in, __int128& a) { int64_t b; cin >> b; a = b; return in; }
ostream& operator<<(ostream& out, const __int128 a) {
    unsigned __int128 b = a < 0 ? -a : a; char buf[128]; char* c = end(buf); do { --c; *c = "0123456789"[b % 10]; b /= 10; } while (b);
    if (a < 0) { --c; *c = '-'; } int len = end(buf) - c; out.rdbuf()->sputn(c, len); return out;
}
#endif

con int N = 2e5 + 5;
con int inf = 1e9;

vec<pii> lr[N];
vec<pii> du[N];
vec<pii> ur[2*N];
vec<pii> ul[2*N];
vec<pii> dr[2*N];
vec<pii> dl[2*N];

int sol(vec<pii>& a) {
	sort(a);
	int res = inf;
	int mx = -inf;
	for (pii i : a) {
		if (i.se && mx > -inf) {
			mini(res, 10 * (i.fi - mx));
		}
		if (!i.se) maxi(mx, i.fi);
	}
    if (res == inf) return 2 * res;
	return res;
}

void Solve() {
	int n;
	cin >> n;
	forn(i, 1, n) {
		int x, y;
		char c;
		cin >> x >> y >> c;
		if (c == 'U') {
			du[x].pb({y, 0});
			ur[x+y].pb({x, 1});
			ul[x-y+N].pb({x, 0});
		}
		else if (c == 'D') {
			du[x].pb({y, 1});
			dr[x-y+N].pb({x, 1});
			dl[x+y].pb({x, 0});
		}
		else if (c == 'L') {
			lr[y].pb({x, 1});
			ul[x-y+N].pb({x, 1});
			dl[x+y].pb({x, 1});
		}
		else if (c == 'R') {
			lr[y].pb({x, 0});
			ur[x+y].pb({x, 0});
			dr[x-y+N].pb({x, 0});
		}
	}
	int ans = inf;
	forn(i, 0, N-1) {
		mini(ans, sol(du[i]) / 2);
		mini(ans, sol(lr[i]) / 2);
	}
	forn(i, 0, 2*N-1) {
		mini(ans, sol(ur[i]));
		mini(ans, sol(ul[i]));
		mini(ans, sol(dr[i]));
		mini(ans, sol(dl[i]));
	}
	if (ans == inf) {
        out("SAFE");
        return;
	}
	out(ans);
}

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
#endif
	Solve();
	return 0;
}


Measured memory_bytes: 69096