#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
 
ll power(ll x, ll y); 
#define repp(I, A, B) for(int I = A; I <= B; I++)
#define rep(I, A, B) for(int I = A; I < B; I++)
#define rrep(I, B, A) for(int I = B; I >= A; I--)
#define pb emplace_back
#define ff first
#define ss second
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define LB lower_bound
#define UB upper_bound
#define MP make_pair
#define mem(A, B) memset(A, B, sizeof(A));
#define mem0(A) memset(A, 0, sizeof(A));
#define quickio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define debug(this) cerr<<"> "<<#this<<" : "<<this<<"\n"
#define bitcount(n) __builtin_popcountll(n)
#define in_edges(M) repp(I, 1, M){int A, B; cin >> A >> B; v[A].pb(B), v[B].pb(A);}
#define in_edges2(M) repp(I, 1, M){int A, B; cin >> A >> B; v[A].pb(B);}
#define in_edges3(M) repp(I, 1, M){int A, B, C; cin >> A >> B >> C; v[A].pb(B, C), v[B].pb(A, C);}
#define endl "\n"

const int inf = 2e9;
const int N = 5e5 + 20;

set<pair<int, int> > U[N], D[N], L[N], R[N];
set<pair<int, int> > UU[N], DD[N], LL[N], RR[N];
set<int> DY[N], UY[N], LX[N], RX[N];

bool cum(pair<int, int> a, pair<int, int> b){
    return b.ss <= a.ss;
}

int main() //PointBlank's code ¯\_(ツ)_/¯
{
    quickio
    vector<pair<int, int> > u, d, l, r;
    int n;
    cin >> n;
    rep(i, 0, n){
        int x, y;
        char s;
        cin >> x >> y >> s;
        if(s == 'U') u.pb(x, y), UY[x].emplace(y);
        if(s == 'D') d.pb(x, y), DY[x].emplace(y);
        if(s == 'L') l.pb(x, y), LX[y].emplace(x);
        if(s == 'R') r.pb(x, y), RX[y].emplace(x);
    }

    sort(all(u));
    sort(all(d));
    sort(all(l));
    sort(all(r));
    for(auto i : u) U[i.ff + i.ss].emplace(i);
    for(auto i : d) D[i.ff + i.ss].emplace(i);
    for(auto i : l) L[i.ff + i.ss].emplace(i);
    for(auto i : r) R[i.ff + i.ss].emplace(i);
    for(auto i : u) UU[i.ff - i.ss + 200005].emplace(i);
    for(auto i : d) DD[i.ff - i.ss + 200005].emplace(i);
    for(auto i : l) LL[i.ff - i.ss + 200005].emplace(i);
    for(auto i : r) RR[i.ff - i.ss + 200005].emplace(i);

    

    int mn = inf, mm = inf;

    for(auto i : u){
        auto it = DY[i.ff].UB(i.ss);
        if(it != DY[i.ff].end()){
            mm = min(abs(*it - i.ss), mm);
        }
    }

    for(auto i : r){
        auto it = LX[i.ss].UB(i.ff);
        if(it != LX[i.ss].end()){
            mm = min(abs(*it - i.ff), mm);
        }
    }

    repp(i, 1, 400005){
        for(auto j : R[i]){
            auto a = U[i].UB(j);
            if(a != U[i].end())
            mn = min(mn, (*a).ff - j.ff);
        }
        for(auto j : D[i]){
            auto a = L[i].UB(j);
            if(a != L[i].end())
            mn = min(mn, (*a).ff - j.ff);
        }
    }
    
    repp(i, 1, 400005){
        for(auto j : UU[i]){
            auto a = LL[i].UB(j);
            if(a != LL[i].end())
            mn = min(mn, (*a).ff - j.ff);
        }

        for(auto j : RR[i]){
            auto a = DD[i].UB(j);
            if(a != DD[i].end())
            mn = min(mn, (*a).ff - j.ff);
        }
    }

    ll res = 2e18;
    if(mn != inf) res = min(res, (ll)mn * 10);
    if(mm != inf) res = min(res, mm*5LL);

    ll xx = 2e18;
    if(res != xx) cout << res;
    else cout << "SAFE";
}

Measured memory_bytes: 314212