//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using ld=long double;
using ch=char;
typedef pair<ll,ll> P;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<P> vP;
typedef vector<ch> vc;
typedef vector<vc> vvc;
#define FOR(i,a,b) for(ll i=a;i<b;i++)
#define rep(i,n) FOR(i,0,n)
#define ROF(i,a,b) for(ll i=a;i>=b;i--)
#define per(i,a) ROF(i,a,0)
#define pb push_back
const ll MOD=1000000007;
const ll MOD2=998244353;
const ld PI=acos(-1);
const ll INF=1e18;
string abc="abcdefghijklmnopqrstuvwxyz";
string ABC="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
struct edge{ll to,cost;};
struct edge2{ll from,to,cost;};


int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll N;
    cin >> N;
    vP u0,r0,d0,l0,u1,r1,d1,l1,u2,r2,d2,l2;
    rep(i,N){
        ll x,y;
        char c;
        cin >> x >> y >> c;
        if(c=='U'){
            u0.pb({x,y});//up&down
            u1.pb({y-x,x});//up&left
            u2.pb({x+y,x});//up&right
        }
        if(c=='D'){
            d0.pb({x,y});//up&down
            d1.pb({y-x,x});//down&right
            d2.pb({x+y,x});//down&left
        }
        if(c=='L'){
            l0.pb({y,x});//left&right
            l1.pb({y-x,x});//up&left
            l2.pb({x+y,x});//down&left
        }
        if(c=='R'){
            r0.pb({y,x});//left&right
            r1.pb({y-x,x});//down&right
            r2.pb({x+y,x});//up&right
        }
    }
    sort(u0.begin(),u0.end());
    sort(u1.begin(),u1.end());
    sort(u2.begin(),u2.end());
    sort(d0.begin(),d0.end());
    sort(d1.begin(),d1.end());
    sort(d2.begin(),d2.end());
    sort(l0.begin(),l0.end());
    sort(l1.begin(),l1.end());
    sort(l2.begin(),l2.end());
    sort(r0.begin(),r0.end());
    sort(r1.begin(),r1.end());
    sort(r2.begin(),r2.end());
    ll ans=INF;
    ll U=u0.size(),D=d0.size(),L=l0.size(),R=r0.size();
    ll ok,ng,mid;
    rep(i,U){
        if(D){
            ok=D;ng=-1;
            while(abs(ok-ng)>1){
                mid=(ok+ng)/2;
                if(u0[i]<d0[mid]){
                    ok=mid;
                }
                else{
                    ng=mid;
                }
            }
            if(u0[i].first==d0[ok].first&&u0[i].second<d0[ok].second){
                ans=min(ans,5*abs(u0[i].second-d0[ok].second));
            }
        }
        if(L){
            ok=L;ng=-1;
            while(abs(ok-ng)>1){
                mid=(ok+ng)/2;
                if(u1[i]<l1[mid]){
                    ok=mid;
                }
                else{
                    ng=mid;
                }
            }
            if(u1[i].first==l1[ok].first&&u1[i].second<l1[ok].second){
                ans=min(ans,10*abs(u1[i].second-l1[ok].second));
            }
        }
        if(R){
            ok=-1;ng=R;
            while(abs(ok-ng)>1){
                mid=(ok+ng)/2;
                if(u2[i]>r2[mid]){
                    ok=mid;
                }
                else{
                    ng=mid;
                }
            }
            if(u2[i].first==r2[ok].first&&u2[i].second>r2[ok].second){
                ans=min(ans,10*abs(u2[i].second-r2[ok].second));
            }
        }
    }
    rep(i,D){
        if(R){
            ok=-1;ng=R;
            while(abs(ok-ng)>1){
                mid=(ok+ng)/2;
                if(d1[i]>r1[mid]){
                    ok=mid;
                }
                else{
                    ng=mid;
                }
            }
            if(d1[i].first==r1[ok].first&&d1[i].second>r1[ok].second){
                ans=min(ans,10*abs(d1[i].second-r1[ok].second));
            }
        }
        if(L){
            ok=L;ng=-1;
            while(abs(ok-ng)>1){
                mid=(ok+ng)/2;
                if(d2[i]<l2[mid]){
                    ok=mid;
                }
                else{
                    ng=mid;
                }
            }
            if(d2[i].first==l2[ok].first&&d2[i].second<l2[ok].second){
                ans=min(ans,10*abs(d2[i].second-l2[ok].second));
            }
        }
    }
    rep(i,L){
        if(R){
            ok=-1;ng=R;
            while(abs(ok-ng)>1){
                mid=(ok+ng)/2;
                if(l0[i]>r0[mid]){
                    ok=mid;
                }
                else{
                    ng=mid;
                }
            }
            if(l0[i].first==r0[ok].first&&l0[i].second>r0[ok].second){
                ans=min(ans,5*abs(l0[i].second-r0[ok].second));
            }
        }
    }
    if(ans==INF){
        cout << "SAFE" << endl;
        return 0;
    }
    cout << ans << endl;
}


Measured memory_bytes: 15388