#include <bits/stdc++.h>
template<class T> inline bool chmin(T&a, T b){if(a > b){a = b; return true;}else{return false;}}
template<class T> inline bool chmax(T&a, T b){if(a < b){a = b; return true;}else{return false;}}
#define ll long long
#define double long double
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,n) for(int i=1;i<=(n);i++)
#define mod (ll)(1e9+7)
#define inf (ll)(3e18+7)
#define pi (double) acos(-1.0)
#define P pair<int,int>
#define PiP pair<int,pair<int,int>>
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
using namespace std;

int MAX_N = 200010;

int n; ll ans = mod;
map<int, set<int>> u, d, l, r;
vector<int> x(MAX_N), y(MAX_N);
vector<char> a(MAX_N);

void make_map(int mode){
    u.clear(); d.clear(); l.clear(); r.clear();
    rep(i, n){
        if(a[i] == 'U')u[(!mode ? x[i] : y[i]+mode*x[i])].insert((!mode ? y[i] : x[i]));
        if(a[i] == 'D')d[(!mode ? x[i] : y[i]+mode*x[i])].insert((!mode ? y[i] : x[i]));
        if(a[i] == 'L')l[(!mode ? y[i] : y[i]+mode*x[i])].insert(x[i]);
        if(a[i] == 'R')r[(!mode ? y[i] : y[i]+mode*x[i])].insert(x[i]);
    }
}

void solve1(){
    rep(i, n){
        int now = y[i]-x[i];
        if(a[i] == 'U'){
            auto it = l[now].lower_bound(x[i]);
            if(it != l[now].end())chmin(ans, abs(*it-x[i])*10LL);
        }
        if(a[i] == 'D'){
            auto it = r[now].lower_bound(x[i]);
            if(it != r[now].begin()){it--;chmin(ans, abs(*it-x[i])*10LL);}
        }
        if(a[i] == 'L'){
            auto it = u[now].lower_bound(x[i]);
            if(it != u[now].begin()){it--;chmin(ans, abs(*it-x[i])*10LL);}
        }
        if(a[i] == 'R'){
            auto it = d[now].lower_bound(x[i]);
            if(it != d[now].end())chmin(ans, abs(*it-x[i])*10LL);
        }
    }
}

void solve2(){
    rep(i, n){
        int now = y[i]+x[i];
        if(a[i] == 'U'){
            auto it = r[now].lower_bound(x[i]);
            if(it != r[now].begin()){it--;chmin(ans, abs(*it-x[i])*10LL);}
        }
        if(a[i] == 'D'){
            auto it = l[now].lower_bound(x[i]);
            if(it != l[now].end())chmin(ans, abs(*it-x[i])*10LL);
        }
        if(a[i] == 'L'){
            auto it = d[now].lower_bound(x[i]);
            if(it != d[now].begin()){it--; chmin(ans, abs(*it-x[i])*10LL);}
        }
        if(a[i] == 'R'){
            auto it = u[now].lower_bound(x[i]);
            if(it != u[now].end())chmin(ans, abs(*it-x[i])*10LL);
        }
    }
}

void solve3(){
    rep(i, n){
        if(a[i] == 'U'){
            auto it = d[x[i]].lower_bound(y[i]);
            if(it != d[x[i]].end())chmin(ans, abs(*it-y[i])*5LL);
        }
        if(a[i] == 'D'){
            auto it = u[x[i]].lower_bound(y[i]);
            if(it != u[x[i]].begin()){it--;chmin(ans, abs(*it-y[i])*5LL);}
        }
        if(a[i] == 'L'){
            auto it = r[y[i]].lower_bound(x[i]);
            if(it != r[y[i]].begin()){it--;chmin(ans, abs(*it-x[i])*5LL);}
        }
        if(a[i] == 'R'){
            auto it = l[y[i]].lower_bound(x[i]);
            if(it != l[y[i]].end())chmin(ans, abs(*it-x[i])*5LL);
        }
    }
}

int main() {
    cin >> n;
    rep(i, n)cin >> x[i] >> y[i] >> a[i];
    make_map(-1);
    solve1();
    make_map(1);
    solve2();
    make_map(0);
    solve3();
    if(ans == mod)cout << "SAFE" << endl;
    else cout << ans  << endl;
}

Measured memory_bytes: 51452