// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") // codeforces
//#pragma GCC target("avx,avx2,fma")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native") // yandex

#include "bits/stdc++.h"
using namespace std;
#ifndef LOCAL
#define endl '\n'
#endif

#define fr(i, a, b) for(int i = a; i <= b; i++)
#define rf(i, a, b) for(int i = a; i >= b; i--)
#define pf push_front
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)x.size()
#define lb lower_bound
#define ub upper_bound

typedef long long ll;
typedef long double f80;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

int pct(int x) { return __builtin_popcount(x); }
int pct(ll x) { return __builtin_popcountll(x); }
int bt(int x) { return 31 - __builtin_clz(x); } // floor(log2(x))
int bt(ll x) { return 63 - __builtin_clzll(x); } // floor(log2(x))
int cdiv(int a, int b) { return a / b + !(a < 0 || a % b == 0); }
ll cdiv(ll a, ll b) { return a / b + !(a < 0 || a % b == 0); }
int nxt_C(int x) { int c = x & -x, r = x + c; return (((r ^ x) >> 2) / c) | r; }
ll nxt_C(ll x) { ll c = x & -x, r = x + c; return (((r ^ x) >> 2) / c) | r; }

vector<int> get_bits(int mask) {
	vector<int> bb;
	while(mask) { int b = bt(mask); bb.pb(b); mask ^= (1 << b); }
	reverse(all(bb));
	return bb;
}

int get_mask(vector<int> v) {
	int mask = 0;
	for(int x : v) { mask ^= (1 << x); }
	return mask;
}

template<typename T>
void uniq(vector<T> &v) { sort(all(v)); v.resize(unique(all(v)) - v.begin()); }

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

ll rand(ll l, ll r){
	uniform_int_distribution<ll> uid(l, r);
	return uid(rng);
}

void sc() {}

template <typename Head, typename... Tail>
void sc(Head &H, Tail &... T) { cin >> H; sc(T...); }

#ifdef LOCAL
#define debug(...) cerr << "[L:" << __LINE__ << "][" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

// #ifndef LOCAL
// string to_string(__int128 x) {
// 	string s = "";
// 	bool neg = 0;
// 	if(x < 0) { s += "-"; neg = 1; x = -x; }
// 	if(!x) s += '0';
// 	while(x) {
// 		int rem = x % 10;
// 		s += to_string(rem);
// 		x /= 10;
// 	}
// 	reverse(s.begin() + neg, s.end());
// 	return s;
// }
// #endif

const int mod = 1e9 + 7; // 998244353;

int pwr(int a,ll b) {
	int ans = 1;
	while(b) {
		if(b & 1) ans = (ans * 1LL * a) % mod;
		a = (a * 1LL * a) % mod;
		b >>= 1;
	}
	return ans;
}

/*
	Lookout for overflows!!
	Check array sizes!!
	Clear before test cases!!
	Use the correct modulo!!
	Check for corner cases!!
	Are you forgetting something?!
	Read problem statement carefully!!!
*/

const int N = 2e5 + 5;
int x[N], y[N];
char c[N];
vector<char> dir = {'L', 'R', 'U', 'D'};
vector<pii> v = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};

ll get(int i1,int i2) {
    if(x[i1] == x[i2]) return 5 * abs(y[i1] - y[i2]);
    else if(y[i1] == y[i2]) return 5 * abs(x[i1] - x[i2]);
    else return 10 * abs(x[i1] - x[i2]);
}

ll go(vector<int> v1, vector<int> v2, char ch1, char ch2) {
    pii dd = {0, 0};
    fr(i, 0, 3) {
        if(ch1 == dir[i]) dd.fi += v[i].fi, dd.se += v[i].se;
        if(ch2 == dir[i]) dd.fi -= v[i].fi, dd.se -= v[i].se;
    }
    map<int, vector<int>> m;
    pii mul = {0, 0};
    if(dd.fi == 0) {
        mul = {1, 0};
    }
    else if(dd.se == 0) {
        mul = {0, 1};
    }
    else if(dd.fi == dd.se) {
        mul = {1, -1};
    }
    else {
        mul = {1, 1};
    }
    for(int idx : v1) {
        m[mul.fi * x[idx] + mul.se * y[idx]].pb(idx);
    }
    for(int idx : v2) {
        m[mul.fi * x[idx] + mul.se * y[idx]].pb(idx);
    }
    auto cmp = [&](const int i, const int j) {
        return dd.fi * x[i] + dd.se * y[i] < dd.fi * x[j] + dd.se * y[j];
    };
    ll ans = LLONG_MAX;
    for(auto x : m) {
        sort(all(x.se), cmp);
        int l = 0;
        for(int idx : x.se) {
            if(c[idx] == ch1) l = idx;
            if(l && c[idx] == ch2) {
                ans = min(ans, get(l, idx));
            }
        }
    }
    debug(ans);
    return ans;
}

void solve() {
	int n;
    sc(n);
    fr(i, 1, n) {
        sc(x[i], y[i], c[i]);
    }
    ll ans = LLONG_MAX;
    for(char ch1 : {'U', 'L', 'R', 'D'}) {
        for(char ch2 : {'U', 'L', 'R', 'D'}) {
            if(ch1 == ch2) continue;
            vector<int> v1, v2;
            fr(i, 1, n) {
                if(c[i] == ch1) v1.pb(i);
                if(c[i] == ch2) v2.pb(i);
            }
            ans = min(ans, go(v1, v2, ch1, ch2));
        }
    }
    if(ans == LLONG_MAX) {
        cout << "SAFE";
    }
    else
        cout << ans;
}

int main() {
	ios :: sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int t = 1;
	// cin >> t;
	for(int tt = 1; tt <= t; tt++) {
		solve();
	}
	return 0;
}

Measured memory_bytes: 28820