// I make this just for fun because i'm done
// Aimi Haraguni >> Konomi Suzuki >> Yui >>  Ikimono Gakari >> Garnidelia >> Kalafina >> Eir Aoi.   .. dude? 
// problems that involves any kind of persistent data structures are the best of the best, are not them?
 
// #pragma GCC optimize ("Ofast,unroll-loops")
// #pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
 
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
 
#define pb push_back
#define ff  first
#define ss second
#define tm1 first
#define tm2 second.first
#define tm3 second.second
#define sz(x) ll(x.size())
#define all(v) (v).begin(), (v).end()
// #define fill(x, v) memset(x, v, sizeof(x))
#define FER(i,a,b) for(int i=int(a); i< int(b); ++i)
#define IFR(i,a,b) for(int i=int(a); i>=int(b); --i )
#define fastio ios_base::sync_with_stdio(0); cin.tie(0)
 
// #define N 21000000
// #define M 19
// #define mod 
#define mod  998244353
#define mod1 1000000007
#define mod2 1000000009
#define bas 987625403
#define sqr(x) (x)*(x)  
#define INF 3000000000000000000
 
using namespace std;
using namespace __gnu_pbds;
 
typedef long long ll;
typedef pair<ll, ll> ii;
typedef pair<ll, ii > tri;
typedef vector<ll> vi;
typedef vector<vi> vv;
typedef vector<ii> vii;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> S_t;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
 
#define trace(...) fff(#__VA_ARGS__, __VA_ARGS__)
template<typename t> void fff(const char* x, t&& val1) { cout<<x<< " : "<<val1<<"\n";}
template<typename t1, typename... t2> void fff(const char* x, t1&& val1, t2&&... val2){
    const char* xd=strchr(x+1, ',');
    cout.write(x, xd-x)<<" : "<<val1<<" | ";
    fff(xd+1, val2...);
}
 
inline ll add(ll a, ll b, ll m) { return a+b < m? a+b : a+b-m;}
inline ll rem(ll a, ll b, ll m) { return a >= b? a - b: a- b + m;}
inline ll mul(ll a, ll b, ll m) { return (long long) a*b%m;}
inline void Mul(ll &a, ll b, ll m) { a = (long long) a*b%m;}
inline ll bp(ll a, ll p, ll m){
	ll ret;
	for(ret = 1; p; p>>=1, Mul(a, a, m)) (p & 1) && (Mul(ret, a, m), 1);
	return ret;
}
 
static inline void read(ll &x) { cin>>x;}

// 0 es U
// 1 es R
// 2 es D
// 3 es L

// 0 necesita 2
// L necesida R

inline ll choca(ll x){
	if(x == 0) return 2;
	if(x == 2) return 0;
	if(x == 3) return 1;
	return 3;
}

vi X[1<<20][4], Y[1<<20][4];
vi S[1<<20][4], D[1<<20][4];

inline ll eval(char s){
	if(s == 'U') return 0;
	if(s == 'R') return 1;
	if(s == 'D') return 2;
	return 3;
}

ll zzs, zzd;
map<ll, ll > mts, mtd;

inline ll OCS(ll x){
	if(mts.count(x)) return mts[x];
	mts[x] = zzs++;
	return mts[x];
}

inline ll OCD(ll x){
	if(mtd.count(x)) return mtd[x];
	mtd[x] = zzd++;
	return mtd[x];
}


