#include <bits/stdc++.h>
#define be(v) (v).begin(),(v).end()
#define pb(q) push_back(q)
typedef long long ll;
using namespace std;
const ll mod=1000000007, INF=(1LL<<60);
#define doublecout(a) cout<<fixed<<setprecision(10)<<a<<endl;

int main() {
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(false);
    int n, inf = 1000000000, N = 200001;
    cin >> n;
    int ans = inf;
    vector<set<int> > U(N), D(N), R(N), L(N);
    vector<vector<set<int> > > u(2, vector<set<int> > (N*2));
    vector<vector<set<int> > > d(2, vector<set<int> > (N*2));
    vector<vector<set<int> > > r(2, vector<set<int> > (N*2));
    vector<vector<set<int> > > l(2, vector<set<int> > (N*2));
    int a, b;
    char c;
    for(int i=0;i<n;i++){
    	cin >> a >> b >> c;
    	if(c == 'U'){
    		U[a].insert(b);
    		u[0][b - a + N].insert(a);
    		u[1][a + b].insert(a);
    	}
    	if(c == 'D'){
    		D[a].insert(b);
    		d[0][b - a + N].insert(a);
    		d[1][a + b].insert(a);
    	}
    	if(c == 'R'){
    		R[b].insert(a);
    		r[0][b - a + N].insert(a);
    		r[1][a + b].insert(a);
    	}
    	if(c == 'L'){
    		L[b].insert(a);
    		l[0][b - a + N].insert(a);
    		l[1][a + b].insert(a);
    	}
    }
    for(int i=0;i<N;i++){
    	for(auto& j: U[i]){
    		if(D[i].lower_bound(j) == D[i].end()) continue;
    		ans = min(ans, (*(D[i].lower_bound(j)) - j) * 5);
    	}
    	for(auto& j: R[i]){
    		if(L[i].lower_bound(j) == L[i].end()) continue;
    		ans = min(ans, (*(L[i].lower_bound(j)) - j)* 5);
    	}
    }
    for(int i=0;i<N*2;i++){    	
    	for(auto& j : d[0][i]){
    		auto itr = r[0][i].lower_bound(j);
    		if(itr != r[0][i].begin()){
    			itr--;
    			ans = min(ans, (j - *itr) * 10);
    		}
    	}
    	for(auto& j : d[1][i]){
    		auto itr = l[1][i].lower_bound(j);
    		if(itr != l[1][i].end()){
    			ans = min(ans, (*itr - j) * 10);
    		}
    	}
    	for(auto& j : u[0][i]){
    		auto itr = l[0][i].lower_bound(j);
    		if(itr != l[0][i].end()){
    			ans = min(ans, (*itr - j) * 10);
    		}
    	}
    	for(auto& j : u[1][i]){
    		auto itr = r[1][i].lower_bound(j);
    		if(itr != r[1][i].begin()){
    			itr--;
    			ans = min(ans, (j - *itr) * 10);
    		}
    	}
    }
    if(ans == inf) cout << "SAFE" << endl;
    else cout << ans << endl;

    return 0;
}
