#include <bits/stdc++.h>
using namespace std;
#define TRACE(x) 
#define WATCH(x) TRACE( cout << #x" = " << x << endl)
#define PRINT(x) TRACE(printf(x))
#define WATCHR(a, b) TRACE( for(auto c = a; c != b;) cout << *(c++) << " "; cout << endl)
#define WATCHC(V) TRACE({cout << #V" = "; WATCHR(V.begin(), V.end()); } )
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()
#define mp make_pair
#define st first
#define nd second

using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using ld = long double;
using pll = pair<ll, ll>;
using vll = vector<ll>;
using pll = pair<ll, ll>;

void buff() { ios::sync_with_stdio( false ); cin.tie( nullptr ); }

constexpr ll MOD = 1e9 + 7;

inline ll pow_mod( ll a, ll b, ll mod = MOD) {
    ll res = 1; a %= mod; assert(b >= 0);
    for(;b;b>>=1) {
        if(b&1) res = (res * a) % mod;
        a = (a * a) % mod;
    }
    return res;
}

template<typename T>
inline void remax(T& x, T y) { x = max(x, y); }

template<typename T>
inline void remin(T& x, T y) { x = min(x, y); }

constexpr int ms = 200013;
// End of template code!
set<int> diag_sum_right_by_x[2 * ms];
set<int> diag_diff_right_by_x[2 * ms];
set<int> diag_sum_left_by_x[2 * ms];
set<int> diag_diff_left_by_x[2 * ms];

vector<int> flights_up[ms];
vector<int> flights_down[ms];

vector<int> flights_left[ms];
vector<int> flights_right[ms];
    
int main() 
{
    buff();
    int n;
    cin >> n;
    string look = "URDL"; 
    vector< tuple<int, int, int> > vertical_flights; 
    
    // agora, precisamos detectar as colisoes ortogonais


    int collide = -1;
    constexpr int limit = 200000;
    for(int i = 0; i < n; ++i)
    {
        int x, y; char c;
        cin >> x >> y >> c;
        int pos = 0;
        for(int j = 0; j < 4; ++j) if(c == look[j]) pos = j;
        
        // Parte essencial para detectar colisões não-triviais
        if(pos == 0 || pos == 2) vertical_flights.emplace_back(x, y, pos); 
        else {
            if(pos == 1) {
                diag_sum_right_by_x[x + y].insert(x);
                diag_diff_right_by_x[(x - y) + limit].insert(x);
            }
            else {
                diag_sum_left_by_x[x + y].insert(x);
                diag_diff_left_by_x[(x - y) + limit].insert(x);
            }
        }


        if(pos == 0) {
            flights_up[x].push_back(y);
        }
        else if(pos == 1) {
            flights_right[y].push_back(x);
        }
        else if(pos == 2) {
            flights_down[x].push_back(y);
        }
        else flights_left[y].push_back(x);
    }
    
    // vamos detectar aqui as colisoes triviais
    for(int i = 0; i < ms - 10; ++i) {
        if(!flights_up[i].empty() && !flights_down[i].empty())
        {
            sort(all(flights_up[i]));
            sort(all(flights_down[i]));
        
            for(const auto& val : flights_up[i]) {
                auto it = upper_bound( all(flights_down[i]), val );
                if(it != flights_down[i].end()) {
                    int collision_time = 5 * (*it - val);
                    if(collide == -1) collide = collision_time;
                    else collide = min(collide, collision_time);
                    TRACE( cout << "collide 1 = " << collide << '\n'; ); 
                }
            }
        }
        if(!flights_right[i].empty() && !flights_left[i].empty())
        {
            sort(all(flights_left[i]));
            sort(all(flights_right[i]));
        
            for(const auto& val : flights_right[i]) {
                auto it = upper_bound( all(flights_left[i]), val );
                if(it != flights_left[i].end()) {
                    int collision_time = 5 * (*it - val);
                    if(collide == -1) collide = collision_time;
                    else collide = min(collide, collision_time);
                    TRACE( cout << "collide 2 = " << collide << '\n'; ); 
                }
            }
        }
    }

    for(const auto& v : vertical_flights)
    {
        int is_up = !(get<2>(v));
        int x = get<0>(v); int y = get<1>(v);
        if(is_up) {
            int sum = x + y; 
            int diff = x - y + limit;
            if(!diag_diff_left_by_x[diff].empty()) {
                auto cand = diag_diff_left_by_x[diff].upper_bound(x);
                if(cand != diag_diff_left_by_x[diff].end()) {
                    int dist = (*cand - x) * 10;
                    if(collide == -1) collide = dist;
                    else remin(collide, dist);
                
                    TRACE( cout << "collide 3 = " << collide << '\n'; ); 
                
                }
            }
            if(!diag_sum_right_by_x[sum].empty()) {
                auto cand = diag_sum_right_by_x[sum].lower_bound(x);
                if(cand != diag_sum_right_by_x[sum].begin()) {
                    --cand;
                    int dist = (x - *cand) * 10;
                    if(collide == -1) collide = dist;
                    else remin(collide, dist);
                
                    TRACE( cout << "collide 4 = " << collide << '\n'; ); 
                }
            }
        }
        else {
            int sum = x + y;
            int diff = x - y + limit;
            if(!diag_diff_right_by_x[diff].empty()) {
                auto cand = diag_diff_right_by_x[diff].lower_bound(x);
                if(cand != diag_diff_right_by_x[diff].begin()) {
                    --cand;
                    int dist = (x - *cand) * 10;
                    if(collide == -1) collide = dist;
                    else remin(collide, dist);

                    TRACE( cout << "collide 5 = " << collide << '\n'; ); 
                }
            }
            if(!diag_sum_left_by_x[sum].empty()) {
                auto cand = diag_sum_left_by_x[sum].upper_bound(x);
                if(cand != diag_sum_left_by_x[sum].end()) {
                    int dist = (*cand - x) * 10;
                    if(collide == -1) collide = dist;
                    else remin(collide, dist);

                    TRACE( cout << "collide 6 = " << collide << '\n'; ); 
                }
            }
        }
    }

    if(collide == -1) cout << "SAFE\n";
    else cout << collide << '\n';
    
    

    return 0;
}





Measured memory_bytes: 118652