#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <iomanip>
#include <numeric>
 
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
priority_queue<P> A[600000];
priority_queue<P> B[600000];
priority_queue<P> C[600000];
priority_queue<P> D[600000];
priority_queue<P> E[600000];
priority_queue<P> F[600000];


ll N,K,M;

int main() {
    ll m = 1000000000;
    cin >> N;
    int x,y;
    char u;
    for(int i=0;i<N;i++)
    {
        cin >> x >> y >> u;
    int w;
    if(u=='U'){
        w=0;
        A[x+y+200000].push(P(x,w));
        C[x-y+200000].push(P(x,w));
        E[x+200000].push(P(y,w));
    }else if(u=='R'){
        w=1;
        A[x+y+200000].push(P(x,w));
        D[x-y+200000].push(P(x,w));
        F[y+200000].push(P(x,w));
    }
    else if(u=='D'){
        w=2;
        B[x+y+200000].push(P(x,w));
        D[x-y+200000].push(P(x,w));
        E[x+200000].push(P(y,w));
    }else{
        w=3;
        B[x+y+200000].push(P(x,w));
        C[x-y+200000].push(P(x,w));
        F[y+200000].push(P(x,w));
    }
    }
    for(int i=0;i<600000;i++){
        if(A[i].size()>=2){
            P p = A[i].top();
            A[i].pop();
            ll l=A[i].size();
            for(int j=1;j<=l;j++){
                P q = A[i].top();
                A[i].pop();
                if(p.second<q.second){
                    if(m>(-q.first+p.first)*10){
                        m =(-q.first+p.first)*10;
                    }
                }
                p = q;
            }
        }
    }
    for(int i=0;i<600000;i++){
        if(B[i].size()>=2){
            P p = B[i].top();
            B[i].pop();
            ll l=B[i].size();
            for(int j=1;j<=l;j++){
                P q = B[i].top();
                B[i].pop();
                if(p.second>q.second){
                    if(m>(-q.first+p.first)*10){
                        m = (-q.first+p.first)*10;
                    }
                }
                p = q;
            }
        }
    }
    for(int i=0;i<600000;i++){
        if(C[i].size()>=2){
            P p = C[i].top();
            C[i].pop();
            ll l=C[i].size();
            for(int j=1;j<=l;j++){
                P q = C[i].top();
                C[i].pop();
                if(p.second>q.second){
                    if(m>(-q.first+p.first)*10){
                        m = (-q.first+p.first)*10;
                    }
                }
                p = q;
            }
        }
    }
    for(int i=0;i<600000;i++){
        if(D[i].size()>=2){
            P p = D[i].top();
            D[i].pop();
            ll l=D[i].size();
            for(int j=1;j<=l;j++){
                P q = D[i].top();
                D[i].pop();
                if(p.second>q.second){
                    if(m>(-q.first+p.first)*10){
                        m = (-q.first+p.first)*10;
                    }
                }
                p = q;
            }
        }
    }
    for(int i=0;i<600000;i++){
        if(E[i].size()>=2){
            P p = E[i].top();
            E[i].pop();
            ll l =E[i].size();
            for(int j=1;j<=l;j++){
                P q = E[i].top();
                E[i].pop();
                if(p.second>q.second){
                    if(m>(-q.first+p.first)*5){
                        m = (-q.first+p.first)*5;
                    }
                }
                p = q;
            }
            
        }
        
    }
    for(int i=0;i<600000;i++){
        if(F[i].size()>=2){
            P p = F[i].top();
            F[i].pop();
            ll l=F[i].size();
            for(int j=1;j<=l;j++){
                P q = F[i].top();
                F[i].pop();
                if(p.second>q.second){
                    if(m>(-q.first+p.first)*5){
                        m = (-q.first+p.first)*5;
                    }
                }
                p = q;
            }
        }
    }
    if(m==1000000000){
        cout << "SAFE" << endl;
    }else{
       cout << m << endl;
    }
    
}


Measured memory_bytes: 134500