#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,char> P;
typedef pair<int,int> Pi;
#define rep(i,n) for(ll i=0;i<n;i++)
#define FOR(i,a,b) for(ll i=a;i<b;i++)
#define fi first
#define se second
#define endl "\n"

template<typename T> inline bool chmax(T &a, T b){if(a<b){a=b;return true;}return false;}
template<typename T> inline bool chmin(T &a, T b){if(a>b){a=b;return true;}return false;}
template<typename T> ostream& operator<<(ostream& s,const complex<T>& d) {return s<<"("<<d.real()<<", "<<d.imag()<< ")";}
template<typename T1, typename T2> ostream& operator<<(ostream& s,const pair<T1,T2>& d) {return s<<"("<<d.first<<", "<<d.second<<")";}
template<typename T> ostream& operator<<(ostream& s, const vector<T>& d){int len=d.size();rep(i,len){s<<d[i];if(i<len-1) s<<" ";}return s;}
template<typename T> ostream& operator<<(ostream& s,const vector<vector<T>>& d){int len=d.size();rep(i,len){s<<d[i]<<endl;}return s;}
template<typename T> ostream& operator<<(ostream& s,const set<T>& v){s<<"{ ";for(auto itr=v.begin();itr!=v.end();++itr) {if (itr!=v.begin()) {s<< ", ";}s<<(*itr);}s<<" }";return s;}
template<typename T> ostream& operator<<(ostream& s,const multiset<T>& v){s<<"{ ";for(auto itr=v.begin();itr!=v.end();++itr) {if (itr!=v.begin()) {s<< ", ";}s<<(*itr);}s<<" }";return s;}
template<typename T1, typename T2> ostream& operator<<(ostream& s,const map<T1,T2>& m){s<<"{"<<endl;for(auto itr=m.begin();itr!=m.end();++itr){s<<" "<<(*itr).first<<" : "<<(*itr).second<<endl;}s<<"}"<<endl;return s;}

const ll mod=1'000'000'007;
const ll inf=1'000'000'000'000'000'00;
const int INF=1'000'000'000;
const double EPS=1e-10;
const double PI=acos(-1);

struct state{
	ll x,y;
	char u;
};

int main(){
	cin.tie(0);ios::sync_with_stdio(false);
	ll n;
	cin>>n;
	vector<state> g(n);
	rep(i,n){
		cin>>g[i].x>>g[i].y>>g[i].u;
	}
	ll m=400010,ans=inf;
	vector<vector<P>> vec(m);
	//UとDの衝突
	for(auto [x,y,u]:g){
		vec[x].push_back(P(y,u));
	}
	for(auto &v:vec){
		sort(begin(v),end(v));
		vector<ll> U,D;
		for(auto [y,u]:v){
			if(u=='U') U.push_back(y);
			else if(u=='D') D.push_back(y);
		}
		for(auto y:U){
			auto itr=upper_bound(begin(D),end(D),y);
			if(itr!=end(D)){
				chmin(ans,(*itr-y)*5);
			}
		}
	}
	//LとRの衝突
	for(auto &v:vec) v.clear();
	for(auto [x,y,u]:g){
		vec[y].push_back(P(x,u));
	}
	for(auto &v:vec){
		sort(begin(v),end(v));
		vector<ll> L,R;
		for(auto [x,u]:v){
			if(u=='L') L.push_back(x);
			else if(u=='R') R.push_back(x);
		}
		for(auto x:R){
			auto itr=upper_bound(begin(L),end(L),x);
			if(itr!=end(L)){
				chmin(ans,(*itr-x)*5);
			}
		}
	}
	//RとUの衝突,DとLの衝突
	for(auto &v:vec) v.clear();
	for(auto [x,y,u]:g){
		vec[x+y].push_back(P(y,u));
	}
	for(auto &v:vec){
		sort(begin(v),end(v));
		vector<ll> U,R,D,L;
		for(auto [y,u]:v){
			if(u=='U') U.push_back(y);
			else if(u=='R') R.push_back(y);
			else if(u=='D') D.push_back(y);
			else L.push_back(y);
		}
		for(auto y:U){
			auto itr=upper_bound(begin(R),end(R),y);
			if(itr!=end(R)){
				chmin(ans,(*itr-y)*10);
			}
		}
		for(auto y:L){
			auto itr=upper_bound(begin(D),end(D),y);
			if(itr!=end(D)){
				chmin(ans,(*itr-y)*10);
			}
		}
	}
	//RとDの衝突,LとUの衝突
	for(auto &v:vec) v.clear();
	for(auto [x,y,u]:g){
		if(x-y>=0) vec[x-y].push_back(P(y,u));
	}
	for(auto &v:vec){
		sort(begin(v),end(v));
		vector<ll> U,R,D,L;
		for(auto [y,u]:v){
			if(u=='U') U.push_back(y);
			else if(u=='R') R.push_back(y);
			else if(u=='D') D.push_back(y);
			else L.push_back(y);
		}
		for(auto y:R){
			auto itr=upper_bound(begin(D),end(D),y);
			if(itr!=end(D)){
				chmin(ans,(*itr-y)*10);
			}
		}
		for(auto y:U){
			auto itr=upper_bound(begin(L),end(L),y);
			if(itr!=end(L)){
				chmin(ans,(*itr-y)*10);
			}
		}
	}
	for(auto &v:vec) v.clear();
	for(auto [x,y,u]:g){
		if(y-x>=0) vec[y-x].push_back(P(y,u));
	}
	for(auto &v:vec){
		sort(begin(v),end(v));
		vector<ll> U,R,D,L;
		for(auto [y,u]:v){
			if(u=='U') U.push_back(y);
			else if(u=='R') R.push_back(y);
			else if(u=='D') D.push_back(y);
			else L.push_back(y);
		}
		sort(begin(U),end(U)),sort(begin(R),end(R)),sort(begin(D),end(D)),sort(begin(L),end(L));
		for(auto y:R){
			auto itr=upper_bound(begin(D),end(D),y);
			if(itr!=end(D)){
				chmin(ans,(*itr-y)*10);
			}
		}
		for(auto y:U){
			auto itr=upper_bound(begin(L),end(L),y);
			if(itr!=end(L)){
				chmin(ans,(*itr-y)*10);
			}
		}
	}
	if(ans!=inf) cout<<ans<<endl;
	else cout<<"SAFE"<<endl;
}

Measured memory_bytes: 32772