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

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef vector<vector<long long>> VVL;
typedef vector<string> VS;
typedef pair<int, int> P;
typedef tuple<int,int,int> tpl;

#define ALL(a)  (a).begin(),(a).end()
#define SORT(c) sort((c).begin(),(c).end())
#define REVERSE(c) reverse((c).begin(),(c).end())
#define EXIST(m,v) (m).find((v)) != (m).end()
#define LB(a,x) lower_bound((a).begin(), (a).end(), x) - (a).begin()
#define UB(a,x) upper_bound((a).begin(), (a).end(), x) - (a).begin()

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define RFOR(i,a,b) for(int i=(a)-1;i>=(b);--i)
#define RREP(i,n) RFOR(i,n,0)

#define en "\n"

constexpr double EPS = 1e-9;
constexpr double PI  = 3.1415926535897932;
constexpr int INF = 2147483647;
constexpr long long LINF = 1LL<<60;
constexpr long long MOD = 1000000007; // 998244353;

#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

void Main(){
    int N; cin >> N;
    VI ux[200200],dx[200200],ly[200200],ry[200200];
    VI uxy1[400400],uxy2[400400],dxy1[400400],dxy2[400400];
    VI X(N),Y(N);
    char U[N];

    auto f1 = [&](int x, int y, int *n, int *t){
        if(x<=y){
            *n = 200000-(y-x);
            *t = x;
        }
        else{
            *n = 200000+(x-y);
            *t = y;
        }
    };
    auto f2 = [&](int x, int y, int *n, int *t){
        if(y>=-x+200000){
            *n = 200000-(y+x-200000);
            *t = 200000-x;
        }
        else{
            *n = 400000-(x+y);
            *t = y;
        }
    };

    REP(i,N){
        cin >> X[i] >> Y[i] >> U[i];
        int n1,t1,n2,t2; f1(X[i],Y[i],&n1,&t1); f2(X[i],Y[i],&n2,&t2);
        if(U[i]=='L'){
            ly[Y[i]].push_back(X[i]);
        }
        else if(U[i]=='R'){
            ry[Y[i]].push_back(X[i]);
        }
        else if(U[i]=='U'){
            ux[X[i]].push_back(Y[i]);
            uxy1[n1].push_back(t1);
            uxy2[n2].push_back(t2);
        }
        else if(U[i]=='D'){
            dx[X[i]].push_back(Y[i]);
            dxy1[n1].push_back(t1);
            dxy2[n2].push_back(t2);
        }
    }

    REP(i,200001){
        SORT(ux[i]); SORT(dx[i]);
        SORT(ly[i]); SORT(ry[i]);
    }
    REP(i,400001){
        SORT(uxy1[i]); SORT(uxy2[i]);
        SORT(dxy1[i]); SORT(dxy2[i]);
    }

    int ans = INF;
    REP(i,N){
        int k;
        int n1,t1,n2,t2; f1(X[i],Y[i],&n1,&t1); f2(X[i],Y[i],&n2,&t2);
        if(U[i]=='L'){
            int l = -1, r = uxy1[n1].size();
            while(r-l>1){
                int x = (l+r)/2;
                if(uxy1[n1][x]<t1) l = x;
                else r = x;
            }
            int t = l;
            if(t>-1) chmin(ans, (t1-uxy1[n1][t])*10);
            t = LB(dxy2[n2],t2+1);
            k = dxy2[n2].size();
            if(t<k) chmin(ans, (dxy2[n2][t]-t2)*10);
        }
        else if(U[i]=='R'){
            int t = LB(ly[Y[i]], X[i]); k = ly[Y[i]].size();
            if(t<k) chmin(ans, (ly[Y[i]][t]-X[i])*5);
            int l=-1, r=uxy2[n2].size();
            while(r-l>1){
                int x = (l+r)/2;
                if(uxy2[n2][x]<t2) l = x;
                else r = x;
            }
            t = l;
            if(t>-1) chmin(ans, (t2-uxy2[n2][t])*10);
            t = LB(dxy1[n1],t1+1);
            k = dxy1[n1].size();
            if(t<k) chmin(ans, (dxy1[n1][t]-t1)*10);
        }
        else if(U[i]=='U'){
            int t = LB(dx[X[i]],Y[i]);
            k = dx[X[i]].size();
            if(t<k) chmin(ans, (dx[X[i]][t]-Y[i])*5);
        }
    }

    if(ans<INF) cout << ans << en;
    else cout << "SAFE" << en;
    return;
}

int main(void){
    cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);cout<<fixed<<setprecision(15);
    int t=1; //cin>>t;
    REP(_,t) Main();
    return 0;
}

Measured memory_bytes: 79672