#include<bits/stdc++.h>
#define pir pair<int,int>
#define qmin(x,y) (x=min(x,y))
#define qmax(x,y) (x=max(x,y))
#define rsort(x,y) sort(x,y),reverse(x,y)
#define fr first
#define se second
using namespace std;
typedef long long ll;
const int Maxn=4100000;
const int inf=0x3f3f3f3f;

int n,x[Maxn],y[Maxn],dir[Maxn],bj[Maxn];
vector<pir> v[Maxn];

int getadir() {
    char ch=getchar();
    while(!isalpha(ch)) ch=getchar();
    switch(ch) {
        case 'U' : return 0;
        case 'R' : return 1;
        case 'D' : return 2;
        case 'L' : return 3;
    }
}

signed main() {
    // freopen("test.in","r",stdin);
    scanf("%d",&n);
    for(int i=1;i<=n;i++) {
        scanf("%d%d",&x[i],&y[i]);
        dir[i]=getadir();
    }
    int ans=inf;
    for(int i=1;i<=n;i++) if((dir[i]&1)==0) v[x[i]].push_back(make_pair(y[i],i)),bj[x[i]]=1;
    for(int i=1;i<Maxn;i++) if(bj[i]) {
        sort(v[i].begin(),v[i].end());
        int temp=0;
        for(pir j:v[i]) {
            if(dir[j.se]==2) {
                if(temp) qmin(ans,j.fr-y[temp]);
            }
            else temp=j.se;
        }
    }
    for(int i=1;i<Maxn;i++) if(bj[i]) v[i].clear(),bj[i]=0;
    for(int i=1;i<=n;i++) if(dir[i]&1) v[y[i]].push_back(make_pair(x[i],i)),bj[y[i]]=1;
    for(int i=1;i<Maxn;i++) if(bj[i]) {
        sort(v[i].begin(),v[i].end());
        int temp=0;
        for(pir j:v[i]) {
            if(dir[j.se]==3) {
                if(temp) qmin(ans,j.fr-x[temp]);
            }
            else temp=j.se;
        }
    }
    for(int i=1;i<Maxn;i++) if(bj[i]) v[i].clear(),bj[i]=0;
    for(int i=1;i<=n;i++) v[x[i]+y[i]].push_back(make_pair(x[i],i)),bj[x[i]+y[i]]=1;
    for(int i=1;i<Maxn;i++) if(bj[i]) {
        sort(v[i].begin(),v[i].end());
        int tempx=0,tempy=0;
        for(pir j:v[i]) {
            if(dir[j.se]==0) {
                if(tempy) qmin(ans,(j.fr-x[tempy])*2);
            }
            else if(dir[j.se]==1) tempy=j.se;
            else if(dir[j.se]==2) tempx=j.se;
            else if(tempx) qmin(ans,(j.fr-x[tempx])*2);
        }
    }
    for(int i=1;i<Maxn;i++) if(bj[i]) v[i].clear(),bj[i]=0;
    for(int i=1;i<=n;i++) v[x[i]-y[i]+2000000].push_back(make_pair(x[i],i)),bj[x[i]-y[i]+2000000]=1;
    for(int i=1;i<Maxn;i++) if(bj[i]) {
        sort(v[i].begin(),v[i].end());
        int tempx=0,tempy=0;
        for(pir j:v[i]) {
            if(dir[j.se]==3) {
                if(tempy) qmin(ans,(j.fr-x[tempy])*2);
            }
            else if(dir[j.se]==0) tempy=j.se;
            else if(dir[j.se]==1) tempx=j.se;
            else if(tempx) qmin(ans,(j.fr-x[tempx])*2);
        }
    }
    for(int i=1;i<Maxn;i++) if(bj[i]) v[i].clear(),bj[i]=0;
    if(ans==inf) return 0*puts("SAFE");
    ans*=5;
    printf("%d\n",ans);
    return 0;
}
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll MOD = 1e9+7;
const ll INF = 1e16;
#define rep(i,m,n) for(ll i = (m); i <= (n); i++)
#define zep(i,m,n) for(ll i = (m); i < (n); i++)
#define rrep(i,m,n) for(ll i = (m); i >= (n); i--)
#define print(x) cout << (x) << endl;
#define printa(x,m,n) for(int i = (m); i <= n; i++){cout << (x[i]) << " ";} cout<<endl;

