#include <bits/stdc++.h>
using namespace std;
#define INF 1000000000

int calc(vector<vector<int>> &A, vector<vector<int>> &B,
         int mult, int comp_field, int diff_field) {
    auto sort_func = [&](vector<int> s, vector<int> t) {
        if (s[comp_field] < t[comp_field]) {
            return true;
        } else if (s[comp_field] > t[comp_field]) {
            return false;
        } else if (s[diff_field] < t[diff_field]) {
            return true;
        } else {
            return false;
        }
    };
    sort(A.begin(), A.end(), sort_func);
    sort(B.begin(), B.end(), sort_func);

    int a = 0;
    int b = 0;

    int min_time = INF;
    while (a < (int)A.size() and b < (int)B.size()) {
        if (A[a][comp_field] == B[b][comp_field]) {
            int asame = a + 1;
            while (asame < (int)A.size() && A[asame][comp_field] == A[a][comp_field]) {
                asame++;
            }
            int aend = asame - 1;

            int bsame = b + 1;
            while (bsame < (int)B.size() && B[bsame][comp_field] == B[b][comp_field]) {
                bsame++;
            }
            int bend = bsame - 1;

            int aa = a;
            int bb = b;
            while (aa <= aend && bb <= bend) {
                int diff = B[bb][diff_field] - A[aa][diff_field];
                if (diff > 0) {
                    min_time = min(min_time, diff * mult);
                }
                if (aa == aend) {
                    bb++;
                } else if (bb == bend) {
                    aa++;
                } else if (diff > 0) {
                    aa++;
                } else {
                    bb++;
                }
            }
            a = aend + 1;
            b = bend + 1;
        } else if (a == (int)A.size() - 1) {
            b++;
        } else if (b == (int)B.size() - 1) {
            a++;
        } else if (A[a][comp_field] > B[b][comp_field]) {
            b++;
        } else if (A[a][comp_field] < B[b][comp_field]) {
            a++;
        } else {
            throw exception();
        }
    }
    return min_time;
}
int main() {
    int N;
    cin >> N;
    vector<vector<int>> U;
    vector<vector<int>> R;
    vector<vector<int>> D;
    vector<vector<int>> L;
    for (int i = 0; i < N; i++) {
        int x, y;
        char direction;
        cin >> x;
        cin >> y;
        cin >> direction;
        if (direction == ' ') {
            cin >> direction;
        }
        vector<int> v{x, y, x + y, y - x};
        if (direction == 'U') {
            U.push_back(v);
        } else if (direction == 'R') {
            R.push_back(v);
        } else if (direction == 'D') {
            D.push_back(v);
        } else if (direction == 'L') {
            L.push_back(v);
        } else {
            throw exception();
        }
    }
    int X_FIELD = 0;
    int Y_FIELD = 1;
    int ADD_FIELD = 2;
    int SUB_FIELD = 3;

    int min_time = INF;
    min_time = min(min_time, calc(U, D, 5, X_FIELD, Y_FIELD));
    min_time = min(min_time, calc(R, L, 5, Y_FIELD, X_FIELD));
    min_time = min(min_time, calc(R, U, 10, ADD_FIELD, X_FIELD));
    min_time = min(min_time, calc(D, L, 10, ADD_FIELD, X_FIELD));
    min_time = min(min_time, calc(U, L, 10, SUB_FIELD, X_FIELD));
    min_time = min(min_time, calc(R, D, 10, SUB_FIELD, X_FIELD));

    if (min_time == INF) {
        cout << "SAFE" << endl;
    } else {
        cout << min_time << endl;
    }
}


Measured memory_bytes: 15848