#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
int main() {
    
    ll n, x[200001], y[200001], i, j, k, min=200000000000, imsi1[200001], imsi2[200001], a, b, begin, end, mid;
    vector<ll> up[200001], down[200001], left[200001], right[200001];
    vector<ll> uplus[400001], dplus[400001], lplus[400001], rplus[400001];
    vector<ll> u[400001], d[400001], l[400001], r[400001];
    char U[200001];
    cin>>n;
    for(i=0;i<=200000;i++)
    {
        up[i].clear();
        down[i].clear();
        left[i].clear();
        right[i].clear();
    }
    for(i=0;i<=400000;i++)
    {
        uplus[i].clear();
        lplus[i].clear();
        rplus[i].clear();
        dplus[i].clear();
        u[i].clear();
        l[i].clear();
        r[i].clear();
        d[i].clear();
    }
    for(i=1;i<=n;i++)
    {
        cin>>x[i]>>y[i];
        scanf(" %c ", &U[i]);
        if(U[i]=='U'){
        up[x[i]].push_back(y[i]);
        uplus[x[i]+y[i]].push_back(x[i]);
        u[y[i]-x[i]+200000].push_back(x[i]);
        }
        else if(U[i]=='D'){
        down[x[i]].push_back(y[i]);
        dplus[x[i]+y[i]].push_back(x[i]);
        d[y[i]-x[i]+200000].push_back(x[i]);
        
        }
        else if(U[i]=='L'){
        left[y[i]].push_back(x[i]);
        lplus[x[i]+y[i]].push_back(x[i]);
        l[y[i]-x[i]+200000].push_back(x[i]);
        
        }
        else{
        right[y[i]].push_back(x[i]);
        rplus[x[i]+y[i]].push_back(x[i]);
        r[y[i]-x[i]+200000].push_back(x[i]);
        
        }
        
    }
    for(i=0;i<=200000;i++){
    sort(up[i].begin(), up[i].end());
    sort(down[i].begin(), down[i].end());
    sort(left[i].begin(), left[i].end());
    sort(right[i].begin(), right[i].end());
    }
    for(i=0;i<=200000;i++)
    {
        for(j=0;j<up[i].size();j++)
        {
            begin=0;
            end=down[i].size();
            while(begin!=end)
            {
                mid=(begin+end)/2;
                
                if(down[i][mid]>=up[i][j])
                end=mid;
                else
                begin=mid+1;
            }
            if(begin<down[i].size())
            {
                min=min<5*(down[i][begin]-up[i][j])?min:5*(down[i][begin]-up[i][j]);
            }
        }
    }
    
    for(i=0;i<=200000;i++)
    {
        for(j=0;j<right[i].size();j++)
        {
            begin=0;
            end=left[i].size();
            while(begin!=end)
            {
                mid=(begin+end)/2;
                
                if(left[i][mid]>=right[i][j])
                end=mid;
                else
                begin=mid+1;
            }
            if(begin<left[i].size())
            {
                min=min<5*(left[i][begin]-right[i][j])?min:5*(left[i][begin]-right[i][j]);
            }
        }
    }
    
    
    for(i=0;i<=400000;i++)
    {
        sort(uplus[i].begin(), uplus[i].end());
        sort(dplus[i].begin(), dplus[i].end());
        sort(lplus[i].begin(), lplus[i].end());
        sort(rplus[i].begin(), rplus[i].end());   
    }
    
    
       for(i=0;i<=400000;i++)
    {
        for(j=0;j<rplus[i].size();j++)
        {
            begin=0;
            end=uplus[i].size();
            while(begin!=end)
            {
                mid=(begin+end)/2;
                
                if(uplus[i][mid]>=rplus[i][j])
                end=mid;
                else
                begin=mid+1;
            }
            if(begin<uplus[i].size())
            {
                min=min<10*(uplus[i][begin]-rplus[i][j])?min:10*(uplus[i][begin]-rplus[i][j]);
            }
        }
    } 
    
    
    for(i=0;i<=400000;i++)
    {
        for(j=0;j<dplus[i].size();j++)
        {
            begin=0;
            end=lplus[i].size();
            while(begin!=end)
            {
                mid=(begin+end)/2;
                
                if(lplus[i][mid]>=dplus[i][j])
                end=mid;
                else
                begin=mid+1;
            }
            if(begin<lplus[i].size())
            {
                min=min<10*(lplus[i][begin]-dplus[i][j])?min:10*(lplus[i][begin]-dplus[i][j]);
            }
        }
    } 
    
    for(i=0;i<=400000;i++)
    {
        sort(u[i].begin(), u[i].end());
        sort(d[i].begin(), d[i].end());
        sort(l[i].begin(), l[i].end());
        sort(r[i].begin(), r[i].end());   
    }
    
    for(i=0;i<=400000;i++)
    {
        for(j=0;j<u[i].size();j++)
        {
            begin=0;
            end=l[i].size();
            while(begin!=end)
            {
                mid=(begin+end)/2;
                
                if(l[i][mid]>=u[i][j])
                end=mid;
                else
                begin=mid+1;
            }
            if(begin<l[i].size())
            {
                min=min<10*(l[i][begin]-u[i][j])?min:10*(l[i][begin]-u[i][j]);
            }
        }
    } 
    
    for(i=0;i<=400000;i++)
    {
        for(j=0;j<r[i].size();j++)
        {
            begin=0;
            end=d[i].size();
            while(begin!=end)
            {
                mid=(begin+end)/2;
                
                if(d[i][mid]>=r[i][j])
                end=mid;
                else
                begin=mid+1;
            }
            if(begin<d[i].size())
            {
                min=min<10*(d[i][begin]-r[i][j])?min:10*(d[i][begin]-r[i][j]);
            }
        }
    }
    
    
    
    
    if(min==200000000000)
    printf("SAFE");
    else
    cout<<min;
    
    return 0;
}

Measured memory_bytes: 119500