#include <algorithm>
#include <complex>
#include <cstdlib>
#include <ctime>
#include <time.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <numeric>
#include <limits>
#include <type_traits>
#include <locale>
#include <omp.h>

using namespace std;
#define SAY_YES cout << "YES" << endl;
#define SAY_Yes cout << "Yes" << endl;
#define SAY_NO cout << "NO" << endl;
#define SAY_No cout << "No" << endl;
#define IFYES(TRUE_OR_FALSE)   \
    if (TRUE_OR_FALSE)         \
    {                          \
        cout << "YES" << endl; \
    }                          \
    else                       \
    {                          \
        cout << "NO" << endl;  \
    }
#define IFYes(TRUE_OR_FALSE)   \
    if (TRUE_OR_FALSE)         \
    {                          \
        cout << "Yes" << endl; \
    }                          \
    else                       \
    {                          \
        cout << "No" << endl;  \
    }
#define IFyes(TRUE_OR_FALSE)   \
    if (TRUE_OR_FALSE)         \
    {                          \
        cout << "yes" << endl; \
    }                          \
    else                       \
    {                          \
        cout << "no" << endl;  \
    }
#define DEBUG_OUTPUT_ARRAY(XXX, ONE)                            \
    for (int i = 0; i < (ONE); i++)                             \
    {                                                           \
        cout << "DEBUG: i = " << i << " -> " << XXX[i] << endl; \
    }
#define DEBUG_OUTPUT_ARRAY2(XXX, ONE, TWO)                             \
    for (int i = 0; i < (ONE); i++)                                    \
    {                                                                  \
        cout << "<<< i = " << i << " >>>" << endl;                     \
        for (int j = 0; j < (TWO); j++)                                \
        {                                                              \
            cout << "DEBUG: j = " << j << " -> " << XXX[i][j] << endl; \
        }                                                              \
    }
#define DEBUG_OUTPUT_ARRAY2_BOX(XXX, ONE, TWO) \
    for (int i = 0; i < (ONE); i++)            \
    {                                          \
        cout << i << "  ";                     \
        for (int j = 0; j < (TWO); j++)        \
        {                                      \
            cout << XXX[i][j] << " ";          \
        }                                      \
        cout << endl;                          \
    }

typedef pair<long long int, long long int> pll;
typedef pair<long long int, pll> lpll;

