#include <bits/stdc++.h>

#define ll long long
using namespace std;

int getBest(vector<int> &P, vector<int> &Q)
{
	int best = 10000000;
	int Pi = 0;
	int Qi = 0;
	int Pprev = -best;
	int Qprev = Pprev;
	while(Pi+Qi!=P.size()+Q.size())
	{
		if(Qi==Q.size() || (Pi<P.size() && P[Pi]<Q[Qi]))
		{
			Pprev = P[Pi];
			++Pi;
		}
		else
		{
			best = min(best, Q[Qi]-Pprev);
			Qprev = Q[Qi];
			++Qi;
		}
	}
	return best;
}
void solve()
{
	int N;
	cin>>N;
	vector<pair<pair<int,int>,char>> A(N);
	for(auto &a: A)
		cin>>a.first.first>>a.first.second>>a.second;
	map<int,vector<int>> SU, SD, SL, SR;
	map<int,vector<int>> DU, DD, DL, DR;
	map<int,vector<int>> U, D, L, R;
	for(int i=0; i<N; ++i)
	{

		auto &a = A[i].first;
		if(A[i].second == 'U')
		{
			SU[a.first+a.second].push_back(a.first-a.second);
			DU[a.first-a.second].push_back(a.first+a.second);
			U[a.first].push_back(a.second);
		}
		else if(A[i].second == 'D')
		{
			SD[a.first+a.second].push_back(a.first-a.second);
			DD[a.first-a.second].push_back(a.first+a.second);
			D[a.first].push_back(a.second);
		}
		else if(A[i].second == 'L')
		{
			SL[a.first+a.second].push_back(a.first-a.second);
			DL[a.first-a.second].push_back(a.first+a.second);
			L[a.second].push_back(a.first);
		}
		else if(A[i].second == 'R')
		{
			SR[a.first+a.second].push_back(a.first-a.second);
			DR[a.first-a.second].push_back(a.first+a.second);
			R[a.second].push_back(a.first);
		}
	}
	for(auto &a: SU)
		sort(a.second.begin(),a.second.end());
	for(auto &a: SD)
		sort(a.second.begin(),a.second.end());
	for(auto &a: SL)
		sort(a.second.begin(),a.second.end());
	for(auto &a: SR)
		sort(a.second.begin(),a.second.end());

	for(auto &a: DU)
		sort(a.second.begin(),a.second.end());
	for(auto &a: DD)
		sort(a.second.begin(),a.second.end());
	for(auto &a: DL)
		sort(a.second.begin(),a.second.end());
	for(auto &a: DR)
		sort(a.second.begin(),a.second.end());

	for(auto &a: U)
		sort(a.second.begin(),a.second.end());
	for(auto &a: D)
		sort(a.second.begin(),a.second.end());
	for(auto &a: L)
		sort(a.second.begin(),a.second.end());
	for(auto &a: R)
		sort(a.second.begin(),a.second.end());

	int best = 1000000;
	for(auto &a: SD)
	{
		auto b = SL.find(a.first);
		if(b!=SL.end())
		{
			best = min(best,getBest(a.second,b->second));
		}
	}
	//cerr<<2<<" "<<best<<endl;
	for(auto &a: SU)
	{
		auto b = SR.find(a.first);
		if(b!=SR.end())
		{
			best = min(best,getBest(b->second,a.second));
		}
	}
	//cerr<<3<<" "<<best<<endl;
	for(auto &a: DD)
	{
		auto b = DR.find(a.first);
		if(b!=DR.end())
		{
			best = min(best,getBest(b->second,a.second));
		}
	}
	//cerr<<4<<" "<<best<<endl;
	for(auto &a: DU)
	{
		auto b = DL.find(a.first);
		if(b!=DL.end())
		{
			best = min(best,getBest(a.second,b->second));
		}
	}
	
	//cerr<<5<<" "<<best<<endl;
	for(auto &a: R)
	{
		auto b = L.find(a.first);
		if(b!=L.end())
		{
			best = min(best,getBest(a.second,b->second));
		}
	}
	//cerr<<6<<" "<<best<<endl;
	for(auto &a: U)
	{
		auto b = D.find(a.first);
		if(b!=D.end())
		{
			best = min(best,getBest(a.second,b->second));
		}
	}
	if(best < 1000000)
		cout<<best*5<<endl;
	else
	{
		cout<<"SAFE"<<endl;
	}
	
}


int main()
{
	//xyz();
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	//int T;
	//cin>>T;
	//while(T--)
		solve();
		
	return 0;
}


Measured memory_bytes: 71148