#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
const ll INF = ll(1e18)+5;

void solve(long long N, std::vector<long long> X, std::vector<long long> Y, std::vector<char> U){
    // 特徴量を作る（大きさ,座標u,dはy,l,rはx座標)。
    set<ll> lfeature,rfeature,lfeature2,rfeature2;
    map<ll,set<ll>> lfeatureToY,rfeatureToY,lfeatureToY2,rfeatureToY2;
    // 座標ごとのmapを作る。
    map<ll,set<ll>> ump,rmp,dmp,lmp;
    for(int i=0;i<N;i++){
        if(U[i] == 'U'){
            ump[X[i]].insert(Y[i]);
        }else if(U[i] == 'R'){
            rmp[Y[i]].insert(X[i]);
            rfeature.insert(X[i]-Y[i]);
            rfeatureToY[X[i]-Y[i]].insert(Y[i]);
            rfeature2.insert(X[i]+Y[i]);
            rfeatureToY2[X[i]+Y[i]].insert(Y[i]);
        }else if(U[i] == 'D'){
            dmp[X[i]].insert(Y[i]);
        }else{
            lmp[Y[i]].insert(X[i]);
            lfeature.insert(X[i]-Y[i]);
            lfeatureToY[X[i]-Y[i]].insert(Y[i]);
            lfeature2.insert(X[i]+Y[i]);
            lfeatureToY2[X[i]+Y[i]].insert(Y[i]);
        }
    }
   
    // 軸方向が同じものの衝突の判定を行う。
    ll ans = INF;
    for(int i=0;i<N;i++){
         if(U[i] == 'U'){
             if(dmp.find(X[i]) == dmp.end()){
                 continue;
             }
            auto itr = dmp[X[i]].upper_bound(Y[i]);
            if(itr == dmp[X[i]].end()){
                continue;
            }
            ans = min(ans,((*itr)-Y[i])*5);
        }else if(U[i] == 'R'){
             if(lmp.find(Y[i]) == lmp.end()){
                 continue;
             }
            auto itr = lmp[Y[i]].upper_bound(X[i]);
            if(itr == lmp[Y[i]].end()){
                continue;
            }
            ans = min(ans,((*itr)-X[i])*5);
        }
    }

    // ねじれの角度について求める。

    for(int i=0;i<N;i++){
        if(U[i] == 'U'){
            if(lfeature.find(X[i]-Y[i]) != lfeature.end()){
                auto itr = lfeatureToY[X[i]-Y[i]].upper_bound(Y[i]);
                if(itr != lfeatureToY[X[i]-Y[i]].end()){
                    ans = min(ans,((*itr)-Y[i])*10);
                }
            }
            if(rfeature2.find((X[i]+Y[i])) != rfeature2.end()){
                auto itr = rfeatureToY2[(X[i]+Y[i])].upper_bound(Y[i]);
                if(itr != rfeatureToY2[(X[i]+Y[i])].end()){
                    ans = min(ans,((*itr)-Y[i])*10);
                }
            }

        }
        else if(U[i] == 'D'){
            if(lfeature2.find((X[i]+Y[i])) != lfeature2.end()){
                auto itr = lfeatureToY2[(X[i]+Y[i])].upper_bound(Y[i]);
                if(itr != lfeatureToY2[(X[i]+Y[i])].begin()){
                    --itr;
                    ans = min(ans,(Y[i]-(*itr))*10);
                }
            }
            if(rfeature.find((X[i]-Y[i])) != rfeature.end()){
                auto itr = rfeatureToY[(X[i]-Y[i])].lower_bound(Y[i]);
                if(itr != rfeatureToY[(X[i]-Y[i])].begin()){
                    --itr;
                    ans = min(ans,(Y[i]-(*itr))*10);
                }
            }
        }
    }


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


}

// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
int main(){
    long long N;
    scanf("%lld",&N);
    std::vector<long long> X(N);
    std::vector<long long> Y(N);
    std::vector<char> U(N);
    for(int i = 0 ; i < N ; i++){
        scanf("%lld",&X[i]);
        scanf("%lld",&Y[i]);
        std::cin >> U[i];
    }
    solve(N, std::move(X), std::move(Y), std::move(U));
    return 0;
}


Measured memory_bytes: 89960