#include<bits/stdc++.h>

using namespace std;
using ll = long long int;
using lc = complex<double>;

int main(void) {
    constexpr ll MOD = 1e9 + 7;
    constexpr double PI = acos(-1);
    cout << fixed << setprecision(32);
    cin.tie(0); ios::sync_with_stdio(false);

    ll n;
    cin >> n;
    vector<tuple<ll,ll,ll>> pls(n);
    for(auto &[x, y, u]: pls) {
        u = 0;
        string s;
        cin >> x >> y >> s;
        switch(s[0]) {
            case 'U': // 3
                u++;
            case 'R': // 2
                u++;
            case 'D': // 1
                u++;
            case 'L': // 0
                ;
        }
    }
    constexpr ll U = 3, R = 2, D = 1, L = 0;

    double res = 1e18;
    // →←
    {
        vector<tuple<ll,ll,ll>> v(n);
        for(ll i=0; i<n; i++) {
            auto &[x, y, u] = pls[i];
            v[i] = make_tuple(y, x, u);
        }
        sort(v.begin(), v.end());
        for(ll i=0; i<n; ) {
            ll yy = get<0>(v[i]);
            double p = -1e15;
            while(i < n) {
                auto [y, x, u] = v[i];
                if(y != yy) break;
                if(u == R) p = x;
                if(u == L) res = min(res, (x - p)/2);
                i++;
            }
        }
    }
    // ↓↑
    {
        vector<tuple<ll,ll,ll>> v(n);
        for(ll i=0; i<n; i++) {
            auto &[x, y, u] = pls[i];
            v[i] = make_tuple(x, y, u);
        }
        sort(v.begin(), v.end());
        for(ll i=0; i<n; ) {
            ll xx = get<0>(v[i]);
            double p = -1e15;
            while(i < n) {
                auto [x, y, u] = v[i];
                if(x != xx) break;
                if(u == U) p = y;
                if(u == D) res = min(res, (y - p)/2);
                i++;
            }
        }
    }
    // →↓
    {
        vector<tuple<ll,ll,ll>> v(n);
        for(ll i=0; i<n; i++) {
            auto &[x, y, u] = pls[i];
            v[i] = make_tuple(x-y, x, u);
        }
        sort(v.begin(), v.end());
        for(ll i=0; i<n; ) {
            ll tt = get<0>(v[i]);
            double p = -1e15;
            while(i < n) {
                auto [t, x, u] = v[i];
                if(t != tt) break;
                if(u == R) p = x;
                if(u == D) res = min(res, (x - p));
                i++;
            }
        }
    }
    // ↑←
    {
        vector<tuple<ll,ll,ll>> v(n);
        for(ll i=0; i<n; i++) {
            auto &[x, y, u] = pls[i];
            v[i] = make_tuple(x-y, x, u);
        }
        sort(v.begin(), v.end());
        for(ll i=0; i<n; ) {
            ll tt = get<0>(v[i]);
            double p = -1e15;
            while(i < n) {
                auto [t, x, u] = v[i];
                if(t != tt) break;
                if(u == U) p = x;
                if(u == L) res = min(res, (x - p));
                i++;
            }
        }
    }
    // →↑
    {
        vector<tuple<ll,ll,ll>> v(n);
        for(ll i=0; i<n; i++) {
            auto &[x, y, u] = pls[i];
            v[i] = make_tuple(x+y, x, u);
        }
        sort(v.begin(), v.end());
        for(ll i=0; i<n; ) {
            ll tt = get<0>(v[i]);
            double p = -1e15;
            while(i < n) {
                auto [t, x, u] = v[i];
                if(t != tt) break;
                if(u == R) p = x;
                if(u == U) res = min(res, (x - p));
                i++;
            }
        }
    }
    // ↓←
    {
        vector<tuple<ll,ll,ll>> v(n);
        for(ll i=0; i<n; i++) {
            auto &[x, y, u] = pls[i];
            v[i] = make_tuple(x+y, x, u);
        }
        sort(v.begin(), v.end());
        for(ll i=0; i<n; ) {
            ll tt = get<0>(v[i]);
            double p = -1e15;
            while(i < n) {
                auto [t, x, u] = v[i];
                if(t != tt) break;
                if(u == D) p = x;
                if(u == L) res = min(res, (x - p));
                i++;
            }
        }
    }
    if(res < 1e12) cout << (ll)(res*10) << endl;
    else cout << "SAFE" << endl;
}

Measured memory_bytes: 12640