#include "bits/stdc++.h"

using namespace std;

typedef long double ld;
typedef long long ll;

#define sz(x) (int)(x).size()
#define pb push_back
#define mp make_pair
#define f first
#define s second

template<typename T, typename U> bool ckmin(T &a, U b){ return b < a ? a = b, true : false; }
template<typename T, typename U> bool ckmax(T &a, U b){ return b > a ? a = b, true : false; }

const int INF = 2e9;

int n;
double res = INF;
//{U, D, R, L}
vector<vector<pair<int, int>>> planes;
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n;
    planes.assign(4, vector<pair<int, int>>());
    for(int i = 0; i < n; ++i){
        int x, y; char u;
        cin >> x >> y >> u;
        if(u == 'U') planes[0].pb(mp(x, y));
        if(u == 'D') planes[1].pb(mp(x, y));
        if(u == 'R') planes[2].pb(mp(x, y));
        if(u == 'L') planes[3].pb(mp(x, y));
    }
    map<int, vector<int>> stx, sty;
    for(auto i : planes[0]){
        stx[i.f].pb(i.s);
    }
    for(auto i : planes[2]){
        sty[i.s].pb(i.f);
    }
    for(auto &i : stx){
        sort(i.s.begin(), i.s.end(), greater<int>());
    }
    for(auto &i : sty){
        sort(i.s.begin(), i.s.end(), greater<int>());
    }
    // for(auto i : stx){
    //     cout << i.f << ": ";
    //     for(auto j : i.s) cout << j << " ";
    //     cout << "\n";
    // }
    //Direct collision case
    for(auto i : planes[1]){
        auto it = stx.find(i.f);
        if(it != stx.end()){
            // cout << i.s << " ";
            auto it2 = lower_bound(it->s.begin(), it->s.end(), i.s, greater<int>());
            if(it2 != it->s.end()) ckmin(res, (i.s - *it2)/2.0);
        }
    }
    for(auto i : planes[3]){
        auto it = sty.find(i.s);
        if(it != sty.end()){
            auto it2 = lower_bound(it->s.begin(), it->s.end(), i.f, greater<int>());
            if(it2 != it->s.end()) ckmin(res, (i.f - *it2)/2.0);
        }
    }
    // UR case [x + y] = {{y, is U}} and DL case
    map<int, vector<pair<int, bool>>> UR, DL;
    for(auto i : planes[0]){
        UR[i.f + i.s].pb(mp(i.s, 1));
    }
    for(auto i : planes[2]){
        UR[i.f + i.s].pb(mp(i.s, 0));
    }
    for(auto i : planes[1]){
        DL[i.f + i.s].pb(mp(i.f, 1));
    }
    for(auto i : planes[3]){
        DL[i.f + i.s].pb(mp(i.f, 0));
    }
    for(auto &i : UR){
        sort(i.s.begin(), i.s.end(), greater<pair<int, bool>>());
        int bestR = INF;
        for(auto j : i.s){
            if(j.s && bestR != INF) ckmin(res, bestR - j.f);
            else if(!j.s) bestR = j.f;
        }
    }
    for(auto &i : DL){
        sort(i.s.begin(), i.s.end(), greater<pair<int, bool>>());
        int bestL = INF;
        for(auto j : i.s){
            if(j.s && bestL != INF) ckmin(res, bestL - j.f);
            else if(!j.s) bestL = j.f;
        }
    }
    // UL case [x + y] = {{y, is U}} and DR case
    map<int, vector<pair<int, bool>>> UL, DR;
    for(auto i : planes[0]){
        UL[i.s - i.f].pb(mp(i.f, 1));
    }
    for(auto i : planes[3]){
        UL[i.s - i.f].pb(mp(i.f, 0));
    }
    for(auto i : planes[1]){
        DR[i.s - i.f].pb(mp(i.s, 1));
    }
    for(auto i : planes[2]){
        DR[i.s - i.f].pb(mp(i.s, 0));
    }
    for(auto &i : UL){
        sort(i.s.begin(), i.s.end());
        int bestU = INF;
        for(auto j : i.s){
            if(!j.s && bestU != INF) ckmin(res, j.f - bestU);
            else if(j.s) bestU = j.f;
        }
    }
    //SUS
    for(auto &i : DR){
        sort(i.s.begin(), i.s.end());
        int bestR = INF;
        for(auto j : i.s){
            if(j.s && bestR != INF) ckmin(res, j.f - bestR);
            else if(!j.s) bestR = j.f;
        }
    }
    if(res == INF) cout << "SAFE";
    else cout << ((int)(res * 10));
    return 0;
}

Measured memory_bytes: 58908