#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
using P = pair<ll,ll>;
const ll mod = 1e9+7;
#define ALL(x) (x).begin(),(x).end()
#define REP(i,n) for(ll (i)=0;(i)<(n);(i)++)
#define REPS(i,n) for(ll (i)=1;(i)<=(n);(i)++)
#define RREP(i,n) for(ll (i)=(n-1);(i)>=0;(i)--)
#define RREPS(i,n) for(ll (i)=(n);(i)>0;(i)--)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define UNIQUE(v) v.erase(unique(ALL(v)),v.end());

template<class T> inline void chmin(T& a, T b){
    if (a > b){
        a = b;
    }
}

template<class T> inline void chmax(T& a, T b){
    if (a < b){
        a = b;
    }
}

signed main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout<<fixed<<setprecision(12);
    ll N;
    cin >> N;
    vector<vector<ll>> upx(200001,vector<ll>(0));
    vector<vector<ll>> upp(400001,vector<ll>(0));
    vector<vector<ll>> upm(400001,vector<ll>(0));
    vector<vector<ll>> dowx(200001,vector<ll>(0));
    vector<vector<ll>> dowp(400001,vector<ll>(0));
    vector<vector<ll>> dowm(400001,vector<ll>(0));
    vector<vector<ll>> riy(200001,vector<ll>(0));
    vector<vector<ll>> rip(400001,vector<ll>(0));
    vector<vector<ll>> rim(400001,vector<ll>(0));
    vector<vector<ll>> ley(200001,vector<ll>(0));
    vector<vector<ll>> lep(400001,vector<ll>(0));
    vector<vector<ll>> lem(400001,vector<ll>(0));
    REP(i,N){
        ll X,Y;
        char U;
        cin >> X >> Y>> U;
        if(U == 'U'){
            upx[X].pb(Y);
            upp[X+Y].pb(Y);
            upm[200000+X-Y].pb(Y);
        }else if(U == 'R'){
            riy[Y].pb(X);
            rip[X+Y].pb(Y);
            rim[200000+X-Y].pb(Y);
        }else if(U == 'D'){
            dowx[X].pb(Y);
            dowp[X+Y].pb(Y);
            dowm[200000+X-Y].pb(Y);
        }else if(U == 'L'){
            ley[Y].pb(X);
            lep[X+Y].pb(Y);
            lem[200000+X-Y].pb(Y);
        }
    }
    ll time = 100000000000;
    REP(i,200001){
        if(upx[i].size()!=0&&dowx[i].size()!=0){
            sort(ALL(upx[i]));
            sort(ALL(dowx[i]));
            ll de = 0;
            REP(as,upx[i].size()){
                while(upx[i][as] > dowx[i][de]){
                    if(de >= dowx[i].size()-1){
                        break;
                    }
                    de++;
                }
                if(upx[i][as] < dowx[i][de]){
                    chmin(time,(dowx[i][de] - upx[i][as])*5);
                }
            }
        }
    }
    REP(i,200001){
        if(riy[i].size()!=0&&ley[i].size()!=0){
            sort(ALL(riy[i]));
            sort(ALL(ley[i]));
            ll de = 0;
            REP(as,riy[i].size()){
                while(riy[i][as] > ley[i][de]){
                    if(de >= ley[i].size()-1){
                        break;
                    }
                    de++;
                }
                if(riy[i][as] < ley[i][de]){
                    chmin(time,(ley[i][de] - riy[i][as])*5);
                }
            }
        }
    }
    REP(i,400001){
        if(upp[i].size()!=0&&rip[i].size()!=0){
            sort(ALL(upp[i]));
            sort(ALL(rip[i]));
            ll de = 0;
            REP(as,upp[i].size()){
                while(upp[i][as] > rip[i][de]){
                    if(de >= rip[i].size()-1){
                        break;
                    }
                    de++;
                }
                if(upp[i][as] < rip[i][de]){
                    chmin(time,(rip[i][de] - upp[i][as])*10);
                }
            }
        }
    }
    REP(i,400001){
        if(lep[i].size()!=0&&dowp[i].size()!=0){
            sort(ALL(lep[i]));
            sort(ALL(dowp[i]));
            ll de = 0;
            REP(as,lep[i].size()){
                while(lep[i][as] > dowp[i][de]){
                    if(de >= dowp[i].size()-1){
                        break;
                    }
                    de++;
                }
                if(lep[i][as] < dowp[i][de]){
                    chmin(time,(dowp[i][de] - lep[i][as])*10);
                }
            }
        }
    }
    REP(i,400001){
        if(upm[i].size()!=0&&lem[i].size()!=0){
            sort(ALL(upm[i]));
            sort(ALL(lem[i]));
            ll de = 0;
            REP(as,upm[i].size()){
                while(upm[i][as] > lem[i][de]){
                    if(de >= lem[i].size()-1){
                        break;
                    }
                    de++;
                }
                if(upm[i][as] < lem[i][de]){
                    chmin(time,(lem[i][de] - upm[i][as])*10);
                }
            }
        }
    }
    REP(i,400001){
        if(rim[i].size()!=0&&dowm[i].size()!=0){
            sort(ALL(rim[i]));
            sort(ALL(dowm[i]));
            ll de = 0;
            REP(as,rim[i].size()){
                while(rim[i][as] > dowm[i][de]){
                    if(de >= dowm[i].size()-1){
                        break;
                    }
                    de++;
                }
                if(rim[i][as] < dowm[i][de]){
                    chmin(time,(dowm[i][de] - rim[i][as])*10);
                }
            }
        }
    }
    if(time == 100000000000){
        cout << "SAFE" << "\n";
    }else{
        cout << time << "\n";
    }
}

Measured memory_bytes: 115632