#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <numeric>
#include <utility>
#include <tuple>

#define REP(i, a, b) for (int i = int(a); i < int(b); i++)
using namespace std;
using ll = long long int;
using P = pair<ll, ll>;

// clang-format off
#ifdef _DEBUG_
#define dump(...) do{ cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; PPPPP(__VA_ARGS__); cerr << endl; } while(false)
template<typename T> void PPPPP(T t) { cerr << t; }
template<typename T, typename... S> void PPPPP(T t, S... s) { cerr << t << ", "; PPPPP(s...); }
#else
#define dump(...) do{ } while(false)
#endif
template<typename T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); }
template<typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); }
template<typename T> bool chmin(T &a, T b) { if (a > b) {a = b; return true; } return false; }
template<typename T> bool chmax(T &a, T b) { if (a < b) {a = b; return true; } return false; }
template<typename T> void print(T a) { cout << a << endl; }
template<typename T, typename... Ts> void print(T a, Ts... ts) { cout << a << ' '; print(ts...); }
template<typename T> istream &operator,(istream &in, T &t) { return in >> t; }
// clang-format on

#include <map>

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n;
    cin, n;
    vector<int> x(n), y(n);
    vector<char> d(n);
    const int MAX = 200020;
    vector<vector<int>> U(MAX), D(MAX), L(MAX), R(MAX);
    map<ll, vector<int>> U1, U2, D1, D2, L1, L2, R1, R2;

    REP(i, 0, n) {
        cin, x[i], y[i], d[i];
        dump(x[i] + y[i]);
        dump(-x[i] + y[i]);
        if (d[i] == 'U') {
            U[x[i]].push_back(y[i]);
            U1[x[i] + y[i]].push_back(x[i]);
            U2[-x[i] + y[i]].push_back(x[i]);
        }
        if (d[i] == 'D') {
            D[x[i]].push_back(y[i]);
            D1[x[i] + y[i]].push_back(x[i]);
            D2[-x[i] + y[i]].push_back(x[i]);
        }
        if (d[i] == 'L') {
            L[y[i]].push_back(x[i]);
            L1[x[i] + y[i]].push_back(x[i]);
            L2[-x[i] + y[i]].push_back(x[i]);
        }
        if (d[i] == 'R') {
            R[y[i]].push_back(x[i]);
            R1[x[i] + y[i]].push_back(x[i]);
            R2[-x[i] + y[i]].push_back(x[i]);
        }
    }
    REP(i, 0, MAX) {
        sort(L[i].begin(), L[i].end());
        sort(R[i].begin(), R[i].end());
        sort(U[i].begin(), U[i].end());
        sort(D[i].begin(), D[i].end());
    }
    for (auto &z : U1) {
        sort(z.second.begin(), z.second.end());
    }
    for (auto &z : U2) {
        sort(z.second.begin(), z.second.end());
    }
    for (auto &z : D1) {
        sort(z.second.begin(), z.second.end());
    }
    for (auto &z : D2) {
        sort(z.second.begin(), z.second.end());
    }
    for (auto &z : L1) {
        sort(z.second.begin(), z.second.end());
    }
    for (auto &z : L2) {
        sort(z.second.begin(), z.second.end());
    }
    for (auto &z : R1) {
        sort(z.second.begin(), z.second.end());
    }
    for (auto &z : R2) {
        sort(z.second.begin(), z.second.end());
    }

    const ll inf = 1LL << 60;
    ll ans = inf;

    auto calc = [&](vector<vector<int>> &A, vector<vector<int>> &B) {
        REP(i, 0, MAX) {
            if (A[i].size() && B[i].size()) {
                int dd = 0;
                REP(uu, 0, A[i].size()) {
                    while (dd < B[i].size() && A[i][uu] >= B[i][dd]) {
                        dd++;
                    }
                    dump(A[i][uu], B[i][dd]);
                    if (dd < B[i].size()) {
                        chmin(ans, (B[i][dd] - A[i][uu]) * 5LL);
                    }
                }
            }
        }
    };

    auto calc2 = [&](map<ll, vector<int>> &A, map<ll, vector<int>> &B) {
        for (auto &z : A) {
            if (B[z.first].size() != 0) {
                auto &a = A[z.first];
                auto &b = B[z.first];
                if (a.size() && b.size()) {
                    int dd = 0;
                    REP(uu, 0, a.size()) {
                        while (dd < b.size() && a[uu] >= b[dd]) {
                            dd++;
                        }
                        dump(a[uu], b[dd]);
                        if (dd < b.size()) {
                            chmin(ans, (b[dd] - a[uu]) * 10LL);
                        }
                    }
                }
            }
        }
    };

    calc(U, D);
    calc(R, L);
    calc2(U2, L2);
    calc2(R1, U1);
    calc2(D1, L1);
    calc2(R2, D2);

    if (ans == inf) {
        print("SAFE");
    } else {
        print(ans);
    }

    return 0;
}


Measured memory_bytes: 88736