#include <bits/stdc++.h>
#define all(vec) vec.begin(), vec.end()
//#define pb push_back
#define eb emplace_back
#define fi first
#define se second
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
template <class T>
using V = vector<T>;
template <class T>
inline void chmin(T &a, const T &b) { a = min(a, b); }
template <class T>
inline void chmax(T &a, const T &b) { a = max(a, b); }
template <class T>
inline bool kbit(const T &x, const int &k) { return ((x >> k) & 1LL); }
inline int popcount(const int &n) { return __builtin_popcount(n); }
inline ll popcountll(const ll &n) { return __builtin_popcountll(n); }
inline ll mask(const ll &k) { return (1LL << k) - 1LL; }
template <class T>
void zip(V<T> &v) {
    sort(all(v));
    v.erase(unique(all(v)), v.end());
}
template <class T>
int lwb(V<T> &v, const T &x) { return lower_bound(all(v), x) - v.begin(); }
template <class T>
int upb(V<T> &v, const T &x) { return upper_bound(all(v), x) - v.begin(); }
void dump() {
    cerr << '\n';
}
template <class Head, class... Tail>
void dump(Head &&head, Tail &&... tail) {
    cerr << head << (sizeof...(Tail) == 0 ? " " : ", ");
    dump(std::move(tail)...);
}
template <class T>
void print(const vector<T> &v) {
    for (int i = 0; i < v.size(); i++) cout << v[i] << (i + 1 == v.size() ? '\n' : ' ');
}
template <class T>
void read(vector<T> &v) {
    for (int i = 0; i < v.size(); i++) cin >> v[i];
}
constexpr char sp = ' ', newl = '\n';
constexpr int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
constexpr ll INF = (1LL << 30) - 1LL;
constexpr ll MOD = 1e9 + 7;
//////////////////////////////////////////////INSERT ABOVE HERE
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    V<int> x(n), y(n), u(n);
    int of = 200010;
    V<V<int>> pa(2 * of), pb(2 * of), px(2 * of), py(2 * of);
    for (int i = 0; i < n; i++) {
        cin >> x[i] >> y[i];
        char uu;
        cin >> uu;
        if (uu == 'U') {
            u[i] = 0;
        } else if (uu == 'R') {
            u[i] = 1;
        } else if (uu == 'D') {
            u[i] = 2;
        } else {
            u[i] = 3;
        }
        pa[x[i] + y[i]].eb(i);
        pb[x[i] - y[i] + of].eb(i);
        px[x[i]].eb(i);
        py[y[i]].eb(i);
    }
    int res = INF;
    for (int i = 0; i < 2 * of; i++) {
        sort(all(px[i]), [&](int ii, int jj) { return y[ii] < y[jj]; });
        int mu = -INF;
        for (int j = 0; j < px[i].size(); j++) {
            if (u[px[i][j]] == 0) {
                chmax(mu, y[px[i][j]]);
            } else if (u[px[i][j]] == 2) {
                if (mu != -INF) chmin(res, 5 * (y[px[i][j]] - mu));
            }
        }
    }
    for (int i = 0; i < 2 * of; i++) {
        sort(all(py[i]), [&](int ii, int jj) { return x[ii] < x[jj]; });
        int mr = -INF;
        for (int j = 0; j < py[i].size(); j++) {
            if (u[py[i][j]] == 1) {
                chmax(mr, x[py[i][j]]);
            } else if (u[py[i][j]] == 3) {
                if (mr != -INF) chmin(res, 5 * (x[py[i][j]] - mr));
            }
        }
    }
    // dump(res);
    for (int i = 0; i < 2 * of; i++) {
        sort(all(pa[i]), [&](int ii, int jj) { return x[ii] < x[jj]; });
        V<int> ma(4, -INF);
        int to[4] = {1, 0, 3, 2};
        for (auto &id : pa[i]) {
            if ((u[id] == 0 || u[id] == 3) && ma[to[u[id]]] != -INF) {
                chmin(res, 10 * (x[id] - ma[to[u[id]]]));
            }
            ma[u[id]] = x[id];
        }
    }
    //  dump(res);
    for (int i = 0; i < 2 * of; i++) {
        sort(all(pb[i]), [&](int ii, int jj) { return x[ii] < x[jj]; });
        V<int> ma(4, -INF);
        int to[4] = {3, 2, 1, 0};
        for (auto &id : pb[i]) {
            if ((u[id] == 2 || u[id] == 3) && ma[to[u[id]]] != -INF) {
                chmin(res, 10 * (x[id] - ma[to[u[id]]]));
            }
            ma[u[id]] = x[id];
        }
    }
    if (res == INF) {
        cout << "SAFE" << newl;
        return 0;
    }
    cout << res << newl;
}

Measured memory_bytes: 62292