#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;

typedef long long ll;

int main() {
    int N, X, Y;
    cin >> N;
    //vector<ll> X(N), Y(N);
    //vector<char> U(N);
    char U;
    map<int, vector<int>> UR, RU, UL, LU, DR, RD, DL, LD, UD, DU, RL, LR;
    rep(i, N) {
        cin >> X >> Y >> U;
        if (U == 'U') {
            UR[X + Y].push_back(X);
            UL[X - Y].push_back(X);
            UD[X].push_back(Y);
        }
        if (U == 'D') {
            DR[X - Y].push_back(X);
            DL[X + Y].push_back(X);
            DU[X].push_back(Y);
        }
        if (U == 'R') {
            RU[X + Y].push_back(X);
            RD[X - Y].push_back(X);
            RL[Y].push_back(X);
        }
        if (U == 'L') {
            LU[X - Y].push_back(X);
            LD[X + Y].push_back(X);
            LR[Y].push_back(X);
        }
    }
    int ans = 1e9;
    for (auto it : UR) {
        int i = it.first;
        sort(UR[i].begin(), UR[i].end());
        if (UR[i].size() > 0) {
            if (RU.find(i) != RU.end()) {
                sort(RU[i].begin(), RU[i].end());
                rep(j, UR[i].size()) {
                    int k = lower_bound(RU[i].begin(), RU[i].end(), UR[i][j]) - RU[i].begin();
                    if (k > 0) {
                        ans = min(ans, (UR[i][j] - RU[i][k - 1]) * 10);
                    }
                }
            }
        }
    }
    for (auto it : LU) {
        int i = it.first;
        sort(LU[i].begin(), LU[i].end());
        if (LU[i].size() > 0) {
            if (UL.find(i) != UL.end()) {
                sort(UL[i].begin(), UL[i].end());
                rep(j, LU[i].size()) {
                    int k = lower_bound(UL[i].begin(), UL[i].end(), LU[i][j]) - UL[i].begin();
                    if (k > 0) {
                        ans = min(ans, (LU[i][j] - UL[i][k - 1]) * 10);
                    }
                }
            }
        }
    }
    for (auto it : DR) {
        int i = it.first;
        sort(DR[i].begin(), DR[i].end());
        if (DR[i].size() > 0) {
            if (RD.find(i) != RD.end()) {
                sort(RD[i].begin(), RD[i].end());
                rep(j, DR[i].size()) {
                    int k = lower_bound(RD[i].begin(), RD[i].end(), DR[i][j]) - RD[i].begin();
                    if (k > 0) {
                        ans = min(ans, (DR[i][j] - RD[i][k - 1]) * 10);
                    }
                }
            }
        }
    }
    for (auto it : LD) {
        int i = it.first;
        sort(LD[i].begin(), LD[i].end());
        if (LD[i].size() > 0) {
            if (DL.find(i) != DL.end()) {
                sort(DL[i].begin(), DL[i].end());
                rep(j, LD[i].size()) {
                    int k = lower_bound(DL[i].begin(), DL[i].end(), LD[i][j]) - DL[i].begin();
                    if (k > 0) {
                        ans = min(ans, (LD[i][j] - DL[i][k - 1]) * 10);
                    }
                }
            }
        }
    }
    for (auto it : DU) {
        int i = it.first;
        sort(DU[i].begin(), DU[i].end());
        if (DU[i].size() > 0) {
            if (UD.find(i) != UD.end()) {
                sort(UD[i].begin(), UD[i].end());
                rep(j, DU[i].size()) {
                    int k = lower_bound(UD[i].begin(), UD[i].end(), DU[i][j]) - UD[i].begin();
                    if (k > 0) {
                        ans = min(ans, (DU[i][j] - UD[i][k - 1]) * 5);
                    }
                }
            }
        }
    }
    for (auto it : LR) {
        int i = it.first;
        sort(LR[i].begin(), LR[i].end());
        if (LR[i].size() > 0) {
            if (RL.find(i) != RL.end()) {
                sort(RL[i].begin(), RL[i].end());
                rep(j, LR[i].size()) {
                    int k = lower_bound(RL[i].begin(), RL[i].end(), LR[i][j]) - RL[i].begin();
                    if (k > 0) {
                        ans = min(ans, (LR[i][j] - RL[i][k - 1]) * 5);
                    }
                }
            }
        }
    }
    if (ans == 1e9) cout << "SAFE\n";
    else cout << ans << "\n";
}

Measured memory_bytes: 69160