#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
#define sz(x) int(x.size())
#define pb push_back
#define eb emplace_back
using ll=long long;
using P = pair<int,int>;
using LP=pair<ll,ll>;
#define chmax(x,y) x = max(x,y)
#define chmin(x,y) x = min(x,y)
const ll MOD=1000000007,MOD2=998244353;

const int INF=1e9;

int main() {
    int N;
    cin>>N;
    vector<pair<int,int>> U(0),R(0),D(0),L(0);
    rep(i,N){
        int x,y;
        cin>>x>>y;
        char c;
        cin>>c;
        if(c=='U')U.pb({x,y});
        if(c=='R')R.pb({x,y});
        if(c=='D')D.pb({x,y});
        if(c=='L')L.pb({x,y});
    }
    int ans=INF;

    map<int,int> up;
    vector<P> UP;
    rep(i,U.size()){
        up[U.at(i).first]=U.at(i).second;
        UP.pb(U.at(i));
    }
    sort(UP.begin(),UP.end());
    rep(i,D.size()){
        if(!up.count(D.at(i).first))continue;
        int u=lower_bound(UP.begin(),UP.end(),D.at(i))-UP.begin();
        if(u==0)continue;u--;
        if(UP.at(u).first!=D.at(i).first)continue;
        chmin(ans,5*(D.at(i).second-UP.at(u).second));
    }

    map<int,int> right;
    vector<P> RIGHT;
    rep(i,R.size()){
        right[R.at(i).second]=R.at(i).first;
        RIGHT.pb({R.at(i).second,R.at(i).first});
    }
    sort(RIGHT.begin(),RIGHT.end());
    rep(i,L.size()){
        if(!right.count(L.at(i).second))continue;
        P l;
        l.first=L.at(i).second;
        l.second=L.at(i).first;
        int r=lower_bound(RIGHT.begin(),RIGHT.end(),l)-RIGHT.begin();
        if(r==0)continue;r--;
        if(RIGHT.at(r).first!=l.first)continue;
        chmin(ans,5*(l.second-RIGHT.at(r).second));
    }

    map<int,int> ur;
    vector<P> UR(0);
    rep(i,U.size()){
        ur[U.at(i).first+U.at(i).second]=U.at(i).first;
        UR.pb({U.at(i).first+U.at(i).second,U.at(i).first});
    }
    sort(UR.begin(),UR.end());
    rep(i,R.size()){
        if(!ur.count(R.at(i).first+R.at(i).second))continue;
        P r;
        r.first=R.at(i).first+R.at(i).second;
        r.second=R.at(i).first;
        int u=lower_bound(UR.begin(),UR.end(),r)-UR.begin();
        if(u==UR.size())continue;
        if(UR.at(u).first!=r.first)continue;
        chmin(ans,10*(UR.at(u).second-r.second));
    }

    map<int,int> dl;
    vector<P> DL(0);
    rep(i,D.size()){
        dl[D.at(i).first+D.at(i).second]=D.at(i).first;
        DL.pb({D.at(i).first+D.at(i).second,D.at(i).first});
    }
    sort(DL.begin(),DL.end());
    rep(i,L.size()){
        if(!dl.count(L.at(i).first+L.at(i).second))continue;
        /*int d=dl[L.at(i).first+L.at(i).second];
        int l=L.at(i).first;
        if(d>l)continue;
        chmin(ans,10*(l-d));*/
        P l;
        l.first=L.at(i).first+L.at(i).second;
        l.second=L.at(i).first;
        int d=lower_bound(DL.begin(),DL.end(),l)-DL.begin();
        if(d==0)continue;d--;
        if(DL.at(d).first!=l.first)continue;
        chmin(ans,10*(l.second-DL.at(d).second));
    }

    map<int,int> ul;
    vector<P> UL(0);
    rep(i,U.size()){
        ul[U.at(i).first-U.at(i).second]=U.at(i).first;
        UL.pb({U.at(i).first-U.at(i).second,U.at(i).first});
    }
    sort(UL.begin(),UL.end());
    rep(i,L.size()){
        if(!ul.count(L.at(i).first-L.at(i).second))continue;
        /*int u=ul[L.at(i).first-L.at(i).second];
        int l=L.at(i).first;
        if(u>l)continue;
        chmin(ans,10*(l-u));*/
        P l;
        l.first=L.at(i).first-L.at(i).second;
        l.second=L.at(i).first;
        int u=lower_bound(UL.begin(),UL.end(),l)-UL.begin();
        if(u==0)continue;u--;
        if(UL.at(u).first!=l.first)continue;
        chmin(ans,10*(l.second-UL.at(u).second));
    }

    map<int,int> dr;
    vector<P> DR(0);
    rep(i,D.size()){
        dr[D.at(i).first-D.at(i).second]=D.at(i).first;
        DR.pb({D.at(i).first-D.at(i).second,D.at(i).first});
    }
    sort(DR.begin(),DR.end());
    rep(i,R.size()){
        if(!dr.count(R.at(i).first-R.at(i).second))continue;
        /*int d=dr[R.at(i).first-R.at(i).second];
        int r=R.at(i).first;
        if(r>d)continue;
        chmin(ans,10*(d-r));*/
        P r;
        r.first=R.at(i).first-R.at(i).second;
        r.second=R.at(i).first;
        int d=lower_bound(DR.begin(),DR.end(),r)-DR.begin();
        if(d==DR.size())continue;
        if(DR.at(d).first!=r.first)continue;
        chmin(ans,10*(DR.at(d).second-r.second));
    }
    if(ans==INF)cout<<"SAFE"<<endl;
    else cout<<ans<<endl;
}

Measured memory_bytes: 31936