#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 1000
#else
#define SIZE 423450
#endif

int N,Q,K;

set<int> um[2][SIZE],dm[2][SIZE];
int BASE = SIZE/2;

struct Item2 {
    int x,y;
    char u;
};
vector<Item2> V;

set<int> ds[SIZE],ls[SIZE];

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


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef pair <int , int> pii;
const int MAXN = 4e5 + 10 , INF = 202020;
int ans = 1e9;
vector <pii> v[MAXN];
int x[MAXN] , y[MAXN] , t[MAXN];
int tu(char c){
	if(c == 'U') return 0;
	else if(c == 'R') return 1;
	else if(c == 'D') return 2;
	return 3;
}
void check(int m){
	for(int i = 0 ; i <= 400000 ; ++ i)
		if((int)v[i].size() > 0){
			sort(v[i].begin() , v[i].end());
			int la = -INF;
			for(int k = 0 ; k < (int)v[i].size() ; ++ k)
				if(v[i][k].second == -1){
					if(la != -INF)
						ans = min(ans , (v[i][k].first - la) * m);
				}
				else
					la = v[i][k].first;
			v[i].resize(0);
		}
}
int main(){
	int N;
	scanf("%d" , &N);
	for(int i = 1 ; i <= N ; ++ i){
		char s[2];
		scanf("%d%d%s" , &x[i] , &y[i] , s);
	//	cout << i << " " << x[i] + y[i] << " " << x[i] - y[i] << " " << t[i] << endl; 
		t[i] = tu(s[0]);
	}
	for(int i = 1 ; i <= N ; ++ i)
		if(t[i] % 2 == 0)
			v[x[i]].push_back(make_pair(y[i] , (t[i] == 0 ? 1 : -1)));
	check(5);
	for(int i = 1 ; i <= N ; ++ i)
		if(t[i] % 2 == 1)
			v[y[i]].push_back(make_pair(x[i] , (t[i] == 1 ? 1 : -1)));
	check(5);
	for(int i = 1 ; i <= N ; ++ i)
		if(t[i] == 0 || t[i] == 3)
			v[y[i] - x[i] + 200000].push_back(make_pair(x[i] , (t[i] == 0 ? 1 : -1)));
	check(10);
	for(int i = 1 ; i <= N ; ++ i)
		if(t[i] == 1 || t[i] == 2)
			v[y[i] - x[i] + 200000].push_back(make_pair(x[i] , (t[i] == 1 ? 1 : -1)));
	check(10);
	for(int i = 1 ; i <= N ; ++ i)
		if(t[i] == 0 || t[i] == 1)
			v[x[i] + y[i]].push_back(make_pair(x[i] , (t[i] == 1 ? 1 : -1)));
	check(10);
	for(int i = 1 ; i <= N ; ++ i)
		if(t[i] == 2 || t[i] == 3)
			v[x[i] + y[i]].push_back(make_pair(x[i] , (t[i] == 2 ? 1 : -1)));
	check(10);
	if(ans == 1e9)
		cout << "SAFE" << endl;
	else
		cout << ans << endl;
	return 0;
}