#include "bits/stdc++.h"
using namespace std;
#define int long long
#define FOR(i, a, b) for(int i=(a);i<(b);i++)
#define RFOR(i, a, b) for(int i=(b-1);i>=(a);i--)
#define REP(i, n) for(int i=0; i<(n); i++)
#define RREP(i, n) for(int i=(n-1); i>=0; i--)
#define REP1(i, n) for(int i=1; i<=(n); i++)
#define RREP1(i, n) for(int i=(n); i>=1; i--)
#define ALL(a) (a).begin(),(a).end()
#define UNIQUE_SORT(l) sort(ALL(l)); l.erase(unique(ALL(l)), l.end());
#define CONTAIN(a, b) find(ALL(a), (b)) != (a).end()
#define out(...) printf(__VA_ARGS__)
#if DEBUG
#define debug(...) printf(__VA_ARGS__)
#else
#define debug(...) /* ... */
#endif
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}

void solve();
signed main()
{
#if DEBUG
    std::ifstream in("input.txt");
    std::cin.rdbuf(in.rdbuf());
#endif
    cin.tie(0);
    ios::sync_with_stdio(false);
    solve();
    return 0;
}
/*================================*/
#if DEBUG
#define SIZE 100
#else
#define SIZE 223450
#endif

int N,Q,K;

struct Item {
    int p, c;
    char u;
    bool operator < (const Item& that) const {
        if (u != that.u) return u < that.u;
        if (p != that.p) return p < that.p;
        return c < that.c;
    }
};
map<Item, set<int>> M;

struct Item2 {
    int x,y;
    char u;
};
vector<Item2> V;
map<int, set<int>> ds,ls;

void solve() {
    cin>>N;
    int x,y;
    char u;
    REP(i,N) {
        cin>>x>>y>>u;
        M[{1,y-x,u}].insert(x);
        M[{-1,y+x,u}].insert(x);
        V.push_back({x,y,u});
        if (u=='D') ds[x].insert(y);
        if (u=='L') ls[y].insert(x);
    }
    int ans = INT_MAX;
    REP(i,N) {
        auto item = V[i];
        if (item.u=='R') {
            {
                auto s = &M[{1,item.y-item.x,'D'}];
                auto it = s->upper_bound(item.x);
                if (it != s->end()) chmin(ans, (*it - item.x) * 10);
            }
            {
                auto s = &M[{-1,item.y+item.x,'U'}];
                auto it = s->upper_bound(item.x);
                if (it != s->end()) chmin(ans, (*it - item.x) * 10);
            }
            {
                auto s = &ls[item.y];
                auto it = s->upper_bound(item.x);
                if (it != s->end()) {
                    chmin(ans, (*it - item.x) * 5);
                }
            }
        }
        if (item.u=='L') {
            {
                auto s = &M[{1,item.y-item.x,'U'}];
                auto it = s->lower_bound(item.x);
                if (it != s->begin()) chmin(ans, (item.x - *(--it)) * 10);
            }
            {
                auto s = &M[{-1,item.y+item.x,'D'}];
                auto it = s->lower_bound(item.x);
                if (it != s->begin()) chmin(ans, (item.x - *(--it)) * 10);
            }
        }
        if (item.u=='U') {
            auto s = &ds[item.x];
            auto it = s->upper_bound(item.y);
            if (it != s->end()) {
                chmin(ans, (*it - item.y)*5);
            }
        }
    }
    
    if (ans==INT_MAX) {
        cout << "SAFE" << endl;
    } else {
        cout << ans << endl;
    }
}



Measured memory_bytes: 117720