int main(){
	ll n; read(n);
	vector<pair<char, ii>> tnt;
	map<pair<char, ii>, ll> aea;
	FER(i, 0, n){
		ll x, y; cin>>x>>y;
		char s; cin>>s;
		tnt.pb({s, {x, y}});
		aea[{s, {x, y}}]++;
		ll id = eval(s), idx;
		X[x][id].pb(y);
		Y[y][id].pb(x);
		idx = OCS(x + y);
		S[idx][id].pb(y);
		idx = OCD(y - x);
		D[idx][id].pb(y);
	}
	for(auto xd: aea){
		if(xd.ss > 1){
			cout<<0<<"\n";
			return 0;
		}
	}
	FER(i, 0, 1<<20){
		FER(j, 0, 4){
			sort(all(X[i][j]));
			sort(all(Y[i][j]));
			sort(all(S[i][j]));
			sort(all(D[i][j]));
		}
	}
	// return 0;

	// 0 es U
	// 1 es R
	// 2 es D
	// 3 es L
	
	vi froz;
	for(auto xd: tnt){
		ll id = eval(xd.tm1);
		ll x = xd.tm2;
		ll y = xd.tm3;
		ll idx = choca(id);
		if(xd.tm1 == 'U'){
			ll val, ret;
			auto it = lower_bound(all(X[x][idx]), y+1);
			if(it != X[x][idx].end()){
				val = *it;
				ret = (val - y);
				froz.pb(ret);
			}
			ll index = OCS(x + y);
			it = lower_bound(all(S[index][1]), y+1);
			if(it != S[index][1].end()){
				val = *it;
				ret = (val - y);
				ret<<=1;
				froz.pb(ret);
			}
			index = OCD(y - x);
			it = lower_bound(all(D[index][3]), y+1);
			if(it != D[index][3].end()){
				val = *it;
				ret = (val - y);
				ret<<=1;
				froz.pb(ret);
			}
			// trace(sz(froz));
		}
		else if(xd.tm1 == 'D'){
			// 0 es U
			// 1 es R
			// 2 es D
			// 3 es L
			ll val, ret;
			auto it = lower_bound(all(X[x][idx]), y);
			if(it != X[x][idx].begin()){
				it--;
				val = *it;
				ret = (y - val);
				froz.pb(ret);
			}
			ll index = OCS(x + y);
			it = lower_bound(all(S[index][3]), y);
			if(it != S[index][3].begin()){
				it--;
				val = *it;
				ret = (y - val);
				ret<<=1;
				froz.pb(ret);
			}
			index = OCD(y - x);
			it = lower_bound(all(D[index][1]), y);
			if(it != D[index][1].begin()){
				it--;
				val = *it;
				ret = (y - val);
				ret<<=1;
				froz.pb(ret);
			}
			// trace(sz(froz));
		} 
		else if(xd.tm1 == 'L'){
			// 0 es U
			// 1 es R
			// 2 es D
			// 3 es L
			ll val, ret;
			auto it = lower_bound(all(Y[y][idx]), x);
			if(it != Y[y][idx].begin()){
				it--;
				val = *it;
				ret = (x - val);
				ret<<=1;
				froz.pb(ret);
			}
			// trace("aea", sz(froz));
			ll index = OCS(x + y);
			it = lower_bound(all(S[index][2]), y+1);
			if(it != S[index][2].end()){
				// it--;
				val = *it;
				ret = (val-y);
				ret<<=1;
				froz.pb(ret);
			}
			index = OCD(y - x);
			it = lower_bound(all(D[index][0]), y);
			if(it != D[index][0].begin()){
				it--;
				val = *it;
				ret = (y-val);
				ret<<=1;
				froz.pb(ret);
			}
			// trace(sz(froz), "ga");
		}
		else{
			// 0 es U
			// 1 es R
			// 2 es D
			// 3 es L
			ll val, ret;
			auto it = lower_bound(all(Y[y][idx]), x+1);
			if(it != Y[y][idx].end()){
				// it--;
				val = *it;
				ret = (val-x);
				froz.pb(ret);
			}
			ll index = OCS(x + y);
			it = lower_bound(all(S[index][0]), y);
			if(it != S[index][0].begin()){
				it--;
				val = *it;
				ret = (y - val);
				ret<<=1;
				froz.pb(ret);
			}
			index = OCD(y - x);
			it = lower_bound(all(D[index][2]), y+1);
			if(it != D[index][2].end()){
				// it--;
				val = *it;
				ret = (val-y);
				ret<<=1;
				froz.pb(ret);
			}
		}
	}
	if(!sz(froz)){
		cout<<"SAFE\n";
		return 0;
	}
	sort(all(froz));
	reverse(all(froz));
	auto resp = froz.back();
	resp*=5;
	cout<<resp<<"\n";
	return 0;
}

Measured memory_bytes: 464848