#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define mp make_pair
#define pb push_back
#define ip pair<ll,ll>
#define ff first
#define ss second
#define MAX 100005
const ll MOD = 1000000007;

int main(){
	ios_base::sync_with_stdio(0); cin.tie(0);

	int n;
	cin>>n;

	vector<ip> v[4];
	for(int i=0;i<n;i++){
		int x,y;
		char u;
		cin>>x>>y>>u;
		if(u == 'U')
			v[0].pb({x,y});
		else if(u == 'R')
			v[1].pb({x,y});
		else if(u == 'D')
			v[2].pb({x,y});
		else
			v[3].pb({x,y});
	}

	ll best = -1;
	map<int,vector<ip> > f;
	map<int,vector<ip> >::iterator it;
	for(ip z: v[0])
		f[z.ff].pb({z.ss,0});
	for(ip z: v[2])
		f[z.ff].pb({z.ss,2});
		
	for(it = f.begin(); it != f.end(); it++){
		vector<ip> vec = it->ss;
		sort(vec.begin(),vec.end());

		ip prev = mp(-1,-1);
		for(ip z: vec){
			if(prev.ss == -1)
				prev = z;
			else{
				if(prev.ss == 0 && z.ss == 2){
					if(best == -1)
						best = 5*(z.ff-prev.ff);
					else
						best = min(best,5*(z.ff-prev.ff));
				}
				prev = z;
			}
		}
	}

	f.clear();

	for(ip z: v[1])
		f[z.ss].pb({z.ff,0});
	for(ip z: v[3])
		f[z.ss].pb({z.ff,2});
		
	for(it = f.begin(); it != f.end(); it++){
		vector<ip> vec = it->ss;
		sort(vec.begin(),vec.end());

		ip prev = mp(-1,-1);
		for(ip z: vec){
			if(prev.ss == -1)
				prev = z;
			else{
				if(prev.ss == 0 && z.ss == 2){
					if(best == -1)
						best = 5*(z.ff-prev.ff);
					else
						best = min(best,5*(z.ff-prev.ff));
				}
				prev = z;
			}
		}
	}

	f.clear();

	for(ip z: v[1])
		f[z.ff+z.ss].pb({z.ff,0});
	for(ip z: v[0])
		f[z.ff+z.ss].pb({z.ff,2});

	for(it = f.begin(); it != f.end(); it++){
		vector<ip> vec = it->ss;
		sort(vec.begin(),vec.end());

		ip prev = mp(-1,-1);
		for(ip z: vec){
			if(prev.ss == -1)
				prev = z;
			else{
				if(prev.ss == 0 && z.ss == 2){
					if(best == -1)
						best = 10*(z.ff-prev.ff);
					else
						best = min(best,10*(z.ff-prev.ff));
				}
				prev = z;
			}
		}
	}

	f.clear();

	for(ip z: v[0])
		f[z.ss-z.ff].pb({z.ff,0});
	for(ip z: v[3])
		f[z.ss-z.ff].pb({z.ff,2});

	for(it = f.begin(); it != f.end(); it++){
		vector<ip> vec = it->ss;
		sort(vec.begin(),vec.end());

		ip prev = mp(-1,-1);
		for(ip z: vec){
			if(prev.ss == -1)
				prev = z;
			else{
				if(prev.ss == 0 && z.ss == 2){
					if(best == -1)
						best = 10*(z.ff-prev.ff);
					else
						best = min(best,10*(z.ff-prev.ff));
				}
				prev = z;
			}
		}
	}

	f.clear();

	for(ip z: v[1])
		f[z.ff-z.ss].pb({z.ff,0});
	for(ip z: v[2])
		f[z.ff-z.ss].pb({z.ff,2});

	for(it = f.begin(); it != f.end(); it++){
		vector<ip> vec = it->ss;
		sort(vec.begin(),vec.end());

		ip prev = mp(-1,-1);
		for(ip z: vec){
			if(prev.ss == -1)
				prev = z;
			else{
				if(prev.ss == 0 && z.ss == 2){
					if(best == -1)
						best = 10*(z.ff-prev.ff);
					else
						best = min(best,10*(z.ff-prev.ff));
				}
				prev = z;
			}
		}
	}

	f.clear();

	for(ip z: v[2])
		f[z.ss+z.ff].pb({z.ff,0});
	for(ip z: v[3])
		f[z.ss+z.ff].pb({z.ff,2});

	for(it = f.begin(); it != f.end(); it++){
		vector<ip> vec = it->ss;
		sort(vec.begin(),vec.end());

		ip prev = mp(-1,-1);
		for(ip z: vec){
			if(prev.ss == -1)
				prev = z;
			else{
				if(prev.ss == 0 && z.ss == 2){
					if(best == -1)
						best = 10*(z.ff-prev.ff);
					else
						best = min(best,10*(z.ff-prev.ff));
				}
				prev = z;
			}
		}
	}

	if(best == -1)
		cout<<"SAFE\n";
	else
		cout<<best<<"\n";
}

Measured memory_bytes: 27892