#include <bits/stdc++.h>
using namespace std;
using Pair = pair<int, int>;

const int INF = 1e7;

enum {
    U, R, D, L,
};

int solve(map<int, vector<Pair>> &mp) {
    int res = INF;
    for(auto &[x, planes] : mp) {
        for(int i = 0; i < planes.size() - 1; ++i) {
            auto[y1, d1] = planes[i];
            auto[y2, d2] = planes[i + 1];
            if(d1 == U && d2 != U) res = min(res, y2 - y1);
        }
    }
    return res;
}

int main() {
    int N; cin >> N;
    vector<int> X(N), Y(N), dir(N);
    for(int i = 0; i < N; ++i) {
        char u;
        cin >> X[i] >> Y[i] >> u;
        if(u == 'U') dir[i] = U;
        if(u == 'R') dir[i] = R;
        if(u == 'D') dir[i] = D;
        if(u == 'L') dir[i] = L;
    }
    int ans = INF;
    for(int _ = 0; _ < 4; ++_) {
        map<int, vector<Pair>> mp;
        for(int i = 0; i < N; ++i) {
            if(dir[i] == R || dir[i] == L) continue;
            mp[X[i]].emplace_back(Y[i], dir[i]);
        }
        for(auto &[key, val] : mp) sort(val.begin(), val.end());
        ans = min(ans, 5 * solve(mp));

        map<int, vector<Pair>> mp2;
        for(int i = 0; i < N; ++i) {
            if(dir[i] == D || dir[i] == L) continue;
            mp2[X[i] + Y[i]].emplace_back(Y[i], dir[i]);
        }
        for(auto &[key, val] : mp2) sort(val.begin(), val.end());
        ans = min(ans, 10 * solve(mp2));

        for(int i = 0; i < N; ++i) {
            swap(X[i], Y[i]);
            Y[i] = -Y[i];
            dir[i] = (dir[i] + 1) % 4;
        }
    }
    if(ans == INF) cout << "SAFE" << '\n';
    else cout << ans << '\n';
    return 0;
}
