//clear adj and visited vector declared globally after each test case
//check for long long overflow
//while adding and subs check if mod becomes -ve
//while using an integer directly in a builtin function add ll
//Mod wale question mein last mein if dalo ie. Ans<0 then ans+=mod;
//Dont keep array name as size or any other key word

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define int long long
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);cout.precision(dbl::max_digits10);
#define pb push_back
#define mod 1000000007 //998244353
#define lld long double
#define mii map<int, int>
#define mci map<char, int>
#define msi map<string, int>
#define pii pair<int, int>
#define ff first
#define ss second 
#define all(x) (x).begin(), (x).end()
#define rep(i,x,y) for(int i=x; i<y; i++)
#define fill(a,b) memset(a, b, sizeof(a))
#define vi vector<int>
#define setbits(x) __builtin_popcountll(x)
#define print2d(dp,n,m) for(int i=0;i<=n;i++){for(int j=0;j<=m;j++)cout<<dp[i][j]<<" ";cout<<"\n";}
typedef std::numeric_limits< double > dbl;
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
const long long N=200005, INF=2000000000000000000;
lld pi=3.1415926535897932;
int lcm(int a, int b)
{
    int g=__gcd(a, b);
    return a/g*b;
}
int power(int a, int b, int p)
    {
        if(a==0)
        return 0;
        int res=1;
        a%=p;
        while(b>0)
        {
            if(b&1)
            res=(res*a)%p;
            b>>=1;
            a=(a*a)%p;
        }
        return res;
    }

vector <int> x[N],y[N],l[2*N],d[2*N],ra[2*N], la[2*N];
int32_t main()
{
    IOS;
    vector <pii> u,r,dd;
    int n;
    cin>>n;
    int ad=200001;
    for(int i=0;i<n;i++)
    {
        int a,b;
        char c;
        cin>>a>>b>>c;
        if(c=='U')
        u.pb({a, b});
        else if(c=='R')
        {
            r.pb({a, b});
            ra[a+b].pb(b);
        }
        else if(c=='L')
        {
            y[b].pb(a);
            l[a-b+ad].pb(a);
            la[a+b].pb(a);
        }
        else
        {
            x[a].pb(b);
            d[a-b+ad].pb(a);
            dd.pb({a, b});
        }
    }
    for(int i=0;i<2*N;i++)
    {
        if(i<N){
        sort(all(x[i]));
        sort(all(y[i]));
        }
        sort(all(l[i]));
        sort(all(d[i]));
        sort(all(la[i]));
        sort(all(ra[i]));
    }
    int ans=INF;
    for(pii p:u)
    {
        int a=p.ff,b=p.ss;
        auto it=lower_bound(all(x[a]), b);
        if(it!=x[a].end())
        ans=min(ans, (*it)-b);
        it=lower_bound(all(l[a-b+ad]), a);
        if(it!=l[a-b+ad].end())
        ans=min(ans, ((*it)-a)*2);
        it=lower_bound(all(ra[a+b]), b);
        if(it!=ra[a+b].end())
        ans=min(ans, ((*it)-b)*2);
    }
    for(pii p:r)
    {
        int a=p.ff,b=p.ss;
        auto it=lower_bound(all(y[b]), a);
        if(it!=y[b].end())
        ans=min(ans, (*it)-a);
        it=lower_bound(all(d[a-b+ad]), a);
        if(it!=d[a-b+ad].end())
        ans=min(ans, ((*it)-a)*2);
    }
    for(pii p:dd)
    {
        int a=p.ff,b=p.ss;
        auto it=lower_bound(all(la[a+b]), a);
        if(it!=la[a+b].end())
        ans=min(ans, ((*it)-a)*2);
    }
    if(ans==INF)
    cout<<"SAFE";
    else
    cout<<ans*5;
}

Measured memory_bytes: 65280