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

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
//typedef vector<vector<ll>> Graph;

const ll mod = 1e9 + 7;
//const ll mod = 998244353;

#define REP(i,n) for(ll i=0;i<(ll)n;i++)
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define spa << " " <<
#define fi first
#define se second

template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }

template<class S, class T> ostream& operator << (ostream& os, const pair<S, T> v){
os << "(" << v.first << ", " << v.second << ")"; return os;
}
template<class T> ostream& operator << (ostream& os, const vector<T> v){
for(int i = 0; i < (int)v.size(); i++){if(i > 0){os << " ";} os << v[i];} return os;
}
template<class T> ostream& operator << (ostream& os, const vector<vector<T>> v){
for(int i = 0; i < (int)v.size(); i++){if(i > 0){os << endl;} os << v[i];} return os;
}

template<typename T> void debug(vector<vector<T>>&v,ll h,ll w){for(ll i=0;i<h;i++)
{cerr<<v[i][0];for(ll j=1;j<w;j++)cerr spa v[i][j];cerr<<endl;}};
template<typename T> void debug(vector<T>&v,ll n){if(n!=0)cerr<<v[0];
for(ll i=1;i<n;i++)cerr spa v[i];
cerr<<endl;};

const ll INF = (1ll<<60);

void rotate(vl &x, vl &y, vl &p){
    ll N = x.size();
    REP(i, N) swap(x[i], y[i]);
    REP(i, N) x[i] *= -1;
    REP(i, N) p[i] = (p[i]+1)%4;
    return;
}

ll solve(vl &X, vl &Y, vl &P){
    ll res = INF;
    ll N = X.size();

    vector<ll> idx;
    REP(i, N){
        if(P[i] == 3 or P[i] == 1){
            idx.push_back(i);
        }
    }
    ll M = idx.size();
    sort(idx.begin(), idx.end(), [&](ll i, ll j){
        if(X[i] == X[j]) return Y[i] < Y[j];
        else return X[i] < X[j];
    });
    REP(i, M-1){
        if(X[idx[i]] == X[idx[i+1]] and P[idx[i]] == 1 and P[idx[i+1]] == 3){
            ll d = (Y[idx[i+1]] - Y[idx[i]])*5;
            chmin(res, d);
        }
    }

    idx.clear();
    idx.shrink_to_fit();
    REP(i, N){
        if(P[i] == 0 or P[i] == 1){
            idx.push_back(i);
        }
    }
    M = idx.size();
    sort(idx.begin(), idx.end(), [&](ll i, ll j){
        if(X[i]+Y[i] == X[j]+Y[j]) return Y[i] < Y[j];
        else return X[i]+Y[i] < X[j]+Y[j];
    });
    REP(i, M-1){
        if(X[idx[i]]+Y[idx[i]] == X[idx[i+1]]+Y[idx[i+1]] and P[idx[i]] == 1 and P[idx[i+1]] == 0){
            ll d = (Y[idx[i+1]] - Y[idx[i]])*10;
            chmin(res, d);
        }
    }    

    return res;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    map<char, ll> mp;
    mp['R'] = 0;
    mp['U'] = 1;
    mp['L'] = 2;
    mp['D'] = 3;

    ll N;
    cin >> N;
    vl X(N), Y(N), P(N);
    REP(i, N){
        cin >> X[i] >> Y[i];
        char tmp;
        cin >> tmp;
        P[i] = mp[tmp];
    }

    ll res = INF;
    REP(t, 4){
        chmin(res, solve(X, Y, P));
        rotate(X, Y, P);
    }

    if(res == INF) cout << "SAFE\n";
    else cout << res << endl;

    return 0;
}

Measured memory_bytes: 11520