/*
** Vishal Raj Roy
** Indian Institute of Technology Kharagpur
*/
 
#include "bits/stdc++.h"
#include "ext/pb_ds/assoc_container.hpp"
#include "ext/pb_ds/tree_policy.hpp"
 
using namespace std;
using namespace __gnu_pbds;
 
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
 
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define xx first
#define yy second
#define sz(x) ((int)x.size())
#define nl cout << "\n";
#define li long long int
#define u64 uint64_t
#define u128 __uint128_t
#define db double
#define ld long double
#define fo(i, l, r) for (int i = l - (l > r); i != r - (l > r); i += 1 - 2 * (l > r))
#define se(X, C) for (auto& X : C)
#define ima INT_MAX
#define imi INT_MIN
#define lma LLONG_MAX
#define lmi LLONG_MIN
#define mem(X, A) memset(X, A, sizeof(X))    
#define fast ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define name(X) (#X)
#define re(...)             __VA_ARGS__; tk(__VA_ARGS__)
#define rl(name, ...)  name(__VA_ARGS__); cin >> name
#define tl(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); tell(_it, args); }
 
template <class ...As> auto &tk(As &...as) { return (cin >> ... >> as); } template <class T> auto &operator>>(istream &is, vector<T> &V) { for (auto &x : V) is >> x; return is; } template <class L, class R> auto &operator>>(istream& is, pair<L, R>& P) { return is >> P.first >> P.second; } template <class T> struct custom_hash { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } T operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM);} T operator()(tuple<uint64_t,uint64_t> x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(get<0>(x) + FIXED_RANDOM)^(splitmix64(get<1>(x) + FIXED_RANDOM) >> 1); } }; void tell(istream_iterator<string> it) {} template<typename T, typename... Args> void tell(istream_iterator<string> it, T a, Args... args) { cout << *it << " = " << a << "\n"; tell(++it, args...);} template <class L, class R> ostream & operator<<(ostream &os, pair<L,R> P) { return os << "(" << P.xx << "," << P.yy << ")"; } template<class L, class R> ostream &operator<<(ostream &os, tuple<L,R> P) {return os << "(" << get<0>(P) << "," << get<1>(P) <<")"; } template<class L, class R, class S> ostream &operator<<(ostream &os, tuple<L,R,S> P) { return os << "(" << get<0>(P) << "," << get<1>(P) << ","<< get<2>(P) <<")"; } template<class T, class A> ostream &operator<<(ostream& os, vector<T,A> V) {os /*<< name(V)<< " is "*/ << "\n"; fo(i,0,sz(V)) os <<"  "<< name(V) << "["<<i<<"] = "<< V[i] << "\n"; return os; /*<<"\n";*/ } template<class T, class H, class P, class A> ostream &operator<<(ostream& os, unordered_set<T,H,P,A> S) { os /*name(S) << " is"*/<<" ["; int i = 0; for(auto ELE : S) {os << ELE; if(i < sz(S)-1 ) os<<", "; i++; } return os <<  "]"; /*<<"\n"; */ } template<class S, class T, class H, class P, class A> ostream &operator<<(ostream& os, unordered_map<S,T,H,P,A> M) { os /*name(S) << " is"*/<<"\n"; for( auto ELE : M ) os <<"  "<< name(M) << "["<<ELE.xx<<"] = "<< ELE.yy << "\n"; return os; /*<<"\n";*/ } template<class T, class C, class A> ostream &operator<<(ostream& os, multiset<T,C,A> S) { os /*name(S) << " is"*/<<"["; int i = 0; for(auto ELE : S) {os << ELE; if(i < sz(S)-1 ) os<<", "; i++; } return os <<  "]"; /*<<"\n"; */ } template<class T, class R> ostream &operator<<(ostream& os, gp_hash_table<T,null_type,custom_hash<R>> S) { os /*name(S) << " is"*/<<" ["; int i = 0; for(auto ELE : S) {os << ELE; if(i < sz(S)-1 ) os<<", "; i++; } return os <<  "]"; /*<<"\n"; */ } template<class T, class Z, class R> ostream &operator<<(ostream& os, gp_hash_table<T,Z,custom_hash<R>> M) { os /*name(S) << " is"*/<<"\n"; for( auto ELE : M ) os <<"  "<< name(M) << "["<<ELE.xx<<"] = "<< ELE.yy << "\n"; return os; /*<<"\n";*/ } template<class T, class R> ostream &operator<<(ostream& os, cc_hash_table<T,null_type,custom_hash<R>> S) { os /*name(S) << " is"*/<<" ["; int i = 0; for(auto ELE : S) {os << ELE; if(i < sz(S)-1 ) os<<", "; i++; } return os <<  "]"; /*<<"\n"; */ } template<class T, class Z, class R> ostream &operator<<(ostream& os, cc_hash_table<T,Z,custom_hash<R>> M) { os /*name(S) << " is"*/<<"\n"; for( auto ELE : M ) os <<"  "<< name(M) << "["<<ELE.xx<<"] = "<< ELE.yy << "\n"; return os; /*<<"\n";*/ } using st = string; typedef pair<int, int> ii; typedef pair<int, ii> iii; typedef vector<int> vi; typedef vector<ii> vii; typedef vector<vi> vvi; typedef pair<li, li> ll; typedef vector<li> vl; typedef vector<ll> vll; typedef vector<vl> vvl; template <class S> using ve = vector<S>; template <class S, class T = null_type, class R = size_t> using gp = gp_hash_table<S, T, custom_hash<R> >; template <class S, class T = null_type, class R = size_t> using cc = cc_hash_table<S, T, custom_hash<R> >; template <class S, class T, class R = size_t> using um = unordered_map<S, T, custom_hash<R> >; template <class S, class R = size_t> using us = unordered_set<S, custom_hash<R> >; /* Keywords --> break continue order_of_key find_by_order goto here true bitset priority_queue deque stack queue insert top push pop front __builtin_popcount __builtin_popcountll back pop_back push_front pop_front greater less erase find count end begin emplace_back merge iota true false set optional empty gcd lcm merge lambda function binary_search for_each rbegin rend constexpr assign clear */
 #define done {cout<<"SAFE\n"; return;}
 int LB( auto & con, int val ) {
     return lb(all(con),val)-con.begin();
 }
 int UB( auto & con, int val ) {
     return ub(all(con),val)-con.begin();
 }
 void upd( int & a, int b ) { a = min(a,b); }
