//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <queue>

using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) for(int i=0;i<n;++i)
#define mod (ll)(1e9+7)
#define FIX(a) ((a)%mod+mod)%mod
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define fi first
#define se second
#define pb push_back
int dy[]={0, 0, 1, -1};
int dx[]={1, -1, 0, 0};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n;
    cin >> n;
    ll ans = LLINF;
    vector<pair<ll,ll> > xyd;
    vector<pair<ll,ll> > xyu;
    vector<pair<ll,ll> > yxr;
    vector<pair<ll,ll> > yxl;
    vector<pair<ll,ll> > XPYxd;
    vector<pair<ll,ll> > XPYxu;
    vector<pair<ll,ll> > XPYxl;
    vector<pair<ll,ll> > XPYxr;
    vector<pair<ll,ll> > XMYxd;
    vector<pair<ll,ll> > XMYxu;
    vector<pair<ll,ll> > XMYxl;
    vector<pair<ll,ll> > XMYxr;
    vector<pair<pair<ll,ll>,ll> > p(n);
    REP(i,n){
        ll x,y;
        char u;
        cin >> x >> y >> u;
        if(u=='U'){
            p[i] = make_pair(make_pair(x,y), 0);
            xyu.pb(make_pair(x,y));
            XPYxu.pb(make_pair(x+y,x));
            XMYxu.pb(make_pair(x-y,x));
        }else if(u=='D'){
            p[i] = make_pair(make_pair(x,y), 1);
            xyd.pb(make_pair(x,y));
            XPYxd.pb(make_pair(x+y,x));
            XMYxd.pb(make_pair(x-y,x));
        }else if(u=='R'){
            p[i] = make_pair(make_pair(x,y), 2);
            yxr.pb(make_pair(y,x));
            XPYxr.pb(make_pair(x+y,x));
            XMYxr.pb(make_pair(x-y,x));
        }else{
            p[i] = make_pair(make_pair(x,y), 3);
            yxl.pb(make_pair(y,x));
            XPYxl.pb(make_pair(x+y,x));
            XMYxl.pb(make_pair(x-y,x));
        }
    }
    
    ll sd = (ll)xyd.size();
    ll su = (ll)xyu.size();
    ll sr = (ll)yxr.size();
    ll sl = (ll)yxl.size();
    if(su>0) sort(ALL(xyu));
    if(sd>0) sort(ALL(xyd));
    if(sr>0) sort(ALL(yxr));
    if(sl>0) sort(ALL(yxl));
    if(su>0) sort(ALL(XPYxu));
    if(sd>0) sort(ALL(XPYxd));
    if(sr>0) sort(ALL(XPYxr));
    if(sl>0) sort(ALL(XPYxl));
    if(su>0) sort(ALL(XMYxu));
    if(sd>0) sort(ALL(XMYxd));
    if(sr>0) sort(ALL(XMYxr));
    if(sl>0) sort(ALL(XMYxl));
    if(n==1){
        puts("SAFE");
        return 0;
    }
    REP(i,n){
        ll x = p[i].fi.fi;
        ll y = p[i].fi.se;
        if(p[i].se==0){
            //up
            {
                //down
                ll ok = sd;
                ll ng = -1;
                while(abs(ok-ng)>1){
                    ll mid = (ok+ng)/2;
                    if(xyd[mid].fi>x||(xyd[mid].fi==x&&xyd[mid].se>y)){
                        ok = mid;
                    }else{
                        ng = mid;
                    }
                }
                if(ok<sd&&xyd[ok].fi==x){
                    ans = min(ans,(xyd[ok].se-y)*5);
                }
            }
            //left
            {
                ll ok = sl;
                ll ng = -1;
                while(abs(ok-ng)>1){
                    ll mid = (ok+ng)/2;
                    if(XMYxl[mid].fi>x-y||(XMYxl[mid].fi==x-y&&XMYxl[mid].se>x)){
                        ok = mid;
                    }else{
                        ng = mid;
                    }
                }
                if(ok<sl&&XMYxl[ok].fi==x-y){
                    ans = min(ans, (XMYxl[ok].se-x)*10);
                }
            }
            ///right
            {
                ll ok = -1;
                ll ng = sr;
                while(abs(ok-ng)>1){
                    ll mid = (ok+ng)/2;
                    if(XPYxr[mid].fi<x+y||(XPYxr[mid].fi==x+y&&XPYxr[mid].se<x)){
                        ok = mid;
                    }else{
                        ng = mid;
                    }
                }
                if(ok>=0&&XPYxr[ok].fi==x+y){
                    ans = min(ans, (x-XPYxr[ok].se)*10);
                }
            }
        }else if(p[i].se==1){
            //down
            {
                //right
                ll ok = -1;
                ll ng = sr;
                while(abs(ok-ng)>1){
                    ll mid = (ok+ng)/2;
                    if(XMYxr[mid].fi<x-y||(XMYxr[mid].fi==x-y&&XMYxr[mid].se<x)){
                        ok = mid;
                    }else{
                        ng = mid;
                    }
                }
                if(ok>=0&&XMYxr[ok].fi==x-y){
                    ans = min(ans, (x-XMYxr[ok].se)*10);
                }
            }
            {
                //left
                ll ok = sl;
                ll ng = -1;
                while(abs(ok-ng)>1){
                    ll mid = (ok+ng)/2;
                    if(XPYxl[mid].fi>x+y||(XPYxl[mid].fi==x+y&&XPYxl[mid].se>x)){
                        ok = mid;
                    }else{
                        ng = mid;
                    }
                }
                if(ok<sl&&XPYxl[ok].fi==x+y){
                    ans = min(ans, (XPYxl[ok].se-x)*10);
                }
            }
        }else if(p[i].se==2){
            //right
            //left
            ll ok = sl;
            ll ng = -1;
            while(abs(ok-ng)>1){
                ll mid = (ok+ng)/2;
                if(yxl[mid].fi>y||(yxl[mid].fi==y&&yxl[mid].se>x)){
                    ok = mid;
                }else{
                    ng = mid;
                }
            }
            if(ok<sl&&yxl[ok].fi==y){
                ans = min(ans, (yxl[ok].se-x)*5);
            }
        }else{

        }
    }

    if(ans == LLINF){
        puts("SAFE");
    }else{
        cout << ans << endl;
    }

    return 0;
}

Measured memory_bytes: 20044