const long long int mod = 1000000007;
const long long int INF = 1e18;
const long double PI=3.14159265358979323; 
const long long int pl=200000;
long long int N,X[200442],Y[200442],P[2002],K=0,res=INF;
char C[200200];
vector<pll>U,R,D,L;
vector<long long int>tmpU[200200],tmpD[200200],tmpR[200200],tmpL[200200];
vector<long long int>tmpLDD[400400],tmpLDL[400400],tmpRUU[400400],tmpRUR[400400],tmpRDR[400400],tmpRDD[400400],tmpLUU[400400],tmpLUL[400400];
int main(){

    cout << fixed << setprecision(18);
    cin>>N;
    
    for(long long int i = 1; i <= N; i++){
        cin>>X[i]>>Y[i]>>C[i];
        if(C[i]=='U'){
            U.push_back({X[i],Y[i]});
            tmpU[X[i]].push_back(Y[i]);
            tmpRUU[X[i]+Y[i]].push_back(X[i]);
            tmpLUU[Y[i]-X[i]+pl].push_back(X[i]);
        }else if(C[i]=='D'){
            D.push_back({X[i],Y[i]});
            tmpD[X[i]].push_back(Y[i]);
            tmpLDD[X[i]+Y[i]].push_back(X[i]);
            tmpRDD[Y[i]-X[i]+pl].push_back(X[i]);
        }else if(C[i]=='R'){
            R.push_back({X[i],Y[i]});
            tmpR[Y[i]].push_back(X[i]);
            tmpRUR[X[i]+Y[i]].push_back(X[i]);
            tmpRDR[Y[i]-X[i]+pl].push_back(X[i]);
        }else if(C[i]=='L'){
            L.push_back({X[i],Y[i]});
            tmpL[Y[i]].push_back(X[i]);
            tmpLDL[X[i]+Y[i]].push_back(X[i]);
            tmpLUL[Y[i]-X[i]+pl].push_back(X[i]);
        }
    }
    for(long long int i = 0; i <= 200000; i++){
        sort(tmpU[i].begin(),tmpU[i].end());
        sort(tmpD[i].begin(),tmpD[i].end());
        sort(tmpR[i].begin(),tmpR[i].end());
        sort(tmpL[i].begin(),tmpL[i].end());
        long long int tmp=0;
        for(long long int j = 0; j < tmpU[i].size(); j++){
            if(tmp>=tmpD[i].size()){break;}
            while(tmp<tmpD[i].size()&&tmpD[i][tmp]<tmpU[i][j]){
                tmp++;
            }
            if(tmp>=tmpD[i].size()){break;}
            res=min(res,(tmpD[i][tmp]-tmpU[i][j])*5);
        }
        tmp=0;
        for(long long int j = 0; j < tmpR[i].size(); j++){
            if(tmp>=tmpL[i].size()){break;}
            while(tmp<tmpL[i].size()&&tmpL[i][tmp]<tmpR[i][j]){
                tmp++;
            }
            if(tmp>=tmpL[i].size()){break;}
            res=min(res,(tmpL[i][tmp]-tmpR[i][j])*5);
        }
    }

    for(long long int i = 0; i <= 400000; i++){
        sort(tmpLDD[i].begin(),tmpLDD[i].end());
        sort(tmpLDL[i].begin(),tmpLDL[i].end());
        sort(tmpRUU[i].begin(),tmpRUU[i].end());
        sort(tmpRUR[i].begin(),tmpRUR[i].end());
        sort(tmpRDD[i].begin(),tmpRDD[i].end());
        sort(tmpRDR[i].begin(),tmpRDR[i].end());
        sort(tmpLUU[i].begin(),tmpLUU[i].end());
        sort(tmpLUL[i].begin(),tmpLUL[i].end());
        long long int tmp=0;
        for(long long int j = 0; j < tmpLDD[i].size(); j++){
            if(tmp>=tmpLDL[i].size()){break;}
            while(tmp<tmpLDL[i].size()&&tmpLDL[i][tmp]<tmpLDD[i][j]){
                tmp++;
            }
            if(tmp>=tmpLDL[i].size()){break;}
            res=min(res,(tmpLDL[i][tmp]-tmpLDD[i][j])*10);
        }
        tmp=0;
        for(long long int j = 0; j < tmpRUR[i].size(); j++){
            if(tmp>=tmpRUU[i].size()){break;}
            while(tmp<tmpRUU[i].size()&&tmpRUU[i][tmp]<tmpRUR[i][j]){
                tmp++;
            }
            if(tmp>=tmpRUU[i].size()){break;}
            res=min(res,(tmpRUU[i][tmp]-tmpRUR[i][j])*10);
        }
        tmp=0;
        for(long long int j = 0; j < tmpRDR[i].size(); j++){
            if(tmp>=tmpRDD[i].size()){break;}
            while(tmp<tmpRDD[i].size()&&tmpRDD[i][tmp]<tmpRDR[i][j]){
                tmp++;
            }
            if(tmp>=tmpRDD[i].size()){break;}
            res=min(res,(tmpRDD[i][tmp]-tmpRDR[i][j])*10);
        }
        tmp=0;
        for(long long int j = 0; j < tmpLUU[i].size(); j++){
            if(tmp>=tmpLUL[i].size()){break;}
            while(tmp<tmpLUL[i].size()&&tmpLUL[i][tmp]<tmpLUU[i][j]){
                tmp++;
            }
            if(tmp>=tmpLUL[i].size()){break;}
            res=min(res,(tmpLUL[i][tmp]-tmpLUU[i][j])*10);
        }
    }
    if(res==INF){cout<<"SAFE"<<endl;}
    else{
    cout<<res<<endl;
    }
}



Measured memory_bytes: 122100