#include <bits/stdc++.h>
using namespace std;
///////////////////////////////////////////
const long long int INF = 1LL<<60;
const long long int Mod = 1000000007;
using ll = long long int; using ci = const int;
using vi = vector<int>;  using Vi = vector<long long int>;
using P = pair<int, int>;  using PLL = pair<ll, ll>;
using matrix = vector<vector<ll>>;
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define all(x) (x).begin(),(x).end()
#define rp(i,N) for(ll i = 0; i < (ll)N; i++)
#define repi(i,a,b) for(ll i = ll(a); i < ll(b); ++i)
template<class T>bool chmax(T &former, const T &b) { if (former<b) { former=b; return true; } return false; }
template<class T>bool chmin(T &former, const T &b) { if (b<former) { former=b; return true; } return false; }
template<class T>T sqar(T x){ return x*x; }//sqrt(x)は平方根;
#define Sort(v) std::sort(v.begin(), v.end(), std::greater<decltype(v[0])>()) //降順でVをソート
#define p_queue(v) priority_queue<v, vector<v>, greater<v> >
template<class T> inline void princ(T x){cout<<x<<" ";}; 
template<class T> inline void print(T x){cout<<x<<"\n";};
template<class T> inline void Yes(T condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; }
template<class T> inline void YES(T condition){ if(condition) cout << "YES" << endl; else cout << "NO" << endl; }
///////////////////////////////////////////////////////////////////////////////////
#define mt(x,y,z) make_tuple(x,y,z);
void solve(){
    ll ans=INF;
    int n;
    cin >> n;
    Vi x(n),y(n);
    vector<char> u(n);
    rp(i,n) cin >> x.at(i) >> y.at(i) >> u.at(i);
    vector<tuple<ll,ll,char>> xori(n),yori(n),sumori(n),subori(n);
    rp(i,n){
        xori.at(i)=mt(x[i],y[i],u[i]);
        yori.at(i)=mt(y[i],x[i],u[i]);
        sumori.at(i)=mt(x[i]+y[i],x[i],u[i]);
        subori.at(i)=mt(y[i]-x[i],x[i],u[i]);
    }
    sort(all(xori));
    sort(all(yori));
    sort(all(sumori));
    sort(all(subori));
    ll up=-INF,down=-INF,right=-INF,left=-INF;
    rp(i,n){
        
        if(i!=n-1&&get<0>(xori[i])==get<0>(xori[i+1])){
            if(get<2>(xori[i])=='U') up=get<1>(xori[i]);
            if(get<2>(xori[i])=='D'){
                if(up>=-INF+1) chmin(ans,(get<1>(xori[i])-up)*5);
            }
        }else{
            if(get<2>(xori[i])=='U') up=get<1>(xori[i]);
            if(get<2>(xori[i])=='D'){
                if(up>=-INF+1) chmin(ans,(get<1>(xori[i])-up)*5);
            }
            up=-INF; down=-INF;
        }
    }
    //print(ans);
    rp(i,n){
        
        if(i!=n-1&&get<0>(yori[i])==get<0>(yori[i+1])){
            if(get<2>(yori[i])=='R') up=get<1>(yori[i]);
            if(get<2>(yori[i])=='L'){
                if(up>=-INF+1) chmin(ans,(get<1>(yori[i])-up)*5);
            }
        }else{
            if(get<2>(yori[i])=='R') up=get<1>(yori[i]);
            if(get<2>(yori[i])=='L'){
                if(up>=-INF+1) chmin(ans,(get<1>(yori[i])-up)*5);
            }
            up=-INF; down=-INF;
        }
    }
    //print(ans);
    rp(i,n){
        
        if(i!=n-1&&get<0>(sumori[i])==get<0>(sumori[i+1])){
            if(get<2>(sumori[i])=='R') right=get<1>(sumori[i]);
            if(get<2>(sumori[i])=='U'){
                if(right>=-INF+1) chmin(ans,(get<1>(sumori[i])-right)*10);
            }
            if(get<2>(sumori[i])=='D') down=get<1>(sumori[i]);
            if(get<2>(sumori[i])=='L'){
                if(down>=-INF+1) chmin(ans,(get<1>(sumori[i])-down)*10);
            }
        }else{
            if(get<2>(sumori[i])=='R') right=get<1>(sumori[i]);
            if(get<2>(sumori[i])=='U'){
                if(right>=-INF+1) chmin(ans,(get<1>(sumori[i])-right)*10);
            }
            if(get<2>(sumori[i])=='D') down=get<1>(sumori[i]);
            if(get<2>(sumori[i])=='L'){
                if(down>=-INF+1) chmin(ans,(get<1>(sumori[i])-down)*10);
            }
            down=-INF; right=-INF;
        }
    }
    //print(ans);
    rp(i,n){
        
        if(i!=n-1&&get<0>(subori[i])==get<0>(subori[i+1])){
            if(get<2>(subori[i])=='R') right=get<1>(subori[i]);
            if(get<2>(subori[i])=='D'){
                if(right>=-INF+1) chmin(ans,(get<1>(subori[i])-right)*10);
            }
            if(get<2>(subori[i])=='U') up=get<1>(subori[i]);
            if(get<2>(subori[i])=='L'){
                if(up>=-INF+1) chmin(ans,(get<1>(subori[i])-up)*10);
            }
        }else{
            if(get<2>(subori[i])=='R') right=get<1>(subori[i]);
            if(get<2>(subori[i])=='D'){
                if(right>=-INF+1) chmin(ans,(get<1>(subori[i])-right)*10);
            }
            if(get<2>(subori[i])=='U') up=get<1>(subori[i]);
            if(get<2>(subori[i])=='L'){
                if(up>=-INF+1) chmin(ans,(get<1>(subori[i])-up)*10);
            }
            up=-INF; right=-INF;
        }
    }
    if(ans>INF-10) print("SAFE");
    else print(ans);
    return;
}
int main(){
    cin.tie(0);ios::sync_with_stdio(false);
    std::cout<<std::fixed<<std::setprecision(30);
    solve();
    return 0;
}

Measured memory_bytes: 25272