#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define rep(i, n) FOR(i, 0, n)
#define rep1(i, n) FOR(i, 1, n+1)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; --i)
#define whole(x) (x).begin(),(x).end()
#define rwhole(x) (x).rbegin(), (x).rend()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define P pair<ll, ll>
#define debug(var) cerr << "[" << #var << "] " << var << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
const ll mod = 1000000007;
const int dx[] = {-1,0,1,0};
const int dy[] = {0,-1,0,1};
const ll INF = 1E+18;


int main(){
    ll n;
    cin >> n;
    vector<P> u, r, d, l;
    rep(i, n) {
        ll x, y;
        char c;
        cin >> x >> y >> c;
        if (c=='U') u.emplace_back(x, y);
        if (c=='R') r.emplace_back(x, y);
        if (c=='D') d.emplace_back(x, y);
        if (c=='L') l.emplace_back(x, y);
    }
    ll ans = INF;
    //debug(n);
    auto comp1 = [](P a, P b) {
        if (a.second==b.second) return a.first<b.first;
        return a.second<b.second;
    };
    sort(whole(u));
    sort(whole(d));
    rep(i, u.size()) {
        auto it = lower_bound(whole(d), u[i]);
        if (it!=d.end() && it->first==u[i].first) {
            ll t = (it->second - u[i].second)*5;
            chmin(ans, t);
            //debug(i);
            //debug(ans);
        }
    }
    
    sort(whole(l), comp1);
    sort(whole(r), comp1);
    rep(i, r.size()) {
        auto it = lower_bound(whole(l), r[i], comp1);
        if (it!=l.end() && it->second==r[i].second) {
            //debug(it->second);
            //debug(l[i].second);
            ll t = (it->first - r[i].first) * 5;
            chmin(ans, t);
            //debug(i);
            //debug(ans);
        }
    }
    //debug(ans);
    vector<P> xu, xr, xd, xl;

    rep(i, u.size()) {
        xu.emplace_back(u[i].first+u[i].second, u[i].first-u[i].second);
    }
    rep(i, r.size()) {
        xr.emplace_back(r[i].first+r[i].second, r[i].first-r[i].second);
    }
    rep(i, d.size()) {
        xd.emplace_back(d[i].first+d[i].second, d[i].first-d[i].second);
    }
    rep(i, l.size()) {
        xl.emplace_back(l[i].first+l[i].second, l[i].first-l[i].second);
    }
    //debug(n);
    //r and u
    sort(whole(xu));
    rep(i, xr.size()) {
        auto it = lower_bound(whole(xu), xr[i]);
        if (it!=xu.end() && it->first==xr[i].first) {
            ll t = (it->second - xr[i].second)*5;
            chmin(ans, t);
        }
    }
    //r and d
    sort(whole(xd), comp1);
    rep(i, xr.size()) {
        auto it = lower_bound(whole(xd), xr[i], comp1);
        if (it!=xd.end() && it->second==xr[i].second) {
            ll t = (it->first - xr[i].first)*5;
            chmin(ans, t);
        }
    }
    //u and l
    sort(whole(xl), comp1);
    rep(i, xu.size()) {
        auto it = lower_bound(whole(xl), xu[i], comp1);
        if (it!=xl.end() && it->second==xu[i].second) {
            ll t = (it->first-xu[i].first) * 5;
            chmin(ans, t);
        }
    }
    //d and l
    sort(whole(xl));
    rep(i, xd.size()) {
        //debug(i);
        auto it = lower_bound(whole(xl), xd[i]);
        //debug(it->second);
        //debug(xl[i].first);
        if (it!=xl.end() && it->first==xd[i].first) {
            ll t = (it->second-xd[i].second) * 5;
            //debug(it->second);
            //debug(xd[i].second);
            //debug(t);
            chmin(ans, t);
        }
    }

    

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


Measured memory_bytes: 12176