#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define rep1(i, n) for(int i = 1; i <= (int)(n); i++)
#define showmap(is, js, x) {rep(i, is){rep(j, js){cout << x[i][j] << " ";}cout << endl;}}
#define show(x) {for(auto i: x){cout << i << " ";} cout<<endl;}
#define showm(m) {for(auto i: m){cout << m.x << " ";} cout<<endl;}
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> llP;
ll gcd(int x, int y){ return y?gcd(y, x%y):x;}
ll lcm(ll x, ll y){ return (x*y)/gcd(x,y);}
 
/*
正面衝突の場合 U-D, L-R
斜め衝突の場合 U-L, L-D, D-R, R-U
 
 
 
 
 
*/
 
 
int x_in[200050], y_in[200050];
char c_in[200050];
vector<pair<int, char>> ur[400020];
vector<pair<int, char>> ud[400020];
 
int n;
 
int solve_UD(){
    rep(i, 400020){
        ud[i].clear();
    }
    rep(i, n){
        if (c_in[i] != 'U' && c_in[i] != 'D') continue;
        ud[x_in[i]].push_back(make_pair(y_in[i], c_in[i]));
    }
    int tmin = 2000001;
    rep(i, 400020){
        sort(ud[i].begin(), ud[i].end());
        rep(j, ud[i].size()-1){
            if (ud[i][j].second == 'U' && ud[i][j+1].second == 'D'){
                tmin = min(tmin, ud[i][j+1].first - ud[i][j].first);
            }
        }
    }
    return tmin * 5;
}
 
int solve_UR(){
    rep(i, 400020){
        ur[i].clear();
    }
    rep(i, n){
        if (c_in[i] != 'U' && c_in[i] != 'R') continue;
        ur[x_in[i]+y_in[i]].push_back(make_pair(x_in[i], c_in[i]));
    }
    int tmin = 2000001;
    rep(i, 400020){
        sort(ur[i].begin(), ur[i].end());
        rep(j, ur[i].size()-1){
            if (ur[i][j+1].second == 'U' && ur[i][j].second == 'R')
            tmin = min(tmin, ur[i][j+1].first - ur[i][j].first);
        }
    }
    return tmin*10;
}
 
void rotate(){
    int tmp;
    rep(i, n){
        tmp = x_in[i];
        x_in[i] = 200000-y_in[i];
        y_in[i] = tmp;
        if (c_in[i] == 'U') c_in[i] = 'L';
        else if (c_in[i] == 'L') c_in[i] = 'D';
        else if (c_in[i] == 'D') c_in[i] = 'R';
        else c_in[i] = 'U';
    }
    
}
 
 
int main()
{
    cin >> n;
    rep(i, n){
        cin >> x_in[i] >> y_in[i] >> c_in[i];
    }
 
    int t_time = 2000001;    
    rep(t, 4){
        t_time = min(t_time, solve_UD());
        t_time = min(t_time, solve_UR());
        rotate();
    }
 
    if (t_time == 2000001) cout << "SAFE" << endl;
    else cout << t_time << endl;
 
}
 
#include <bits/stdc++.h>
using namespace std;

#define lli long long int
#define REP(i,s,n) for(int i=s;i<n;i++)
#define NUM 2520
#define INF (1LL<<50)
#define DEBUG 0
#define mp(a,b) make_pair(a,b)
#define SORT(V) sort(V.begin(),V.end())
#define PI (3.141592653589794)
#define TO_STRING(VariableName) # VariableName
#define LOG(x) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<endl;
#define LOG2(x,y) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<endl;
#define LOG3(x,y,z) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<" "<<TO_STRING(z)<<"="<<z<<endl;
#define LOG4(w,x,y,z) if(DEBUG)cout<<TO_STRING(w)<<"="<<w<<" "<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<" "<<TO_STRING(z)<<"="<<z<<endl;

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; }

lli ans = INF;
const lli ssize = 400100;

void calc(vector<set<lli>> &lef,vector<set<lli>> &rig){
    REP(i,0,ssize){
        if(rig.size()==0)continue;
        for(auto e:lef[i]){
            auto itr = rig[i].upper_bound(e);
            if(itr==rig[i].end())break;
            LOG3(ans,(*itr),e);
            chmin(ans,(*itr)-e);
        }
    }
}


void func(long long N, std::vector<long long> X, std::vector<long long> Y, std::vector<std::string> U){

    {
    //LR combination
        vector<set<lli>> lef(ssize),rig(ssize);
        REP(i,0,N){
            if(U[i]=="R")lef[Y[i]].insert(X[i]);
            else if(U[i]=="L")rig[Y[i]].insert(X[i]);
        }
        calc(lef,rig);
    }

    {
    //UD combination
        vector<set<lli>> lef(ssize),rig(ssize);
        REP(i,0,N){
            if(U[i]=="U")lef[X[i]].insert(Y[i]);
            else if(U[i]=="D")rig[X[i]].insert(Y[i]);
        }
        calc(lef,rig);
    }

    {
        //y=x+b
        {
        //LU combination
            vector<set<lli>> lef(ssize),rig(ssize);
            REP(i,0,N){
                if(U[i]=="U")lef[X[i]-Y[i]+ssize/2].insert(X[i]*2);
                else if(U[i]=="L")rig[X[i]-Y[i]+ssize/2].insert(X[i]*2);
            }
            calc(lef,rig);
        }

        {
        //RD combination
            vector<set<lli>> lef(ssize),rig(ssize);
            REP(i,0,N){
                if(U[i]=="R")lef[X[i]-Y[i]+ssize/2].insert(X[i]*2);
                else if(U[i]=="D")rig[X[i]-Y[i]+ssize/2].insert(X[i]*2);
            }
            calc(lef,rig);
        }
    }
    
    {
        //y=-x+b
        {
        //LD combination
            vector<set<lli>> lef(ssize),rig(ssize);
            REP(i,0,N){
                if(U[i]=="D")lef[X[i]+Y[i]].insert(X[i]*2);
                else if(U[i]=="L")rig[X[i]+Y[i]].insert(X[i]*2);
            }
            calc(lef,rig);
        }

        {
        //RU combination
            vector<set<lli>> lef(ssize),rig(ssize);
            REP(i,0,N){
                if(U[i]=="R")lef[X[i]+Y[i]].insert(X[i]*2);
                else if(U[i]=="U")rig[X[i]+Y[i]].insert(X[i]*2);
            }
            calc(lef,rig);
        }
    }


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

}

int main(){
    // cout << fixed << setprecision(5);

    long long N;
    scanf("%lld",&N);
    std::vector<long long> X(N);
    std::vector<long long> Y(N);
    std::vector<std::string> U(N);
    for(int i = 0 ; i < N ; i++){
        scanf("%lld",&X[i]);
        scanf("%lld",&Y[i]);
        std::cin >> U[i];
    }
    func(N, std::move(X), std::move(Y), std::move(U));
    return 0;
}
