#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define dump(...)
#endif
#define endl "\n"
#define ll long long
#define int long long
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define INF 2147483647
#define LLINF 9223372036854775807LL
#define vi vector<int>
#define vvi vector<vector<int>>
#define pi pair<int, int>
#define ALL(a) (a).begin(), (a).end()
#define BIT(n) (1LL << (n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()) //sortしてからつかうこと
constexpr ll MOD = 1e9 + 7;
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
ll A, B, C, D, E, F, G, H, N, M, L, K, P, Q, R, W, X, Y, Z;
string S, T;
ll ans = 0;

template <typename T>
istream &operator>>(istream &is, vector<T> &vec) {
    for (T &x : vec)
        is >> x;
    return is;
}

signed main() {
    ans = INF;
    cin >> N;
    M = 200010;
    vector<vector<vector<int>>> pos0(4, vector<vector<int>>(M));
    vector<vector<vector<int>>> pos1(4, vector<vector<int>>(2 * M));
    vector<vector<vector<int>>> pos2(4, vector<vector<int>>(2 * M));
    map<int, int> dir;
    dir['U'] = 0;
    dir['R'] = 1;
    dir['D'] = 2;
    dir['L'] = 3;
    REP(j, 4) {
        REP(i, M) {
            pos0[j][i].push_back(3 * M);
            pos1[j][i].push_back(3 * M);
            pos2[j][i].push_back(3 * M);
        }
    }

    REP(i, N) {
        char u;
        cin >> X >> Y >> u;
        if ((dir[u] & 1) == 0)
            pos0[dir[u]][X].push_back(Y);
        else
            pos0[dir[u]][Y].push_back(X);
        pos1[dir[u]][X - Y + M].push_back(Y);
        pos2[dir[u]][X + Y].push_back(X);
    }

    REP(i, M) {
        for (int j = 0; j < 2; j++) {
            if (pos0[j][i].size() > 0 && pos0[j + 2][i].size() > 0) {
                sort(ALL(pos0[j][i]));
                sort(ALL(pos0[j + 2][i]));
                for (int n : pos0[j][i]) {
                    auto itr = upper_bound(ALL(pos0[j + 2][i]), n);
                    if (itr != pos0[j + 2][i].end())
                        ans = min((*itr - n), ans);
                }
            }
        }
    }
    REP(i, 2 * M) {
        if (pos1[0][i].size() > 0 && pos1[3][i].size() > 0) {
            sort(ALL(pos1[0][i]));
            sort(ALL(pos1[3][i]));
            for (int n : pos1[0][i]) {
                auto itr = upper_bound(ALL(pos1[3][i]), n);
                if (itr != pos1[3][i].end())
                    ans = min((*itr - n) * 2, ans);
            }
        }
        if (pos1[1][i].size() > 0 && pos1[2][i].size() > 0) {
            sort(ALL(pos1[1][i]));
            sort(ALL(pos1[2][i]));
            for (int n : pos1[1][i]) {
                auto itr = upper_bound(ALL(pos1[2][i]), n);
                if (itr != pos1[2][i].end())
                    ans = min((*itr - n) * 2, ans);
            }
        }

        if (pos2[0][i].size() > 0 && pos2[1][i].size() > 0) {
            sort(ALL(pos2[0][i]));
            sort(ALL(pos2[1][i]));
            for (int n : pos2[1][i]) {
                auto itr = upper_bound(ALL(pos2[0][i]), n);
                if (itr != pos2[0][i].end())
                    ans = min((*itr - n) * 2, ans);
            }
        }
        if (pos2[2][i].size() > 0 && pos2[3][i].size() > 0) {
            sort(ALL(pos2[2][i]));
            sort(ALL(pos2[3][i]));
            for (int n : pos2[2][i]) {
                auto itr = upper_bound(ALL(pos2[3][i]), n);
                if (itr != pos2[3][i].end())
                    ans = min((*itr - n) * 2, ans);
            }
        }
    }
    dump(ans);
    if (ans > 400000)
        cout << "SAFE" << endl;
    else
        cout << ans * 10 / 2 << endl;
}


Measured memory_bytes: 180944