#include "bits/stdc++.h"
using namespace std;
const auto start_time = std::chrono::high_resolution_clock::now();
void timecalculater(){
    #ifndef ONLINE_JUDGE
    auto end_time = std::chrono::high_resolution_clock::now();
    std::chrono::duration<double> diff = end_time-start_time;
    cerr<<"Time Taken : "<<diff.count()<<"\n";
    #endif
}//continue break
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void __print(int x) {cout << x;}
void __print(long x) {cout << x;}
void __print(long long x) {cout << x;}
void __print(unsigned x) {cout << x;}
void __print(unsigned long x) {cout << x;}
void __print(unsigned long long x) {cout << x;}
void __print(float x) {cout << x;}
void __print(double x) {cout << x;}
void __print(long double x) {cout << x;}
void __print(char x) {cout << '\'' << x << '\'';}
void __print(const char *x) {cout << '\"' << x << '\"';}
void __print(const string &x) {cout << '\"' << x << '\"';}
void __print(bool x) {cout << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cout << '{'; __print(x.first); cout << ','; __print(x.second); cout << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cout << '{'; for (auto &i: x) cout << (f++ ? "," : ""), __print(i); cout << "}";}
void _print() {cout << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cout << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define dbg(x...) cout << "[" << #x << "] = ["; _print(x)
#else
#define dbg(x...)
#endif
#define fastio  cin.sync_with_stdio(0); cin.tie(0);cin.exceptions(cin.failbit);
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define repA(i, a, n) for(int  i = a; i <= (n); ++i)
#define all(x) x.begin(), x.end()
#define sz(x) (int) (x).size()
#define fill(a,v) memset(a, v, sizeof(a))
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
#define endl '\n'
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<int> vi;
typedef vector<long long int> vll;
typedef vector<pii> vpii;
ll p=1e9+7;
const int N=4e5+5;
int off=2e5;
int main() {
    
    fastio;
    int T=1;
    // cin>>T;
    while(T--){
        int n;cin>>n;
        set<int> Mapu[N];
        set<int> Mapd[N+off];
        set<int> Mapu1[N];
        set<int> Mapd1[N+off];
        set<int> left[N];
        set<int> down[N];
        vpii l,r,u,d;
        int ans=2e9;
        rep(i,n){
            int x,y;
            cin>>x>>y;
            char c;cin>>c;
            if(c=='U'){
                u.pb({x,y});
                Mapu[x+y].insert(x);
                Mapu1[y-x+off].insert(x);
                
            }
            if(c=='D'){
                d.pb({x,y});
                Mapd[y-x+off].insert(x);
                Mapd1[x+y].insert(x);
                down[x].insert(y);
            }
            if(c=='L'){
                left[y].insert(x);
                l.pb({x,y});
            }
            if(c=='R'){
                r.pb({x,y});
            }
        }
        for(auto [x,y]: u){
            auto j=down[x].lower_bound(y);
            if(j!=down[x].end()){
                ans=min(ans,(*j-y)*5);
            }
        }
        for(auto [x,y]: r){
            auto j=left[y].lower_bound(x);
            if(j!=left[y].end()){
                ans=min(ans,(*j-x)*5);
            }
        }
        for(auto [x,y]:r){
            ll s=x+y;
            auto j=Mapu[s].lower_bound(x);
            if(j!=Mapu[s].end()){
                ans=min(ans,10*(*j-x));
            }
            ll d=y-x+off;
            j=Mapd[d].lower_bound(x);
            if(j!=Mapd[d].end()){
                ans=min(ans,10*(*j-x));
            }
        }
        for(auto [x,y]: l){
            ll s=x+y;
            auto j=Mapd1[s].upper_bound(x);
            if(j!=Mapd1[s].begin()){
                j--;
                ans=min(ans,10*(x-(*j)));
            }
            ll d=y-x+off;
            j=Mapu1[d].upper_bound(x);
            if(j!=Mapu1[d].begin()){
                j--;
                ans=min(ans,10*(x-(*j)));
            }
        }
        if(ans==(2000000000)){
            cout<<"SAFE";return 0;
        }
        cout<<ans;
    }   
    timecalculater();
    return 0;   
} 

Measured memory_bytes: 163792