#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define REP(i,n) for (ll i = 0; i < (n); ++i)
#define RREP(i, n) for (ll i = (n) - 1; i >= 0; --i)
#define ALL(v) (v).begin(), (v).end()
template<class T>
using reverse_priority_queue = priority_queue<T, vector<T>, greater<T>>;

ll get_min_time(map<ll, vector<pair<ll, ll>>>& x2y){
    ll ret = numeric_limits<ll>::max();
    for(auto it = x2y.begin(); it != x2y.end(); ++it){
        sort(ALL(it->second));
        REP(i, it->second.size() - 1){
            if(it->second.at(i).second == 0 && it->second.at(i + 1).second == 1){
                ret = min(ret, 5 * (it->second.at(i + 1).first - it->second.at(i).first));
            }
        }
    }
    return ret;
}


int main(){
    ll n;
    cin >> n;

    const map<char, int> c2i = {{'U', 0}, {'R', 1}, {'D', 2}, {'L', 3}};
    const int UI = 0, RI = 1, DI = 2, LI = 3;

    array<vector<pair<ll, ll>>, 4> xy;
    REP(i, n){
        ll xi, yi;
        char ui;
        cin >> xi >> yi >> ui;
        const ll idx = c2i.at(ui);
        xy.at(idx).emplace_back(xi, yi);
    }

    ll ans = numeric_limits<ll>::max();

    // U and D
    {
        map<ll, vector<pair<ll, ll>>> k2v;
        for(auto p : xy.at(UI)){ k2v[p.first].emplace_back(p.second, 0LL); }
        for(auto p : xy.at(DI)){ k2v[p.first].emplace_back(p.second, 1LL); }
        ans = min(ans, get_min_time(k2v));
    }

    // L and R
    {
        map<ll, vector<pair<ll, ll>>> k2v;
        for(auto p : xy.at(RI)){ k2v[p.second].emplace_back(p.first, 0LL); }
        for(auto p : xy.at(LI)){ k2v[p.second].emplace_back(p.first, 1LL); }
        ans = min(ans, get_min_time(k2v));
    }

    // U and L
    {
        map<ll, vector<pair<ll, ll>>> k2v;
        for(auto p : xy.at(UI)){
            k2v[p.first - p.second].emplace_back(p.first + p.second, 0LL);
        }
        for(auto p : xy.at(LI)){
            k2v[p.first - p.second].emplace_back(p.first + p.second, 1LL);
        }
        ans = min(ans, get_min_time(k2v));
    }

    // U and R
    {
        map<ll, vector<pair<ll, ll>>> k2v;
        for(auto p : xy.at(RI)){
            k2v[p.first + p.second].emplace_back(p.first - p.second, 0LL);
        }
        for(auto p : xy.at(UI)){
            k2v[p.first + p.second].emplace_back(p.first - p.second, 1LL);
        }
        ans = min(ans, get_min_time(k2v));
    }

    // D and L
    {
        map<ll, vector<pair<ll, ll>>> k2v;
        for(auto p : xy.at(DI)){
            k2v[p.first + p.second].emplace_back(p.first - p.second, 0LL);
        }
        for(auto p : xy.at(LI)){
            k2v[p.first + p.second].emplace_back(p.first - p.second, 1LL);
        }
        ans = min(ans, get_min_time(k2v));
    }

    // D and R
    {
        map<ll, vector<pair<ll, ll>>> k2v;
        for(auto p : xy.at(RI)){
            k2v[p.first - p.second].emplace_back(p.first + p.second, 0LL);
        }
        for(auto p : xy.at(DI)){
            k2v[p.first - p.second].emplace_back(p.first + p.second, 1LL);
        }
        ans = min(ans, get_min_time(k2v));
    }

    if(ans == numeric_limits<ll>::max()){
        cout << "SAFE" << endl;
    }else{
        cout << ans << endl;
    }

    return 0;
}


Measured memory_bytes: 27924