#include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

int n;
int x[200005],y[200005],u[200005];
vector<int> vec[2][2][600005];
vector<int> vec2[4][600005];
string s="RLUD";
int B=200000;

int main(void){
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		char c;
		scanf(" %d %d %c",&x[i],&y[i],&c);
		for(int j=0;j<4;j++){
			if(c==s[j]){
				u[i]=j;
			}
		}
		if(u[i]==0){
			vec[0][0][B+x[i]+y[i]].push_back(x[i]);
			vec[0][1][B+x[i]-y[i]].push_back(x[i]);
			vec2[0][y[i]].push_back(x[i]);
		}
		if(u[i]==1){
			vec[1][0][B+x[i]-y[i]].push_back(x[i]);
			vec[1][1][B+x[i]+y[i]].push_back(x[i]);
		}
		if(u[i]==2){
			vec2[1][x[i]].push_back(y[i]);
		}
	}
	for(int i=0;i<2;i++){
		for(int j=0;j<2;j++){
			for(int k=0;k<=600000;k++){
				sort(vec[i][j][k].begin(),vec[i][j][k].end());
				sort(vec2[i][k].begin(),vec2[i][k].end());
			}
		}
	}
	int ans=1<<25;
	for(int i=0;i<n;i++){
		if(u[i]==1){
			if(vec2[0][y[i]].size()==0)continue;
			int pos=lower_bound(vec2[0][y[i]].begin(),vec2[0][y[i]].end(),x[i])-vec2[0][y[i]].begin();
			if(pos>0){
				pos--;
				ans=min(ans,abs(x[i]-vec2[0][y[i]][pos])*5);
			}
		}
		if(u[i]==2){
			int pos=lower_bound(vec[0][0][B+x[i]+y[i]].begin(),vec[0][0][B+x[i]+y[i]].end(),x[i])-vec[0][0][B+x[i]+y[i]].begin();
			if(pos>0){
				pos--;
				ans=min(ans,abs(x[i]-vec[0][0][B+x[i]+y[i]][pos])*10);
			}

			pos=lower_bound(vec[1][0][B+x[i]-y[i]].begin(),vec[1][0][B+x[i]-y[i]].end(),x[i])-vec[1][0][B+x[i]-y[i]].begin();
			if(pos<vec[1][0][B+x[i]-y[i]].size()){
				ans=min(ans,abs(x[i]-vec[1][0][B+x[i]-y[i]][pos])*10);
			}
		}
		if(u[i]==3){
			int pos=lower_bound(vec2[1][x[i]].begin(),vec2[1][x[i]].end(),y[i])-vec2[1][x[i]].begin();
			if(pos>0){
				pos--;
				ans=min(ans,abs(y[i]-vec2[1][x[i]][pos])*5);
			}

			pos=lower_bound(vec[0][1][B+x[i]-y[i]].begin(),vec[0][1][B+x[i]-y[i]].end(),x[i])-vec[0][1][B+x[i]-y[i]].begin();
			if(pos>0){
				pos--;
				ans=min(ans,abs(x[i]-vec[0][1][B+x[i]-y[i]][pos])*10);
			}

			pos=lower_bound(vec[1][1][B+x[i]+y[i]].begin(),vec[1][1][B+x[i]+y[i]].end(),x[i])-vec[1][1][B+x[i]+y[i]].begin();
			if(pos<vec[1][1][B+x[i]+y[i]].size()){
				ans=min(ans,abs(x[i]-vec[1][1][B+x[i]+y[i]][pos])*10);
			}
		}
		//printf("%d\n",ans);
	}
	
	if(ans==(1<<25)){
		printf("SAFE\n");
	}else{
		printf("%d\n",ans);
	}
	return 0;
}
