#include <bits/stdc++.h>
#include <string>
#include <cmath>
#include <sstream>
//#include <unordered_map>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORD(i, a, b) for(int i = (a-1); i >= (b); i--)
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define ff first
#define ss second
typedef long long int ll;
using namespace std;
//ll mod=1000000007;
//ll mod=67280421310721;
ll mod=998244353;
ll INF=1e18;
ll po(ll a,ll b)
{
	ll re=1;
	while(b)
	{
		if(b%2==1)
		{
			re=(re*a);
//			re%=mod;
		}
		a=(a*a);
//		a%=mod;
		b>>=1;
	}
	return re;
}
bool vis[20];
bool re;
bool gra[20][20];
int d;
void dfs(int i )
{
	if(i==d)
	{
		re=1;
		return;
	}
	vis[i]=1;
	FOR(j,0,20)
	{
		if(gra[i][j] && vis[j]==0)
		dfs(j);
		if(re)
		return;
	}
	return;
}
bool c1(pair<int,int> a,pair<int,int> b)
{
	if(a.ff==b.ff)
	return a.ss<b.ss;
	return a.ff<b.ff;
}
bool c2(pair<int,int> a,pair<int,int> b)
{
	if(a.ss==b.ss)
	return a.ff<b.ff;
	return a.ss<b.ss;
}
bool c3(pair<int,int> a,pair<int,int> b)
{
	if((a.ff+a.ss)==(b.ff+b.ss))
	return (a.ff-a.ss)<(b.ff-b.ss);
	return (a.ff+a.ss)<(b.ff+b.ss);
}

bool c4(pair<int,int> a,pair<int,int> b)
{
	if((a.ff-a.ss)==(b.ff-b.ss))
	return (a.ff+a.ss)<(b.ff+b.ss);
	return (a.ff-a.ss)<(b.ff-b.ss);
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int test=1;
//	cin>>test;
	while(test--)
	{
		int n;
		cin>>n;
		vector<pair<int,int> > u,d,l,r;
		int x,y;
		ll ans=mod,ti;
		char t;
		FOR(i,0,n)
		{
			cin>>x>>y>>t;
			if(t=='U')
			{
				u.pb(mp(x,y));
			}
			else if(t=='D')
			{
				d.pb(mp(x,y));
			}
			else if(t=='L')
			{
				l.pb(mp(x,y));
			}
			else
			{
				r.pb(mp(x,y));
			}
		}
		int ds=d.size(),us=u.size(),rs=r.size(),ls=l.size();
		sort(u.begin(),u.end(),c1);
		sort(d.begin(),d.end(),c1);
		int j=0;
		FOR(i,0,ds)
		{
			while(j<us && c1(u[j],d[i]))
			j++;
			if(j>0)
			{
				if(u[j-1].ff==d[i].ff)
				{
					ti=d[i].ss-u[j-1].ss;
					ti*=5;
					ans=min(ans,ti);
				}
			}
		}
		sort(l.begin(),l.end(),c2);
		sort(r.begin(),r.end(),c2);
		j=0;
		FOR(i,0,ls)
		{
			while(j<rs && c2(r[j],l[i]))
			j++;
			if(j>0)
			{
				if(r[j-1].ss==l[i].ss)
				{
					ti=l[i].ff-r[j-1].ff;
					ti*=5;
					ans=min(ans,ti);
				}
			}
		}
		sort(u.begin(),u.end(),c3);
		sort(d.begin(),d.end(),c3);
		sort(l.begin(),l.end(),c3);
		sort(r.begin(),r.end(),c3);
		j=0;
		FOR(i,0,us)
		{
			while(j<rs && c3(r[j],u[i]))
			j++;
			if(j>0)
			{
				if((r[j-1].ff+r[j-1].ss)==(u[i].ff+u[i].ss))
				{
					ti=u[i].ff-r[j-1].ff;
					ti*=10;
					ans=min(ans,ti);
				}
			}
		}
		j=0;
		FOR(i,0,ls)
		{
			while(j<ds && c3(d[j],l[i]))
			j++;
			if(j>0)
			{
				if((d[j-1].ff+d[j-1].ss)==(l[i].ff+l[i].ss))
				{
					ti=l[i].ff-d[j-1].ff;
					ti*=10;
					ans=min(ans,ti);
				}
			}
		}
		sort(u.begin(),u.end(),c4);
		sort(d.begin(),d.end(),c4);
		sort(l.begin(),l.end(),c4);
		sort(r.begin(),r.end(),c4);
		j=0;
		FOR(i,0,ds)
		{
			while(j<rs && c4(r[j],d[i]))
			j++;
			if(j>0)
			{
				if((r[j-1].ff-r[j-1].ss)==(d[i].ff-d[i].ss))
				{
					ti=d[i].ff-r[j-1].ff;
					ti*=10;
					ans=min(ans,ti);
				}
			}
		}
		j=0;
		FOR(i,0,ls)
		{
			while(j<us && c4(u[j],l[i]))
			j++;
			if(j>0)
			{
				if((u[j-1].ff-u[j-1].ss)==(l[i].ff-l[i].ss))
				{
					ti=l[i].ff-u[j-1].ff;
					ti*=10;
					ans=min(ans,ti);
				}
			}
		}
		if(ans==mod)
		{
			cout<<"SAFE";
		}
		else
		cout<<ans;
	}
	return 0;
}

Measured memory_bytes: 5784