#include<bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < int(n); ++i)
#define rrep(i,n) for (int i = int(n)-1; i >= 0; i--)
#define rep2(i,s,n) for (int i = int(s); i < int(n); ++i)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vd vector<double>
#define vs vector<string>
#define vc vector<char>
#define vb vector<bool>
#define vp vector<P>
using namespace std;
using ll = long long;
using P = pair<int,int>;
using LP = pair<ll,ll>;
template<class S,class T> istream& operator>>(istream &is,pair<S,T> &p) { return is >> p.first >> p.second; }
template<class S,class T> ostream& operator<<(ostream &os,const pair<S,T> &p) { return os<<'{'<<p.first<<","<<p.second<<'}'; }
template<class T> istream& operator>>(istream &is,vector<T> &v) { for(T &t:v){is>>t;} return is; }
template<class T> ostream& operator<<(ostream &os,const vector<T> &v) { os<<'[';rep(i,v.size())os<<v[i]<<(i==int(v.size()-1)?']':','); return os; }
void Yes(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YES(bool b) { cout << (b ? "YES" : "NO") << endl; }
template<class T> bool chmin(T& a,T b) {if(a > b){a = b; return true;} return false;}
template<class T> bool chmax(T& a,T b) {if(a < b){a = b; return true;} return false;}
const int inf = 1001001001;
const ll linf = 1001001001001001001;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    int n;
    cin >> n;
    vi x(n),y(n);
    vc u(n);
    rep(i,n) cin >> x[i] >> y[i] >> u[i];
    int ans = inf;
    vector<vp> v(200010);
    rep(i,n) {
        if(u[i] == 'D' or u[i] == 'U') continue;
        int j = (u[i] == 'R' ? 0 : 1);
        v[y[i]].eb(x[i],j);
    }
    rep(i,200010) {
        sort(all(v[i]));
        int now = -inf;
        for(auto [j,k] : v[i]) {
            if(k) {
                if(now == -inf) continue;
                chmin(ans,(j-now)*5);
            } else {
                now = j;
            }
        }
    }
    v.clear();
    v.resize(200010);
    rep(i,n) {
        if(u[i] == 'R' or u[i] == 'L') continue;
        int j = (u[i] == 'U' ? 0 : 1);
        v[x[i]].eb(y[i],j);
    }
    rep(i,200010) {
        sort(all(v[i]));
        int now = -inf;
        for(auto [j,k] : v[i]) {
            if(k) {
                if(now == -inf) continue;
                chmin(ans,(j-now)*5);
            } else {
                now = j;
            }
        }
    }
    v.clear();
    v.resize(400010);
    rep(i,n) {
        if(u[i] == 'D' or u[i] == 'L') continue;
        int j = (u[i] == 'R' ? 0 : 1);
        v[x[i]+y[i]].eb(x[i],j);
    }
    rep(i,400010) {
        sort(all(v[i]));
        int now = -inf;
        for(auto [j,k] : v[i]) {
            if(k) {
                if(now == -inf) continue;
                chmin(ans,(j-now)*10);
            } else {
                now = j;
            }
        }
    }
    v.clear();
    v.resize(400010);
    rep(i,n) {
        if(u[i] == 'R' or u[i] == 'U') continue;
        int j = (u[i] == 'D' ? 0 : 1);
        v[x[i]+y[i]].eb(x[i],j);
    }
    rep(i,400010) {
        sort(all(v[i]));
        int now = -inf;
        for(auto [j,k] : v[i]) {
            if(k) {
                if(now == -inf) continue;
                chmin(ans,(j-now)*10);
            } else {
                now = j;
            }
        }
    }
    v.clear();
    v.resize(400010);
    rep(i,n) {
        if(u[i] == 'D' or u[i] == 'R') continue;
        int j = (u[i] == 'U' ? 0 : 1);
        v[y[i]-x[i]+200000].eb(x[i],j);
    }
    rep(i,400010) {
        sort(all(v[i]));
        int now = -inf;
        for(auto [j,k] : v[i]) {
            if(k) {
                if(now == -inf) continue;
                chmin(ans,(j-now)*10);
            } else {
                now = j;
            }
        }
    }
    v.clear();
    v.resize(400010);
    rep(i,n) {
        if(u[i] == 'U' or u[i] == 'L') continue;
        int j = (u[i] == 'R' ? 0 : 1);
        v[y[i]-x[i]+200000].eb(x[i],j);
    }
    rep(i,400010) {
        sort(all(v[i]));
        int now = -inf;
        for(auto [j,k] : v[i]) {
            if(k) {
                if(now == -inf) continue;
                chmin(ans,(j-now)*10);
            } else {
                now = j;
            }
        }
    }
    cout << (ans == inf ? "SAFE" : to_string(ans)) << endl;
}

Measured memory_bytes: 25108