#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cctype>
#include<math.h>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<stdlib.h>
#include<iomanip>
#include<complex>
#include <cassert>

using namespace std;

#define ld long double
#define INF 1e9
#define LINF (ll)INF*INF
#define MOD 1000000007
#define rep(i,n) for(int i=0;i<(n);i++)
#define loop(i,a,n) for(int i=a;i<(n);i++)
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)

#define int long long //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

const double PI=acos(-1);
const double EPS=1e-9; //うまくいかなかったらゆるめる

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pii;
typedef pair<pii,int> ppi;
typedef pair<int,pii> pip;
typedef vector<pii> vp;
typedef vector<vi> vvi;

int gcd(int a, int b){if(b==0) return a;return gcd(b,a%b);}
int lcm(int a, int b){return a/gcd(a,b)*b;}


signed main(void) {
    int n;
    cin >> n;
    vp u,r,d,l;
    map<int, set<int>> uu,dd,rr,ll;
    map<int,set<int>> u1,u2,d1,d2;//1はy=x+b, 2はy=-x+bを入れる
    int ans = INF;
    rep(i,n){
        int x,y;
        string s;
        cin >> x >> y >> s;

        if(s == "U"){
            uu[x].insert(y);
            if(dd[x].size()){
                auto it = dd[x].lower_bound(y);
                if(it != dd[x].end())ans = min(ans, abs(*it-y)*5);
            }
            u1[y-x].insert(x);
            u2[x+y].insert(x);
            u.push_back(pii(x,y));
        }
        if(s == "R"){
            rr[y].insert(x);
            if(ll[y].size()){
                auto it = ll[y].lower_bound(x);
                if(it != ll[y].end())ans = min(ans, abs(*it-x)*5);
            }
            r.push_back(pii(x,y));
        }
        if(s == "D"){
            dd[x].insert(y);
            if(uu[x].size()){
                auto it = uu[x].lower_bound(y);
                if(it != uu[x].begin()){
                    it--;
                    ans = min(ans, abs(*it-y)*5);
                }
            }
            d1[y-x].insert(x);
            d2[x+y].insert(x);
            d.push_back(pii(x,y));
        }
        if(s == "L"){
            ll[y].insert(x);
            if(rr[y].size()){
                auto it = rr[y].lower_bound(x);
                if(it != rr[y].begin()){
                    it--;
                    ans = min(ans, abs(*it-x)*5);
                }
            }
            l.push_back(pii(x,y));
        }
    }

    rep(i,r.size()){
        int p = r[i].first;
        int q = r[i].second;
        //d1のうち、y = x + q-p の直線上にあるものとぶつかる
        //d1[q-p]のもののうち、pより大きく最小のものを探す
        if(d1[q-p].size()){
            auto it = d1[q-p].upper_bound(p);
            if(it != d1[q-p].end())ans = min(ans, (*it-p)*10);
        }

        //u2のうち、y =-x + q+p の直線上にあるものとぶつかる
        //u2[q+p]のうち、pより大きく最小のものを探す
        if(u2[q+p].size()){
            auto it = u2[q+p].upper_bound(p);
            if(it != u2[q+p].end())ans = min(ans, (*it-p)*10);
        }

    }
    rep(i,l.size()){
        int p = l[i].first;
        int q = l[i].second;
        //d2のうち、y = x + q+p の直線上にあるものとぶつかる
        //d2[q+p]のうち、pより小さく最大のものをさがす
        if(d2[q+p].size()){
            auto it = d2[q+p].lower_bound(p);
            if(it != d2[q+p].begin()){
                it--;
                ans = min(ans, (p-*it)*10);
            }
        }

        //u1のうち、y =-x + q-p の直線上にあるものとぶつかる
        //u1[q-p]のうち、pより小さく最大のものを探す
        if(u1[q-p].size()){
            auto it = u1[q-p].lower_bound(p);
            if(it != u1[q-p].begin()){
                it--;
                ans = min(ans, (p-*it)*10);
            }
        }

    }
    if(ans == INF){
        cout << "SAFE" << endl;
    }else{
        cout << ans << endl;
    }
}


Measured memory_bytes: 108236