#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
typedef pair<ll, ll> P;
using VP = vector<P>;
using VVP = vector<VP>;
using VI = vector<ll>;
using VVI = vector<VI>;
using VVVI = vector<VVI>;
const int inf = 1e9 + 7;
const ll INF = 1LL << 61;
const ll mod = 1e9 + 7;

template <class T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

template <class T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int i, j;
    int n;
    cin >> n;
    int m = 2 * 202020;
    VVI v1(m), v2(m), v3(m), v4(m);
    VVI ap(m), an(m);
    VVI bp(m), bn(m);
    VVI cp(m), cn(m);
    VVI dp(m), dn(m);
    m /= 2;
    for (i = 0; i < n; i++) {
        ll x, y;
        char u;
        cin >> x >> y >> u;
        // cin >> s[i] >> t[i] >> q[i];
        if (u == 'U') {
            v1[x].pb(y * 10);
            ap[x + y].pb(10 * (y - x));
            dp[m + y - x].pb(10 * (y + x));
        } else if (u == 'R') {
            v2[y].pb(x * 10);
            an[x + y].pb(10 * (y - x));
            bp[m + y - x].pb(10 * (y + x));
        } else if (u == 'D') {
            v3[x].pb(y * 10);
            bn[m + y - x].pb(10 * (y + x));
            cn[x + y].pb(10 * (y - x));
        } else {
            v4[y].pb(x * 10);
            cp[x + y].pb(10 * (y - x));
            dn[m + y - x].pb(10 * (y + x));
        }
    }
    for (i = 0; i < 2 * m; i++) sort(v1[i].begin(), v1[i].end());
    for (i = 0; i < 2 * m; i++) sort(v2[i].begin(), v2[i].end());
    for (i = 0; i < 2 * m; i++) sort(v3[i].begin(), v3[i].end());
    for (i = 0; i < 2 * m; i++) sort(v4[i].begin(), v4[i].end());
    for (i = 0; i < 2 * m; i++) sort(ap[i].begin(), ap[i].end());
    for (i = 0; i < 2 * m; i++) sort(an[i].begin(), an[i].end());
    for (i = 0; i < 2 * m; i++) sort(bp[i].begin(), bp[i].end());
    for (i = 0; i < 2 * m; i++) sort(bn[i].begin(), bn[i].end());
    for (i = 0; i < 2 * m; i++) sort(cp[i].begin(), cp[i].end());
    for (i = 0; i < 2 * m; i++) sort(cn[i].begin(), cn[i].end());
    for (i = 0; i < 2 * m; i++) sort(dp[i].begin(), dp[i].end());
    for (i = 0; i < 2 * m; i++) sort(dn[i].begin(), dn[i].end());
    ll ans = INF;
    for (i = 0; i < 2 * m; i++) {
        // cout << i << endl;
        for (ll u : v1[i]) {
            if (!v3[i].size()) continue;
            auto it = lower_bound(v3[i].begin(), v3[i].end(), u);
            if (it == v3[i].end()) continue;
            chmin(ans, *it - u);
        }
        for (ll u : v2[i]) {
            if (!v4[i].size()) continue;
            auto it = lower_bound(v4[i].begin(), v4[i].end(), u);
            if (it == v4[i].end()) continue;
            chmin(ans, *it - u);
        }
        for (ll u : ap[i]) {
            if (!an[i].size()) continue;
            auto it = lower_bound(an[i].begin(), an[i].end(), u);
            if (it == an[i].end()) continue;
            chmin(ans, *it - u);
        }
        for (ll u : bp[i]) {
            if (!bn[i].size()) continue;
            auto it = lower_bound(bn[i].begin(), bn[i].end(), u);
            if (it == bn[i].end()) continue;
            chmin(ans, *it - u);
        }
        for (ll u : cp[i]) {
            if (!cn[i].size()) continue;
            auto it = lower_bound(cn[i].begin(), cn[i].end(), u);
            if (it == cn[i].end()) continue;
            chmin(ans, *it - u);
        }
        for (ll u : dp[i]) {
            if (!dn[i].size()) continue;
            auto it = lower_bound(dn[i].begin(), dn[i].end(), u);
            if (it == dn[i].end()) continue;
            chmin(ans, *it - u);
        }
        // cout << i << endl;
    }
    if (ans == INF) {
        cout << "SAFE" << endl;
        return 0;
    }
    cout << ans / 2 << endl;
}

Measured memory_bytes: 135636