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

#define rep(i, m, n) for(int(i) = (int)(m); i < (int)(n); ++i)
#define rep2(i, m, n) for(int(i) = (int)(n)-1; i >= (int)(m); --i)
#define REP(i, n) rep(i, 0, n)
#define REP2(i, n) rep2(i, 0, n)
#define all(hoge) (hoge).begin(), (hoge).end()
#define en '\n'
using ll = long long;
using ull = unsigned long long;
template <class T>
using vec = vector<T>;
template <class T>
using vvec = vector<vec<T>>;
typedef pair<ll, ll> P;
constexpr long long INF = 1LL << 60;
constexpr int INF_INT = 1 << 25;
constexpr long long MOD = (ll)1e9 + 7;
//constexpr long long MOD = 998244353LL;
using ld = long double;
static const ld pi = 3.141592653589793L;
typedef vector<ll> Array;
typedef vector<Array> Matrix;

template <class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

//グラフ関連
struct Edge {
    ll to, cap, rev;
    Edge(ll _to, ll _cap, ll _rev) {
        to = _to;
        cap = _cap;
        rev = _rev;
    }
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

void add_edge(Graph &G, ll from, ll to, ll cap, bool revFlag, ll revCap) {
    G[from].push_back(Edge(to, cap, (ll)G[to].size()));
    if(revFlag)
        G[to].push_back(Edge(from, revCap, (ll)G[from].size() - 1));
}

void solve() {
    ll n;
    cin >> n;

    vec<ll> x(n), y(n);
    vec<char> u(n);
    vvec<vec<int>> xx(4, vvec<int>(202020)), yy(4, vvec<int>(202020));
    vvec<vec<int>> xy(4, vvec<int>(402020)), yx(4, vvec<int>(402020));
    int base = 200000;
    auto conv = [&](char c) -> int {
        if(c == 'L')
            return 0;
        else if(c == 'R')
            return 1;
        else if(c == 'U')
            return 2;
        else
            return 3;
    };

    REP(i, n) {
        cin >> x[i] >> y[i] >> u[i];
        int id = conv(u[i]);
        xx[id][x[i]].push_back(y[i]);
        yy[id][y[i]].push_back(x[i]);
        xy[id][x[i] + y[i]].push_back(x[i]);
        yx[id][y[i] - x[i] + base].push_back(x[i]);
    }

    REP(i, 4) {
        REP(j, base + 10) {
            sort(all(xx[i][j]));
            sort(all(yy[i][j]));
        }
        REP(j, 2 * base + 10) {
            sort(all(xy[i][j]));
            sort(all(yx[i][j]));
        }
    }

    ll ans = INF;
    REP(i, n) {
        if(u[i] == 'R') {
            {
                auto it = upper_bound(all(yy[0][y[i]]), x[i]);
                if(it != yy[0][y[i]].end()) {
                    ll xxx = *it;
                    chmin(ans, (xxx - x[i]) * 5);
                }
            }
            {
                auto it = upper_bound(all(xy[2][x[i] + y[i]]), x[i]);
                if(it != xy[2][x[i] + y[i]].end()) {
                    ll xxx = *it;
                    chmin(ans, (xxx - x[i]) * 10);
                }
            }
            {
                auto it = upper_bound(all(yx[3][y[i] - x[i] + base]), x[i]);
                if(it != yx[3][y[i] - x[i] + base].end()) {
                    ll xxx = *it;
                    chmin(ans, (xxx - x[i]) * 10);
                }
            }
        } else if(u[i] == 'L') {
            {
                auto it = lower_bound(all(xy[3][x[i] + y[i]]), x[i]);
                if(it != xy[3][x[i] + y[i]].begin()) {
                    it--;
                    ll xxx = *it;
                    chmin(ans, (x[i] - xxx) * 10);
                }
            }
            {
                auto it = lower_bound(all(yx[2][y[i] - x[i] + base]), x[i]);
                if(it != yx[2][y[i] - x[i] + base].begin()) {
                    it--;
                    ll xxx = *it;
                    chmin(ans, (x[i] - xxx) * 10);
                }
            }
        } else if(u[i] == 'U') {
            {
                //D
                auto it = upper_bound(all(xx[3][x[i]]), y[i]);
                if(it != xx[3][x[i]].end()) {
                    ll yyy = *it;
                    chmin(ans, (yyy - y[i]) * 5);
                }
            }
        }
    }

    if(ans == INF)
        cout << "SAFE" << en;
    else
        cout << ans << en;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    /*
    ll t;
    cin >> t;
    while(t--)*/
    solve();

    return 0;
}


Measured memory_bytes: 142324