#include <bits/stdc++.h>
using namespace std;

#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define INF 1e9
typedef long long ll;


int main() {
    int N; cin >> N;
    vector<int> X(N);
    vector<int> Y(N);
    vector<char> U(N);
    REP(i,N) cin >> X[i] >> Y[i] >> U[i];

    int ans = INF;

    vector<vector<pair<int,char>>> H(1e6);//縦方向

    REP(i,N){
        if(U[i]=='U' || U[i]=='D'){
            H[X[i]].push_back({Y[i],U[i]});
        }
    }
    REP(i,H.size()){
        if(H[i].size()==0) continue;
        sort(ALL(H[i]));
        REP(j,H[i].size()-1){
            if(H[i][j].second=='U' && H[i][j+1].second=='D'){
                ans = min(ans, 5*(H[i][j+1].first - H[i][j].first));
            }
        }
    }

    vector<vector<pair<int,char>>> V(1e6);//横方向
    REP(i,N){
        if(U[i]=='R' || U[i]=='L'){
            V[Y[i]].push_back({X[i],U[i]});
        }
    }
    REP(i,V.size()){
        if(V[i].size()==0) continue;
        sort(ALL(V[i]));
        REP(j,V[i].size()-1){
            if(V[i][j].second=='R' && V[i][j+1].second=='L'){
                ans = min(ans, 5*(V[i][j+1].first - V[i][j].first));
            }
        }
    }

    vector<int> XX(N);
    vector<int> YY(N);

    REP(i,N){//45度回転
        XX[i] = X[i]-Y[i]+200000;
        YY[i] = X[i]+Y[i]+200000;
    }

    vector<vector<pair<int,char>>> V1(1e6);//横方向
    REP(i,N){
        if(U[i]=='R' || U[i]=='U'){
            V1[YY[i]].push_back({XX[i],U[i]});
        }
    }
    REP(i,V1.size()){
        if(V1[i].size()==0) continue;
        sort(ALL(V1[i]));
        REP(j,V1[i].size()-1){
            if(V1[i][j].second=='R' && V1[i][j+1].second=='U'){
                ans = min(ans, 5*(V1[i][j+1].first - V1[i][j].first));
            }
        }
    }

    vector<vector<pair<int,char>>> V2(1e6);//横方向
    REP(i,N){
        if(U[i]=='D' || U[i]=='L'){
            V2[YY[i]].push_back({XX[i],U[i]});
        }
    }
    REP(i,V2.size()){
        if(V2[i].size()==0) continue;
        sort(ALL(V2[i]));
        REP(j,V2[i].size()-1){
            if(V2[i][j].second=='D' && V2[i][j+1].second=='L'){
                ans = min(ans, 5*(V2[i][j+1].first - V2[i][j].first));
            }
        }
    }

    vector<vector<pair<int,char>>> H1(1e6);//横方向
    REP(i,N){
        if(U[i]=='R' || U[i]=='D'){
            H1[XX[i]].push_back({YY[i],U[i]});
        }
    }
    REP(i,H1.size()){
        if(H1[i].size()==0) continue;
        sort(ALL(H1[i]));
        REP(j,H1[i].size()-1){
            if(H1[i][j].second=='R' && H1[i][j+1].second=='D'){
                ans = min(ans, 5*(H1[i][j+1].first - H1[i][j].first));
            }
        }
    }

    vector<vector<pair<int,char>>> H2(1e6);//横方向
    REP(i,N){
        if(U[i]=='U' || U[i]=='L'){
            H2[XX[i]].push_back({YY[i],U[i]});
        }
    }
    REP(i,H2.size()){
        if(H2[i].size()==0) continue;
        sort(ALL(H2[i]));
        REP(j,H2[i].size()-1){
            if(H2[i][j].second=='U' && H2[i][j+1].second=='L'){
                ans = min(ans, 5*(H2[i][j+1].first - H2[i][j].first));
            }
        }
    }

    if(ans == INF) cout << "SAFE" << endl;
    else cout << ans << endl;
    return 0;
}
#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;
}