#pragma region head
    #include <bits/stdc++.h>
    using namespace std;
    using ll = long long;
    using vi = vector<int>;
    using vll = vector<ll>;
    using pi = pair<int, int>;
    using pll = pair<ll,ll>;
    template <class T> using vv = vector<vector<T>>;
    #define rep(i,n) for(int i = 0; i < (int)(n); i++)
    #define repi(i,a,b) for(int i = (int)(a); i < (int)(b); i++)
    #define rrep(i,n) for(int i = (int)(n)-1; i >= 0; i--)
    #define rrepi(i,a,b) for(int i = (int)(b)-1; i >= (int)(a); i--)
    #define all(x) (x).begin(), (x).end()
    #define bit(n) (1LL<<(n))
    const int INF = 1002003004;
    const ll LINF = 1002003004005006007ll;
    struct preprocess{preprocess()
    {cin.tie(nullptr);ios::sync_with_stdio(false);cout<<fixed<<setprecision(20);}}____;
#pragma endregion head

#pragma region library
#pragma endregion library

int main(){
    int n; cin >> n;
    const int MX = 200005;
    vv<pi> xax(MX),yax(MX);
    vv<vi> inc(2*MX,vv<int>(4)),dec(2*MX,vv<int>(4));
    rep(i,n){
        int x,y; char u;
        cin >> x >> y >> u;
        xax[y].emplace_back(x,u);
        yax[x].emplace_back(y,u);
        int idx = 0;
        if(u=='R') idx = 1;
        if(u=='D') idx = 2;
        if(u=='L') idx = 3;
        inc[y-x+MX][idx].emplace_back(x);
        dec[x+y][idx].emplace_back(x);
    }
    int ans = INF;
    rep(i,MX){
        vector<pi> &v = xax[i];
        sort(all(v));
        char dir = 'U';
        int prv = 0;
        rep(j,v.size()){
            if(dir=='R' && v[j].second=='L'){
                ans = min(ans, (v[j].first-prv)*5);
            } else if(v[j].second=='R'){
                tie(prv,dir) = v[j];
            }
        }
    }
    rep(i,MX){
        vector<pi> &v = yax[i];
        sort(all(v));
        char dir = 'R';
        int prv = 0;
        rep(j,v.size()){
            if(dir=='U' && v[j].second=='D'){
                ans = min(ans, (v[j].first-prv)*5);
            } else if(v[j].second=='U'){
                tie(prv,dir) = v[j];
            }
        }
    }
    rep(i,2*MX){
        if(inc[i][0].size()&&inc[i][3].size()){
            vi &u = inc[i][0];
            vi &l = inc[i][3];
            sort(all(u));
            sort(all(l));
            rep(j,u.size()){
                auto it = upper_bound(all(l),u[j]);
                if(it!=l.end()){
                    ans = min(ans, (*it-u[j])*10);
                }
            }   
        }
        if(inc[i][1].size()&&inc[i][2].size()){
            vi &r = inc[i][1];
            vi &d = inc[i][2];
            sort(all(r));
            sort(all(d));
            rep(j,r.size()){
                auto it = upper_bound(all(d),r[j]);
                if(it!=d.end()){
                    ans = min(ans, (*it-r[j])*10);
                }
            }
        }
    }
    rep(i,2*MX){
        if(dec[i][2].size()&&dec[i][3].size()){
            vi &u = dec[i][2];
            vi &l = dec[i][3];
            sort(all(u));
            sort(all(l));
            rep(j,u.size()){
                auto it = upper_bound(all(l),u[j]);
                if(it!=l.end()){
                    ans = min(ans, (*it-u[j])*10);
                }
            } 
        }  
        if(dec[i][1].size()&&dec[i][0].size()){    
            vi &r = dec[i][1];
            vi &d = dec[i][0];
            sort(all(r));
            sort(all(d));
            rep(j,r.size()){
                auto it = upper_bound(all(d),r[j]);
                if(it!=d.end()){
                    ans = min(ans, (*it-r[j])*10);
                }
            }
        }
    }
    if(ans!=INF){
        cout << ans << '\n';
    } else {
        cout << "SAFE" << '\n';
        
    }
}

Measured memory_bytes: 138920