#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using P = pair<ll,ll>;
using um = unordered_map<ll,ll>;
#define fl cout<<flush;
#define endl '\n'
template <typename T> inline void prt(T v){cout<<v<<'\n';}
template <typename T> inline bool chmax(T &a, const T &b){if (a<b){a=b;return 1;}return 0;}
template <typename T> inline bool chmin(T &a, const T &b){if (a>b){a=b;return 1;}return 0;}
const ll INF=1LL<<60;
const ll MOD=(ll)1e9+7;
const ll MOD2=998244353;
const ld pi=3.14159265358979323846;
const ld eps=1e-10;

signed main(void){
    cin.tie(0);cout.tie(0);ios::sync_with_stdio(false);
    ll N;
    cin >> N;
    vector<ll> X(N,0);
    vector<ll> Y(N,0);
    vector<char> U(N);
    for(ll i=0;i<N;++i)cin>>X[i]>>Y[i]>>U[i];
    ll ans=INF;
    //左右
    while(1){
    vector<vector<ll>> R(400001, vector<ll>(0));
    for(ll i=0;i<N;++i){
        if(U[i]=='R')R[Y[i]].emplace_back(X[i]);
    }
    for(ll i=0;i<400001;++i)sort(R[i].begin(), R[i].end());
    for(ll i=0;i<N;++i){
        if(U[i]=='L'){
            if(R[Y[i]].empty())continue;
            auto itr=upper_bound(R[Y[i]].begin(), R[Y[i]].end(), X[i]);
            if(itr==R[Y[i]].begin())continue;
            chmin(ans,5*(X[i]-(*--itr)));
        }
    }break;
    }

    //上下
    while(1){
    vector<vector<ll>> U2(400001, vector<ll>(0));
    for(ll i=0;i<N;++i){
        if(U[i]=='U')U2[X[i]].emplace_back(Y[i]);
    }
    for(ll i=0;i<400001;++i)sort(U2[i].begin(), U2[i].end());
    for(ll i=0;i<N;++i){
        if(U[i]=='D'){
            if(U2[X[i]].empty())continue;
            auto itr=upper_bound(U2[X[i]].begin(), U2[X[i]].end(), Y[i]);
            if(itr==U2[X[i]].begin())continue;
            chmin(ans,5*(Y[i]-(*--itr)));
        }
    }break;
    }

    //上右
    while(1){
    vector<vector<ll>> R(400001, vector<ll>(0));
    for(ll i=0;i<N;++i){
        if(U[i]=='R')R[X[i]+Y[i]].emplace_back(X[i]);
    }
    for(ll i=0;i<400001;++i)sort(R[i].begin(), R[i].end());
    for(ll i=0;i<N;++i){
        if(U[i]=='U'){
            if(R[X[i]+Y[i]].empty())continue;
            auto itr=upper_bound(R[X[i]+Y[i]].begin(), R[X[i]+Y[i]].end(), X[i]);
            if(itr==R[X[i]+Y[i]].begin())continue;
            chmin(ans,10*(X[i]-(*--itr)));
        }
    }break;
    }

    //上右
    while(1){
    vector<vector<ll>> R(400001, vector<ll>(0));
    for(ll i=0;i<N;++i){
        if(U[i]=='U')R[X[i]-Y[i]+200000].emplace_back(X[i]);
    }
    for(ll i=0;i<400001;++i)sort(R[i].begin(), R[i].end());
    for(ll i=0;i<N;++i){
        if(U[i]=='L'){
            if(R[X[i]-Y[i]+200000].empty())continue;
            auto itr=upper_bound(R[X[i]-Y[i]+200000].begin(), R[X[i]-Y[i]+200000].end(), X[i]);
            if(itr==R[X[i]-Y[i]+200000].begin())continue;
            chmin(ans,10*(X[i]-(*--itr)));
        }
    }break;
    }

    //左下
    while(1){
    vector<vector<ll>> R(400001, vector<ll>(0));
    for(ll i=0;i<N;++i){
        if(U[i]=='D')R[X[i]+Y[i]].emplace_back(X[i]);
    }
    for(ll i=0;i<400001;++i)sort(R[i].begin(), R[i].end());
    for(ll i=0;i<N;++i){
        if(U[i]=='L'){
            if(R[X[i]+Y[i]].empty())continue;
            auto itr=upper_bound(R[X[i]+Y[i]].begin(), R[X[i]+Y[i]].end(), X[i]);
            if(itr==R[X[i]+Y[i]].begin())continue;
            chmin(ans,10*(X[i]-(*--itr)));
        }
    }break;
    }

    //右下
    while(1){
    vector<vector<ll>> R(400001, vector<ll>(0));
    for(ll i=0;i<N;++i){
        if(U[i]=='R')R[X[i]-Y[i]+200000].emplace_back(X[i]);
    }
    for(ll i=0;i<400001;++i)sort(R[i].begin(), R[i].end());
    for(ll i=0;i<N;++i){
        if(U[i]=='D'){
            if(R[X[i]-Y[i]+200000].empty())continue;
            auto itr=upper_bound(R[X[i]-Y[i]+200000].begin(), R[X[i]-Y[i]+200000].end(), X[i]);
            if(itr==R[X[i]-Y[i]+200000].begin())continue;
            chmin(ans,10*(X[i]-(*--itr)));
        }
    }break;
    }
    if(ans==INF)prt("SAFE");
    else prt(ans);
    return 0;
}



Measured memory_bytes: 22000