#include<bits/stdc++.h>

#ifndef LOCAL
#pragma GCC optimize("O3")
#endif
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")

#define endl        '\n'
#define ll          long long
#define pb          push_back
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define rs(v,n)     v.resize(n)
#define hell        1000000007
#define peak        9123372036854775807
#define pii         acos(-1)
#define clr(a,x)    memset(a,x,sizeof(a))
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;

namespace InOp{
template<class x,class y>
istream &operator>>(istream &in,pair<x,y> &p) 
{ 
    in>>p.F>>p.S;
    return in;
}
template<class x>
istream &operator>>(istream &in,vector<x> &v) 
{
    for(auto& i:v)
        in>>i;
    return in;
}
template<class x,class y>
ostream &operator<<(ostream &out,pair<x,y> &p) 
{ 
    out<<"("<<p.F<<","<<p.S<<") ";
    return out; 
}
template<class x>
ostream &operator<<(ostream &out,vector<x> &v) 
{ 
    out<<v.size()<<endl;
    for(auto i:v)
        out<<i<<" ";
    out<<endl;
    return out; 
}
}
using namespace InOp;

namespace ModOp{
template<class x,class y,class z> z add(x a,y b,z m){return (a+b)%m;}
template<class x,class y,class z> z sub(x a,y b,z m){return (a-b+m)%m;}
template<class x,class y,class z> z mul(x a,y b,z m){return ((ll)a*b)%m;}
template<class y,class z> z expo(ll b,y e,z m){ll a=1;b%=m;while(e!=0){if((e&1)==1){a=a*b;a=a%m;}b=b*b;b%=m;e>>=1;}return a%m;}
template<class x,class z> z inv(x b,z m){return expo(b,m-2,m);}
template<class x,class y> x invGeneral(x a,y b){if(a==0)return b==1?0:-1;x m=invGeneral(b%a,a);return m==-1?-1:((1-(ll)b*m)/a+b)%b;}
template<class x,class y,class z> z div(x a,y b,z m){ll c=inv(b,m);return mul(a,c,m);}
}
using namespace ModOp;

namespace Debug{
template<class x> void deb(x a){cerr<<"a"<<": "<<a<<endl;}
template<class x,class y> void deb(x a,y b){cerr<<"a"<<": "<<a<<" | "<<"b"<<": "<<b<<endl;}
template<class x,class y,class z> void deb(x a,y b,z c){cerr<<"a"<<": "<<a<<" | "<<"b"<<": "<<b<<" | "<<"c"<<": "<<c<<endl;}
template<class x,class y,class z,class xy> void deb(x a,y b,z c,xy d){cerr<<"a"<<": "<<a<<" | "<<"b"<<": "<<b<<" | "<<"c"<<": "<<c<<" | "<<"d"<<": "<<d<<endl;}
}
using namespace Debug;

#define N  400005
#define M  200000

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    ll tt=1;
    // cin>>tt;
    while(tt--)
    {
        ll i,j,k,l,r,x,y,m,n;
        cin>>n;
        vector<pair<ll,ll>> v[4];
        vector<vector<vector<ll>>> di(4,vector<vector<ll>>(N));
        vector<vector<vector<vector<ll>>>> vv(4,vector<vector<vector<ll>>>(2,vector<vector<ll>>(N)));
        for(i=0;i<n;i++)
        {
            char m;
            cin>>x>>y>>m;
            if(m=='U')
            {
                v[0].pb({x,y});
                di[0][x].pb(y);
                vv[0][0][x+y].pb(x);
                vv[0][1][x-y+M].pb(x);
            }
            if(m=='R')
            {
                v[1].pb({y,x});
                di[1][y].pb(x);
                vv[1][0][x+y].pb(y);
                vv[1][1][x-y+M].pb(y);
            }
            if(m=='D')
            {
                v[2].pb({x,y});
                di[2][x].pb(y);
                vv[2][0][x+y].pb(x);
                vv[2][1][x-y+M].pb(x);
            }
            if(m=='L')
            {
                v[3].pb({y,x});
                di[3][y].pb(x);
                vv[3][0][x+y].pb(y);
                vv[3][1][x-y+M].pb(y);
            }
        }
        // sort(all(di[0]));sort(all(di[1]));sort(all(di[2]));sort(all(di[3]));
        for(i=0;i<4;i++)
        {
            for(j=0;j<2;j++)
            {
                for(k=0;k<N;k++)sort(all(vv[i][j][k]));
            }
        }
        for(i=0;i<4;i++)
        {
            for(j=0;j<N;j++)
            {
                sort(all(di[i][j]));
            }
        }
        ll ans=hell;
        for(j=0;j<N;j++)
        {
            for(auto i:di[0][j])
            {
                ll x=lower_bound(all(di[2][j]),i)-di[2][j].begin();
                if(x==di[2][j].size())continue;
                ans=min(ans,(di[2][j][x]-i)*5);
            }
        }
        for(j=0;j<N;j++)
        {
            for(auto i:di[1][j])
            {
                ll x=lower_bound(all(di[3][j]),i)-di[3][j].begin();
                if(x==di[3][j].size())continue;
                ans=min(ans,(di[3][j][x]-i)*5);
            }
        }
        // deb(ans);
        for(auto i:v[1])
        {
            x=i.F+i.S;
            y=lower_bound(all(vv[0][0][x]),i.S)-vv[0][0][x].begin();
            if(y!=vv[0][0][x].size())
            {
                ans=min(ans,(vv[0][0][x][y]-i.S)*10);
            }
            x=-i.F+i.S+M;
            y=lower_bound(all(vv[2][1][x]),i.S)-vv[2][1][x].begin();
            if(y!=vv[2][1][x].size())
            {
                ans=min(ans,(vv[2][1][x][y]-i.S)*10);
            }
        }
        for(auto i:v[3])
        {
            x=i.F+i.S;
            y=lower_bound(all(vv[2][0][x]),i.S)-vv[2][0][x].begin();
            if(y!=0)
            {
                ans=min(ans,(-vv[2][0][x][y-1]+i.S)*10);
            }
            x=-i.F+i.S+M;
            y=lower_bound(all(vv[0][1][x]),i.S)-vv[0][1][x].begin();
            if(y!=0)
            {
                ans=min(ans,(-vv[0][1][x][y-1]+i.S)*10);
            }
        }
        if(ans==hell)
        {
            cout<<"SAFE\n";
            continue;
        }
        cout<<ans;
    }
    return 0;
}

Measured memory_bytes: 143832