/**
 *    author:  otera    
**/
#include<iostream>
#include<string> 
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm> 
#include<functional>
#include<iomanip>
#include<queue>
#include<deque>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
using namespace std;

#define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
typedef long double ld;
const int inf=1e9+7;
const ll INF=1LL<<60 ;
const ll mod=1e9+7 ;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<int, int> P;
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(),c.end()
#define pb push_back
#define debug(x)  cerr << #x << " = " << (x) << endl;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};

int n;
vector<int> x, y, d;

int ctoi(char c) {
    if(c == 'U') return 0;
    if(c == 'R') return 1;
    if(c == 'D') return 2;
    return 3; 
}

void rotate() {
    rep(i, n) {
        int cx = y[i], cy = 200000 - x[i], cd = (d[i] + 1) % 4;
        x[i] = cx, y[i] = cy, d[i] = cd;
    }
}

void solve() {
	cin >> n;
    x.resize(n), y.resize(n), d.resize(n);
    rep(i, n) {
        cin >> x[i] >> y[i];
        char a; cin >> a; d[i] = ctoi(a);
    }
    int ret = INF;
    rep(_, 4) {
        // xが一致
        vector<vector<int>> p1(200001, vector<int>());
        vector<vector<int>> p3(200001, vector<int>());
        rep(i, n) {
            if(d[i] == 1) p1[y[i]].pb(x[i]);
            if(d[i] == 3) p3[y[i]].pb(x[i]);
        }
        for(int i = 0; i <= 200000; ++ i) {
            sort(all(p1[i])), sort(all(p3[i]));
            for(int a: p1[i]) {
                auto ite = lower_bound(all(p3[i]), a);
                if(ite == p3[i].end()) continue;
                chmin(ret, abs(*ite - a) * 5);
            }
        }
        // x - yが一致
        vector<vector<int>> q1(400001, vector<int>());
        vector<vector<int>> q2(400001, vector<int>());
        rep(i, n) {
            if(d[i] == 1) q1[x[i] - y[i] + 200000].pb(x[i]);
            if(d[i] == 2) q2[x[i] - y[i] + 200000].pb(x[i]);
        }
        for(int i = 0; i <= 400000; ++ i) {
            sort(all(q1[i])), sort(all(q2[i]));
            for(int a: q1[i]) {
                auto ite = lower_bound(all(q2[i]), a);
                if(ite == q2[i].end()) continue;
                chmin(ret, abs(*ite - a) * 10);
            }
        }
        rotate();
    }
    if(ret == INF) cout << "SAFE" << endl;
    else cout << ret << endl;
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(10);
	//int t; cin >> t; rep(i, t)solve();
	solve();
    return 0;
}

Measured memory_bytes: 49272