//ヘッダー
#include<bits/stdc++.h>
using namespace std;

//型定義
typedef long long ll;

//定数
const int INF=1e+9;
const int MOD=1e+9+7;

//REPマクロ
#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define REP2(i,a,b) for(ll i=a;i<(ll)(b);i++)
#define REPD2(i,a,b) for(ll i=a;i>(ll)(b);i--)

// 多次元 vector 生成
template<class T>
vector<T> make_vec(size_t a){
    return vector<T>(a);
}
template<class T, class... Ts>
auto make_vec(size_t a, Ts... ts){
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}

//vectorの扱い
#define ALL(x) (x).begin(),(x).end() //sortなどの引数省略
#define SIZE(x) ((ll)(x).size()) //size
#define MAX(x) *max_element(ALL(x)) //最大値
#define MIN(x) *min_element(ALL(x)) //最小値

int main(){
    ll N;
    cin>>N;
    vector<ll> X(N),Y(N);
    vector<char> u(N);
    map<ll,set<ll>> U,D,R,L;
    map<ll,set<ll>> U_2,D_2,R_2,L_2;
    map<ll,set<ll>> U_1,D_1,R_1,L_1;
    REP(i,N){
        cin>>X[i]>>Y[i]>>u[i];
        
        if(u[i]=='U'){
            U[X[i]].insert(Y[i]);
            U_1[X[i]+Y[i]].insert(X[i]);
            U_2[X[i]-Y[i]].insert(X[i]);
        }
        if(u[i]=='D'){
            D[X[i]].insert(Y[i]);
            D_1[X[i]+Y[i]].insert(X[i]);
            D_2[X[i]-Y[i]].insert(X[i]);
        }
        if(u[i]=='R'){
            R[Y[i]].insert(X[i]);
            R_1[X[i]+Y[i]].insert(X[i]);
            R_2[X[i]-Y[i]].insert(X[i]);
        }
        if(u[i]=='L'){
            L[Y[i]].insert(X[i]);
            L_1[X[i]+Y[i]].insert(X[i]);
            L_2[X[i]-Y[i]].insert(X[i]);
        }
    }


    ll ans=INF;
    REP(i,N){
        ll x=X[i];
        ll y=Y[i];
        if(u[i]=='U'){
            if(D.count(x)){
                auto itr=D[x].lower_bound(y);
                if(itr!=D[x].end()){
                    ans=min(ans,10*(*itr-y)/2);
                }
            }

            if(R_1.count(x+y)){
                auto itr=R_1[x+y].upper_bound(x);
                if(itr!=R_1[x+y].begin()){
                    itr--;
                    ans=min(ans,10*(x-*itr));
                }
            }

            if(L_2.count(x-y)){
                auto itr=L_2[x-y].upper_bound(x);
                if(itr!=L_2[x-y].end()){
                    ans=min(ans,10*(*itr-x));
                }
            }
        }

        if(u[i]=='D'){
            if(U.count(x)){
                auto itr=U[x].lower_bound(y);
                if(itr!=U[x].begin()){
                    itr--;
                    ans=min(ans,10*(y-*itr)/2);
                    
                }
            }

            if(R_2.count(x-y)){
                auto itr=R_2[x-y].upper_bound(x);
                if(itr!=R_2[x-y].begin()){
                    itr--;
                    ans=min(ans,10*(x-*itr));
                   
                }
            }

            if(L_1.count(x+y)){
                auto itr=L_1[x+y].upper_bound(x);
                if(itr!=L_1[x+y].end()){
                    ans=min(ans,10*(*itr-x));
                }
            }
        }

        if(u[i]=='R'){
            if(L.count(y)){
                auto itr=L[y].lower_bound(x);
                if(itr!=L[y].end()){
                    ans=min(ans,10*(*itr-x)/2);
                }
            }

            if(D_2.count(x-y)){
                auto itr=D_2[x-y].upper_bound(x);
                if(itr!=D_2[x-y].end()){
                    ans=min(ans,10*(*itr-x));
                }
            }

            if(U_1.count(x+y)){
                auto itr=U_1[x+y].upper_bound(x);
                if(itr!=U_1[x+y].end()){
                    ans=min(ans,10*(*itr-x));
                }
            }
        }

        if(u[i]=='L'){
            if(R.count(y)){
                auto itr=R[y].lower_bound(x);
                if(itr!=R[y].begin()){
                    itr--;
                    ans=min(ans,10*(x-*itr)/2);
                }
            }

            if(D_1.count(x+y)){
                auto itr=D_1[x+y].upper_bound(x);
                if(itr!=D_1[x+y].begin()){
                    itr--;
                    ans=min(ans,10*(x-*itr));
                }
            }

            if(U_2.count(x-y)){
                auto itr=U_2[x-y].upper_bound(x);
                if(itr!=U_2[x-y].begin()){
                    itr--;
                    ans=min(ans,10*(x-*itr));
                }
            }
        }

    }

    if(ans==INF){
        cout<<"SAFE"<<endl;
    }else{
        cout<<ans<<endl;
    }
    


}

Measured memory_bytes: 90892