const ll N = 2e5 + 10; 
ll n, x[N], y[N];
char c[N];
map<ll, set<ll>> l, r, u, d;

int main(){
    cin.tie(0); ios::sync_with_stdio(false);
    
    
    cin >> n;
    zep(i, 0, n)cin >> x[i] >> y[i] >> c[i];

    ll ans1 = INF;

    zep(i, 0, n){
        if(c[i] == 'L')l[y[i]].insert(x[i]);
        if(c[i] == 'D')u[x[i]].insert(y[i]);
    }

    zep(i, 0, n){
        if(c[i] == 'R' && l.find(y[i]) != l.end()){
            if(l[y[i]].lower_bound(x[i]) != l[y[i]].end()){
                ans1 = min(ans1, (*l[y[i]].lower_bound(x[i])) - x[i]);
            }
        }
        if(c[i] == 'U' && u.find(x[i]) != u.end()){
            if(u[x[i]].lower_bound(y[i]) != u[x[i]].end()){
                ans1 = min(ans1, (*u[x[i]].lower_bound(y[i])) - y[i]);
            }
        }
    }

    ans1 *= 5;
    
    while(!l.empty())l.erase(l.begin());
    while(!u.empty())u.erase(u.begin());
    
    ll ans2 = INF;

    zep(i, 0, n){
        if(c[i] == 'L')l[x[i] - y[i]].insert(y[i]);
        if(c[i] == 'D')d[x[i] - y[i]].insert(y[i]);
    }

    zep(i, 0, n){
        if(c[i] == 'U' && l.find(x[i] - y[i]) != l.end()){
            if(l[x[i] - y[i]].lower_bound(y[i]) != l[x[i] - y[i]].end()){
                ans2 = min(ans2, (*l[x[i] - y[i]].lower_bound(y[i])) - y[i]);
            }
        }
    }

    zep(i, 0, n){
        if(c[i] == 'R' && d.find(x[i] - y[i]) != d.end()){
            if(d[x[i] - y[i]].lower_bound(y[i]) != d[x[i] - y[i]].end()){
                ans2 = min(ans2, (*d[x[i] - y[i]].lower_bound(y[i])) - y[i]);
            }
        }
    }

    while(!l.empty())l.erase(l.begin());
    while(!d.empty())d.erase(d.begin());

    zep(i, 0, n){
        if(c[i] == 'R')r[x[i] + y[i]].insert(y[i]);
        if(c[i] == 'D')d[x[i] + y[i]].insert(y[i]);
    }

    zep(i, 0, n){
        if(c[i] == 'U' && r.find(x[i] + y[i]) != r.end()){
            if(r[x[i] + y[i]].lower_bound(y[i]) != r[x[i] + y[i]].end()){
                ans2 = min(ans2, (*r[x[i] + y[i]].lower_bound(y[i])) - y[i]);
            }
        }
    }

    zep(i, 0, n){
        if(c[i] == 'L' && d.find(x[i] + y[i]) != d.end()){
            if(d[x[i] + y[i]].lower_bound(y[i]) != d[x[i] + y[i]].end()){
                ans2 = min(ans2, (*d[x[i] + y[i]].lower_bound(y[i])) - y[i]);
            }
        }
    }

    ans2 *= 10;

    ll ans = min(min(ans1, ans2), INF);
    if(ans == INF){
        print("SAFE")
    }else{
        print(ans)
    }

    return 0;
}