#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG //これつけるとA[N]でもいいらしい
//for文のマクロ
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define big 1000000007
#define all(a) sort((a).begin(),(a).end()) //ソートのマクロ
#define Re(a) reverse((a).begin(),(a).end())
#define YN(a) if(a){cout<<"Yes"<<endl;}else cout<<"No"<<endl;//条件によってYes、Noを出力する
#define ld long double
#define More(vec, x) upper_bound(vec.begin(),vec.end(),x)-vec.begin();
//ソート済み配列vec内で、x「より大きい」値が出る添え字を返す なかったら配列の大きさ、つまりnを返す
#define OrMore(vec, x) lower_bound(vec.begin(),vec.end(),x)-vec.begin();
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    //cin,coutの高速化
    int n;
    cin>>n;
    vector<int> x(n),y(n);
    vector<char> u(n);
    unordered_map<int,vector<int>> pr,pl,ml,mr,ex,wi;
    //unordered_map<int,vector<int>> m1,m2,ex,wi;
    rep(i,n){
        cin>>x[i]>>y[i]>>u[i];
        if(u[i]=='R'){
            pr[x[i]+y[i]].push_back(x[i]);
            mr[x[i]-y[i]].push_back(x[i]);
        }
        if(u[i]=='L'){
            pl[x[i]+y[i]].push_back(x[i]);
            ml[x[i]-y[i]].push_back(x[i]);
            wi[y[i]].push_back(x[i]);
        }
        if(u[i]=='D'){
            ex[x[i]].push_back(y[i]);
        }
    }
    for(auto c:pr){
        int f = c.first;
        vector<int> k(c.second.size());
        all(c.second);
        k=c.second;
        pr[f]=k;
        }
    for(auto c:pl){
        int f = c.first;
        vector<int> k(c.second.size());
        all(c.second);
        k=c.second;
        pl[f]=k;
    }
    for(auto c:mr){
        int f = c.first;
        vector<int> k(c.second.size());
        all(c.second);
        k=c.second;
        mr[f]=k;
        }
    for(auto c:ml){
        int f = c.first;
        vector<int> k(c.second.size());
        all(c.second);
        k=c.second;
        ml[f]=k;
        }
    for(auto c:ex){
        int f = c.first;
        vector<int> k(c.second.size());
        all(c.second);
        k=c.second;
        ex[f]=k;}
    for(auto c:wi){
        int f = c.first;
        vector<int> k(c.second.size());
        all(c.second);
        k=c.second;
        wi[f]=k;
        //all(c.second);
        }

    bool crush = false;
    int time=1000000000;
    for(int i=0;i<n;i++){
        if(u[i]=='U'){
            if(pr[x[i]+y[i]].size()>0){
                
                int c = OrMore(pr[x[i]+y[i]],x[i]);
                if(c!=0){
                    crush = true;
                    time = min(time,10*(x[i]-pr[x[i]+y[i]][c-1]));
                }
                /*
                for(auto c:pr[x[i]+y[i]]){
                        if(x[i]-c>0){
                            crush = true;
                            time = min(time,10*(x[i]-c));
                        }
                }
                */
            }
            if(ex[x[i]].size()>0){
                int c = More(ex[x[i]],y[i]);
                if(c!=ex[x[i]].size()){
                    crush = true;
                    time = min(time, 5*(ex[x[i]][c]-y[i]));
                }
                /*
                for(auto c:ex[x[i]]){
                    if(c-y[i]>0){
                       crush = true;
                        time = min(time, 5*(c-y[i]));      
                    }
                }
                */
            }
            if(ml[x[i]-y[i]].size()>0){
                int c = More(ml[x[i]-y[i]],x[i]);
                if(c!=ml[x[i]-y[i]].size()){
                    crush = true;
                    time = min(time,10*(ml[x[i]-y[i]][c]-x[i]));
                }
                
               /*
                for(auto c:ml[x[i]-y[i]]){
                    if(c-x[i]>0){
                        crush = true;
                        time = min(time,10*(c-x[i]));
                    }
                }*/
            }
        }
        if(u[i]=='D'){
            if(mr[x[i]-y[i]].size()>0){
                //ここが全部悪い
                
                int c = OrMore(mr[x[i]-y[i]],x[i]);
                if(c!=0){
                    crush = true;
                    time = min(time,10*(x[i]-mr[x[i]-y[i]][c-1]));
                }
                

               /*
                for(auto c:mr[x[i]-y[i]]){
                    if(x[i]-c>0){
                        crush = true;
                        time = min(time,10*(x[i]-c));               
                    }
                }*/
            }
            if(pl[x[i]+y[i]].size()>0){
                
                int c = More(pl[x[i]+y[i]],x[i]);
                if(c!=pl[x[i]+y[i]].size()){
                    crush = true;
                    time = min(time,10*(pl[x[i]+y[i]][c]-x[i]));
                }
                
                /*
                for(auto c:pl[x[i]+y[i]]){
                    if(c-x[i]>0){
                        crush = true;
                        time = min(time,10*(c-x[i]));
                    }
                }
                */
            }
        }
        if(u[i]=='R'){
            if(wi[y[i]].size()>0){
                
                int c = More(wi[y[i]],x[i]);
                if(c!=wi[y[i]].size()){
                    crush = true;
                    time = min(time, 5*(wi[y[i]][c]-x[i]));
                }
                /*
                for(auto c:wi[y[i]]){
                    if(c-x[i]>0){
                        crush = true;
                        time = min(time, 5*(c-x[i]));
                    }
                }*/
            }
        }
    }
    if(!crush){
        cout<<"SAFE"<<endl;
        return 0;
    }
    cout<<time<<endl;
}

Measured memory_bytes: 44892