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

using ll = long long;
using pii = pair<int, int>;
template <class T>
using V = vector<T>;
template <class T>
using VV = V<V<T>>;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define per(i, b) per2(i, 0, b)
#define per2(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define ALL(c) (c).begin(), (c).end()

constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }

template <class T, class U>
void chmin(T& t, const U& u) {
    if (t > u) t = u;
}
template <class T, class U>
void chmax(T& t, const U& u) {
    if (t < u) t = u;
}

template <class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
    os << "(" << p.first << "," << p.second << ")";
    return os;
}

template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    os << "{";
    rep(i, v.size()) {
        if (i) os << ",";
        os << v[i];
    }
    os << "}";
    return os;
}

#ifdef LOCAL
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
    cerr << " " << H;
    debug_out(T...);
}
#define debug(...) \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif

const ll INF = TEN(9);
const int maxn = 200010;
const int B = 200000;

V<pii> vx[maxn], vy[maxn];
V<pair<pii, int>> vxyp[maxn * 2], vxyn[maxn * 2];

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int N;
    cin >> N;
    V<pair<pii, int>> vec;

    rep(i, N) {
        int X, Y;
        char U;
        cin >> X >> Y >> U;
        int dir = 0;
        if (U == 'U')
            dir = 3;
        else if (U == 'D')
            dir = 2;
        else if (U == 'R')
            dir = 1;
        vx[X].eb(Y, dir);
        vy[Y].eb(X, dir);
        vxyp[X + Y].eb(mp(X, Y), dir);
        vxyn[X - Y + B].eb(mp(X, Y), dir);
    }

    ll ans = INF;

    rep(x, maxn) {
        sort(ALL(vx[x]));
        ll mx = -INF;
        rep(i, vx[x].size()) {
            if (vx[x][i].se == 2) {
                chmin(ans, (vx[x][i].fi - mx) * 5);
            }
            if (vx[x][i].se == 3) {
                chmax(mx, vx[x][i].fi);
            }
        }
    }

    rep(y, maxn) {
        sort(ALL(vy[y]));
        ll mx = -INF;
        rep(i, vy[y].size()) {
            if (vy[y][i].se == 0) {
                chmin(ans, (vy[y][i].fi - mx) * 5);
            }
            if (vy[y][i].se == 1) {
                chmax(mx, vy[y][i].fi);
            }
        }
    }

    rep(a, maxn * 2) {
        sort(ALL(vxyp[a]));
        ll m1 = -INF, m2 = -INF;
        rep(i, vxyp[a].size()) {
            auto pt = vxyp[a][i];
            int x, y;
            tie(x, y) = pt.fi;
            int d = pt.se;
            if (d == 3) {
                chmin(ans, abs(x - m1) * 10);
            } else if (d == 0) {
                chmin(ans, abs(x - m2) * 10);
            } else if (d == 2) {
                chmax(m2, x);
            } else if (d == 1) {
                chmax(m1, x);
            }
        }
    }

    rep(a, maxn * 2) {
        sort(ALL(vxyn[a]));
        ll m1 = -INF, m2 = -INF;
        rep(i, vxyn[a].size()) {
            auto pt = vxyn[a][i];
            int x, y;
            tie(x, y) = pt.fi;
            int d = pt.se;
            if (d == 2) {
                chmin(ans, abs(x - m1) * 10);
            } else if (d == 0) {
                chmin(ans, abs(x - m2) * 10);
            } else if (d == 3) {
                chmax(m2, x);
            } else if (d == 1) {
                chmax(m1, x);
            }
        }
    }

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

    return 0;
}

Measured memory_bytes: 52084