#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T> inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return 1;
    }
    return 0;
}
void debug() { cerr << "\n"; }
template <class T> void debug(const T &x) { cerr << x << "\n"; }
template <class T, class... Args> void debug(const T &x, const Args &... args) {
    cerr << x << " ";
    debug(args...);
}
template <class T> void debugVector(const vector<T> &v) {
    for(const T &x : v) {
        cerr << x << " ";
    }
    cerr << "\n";
}
using ll = long long;

#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
const double EPS = 1e-7;
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
const double PI = acos(-1);
constexpr int MOD = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

//-------------------------------------

using P = pair<int, int>;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    cin >> N;
    vector<int> X(N), Y(N);
    vector<char> dir(N);
    for(int i = 0; i < N; i++) {
        cin >> X[i] >> Y[i] >> dir[i];
    }
    int ans = INF;
    // U-D, L-Rを調べる
    {
        map<char, set<P>> mp;
        for(int i = 0; i < N; i++) {
            mp[dir[i]].insert(P(X[i], Y[i]));
        }
        for(auto p : mp['U']) {
            auto it = mp['D'].lower_bound(P(p.first, p.second + 1));
            if(it == mp['D'].end()) {
                continue;
            }
            // debug("U-D", it->first, it->second);
            if(it->first == p.first && it->second > p.second) {
                chmin(ans, (it->second - p.second) * 5);
            }
        }
        mp.clear();
        for(int i = 0; i < N; i++) {
            mp[dir[i]].insert(P(Y[i], X[i]));
        }
        for(auto p : mp['R']) {
            auto it = mp['L'].lower_bound(P(p.first, p.second + 1));
            if(it == mp['L'].end()) {
                continue;
            }
            // debug("L-R", it->first, it->second);
            if(it->first == p.first && it->second > p.second) {
                chmin(ans, (it->second - p.second) * 5);
            }
        }
    }
    // U-L,D-R
    {
        map<char, set<P>> mp;
        for(int i = 0; i < N; i++) {
            mp[dir[i]].insert(P(X[i] - Y[i], X[i] + Y[i]));
        }
        for(auto p : mp['U']) {
            auto it = mp['L'].lower_bound(P(p.first, p.second + 1));
            if(it == mp['L'].end()) {
                continue;
            }
            // debug("U-L", it->first, it->second);
            if(it->first == p.first && it->second > p.second) {
                chmin(ans, (it->second - p.second) * 5);
            }
        }
        for(auto p : mp['R']) {
            auto it = mp['D'].lower_bound(P(p.first, p.second + 1));
            if(it == mp['D'].end()) {
                continue;
            }
            // debug("R-D", it->first, it->second);
            if(it->first == p.first && it->second > p.second) {
                chmin(ans, (it->second - p.second) * 5);
            }
        }
    }
    // U-R
    {
        map<char, set<P>> mp;
        for(int i = 0; i < N; i++) {
            mp[dir[i]].insert(P(Y[i] + X[i], Y[i] - X[i]));
        }
        for(auto p : mp['U']) {
            auto it = mp['R'].lower_bound(P(p.first, p.second + 1));
            if(it == mp['R'].end()) {
                continue;
            }
            // debug("U-R", it->first, it->second);
            if(it->first == p.first && it->second > p.second) {
                chmin(ans, (it->second - p.second) * 5);
            }
        }
        for(auto p : mp['L']) {
            auto it = mp['D'].lower_bound(P(p.first, p.second + 1));
            if(it == mp['D'].end()) {
                continue;
            }
            // debug("L-D", it->first, it->second);
            if(it->first == p.first && it->second > p.second) {
                chmin(ans, (it->second - p.second) * 5);
            }
        }
    }
    if(ans >= INF) {
        cout << "SAFE\n";
    } else {
        cout << ans << endl;
    }
}

Measured memory_bytes: 14368