#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define dd double
#define endl "\n"
#define pb push_back
#define all(v) v.begin(),v.end()
#define mp make_pair
#define fi first
#define se second
#define vll vector<ll>
#define pll pair<ll,ll>
#define fo(i,n) for(int i=0;i<n;i++)
#define fo1(i,n) for(int i=1;i<=n;i++)
ll mod=1000000007;
ll n,k,t,m,q,flag=0;
ll power(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
// #include <ext/pb_ds/assoc_container.hpp> 
// #include <ext/pb_ds/tree_policy.hpp> 
// using namespace __gnu_pbds; 
// #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
// ordered_set s ; s.order_of_key(a) -- no. of elements strictly less than a
// s.find_by_order(i) -- itertor to ith element (0 indexed)
ll min(ll a,ll b){if(a>b)return b;else return a;}
ll max(ll a,ll b){if(a>b)return a;else return b;}
ll gcd(ll a , ll b){ if(b > a) return gcd(b , a) ; if(b == 0) return a ; return gcd(b , a%b) ;}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    #ifdef NOOBxCODER
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #else 
    #define NOOBxCODER 0
    #endif
    //cin>>t;
    t=1;
    while(t--){
        cin>>n;
        pair<char, pll >p[n];
        set<pll>s1,s2,s3,s4;
        fo(i,n){
            ll a,b;
            char c;
            cin>>a>>b>>c;
            a*=2;
            b*=2;
            p[i]=mp(c,mp(a,b));
            if(c=='U')s1.insert(mp(a,b));
            if(c=='D')s2.insert(mp(a,b));
            if(c=='L')s4.insert(mp(b,a));
            if(c=='R')s3.insert(mp(b,a));
        }
        set<pll>sum1,diff1,sum2,diff2;
        ll ans=mod;
        auto it=s1.begin();
        if(s2.size()>0)fo(i,s1.size()){
            auto it2=s2.begin();
            pll node=*it;
            it2=s2.lower_bound(node);
            pll temp=*it2;
            if(temp.fi==node.fi )ans=min(ans, (temp.se-node.se)/2);
            it++;
        }
        it=s3.begin();
        if(s4.size()>0)fo(i,s3.size()){
            auto it2=s4.begin();
            pll node=*it;
            it2=s4.lower_bound(node);
            pll temp=*it2;
            if(temp.fi==node.fi )ans=min(ans,  (temp.se-node.se)/2);
            it++;
        }
        it=s3.begin();
        fo(i,s3.size()){
            pll node=*it;
            sum1.insert(mp(node.fi+node.se, node.se));
            diff1.insert(mp(node.fi-node.se, node.se));
            it++;
        }
        it=s4.begin();
        fo(i,s4.size()){
            pll node=*it;
            sum2.insert(mp(node.fi+node.se, node.se));
            diff2.insert(mp(node.fi-node.se, node.se));
            it++;
        }
        //cout<<ans<<endl;
        it=s1.begin();
        fo(i,s1.size()){
            pll node=*it;
            
            auto it2=diff2.begin();
            if(diff2.size()!=0){
            it2=diff2.lower_bound(mp(node.se-node.fi, node.fi) );
            pll temp=*it2;
            if(temp.fi==node.se-node.fi )ans=min(ans, temp.se- node.fi);
            }
            if(sum1.size()!=0){
            it2=sum1.lower_bound(mp(node.se+node.fi, node.fi) );
            if(it2!= sum1.begin()){
                it2--;
                pll temp=*it2;
                if(temp.fi==node.se+node.fi )ans=min(ans, node.fi- temp.se);
            }
            }



            it++;

        }
        it=s2.begin();
        fo(i,s2.size()){
            pll node=*it;
            //cout<<node.fi<<endl;
            auto it2=sum2.begin();
            if(sum2.size()!=0){
            it2=sum2.lower_bound(mp(node.se+node.fi, node.fi) );
            pll temp=*it2;
            if(temp.fi==node.se+node.fi )ans=min(ans, temp.se- node.fi);
            }
            //cout<<ans<<endl;
            if(diff1.size()!=0){
            it2=diff1.lower_bound(mp(node.se-node.fi, node.fi) );
            if(it2!= diff1.begin()){
                it2--;
                pll temp=*it2;
                if(temp.fi==node.se-node.fi )ans=min(ans, node.fi- temp.se);
            }
            }


            it++;

        }
        if(ans==mod)cout<<"SAFE"<<endl;
        else cout<<ans*5;



        //cout<<ans;





        
    }
    cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
    return 0;
}


Measured memory_bytes: 45472