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

using ll = long long int;
using ull = unsigned long long int;
using P = pair<ll, ll>;
using P3 = pair<P,int>;
using PP = pair<P, P>;
constexpr int INF = 1 << 30;
constexpr ll MOD = ll(1e9)+7;
constexpr int di[] = {0, 1, 0, -1};
constexpr int dj[] = {1, 0, -1, 0};
constexpr int di8[] = {0, 1, 1, 1, 0, -1, -1, -1};
constexpr int dj8[] = {1, 1, 0, -1, -1, -1, 0, 1};
constexpr double EPS = 1e-9;


int main(){
    int n, ans = INF;
    cin >> n;
    vector<int> x(n), y(n);
    vector<char> u(n);
    map<int,vector<P> > h, v, a1, a2, b1, b2;
    for(int i=0;i<n;i++){
        cin >> x[i] >> y[i] >> u[i];
        if(u[i] == 'R' || u[i] == 'L') h[y[i]].emplace_back(x[i],i);
        else v[x[i]].emplace_back(y[i],i);
    }
    for(auto& p : h){
        auto ls = p.second;
        sort(ls.begin(),ls.end());
        int m = ls.size();
        for(int i=1;i<m;i++){
            int l = ls[i-1].second, r = ls[i].second;
            if(u[l]=='R' && u[r]=='L'){
                ans = min(ans, (x[r]-x[l])*5);
            }
        }
    }
    for(auto& p : v){
        auto ls = p.second;
        sort(ls.begin(),ls.end());
        int m = ls.size();
        for(int i=1;i<m;i++){
            int l = ls[i-1].second, r = ls[i].second;
            if(u[l]=='U' && u[r]=='D'){
                ans = min(ans, (y[r]-y[l])*5);
            }
        }
    }
    vector<int> X(n), Y(n);
    for(int i=0;i<n;i++){
        X[i] = y[i]+x[i];
        Y[i] = y[i]-x[i];
        if(u[i] == 'U'){
            a1[X[i]].emplace_back(Y[i],i);
            b1[Y[i]].emplace_back(X[i],i);
        }
        if(u[i] == 'D'){
            a2[X[i]].emplace_back(Y[i],i);
            b2[Y[i]].emplace_back(X[i],i);
        }
        if(u[i] == 'R'){
            a1[X[i]].emplace_back(Y[i],i);
            b2[Y[i]].emplace_back(X[i],i);
        }
        if(u[i] == 'L'){
            a2[X[i]].emplace_back(Y[i],i);
            b1[Y[i]].emplace_back(X[i],i);
        }
    }
    for(auto& p : a1){
        auto ls = p.second;
        sort(ls.begin(),ls.end());
        int m = ls.size();
        for(int i=1;i<m;i++){
            int l = ls[i-1].second, r = ls[i].second;
            if(u[l]=='U' && u[r]=='R'){
                ans = min(ans, (Y[r]-Y[l])*5);
            }
        }
    }
    for(auto& p : a2){
        auto ls = p.second;
        sort(ls.begin(),ls.end());
        int m = ls.size();
        for(int i=1;i<m;i++){
            int l = ls[i-1].second, r = ls[i].second;
            if(u[l]=='L' && u[r]=='D'){
                ans = min(ans, (Y[r]-Y[l])*5);
            }
        }
    }
    for(auto& p : b1){
        auto ls = p.second;
        sort(ls.begin(),ls.end());
        int m = ls.size();
        for(int i=1;i<m;i++){
            int l = ls[i-1].second, r = ls[i].second;
            if(u[l]=='U' && u[r]=='L'){
                ans = min(ans, (X[r]-X[l])*5);
            }
        }
    }
    for(auto& p : b2){
        auto ls = p.second;
        sort(ls.begin(),ls.end());
        int m = ls.size();
        for(int i=1;i<m;i++){
            int l = ls[i-1].second, r = ls[i].second;
            if(u[l]=='R' && u[r]=='D'){
                ans = min(ans, (X[r]-X[l])*5);
            }
        }
    }
    if(ans < INF) cout << ans << endl;
    else cout << "SAFE" << endl;
    return 0;
}

Measured memory_bytes: 71436