#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repi(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define repm(i, a, b) for (ll i = (ll)(a); i > (ll)(b); i--)
#define all(v) v.begin(), v.end()
#define rall(x) (x).rbegin(), (x).rend()
#define pll pair<long long, long long>
#define pb emplace_back
#define mp make_pair
#define mt make_tuple
#define vc vector<char>
#define vvc vector<vc>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<long long>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vvb = vector<vb>;
template <class T>bool chmax(T &a, const T &b){if (a < b){a = b;return 1;}return 0;}
template <class T>bool chmin(T &a, const T &b){if (b < a){a = b;return 1;}return 0;}
//snippets:tmp,cl,cvl,cs,co,coy,con,cov,setp

struct xy{
    ll x;
    ll y;
    ll xpy;
    ll xmy;
};


int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n;
    cin >> n;
    vvll Ux(200001);
    vvll Uxpy(400001);
    vvll Uxmy(400001);
    vvll Ry(200001);
    vvll Rxpy(400001);
    vvll Rxmy(400001);
    vvll Dx(200001);
    vvll Dxpy(400001);
    vvll Dxmy(400001);
    vvll Ly(200001);
    vvll Lxpy(400001);
    vvll Lxmy(400001);
    vll X(n);
    vll Y(n);
    vc U(n);
    rep(i,n){
        ll x,y;
        char u;
        cin>>x>>y>>u;
        X[i]=x;
        Y[i]=y;
        U[i]=u;
        if(u=='U'){
            Ux[x].pb(y);
            Uxpy[x+y].pb(x);
            Uxmy[x-y+200000].pb(x);
        }else if(u=='R'){
            Ry[y].pb(x);
            Rxpy[x+y].pb(x);
            Rxmy[x-y+200000].pb(x);
        }else if(u=='D'){
            Dx[x].pb(y);
            Dxpy[x+y].pb(x);
            Dxmy[x-y+200000].pb(x);
        }else{
            Ly[y].pb(x);
            Lxpy[x+y].pb(x);
            Lxmy[x-y+200000].pb(x);
        }
    }
    rep(i,200001){
        sort(all(Ux[i]));
        sort(all(Dx[i]));
        sort(all(Ry[i]));
        sort(all(Ly[i]));
    }
    rep(i,400001){
        sort(all(Uxpy[i]));
        sort(all(Uxmy[i]));
        sort(all(Rxpy[i]));
        sort(all(Rxmy[i]));
        sort(all(Dxpy[i]));
        sort(all(Dxmy[i]));
        sort(all(Lxpy[i]));
        sort(all(Lxmy[i]));
    }
    ll ans=100000000;
    //Uを固定
    //Dについて
    rep(i,n){
        if(U[i]=='U'){
            if(Dx[X[i]].size()!=0){
                auto itr=lower_bound(all(Dx[X[i]]),Y[i]);
                if(itr!=Dx[X[i]].end()){
                    ans=min(ans,(*itr-Y[i])*5);
                }
            }
            if(Rxpy[X[i]+Y[i]].size()!=0){
                auto itr=lower_bound(all(Rxpy[X[i]+Y[i]]),X[i]);
                if(itr!=Rxpy[X[i]+Y[i]].begin()){
                    ans=min(ans,(X[i]-*--itr)*10);
                }
            }
            if(Lxmy[X[i]-Y[i]+200000].size()!=0){
                auto itr=lower_bound(all(Lxmy[X[i]-Y[i]+200000]),X[i]);
                if(itr!=Lxmy[X[i]-Y[i]+200000].end()){
                    ans=min(ans,(*itr-X[i])*10);
                }
            }
        }
        else if(U[i]=='D'){
            if(Rxmy[X[i]-Y[i]+200000].size()!=0){
                auto itr=lower_bound(all(Rxmy[X[i]-Y[i]+200000]),X[i]);
                if(itr!=Rxmy[X[i]-Y[i]+200000].begin()){
                    ans=min(ans,(X[i]-*--itr)*10);
                }
            }
            if(Lxpy[X[i]+Y[i]].size()!=0){
                auto itr=lower_bound(all(Lxpy[X[i]+Y[i]]),X[i]);
                if(itr!=Lxpy[X[i]+Y[i]].end()){
                    ans=min(ans,(*itr-X[i])*10);
                }
            }
        }
        else if(U[i]=='R'){
            if(Ly[Y[i]].size()!=0){
                auto itr=lower_bound(all(Ly[Y[i]]),X[i]);
                if(itr!=Ly[Y[i]].end()){
                    ans=min(ans,(*itr-X[i])*5);
                }
            }
        }
    }
    if(ans==100000000) cout<<"SAFE"<<endl;
    else cout<<ans<<endl;
}

Measured memory_bytes: 119072