#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define srep(i,s,t) for(int i = s; i < t; ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
#define yn {puts("Yes");}else{puts("No");}
#define MAX_N 200005
#define MAX_NN 400010

vector<int>
u[MAX_N],
r[MAX_N],
d[MAX_N],
l[MAX_N];

vector<P> u1[MAX_NN], u2[MAX_NN], d1[MAX_NN], d2[MAX_NN];



int main() {
    int n;
    cin >> n;
    int x, y;
    char c;
    rep(i,n){
        cin >> x >> y >> c;
        if(c == 'U') u[x].push_back(y);
        if(c == 'R') r[y].push_back(x);
        if(c == 'D') d[x].push_back(y);
        if(c == 'L') l[y].push_back(x);

        if(c == 'U'){
            u1[x+y].push_back(P(x,0));
            u2[x-y+MAX_N].push_back(P(x,0));
        }

        if(c == 'D'){
            d1[x+y].push_back(P(x,0));
            d2[x-y+MAX_N].push_back(P(x,0));
        }

        if(c == 'R'){
            u1[x+y].push_back(P(x,1));
            d2[x-y+MAX_N].push_back(P(x,1));
        }

        if(c == 'L'){
            u2[x-y+MAX_N].push_back(P(x,1));
            d1[x+y].push_back(P(x,1));
        }

    }
    rep(i,MAX_N){
        sort(u[i].begin(), u[i].end());
        sort(r[i].begin(), r[i].end());
        sort(d[i].begin(), d[i].end());
        sort(l[i].begin(), l[i].end());
    }
    int ans = 1001001001;
    rep(i,MAX_N){
        int m = d[i].size();
        if(m == 0) continue;
        rep(j,u[i].size()){
            int ite = lower_bound(d[i].begin(), d[i].end(), u[i][j]) - d[i].begin();
            if(ite != m){
                ans = min(ans, (d[i][ite]-u[i][j])*5);
            }
        }
    }
    rep(i,MAX_N){
        int m = l[i].size();
        if(m == 0) continue;
        rep(j,r[i].size()){
            int ite = lower_bound(l[i].begin(), l[i].end(), r[i][j]) - l[i].begin();
            if(ite != m){
                ans = min(ans, (l[i][ite]-r[i][j])*5);
            }
        }
    }

    rep(i,MAX_NN){
        sort(u1[i].begin(), u1[i].end());
        sort(u2[i].begin(), u2[i].end());
        sort(d1[i].begin(), d1[i].end());
        sort(d2[i].begin(), d2[i].end());
    }

    rep(i,MAX_NN){
        int now = -1;
        drep(j,u1[i].size()){
            if(u1[i][j].second == 0){
                now = u1[i][j].first;
            }else{
                if(now == -1) continue;
                ans = min(ans, (now - u1[i][j].first) * 10);
            }
        }
        now = -1;
        rep(j,u2[i].size()){
            if(u2[i][j].second == 0){
                now = u2[i][j].first;
            }else{
                if(now == -1) continue;
                ans = min(ans, (u2[i][j].first - now) * 10);
            }
        }
        
        now = -1;
        rep(j,d1[i].size()){
            if(d1[i][j].second == 0){
                now = d1[i][j].first;
            }else{
                if(now == -1) continue;
                ans = min(ans, (d1[i][j].first - now) * 10);
            }
        }
        now = -1;
        drep(j,d2[i].size()){
            if(d2[i][j].second == 0){
                now = d2[i][j].first;
            }else{
                if(now == -1) continue;
                ans = min(ans, (now - d2[i][j].first) * 10);
            }
        }
    }


    if(ans == 1001001001){
        cout << "SAFE" << endl;
    }else{
        cout << ans << endl;
    }
    return 0;
}




Measured memory_bytes: 78412