// Marcin Knapik
#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

using namespace std;

// #include <ext/pb_ds/assoc_container.hpp> // order_of_key
// #include <ext/pb_ds/tree_policy.hpp> // find by order
// using namespace __gnu_pbds; 
// #define ordered_set tree< int , null_type,less< int >, rb_tree_tag,tree_order_statistics_node_update>  

// const ll MAX_LL = 0xFFFFFFFFFFFFFFFF;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define losuj(a, b) uniform_int_distribution<ll>(a, b)(rng)
 
typedef long double             ld;
typedef long long               ll;
typedef pair<int, int>          ii;
typedef vector<int>             vi;
typedef vector<ll>              vll;
typedef vector<vll>             vvll;
typedef vector<ii>              vii;
typedef vector<vi>              vvi;
typedef vector<vii>             vvii;
#define pll pair<long long, long long>
#define vpll vector<pll> 
#define ull unsigned long long

#define sz(a)                   (int)(a).size()
#define pb                      push_back
#define all(c)                  (c).begin(), (c).end()
#define rall(c)                 (c).rbegin(), (c).rend()
#define FOR(i, a)               for (int i = 0; i < (a); i++)
#define f                       first
#define s                       second

template <class T> inline bool setmin(T &a, T b){if (a > b)return a = b, 1;return 0;}
template <class T> inline bool setmax(T &a, T b){if (a < b)return a = b, 1;return 0;}

template<class T> inline T fast(T a,T b,T mod) {ll res = 1; while(b){if(b&1) res = (res*a)%mod;a = (a*a)%mod;b >>= 1;}return res;}
template<class T> istream &operator>>(istream &os, vector<T> &container){for (auto &u : container)os >> u;return os;}
template<class T> ostream &operator<<(ostream &os, const vector<T> &container){for (auto &u : container)os << u << " ";return os;}
template<class T> inline T gcd(T a, T b) { while (b)swap(a %= b, b); return abs(a); }

const ll INF = 1e9 + 7;
const ll mod = 998244353;
const ll BIG_INF = 1e18 + 7;
const ll N = 1e6+7;
const ll T = 1<<20;
const ld inf = 1e18;

ll n, m, q, k, r;

int X[N];
int Y[N];
char U[N];

char nast(char x){
    if(x == 'U')
        return 'L';
    if(x == 'L')
        return 'D';
    if(x == 'D')
        return 'R';
    return 'U';
}

void rotate_left(){
    for(int i = 0; i < n; i++){
        int new_X = -Y[i];
        int new_Y = X[i];
        X[i] = new_X;
        Y[i] = new_Y;
        U[i] = nast(U[i]);
    }
}

int policz(){
    map<int, vector<int> > skos_gora, skos_dol, lewo;
    for(int i = 0; i < n; i++){
        if(U[i] == 'D')
            skos_gora[X[i] - Y[i]].pb(X[i]);
        if(U[i] == 'U')
            skos_dol[X[i] + Y[i]].pb(X[i]);
        if(U[i] == 'L')
            lewo[Y[i]].pb(X[i]);
    }

    for(auto & u : skos_dol)
        sort(all(u.s));
    for(auto & u : skos_gora)
        sort(all(u.s));
    for(auto & u : lewo)
        sort(all(u.s));

    int ret = INF;
    
    for(int i = 0; i < n; i++){
        if(U[i] == 'R'){
            {
                int L = Y[i];
                auto it = upper_bound(all(lewo[L]), X[i]); 
                if(it != lewo[L].end())
                    ret = min(ret, (*it - X[i]) * 5);
            }

            {
                int G = X[i] - Y[i];
                auto it = upper_bound(all(skos_gora[G]), X[i]); 
                if(it != skos_gora[G].end())
                    ret = min(ret, (*it - X[i]) * 10);
            }

            {
                int D = X[i] + Y[i];
                auto it = upper_bound(all(skos_dol[D]), X[i]); 
                if(it != skos_dol[D].end())
                    ret = min(ret, (*it - X[i]) * 10);
            }
        }
    }
    return ret;
}

void solve(){
    cin >> n;
    FOR(i, n)
        cin >> X[i] >> Y[i] >> U[i];

    int time = INF;

    FOR(i, 4){
        setmin(time, policz());
        rotate_left();
    }

    if(time == INF)
        cout << "SAFE\n";
    else
        cout << time << '\n';
}

signed main(){
  ios::sync_with_stdio(0);
  cin.tie(0);

  q = 1;
  // cin >> q;

  for(int i = 1; i <= q; i++)
    solve();
} 

Measured memory_bytes: 57940