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

#ifdef DEBUG
    #include "debug.h"
    #define dump(...)                                                              \
        DUMPOUT << "  " << string(#__VA_ARGS__) << ": "                            \
                << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]"        \
                << endl                                                            \
                << "    ",                                                         \
            dump_func(__VA_ARGS__)
#else
    #define dump(...)
#endif


// http://beet-aizu.hatenablog.com/entry/2018/04/08/145516
template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
  return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

template<typename T,typename V>
typename enable_if<is_class<T>::value==0>::type
fill_v(T &t,const V &v){t=v;}

template<typename T,typename V>
typename enable_if<is_class<T>::value!=0>::type
fill_v(T &t,const V &v){
  for(auto &e:t) fill_v(e,v);
}

//////////////////

#define int long long
 
#define rep(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)

#define YES puts("YES")
#define Yes puts("Yes")
#define NO  puts("NO")
#define No  puts("No")
#define ALL(v) (v).begin(), (v).end()
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }

//*
#define mod 1000000007 /*/
#define mod 998244353 //*/

typedef pair<int, int> P;
#define INF (1LL<<60)




void solve(){
    int N;
    cin >> N;
    struct S{
        int x, y;
        char c;
    };
    vector<S> s(N);
    rep(i,N) cin >> s[i].x >> s[i].y >> s[i].c;

    int ans = INF;
    
    // Right Left
    {
        map<int,vector<int>> right, left;
        set<int> y_coord;

        rep(i,N){
            if(s[i].c == 'R'){
                right[s[i].y].push_back(s[i].x);
                y_coord.insert(s[i].y);
            }
            if(s[i].c == 'L'){
                left[s[i].y].push_back(s[i].x);
                y_coord.insert(s[i].y);
            }
        }
        for(auto y :y_coord){
            auto r = right[y];
            auto l = left[y];
            sort(ALL(r));
            sort(ALL(l));
            for(auto x : r){
                auto i = lower_bound(ALL(l), x);
                if(i!=l.end()){
                    chmin(ans, ((*i)-x)*5);
                }
            }
        }
    }
    
    // UP DOWN
    {
        map<int,vector<int>> up, down;
        set<int> x_coord;

        rep(i,N){
            if(s[i].c == 'U'){
                up[s[i].x].push_back(s[i].y);
                x_coord.insert(s[i].x);
            }
            if(s[i].c == 'D'){
                down[s[i].x].push_back(s[i].y);
                x_coord.insert(s[i].x);
            }
        }
        for(auto x :x_coord){
            auto u = up[x];
            auto d = down[x];
            sort(ALL(u));
            sort(ALL(d));
            for(auto y : u){
                auto i = lower_bound(ALL(d), y);
                if(i!=d.end()){
                    chmin(ans, ((*i)-y)*5);
                }
            }
        }

    }
    
    // 右下がりの４５度 Right Up
    {
        map<int,vector<int>> right, up;
        set<int> coord;

        rep(i,N){
            if(s[i].c == 'R'){
                right[s[i].x+s[i].y].push_back(s[i].x);
                coord.insert(s[i].x+s[i].y);
            }
            if(s[i].c == 'U'){
                up[s[i].x+s[i].y].push_back(s[i].x);
                coord.insert(s[i].x+s[i].y);
            }
        }
        for(auto t :coord){
            auto r = right[t];
            auto u = up[t];
            sort(ALL(r));
            sort(ALL(u));
            for(auto x : r){
                auto i = lower_bound(ALL(u), x);
                if(i!=u.end()){
                    chmin(ans, ((*i)-x)*10);
                }
            }
        }
    }
    // 右下がりの４５度 Down Left
    {
        map<int,vector<int>> down, left;
        set<int> coord;

        rep(i,N){
            if(s[i].c == 'D'){
                down[s[i].x+s[i].y].push_back(s[i].x);
                coord.insert(s[i].x+s[i].y);
            }
            if(s[i].c == 'L'){
                left[s[i].x+s[i].y].push_back(s[i].x);
                coord.insert(s[i].x+s[i].y);
            }
        }
        for(auto t :coord){
            auto d = down[t];
            auto l = left[t];
            sort(ALL(d));
            sort(ALL(l));
            for(auto x : d){
                auto i = lower_bound(ALL(l), x);
                if(i!=l.end()){
                    chmin(ans, ((*i)-x)*10);
                }
            }
        }
    }
    // 右上がりの４５度 Up Left
    {
        map<int,vector<int>> up, left;
        set<int> coord;

        rep(i,N){
            if(s[i].c == 'U'){
                up[s[i].x-s[i].y].push_back(s[i].x);
                coord.insert(s[i].x-s[i].y);
            }
            if(s[i].c == 'L'){
                left[s[i].x-s[i].y].push_back(s[i].x);
                coord.insert(s[i].x-s[i].y);
            }
        }
        for(auto t :coord){
            auto u = up[t];
            auto l = left[t];
            sort(ALL(u));
            sort(ALL(l));

            for(auto x : u){
                auto i = lower_bound(ALL(l), x);
                if(i!=l.end()){
                    chmin(ans, ((*i)-x)*10);
                }
            }
        }
    }
    // 右上がりの４５度 Right Down
    {
        map<int,vector<int>> right, down;
        set<int> coord;

        rep(i,N){
            if(s[i].c == 'R'){
                right[s[i].x-s[i].y].push_back(s[i].x);
                coord.insert(s[i].x-s[i].y);
            }
            if(s[i].c == 'D'){
                down[s[i].x-s[i].y].push_back(s[i].x);
                coord.insert(s[i].x-s[i].y);
            }
        }
        for(auto t :coord){
            
            auto r = right[t];
            auto d = down[t];
            sort(ALL(r));
            sort(ALL(d));
            for(auto x : r){
                auto i = lower_bound(ALL(d), x);
                if(i!=d.end()){
                    chmin(ans, ((*i)-x)*10);
                }
            }
        }
    }
    if(ans==INF) cout << "SAFE" << endl;
    else
    cout << ans << endl;
}


signed main(){
    cout << fixed << setprecision(18);
    cerr << fixed << setprecision(18);
    solve();
}


Measured memory_bytes: 53988