void solve() {
    int re(n);
    if( n == 1 ) done
    gp<int,vi> g[2][2]; // 0 = right, 1 = left
    gp<int,vi> h[2]; // 0 = down and 1 = left
    gp<char,vii> f;
    fo(i,0,n) {
        int re(x,y); x*= 10, y*=10;
        char re(ch);
        f[ch].pb({x,y});
        if( ch == 'D' ) h[0][x].pb(y);
        if( ch == 'L' ) {
            h[1][y].pb(x);
            g[0][1][x-y].pb(x);
            g[1][1][x+y].pb(x);
        }
        if( ch == 'R' ) {
            g[0][0][x+y].pb(x);
            g[1][0][x-y].pb(x);
        }
    }
    fo(i,0,2) se(e,h[i]) sort(all(e.yy));
    fo(i,0,2) fo(j,0,2) se(e,g[i][j]) sort(all(e.yy));
    int ans = 1e9;
    se(e,f['U']) {
        auto [x,y] = e;
        int s = h[0][x].size();
        int pos = LB(h[0][x],y);
        if( pos < s ) upd(ans,(h[0][x][pos]-y)/2);
    }
    se(e,f['R']) {
        auto [x,y] = e;
        int s = h[1][y].size();
        int pos = LB(h[1][y],x);
        if(  pos < s ) upd(ans,(h[1][y][pos]-x)/2);
    }
    se(e,f['U']) {
        auto [x,y] = e; 
        {
            int s = g[0][1][x-y].size();
            int pos = LB(g[0][1][x-y],x);
            if( pos < s ) upd(ans,(g[0][1][x-y][pos]-x)); 
        } 
        {
            int s = g[0][0][x+y].size();
            int pos = LB(g[0][0][x+y],x);
            pos--;
            if( pos < s and pos >= 0 ) upd(ans,(x-g[0][0][x+y][pos])); 
        }
    }
    se(e,f['D']) {
        auto [x,y] = e; 
        {
            int s = g[1][1][x+y].size();
            int pos = LB(g[1][1][x+y],x);
            if( pos < s ) upd(ans,(g[1][1][x+y][pos]-x)); 
        } 
        {
            int s = g[1][0][x-y].size();
            int pos = LB(g[1][0][x-y],x);
            pos--;
            if( pos < s and pos >= 0 ) upd(ans,(x-g[1][0][x-y][pos])); 
        }
    }
    if( ans == 1e9 ) done
    else cout<<ans<<"\n";
}
 
int main() {
    fast;
    solve();
    return 0;
}


Measured memory_bytes: 102120