#include <bits/stdc++.h>
using namespace std;

#define  int        long long int
 #define    sp(x)                 setprecision(x)
#define    bp(x)           __builtin_popcount(x)
 #define  ld         long double
 #define  f(i,j,n)            for(int i = j; i <= n; i++)
#define  r(i,n,j)       for(int i = n; i >= j; i--)
 #define  all(container)       container.begin() , container.end()
#define  sz(container)   (int)container.size()
 #define  ff             first
#define  ss             second
 #define  pii      pair <int , int>
#define  sp(x)       setprecision(x)
 #define  endl    "\n"
#define  mod  1000000007
 #define  pb    push_back
#define  mp    make_pair
 #define  T     int ttt; cin >> ttt; while(ttt--)
#define fast ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);

int power(int x, int y, int p)  
{  
    int res = 1;    
    x = x % p; 
    if (x == 0) return 0; 
    while (y > 0)  
    {     
        if (y & 1)  
            res = (res*x) % p;  
        y = y>>1; 
        x = (x*x) % p;  
    }  
    return res; 
}

int32_t main()
{
    fast
    char c;
    int n,x,y,ans=1e18;
    cin>>n;
    map<int,vector<int> >u1,u2,l1,l2,d1,d2,r1,r2,up,down,left,right;
    map<int,vector<int> > :: iterator it;
    f(i,1,n)
    {
        cin>>x>>y>>c;
        if(c=='U')
        {
            u1[x+y].pb(x);
            u2[x-y].pb(x);
            up[x].pb(y);
        }
        else if(c=='D')
        {
            d1[x+y].pb(x);
            d2[x-y].pb(x);
            down[x].pb(y);
        }
        else if(c=='L')
        {
            l1[x+y].pb(x);
            l2[x-y].pb(x);
            left[y].pb(x);
        }
        else 
        {
            r1[x+y].pb(x);
            r2[x-y].pb(x);
            right[y].pb(x);
        }
    }
    it=u1.begin();
    while(it!=u1.end()){
        x=it->first;
        vector<int>vv=it->second;
        if(r1.find(x)!=r1.end())
        {
            vector<int>v=r1[x];
            sort(all(v));
            f(i,0,sz(vv)-1)
            {
                int l=0,r=sz(v)-1;
                while(l<=r)
                {
                    int m=(l+r)>>1LL;
                    if(v[m]<vv[i])
                    {
                        ans=min(ans,(vv[i]-v[m])*10);
                        l=m+1;
                    }
                    else    r=m-1;
                }
            }
        }
        it++;
    }
    it=u2.begin();
    while(it!=u2.end() ){
        x=it->first;
        vector<int>vv=it->second;
        if(l2.find(x)!=l2.end())
        {
            vector<int>v=l2[x];
            sort(all(v));
            f(i,0,sz(vv)-1)
            {
                int l=0,r=sz(v)-1;
                while(l<=r)
                {
                    int m=(l+r)>>1LL;
                    if(v[m]>vv[i])
                    {
                        ans=min(ans,(v[m]-vv[i])*10);
                        r=m-1;
                    }
                    else    l=m+1;
                }
            }
        }
        it++;
    }
    it=d1.begin();
    while(it!=d1.end() ){
        x=it->first;
        vector<int>vv=it->second;
        if(l1.find(x)!=l1.end())
        {
            vector<int>v=l1[x];
            sort(all(v));
            f(i,0,sz(vv)-1)
            {
                int l=0,r=sz(v)-1;
                while(l<=r)
                {
                    int m=(l+r)>>1LL;
                    if(v[m]>vv[i])
                    {
                        ans=min(ans,(v[m]-vv[i])*10);
                        r=m-1;
                    }
                    else    l=m+1;
                }
            }
        }
        it++;
    }
    it=d2.begin();
    while(it!=d2.end()){
        x=it->first;
        vector<int>vv=it->second;
        if(r2.find(x)!=r2.end())
        {
            vector<int>v=r2[x];
            sort(all(v));
            f(i,0,sz(vv)-1)
            {
                int l=0,r=sz(v)-1;
                while(l<=r)
                {
                    int m=(l+r)>>1LL;
                    if(v[m]<vv[i])
                    {
                        ans=min(ans,(vv[i]-v[m])*10);
                        l=m+1;
                    }
                    else    r=m-1;
                }
            }
        }
        it++;
    }
    it=up.begin();
    while(it!=up.end())
    {
        x=it->first;
        vector<int>vv=it->second;
        if(down.find(x)!=down.end())
        {
            vector<int>v=down[x];
            sort(all(v));
            f(i,0,sz(vv)-1)
            {
                int l=0,r=sz(v)-1;
                while(l<=r)
                {
                    int m=(l+r)>>1LL;
                    if(v[m]>vv[i])
                    {
                        ans=min(ans,(v[m]-vv[i])*5);
                        r=m-1;
                    }
                    else    l=m+1;
                }
            }
        }
        
        it++;
    }
    it=right.begin();
    while(it!=right.end())
    {
        x=it->first;
        vector<int>vv=it->second;
        if(left.find(x)!=left.end())
        {
            vector<int>v=left[x];
            sort(all(v));
            f(i,0,sz(vv)-1)
            {
                int l=0,r=sz(v)-1;
                while(l<=r)
                {
                    int m=(l+r)>>1LL;
                    if(v[m]>vv[i])
                    {
                        ans=min(ans,(v[m]-vv[i])*5);
                        r=m-1;
                    }
                    else    l=m+1;
                }
            }
        }
        it++;
    }
    if(ans==1e18)
        cout<<"SAFE";
    else    
        cout<<ans;
}

Measured memory_bytes: 69088