#include <bits/stdc++.h>
using namespace std;
using pint = pair<int, int>;
using ll = long long;
using ull = unsigned long long;
using vll = vector<long long>;
using pll = pair<ll, ll>;
#define FOR(i, begin, end) \
    for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end) \
    for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define VREP(s, ite) for (auto ite = s.begin(); ite != s.end(); ++ite)
#define FI first
#define SE second
#define ALL(v) v.begin(), v.end()
#define endl "\n"
#define ciosup  \
    cin.tie(0); \
    ios::sync_with_stdio(false);
#define eb emplace_back
#define vint vector<int>
constexpr ll INF = 1e15 + 7LL;
constexpr ll MOD = 1e9 + 7LL;
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
    for (int i = 0; i < v.size(); ++i) {
        is >> v[i];
    }
    return is;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
    for (int i = 0; i < v.size() - 1; ++i) {
        os << v[i] << " ";
    }
    if (v.size() > 0) {
        os << v[v.size() - 1] << endl;
    }
    return os;
}

int main() {
    int n;
    cin>>n;
    vll x(n), y(n);
    vector<char> u(n);
    REP(i, n) { cin >> x[i] >> y[i] >> u[i]; }

    unordered_map<ll, set<ll>> uma, rma;
    REP(i, n) {
        if (u[i] == 'D'){
            uma[x[i]].insert(y[i]);
        }else if (u[i] == 'L') {
            rma[y[i]].insert(x[i]);
        }
    }
    ll ans = LLONG_MAX;
    REP(i, n) {
        if (u[i] == 'U'){
            if (uma.find(x[i]) != uma.end()){
                auto ite = uma[x[i]].lower_bound(y[i]);
                if (ite != uma[x[i]].end()){
                    ans = min(ans, abs(*ite - y[i])*5);
                }
            }
        } else if (u[i] == 'R') {
            if (rma.find(y[i]) != rma.end()) {
                auto ite = rma[y[i]].lower_bound(x[i]);
                if (ite != rma[y[i]].end()) {
                    ans = min(ans, abs(*ite - x[i]) * 5);
                }
            }
        }
    }

    unordered_map<ll, set<ll>> dm_m, dm_p, um_m, um_p;
    REP(i, n) {
        if (u[i] == 'D') {
            dm_m[x[i] - y[i]].insert(y[i]);
            dm_p[x[i] + y[i]].insert(y[i]);
        } else if (u[i] == 'U') {
            um_m[x[i]-y[i]].insert(y[i]);
            um_p[x[i] + y[i]].insert(y[i]);
        }
    }

    REP(i, n) {
        if (u[i] == 'L') {
            if (um_m.find(x[i] - y[i]) != um_m.end()) {
                auto ite = um_m[x[i] - y[i]].lower_bound(y[i]);
                if (ite != um_m[x[i] - y[i]].begin()) {
                    --ite;
                    ans = min(ans, abs(*ite - y[i]) * 10);
                }
            }
            if (dm_p.find(x[i] + y[i]) != dm_p.end()) {
                auto ite = dm_p[x[i] + y[i]].lower_bound(y[i]);
                if (ite != dm_p[x[i] + y[i]].end()) {
                    ans = min(ans, abs(*ite - y[i]) * 10);
                }
            }
        } else if (u[i] == 'R') {
            if (dm_m.find(x[i] - y[i]) != dm_m.end()) {
                auto ite = dm_m[x[i] - y[i]].lower_bound(y[i]);
                if (ite != dm_m[x[i] - y[i]].end()) {
                    ans = min(ans, abs(*ite - y[i]) * 10);
                }
            }
            if (um_p.find(x[i] + y[i]) != um_p.end()) {
                auto ite = um_p[x[i] + y[i]].lower_bound(y[i]);
                if (ite != um_p[x[i] + y[i]].begin()) {
                    --ite;
                    ans = min(ans, abs(*ite - y[i]) * 10);
                }
            }
        }
    }

    if (ans == LLONG_MAX){
        cout << "SAFE" << endl;
    }else{
        cout << ans << endl;
    }
}


Measured memory_bytes: 86584