#include <bits/stdc++.h>

#ifndef M_PI
#define M_PI 3.14159265358979
#endif
#define deg_to_rad(deg) (((deg) / 360) * 2 * M_PI)
#define rad_to_deg(rad) (((rad) / 2 / M_PI) * 360)

using namespace std;

typedef long long ll;
typedef vector<long long> vll;
typedef pair<long long, long long> pll;
typedef long double ld;

const ll INF = 1e15;
const ll MOD = 1e9 + 7;

int main() {
    ll i, j;
    ll N;
    cin >> N;
    vll X, Y;
    vector<char> U;
    ll lim = 200000;
    vector<vector<pll>> A(lim + 1, vector<pll>(0));
    vector<vector<pll>> B(lim + 1, vector<pll>(0));
    vector<vector<pll>> C(2 * lim + 1, vector<pll>(0));
    vector<vector<pll>> D(2 * lim + 1, vector<pll>(0));
    vector<vector<pll>> E(2 * lim + 1, vector<pll>(0));
    vector<vector<pll>> F(2 * lim + 1, vector<pll>(0));

    for(i = 0; i < N; i++) {
        ll x, y;
        char u;
        cin >> x >> y >> u;
        X.push_back(x);
        Y.push_back(y);
        U.push_back(u);

        if(u == 'U') {
            A[x].push_back(pll(y, 1));
            C[x - y + lim].push_back(pll(x, 1));
            E[x + y].push_back(pll(x, 0));
        }
        if(u == 'D') {
            A[x].push_back(pll(y, 0));
            D[x - y + lim].push_back(pll(x, 0));
            F[x + y].push_back(pll(x, 1));
        }
        if(u == 'R') {
            B[y].push_back(pll(x, 1));
            D[x - y + lim].push_back(pll(x, 1));
            E[x + y].push_back(pll(x, 1));
        }
        if(u == 'L') {
            B[y].push_back(pll(x, 0));
            C[x - y + lim].push_back(pll(x, 0));
            F[x + y].push_back(pll(x, 0));
        }
    }
    ll ans = INF;
    ll p, d;
    for(i = 0; i < lim + 1; i++) {
        sort(A[i].begin(), A[i].end());
        p = -1;
        d = INF;
        for(auto a : A[i]) {
            if(a.second == 1) {
                p = a.first;
            }
            if(a.second == 0 && p >= 0) {
                d = min(d, a.first - p);
            }
        }
        d *= 5;
        ans = min(ans, d);
        sort(B[i].begin(), B[i].end());
        p = -1;
        d = INF;
        for(auto a : B[i]) {
            if(a.second == 1) {
                p = a.first;
            }
            if(a.second == 0 && p >= 0) {
                d = min(d, a.first - p);
            }
        }
        d *= 5;
        ans = min(ans, d);
    }

    for(i = 0; i < 2 * lim + 1; i++) {
        sort(C[i].begin(), C[i].end());
        sort(D[i].begin(), D[i].end());
        sort(E[i].begin(), E[i].end());
        sort(F[i].begin(), F[i].end());
        p = -1;
        d = INF;
        for(auto a : C[i]) {
            if(a.second == 1) {
                p = a.first;
            }
            if(a.second == 0 && p >= 0) {
                d = min(d, a.first - p);
            }
        }
        d *= 10;
        ans = min(ans, d);

        p = -1;
        d = INF;
        for(auto a : D[i]) {
            if(a.second == 1) {
                p = a.first;
            }
            if(a.second == 0 && p >= 0) {
                d = min(d, a.first - p);
            }
        }
        d *= 10;
        ans = min(ans, d);

        p = -1;
        d = INF;
        for(auto a : E[i]) {
            if(a.second == 1) {
                p = a.first;
            }
            if(a.second == 0 && p >= 0) {
                d = min(d, a.first - p);
            }
        }
        d *= 10;
        ans = min(ans, d);

        p = -1;
        d = INF;
        for(auto a : F[i]) {
            if(a.second == 1) {
                p = a.first;
            }
            if(a.second == 0 && p >= 0) {
                d = min(d, a.first - p);
            }
        }
        d *= 10;
        ans = min(ans, d);
    }
    if(ans == INF || ans == INF / 2) {
        cout << "SAFE" << endl;
    } else {
        cout << ans << endl;
    }

    return 0;
}


Measured memory_bytes: 72144