/*
AuThOr Gwj
*/
#include<bits/stdc++.h>
#define rb(a,b,c) for(int a=b;a<=c;++a)
#define rl(a,b,c) for(int a=b;a>=c;--a)
#define LL long long
#define IT iterator
#define PB push_back
#define II(a,b) make_pair(a,b)
#define FIR first
#define SEC second
#define FREO freopen("check.out","w",stdout)
#define rep(a,b) for(int a=0;a<b;++a)
#define KEEP while(1)
#define SRAND mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define random(a) rng()%a
#define ALL(a) a.begin(),a.end()
#define POB pop_back
#define ff fflush(stdout)
#define fastio ios::sync_with_stdio(false)
#define debug_pair(A) cerr<<A.FIR<<" "<<A.SEC<<endl;
#define biggest(A,B) A=max(A,B)
#define smallest(A,B) A=min(A,B)
#define R(a) cin>>a
#define R2(a,b) cin>>a>>b
#define x FIR.FIR
#define y FIR.SEC 
using namespace std;
const int INF=0x3f3f3f3f;
typedef pair<int,int> mp;
typedef pair<mp,mp> superpair;
int n;
int res=INF;
pair<mp,char> p[200000+20]; 
bool cmp(pair<mp,char> A ,pair<mp,char> B){
	if(A.FIR.SEC==B.FIR.SEC) return A.FIR.FIR<B.FIR.FIR;
	return A.FIR.SEC>B.FIR.SEC;
}
bool cmp1(pair<mp,char> A ,pair<mp,char> B){
	if(A.FIR.FIR==B.FIR.FIR) return A.FIR.SEC<B.FIR.SEC;
	return A.FIR.FIR<B.FIR.FIR;
}
bool cmp2(pair<mp,char> A,pair<mp,char > B){
	if(A.x+A.y==B.x+B.y) return A.x<B.x;
	return A.x+A.y<B.x+B.y;
}
bool cmp3(pair<mp,char> A,pair<mp,char > B){
	if(A.x-A.y==B.x-B.y) return A.x<B.x;
	return A.x-A.y<B.x-B.y;
}
int main(){
	fastio;
	R(n);
	rb(i,1,n){
		cin>>p[i].FIR.FIR>>p[i].FIR.SEC>>p[i].SEC;
	}
	sort(p+1,p+1+n,cmp);
	int las=-1,is=-1;
	rb(i,1,n){
		if(p[i].FIR.SEC!=is){
			is=p[i].y;
			if(p[i].SEC=='R'){
				las=p[i].x;
			}
			else las=-1;
		}
		else{
			if(las!=-1){
				if(p[i].SEC=='L'){
					res=min(res,(p[i].x-las)*5);
				}
			}
			if(p[i].SEC=='R') las=p[i].x;
		}
	}
	sort(p+1,p+1+n,cmp1);
	 las=-1,is=-1;
	rb(i,1,n){
		if(p[i].x!=is){
			is=p[i].x;
			if(p[i].SEC=='U'){
				las=p[i].y;
			}
			else las=-1;
		}
		else{
			if(las!=-1){
				if(p[i].SEC=='D'){
					res=min(res,(p[i].y-las)*5);
				}
			}
			if(p[i].SEC=='U') las=p[i].y;
		}
	}
	sort(p+1,p+1+n,cmp2);
	las=-1,is=-INF;//下 
	int las2=-1;//→ 
	rb(i,1,n){
		if(p[i].x+p[i].y!=is){
			is=p[i].x+p[i].y;
			if(p[i].SEC=='D'){
				las=p[i].x;
			}
			else las=-1;
			if(p[i].SEC=='R'){
				las2=p[i].x;
			}
			else{
				las2=-1;
			}
		}
		else{
			if(las!=-1){
				if(p[i].SEC=='L'){
					res=min(res,(p[i].x-las)*10);
				}
			}
			if(las2!=-1){
				if(p[i].SEC=='U'){
					res=min(res,(p[i].x-las2)*10);
				}
			}if(p[i].SEC=='D'){
				las=p[i].x;
			}
			if(p[i].SEC=='R'){
				las2=p[i].x;
			}
		}
	}
	sort(p+1,p+1+n,cmp3);
	las=-1,is=-INF;//下 
	las2=-1;//→ 
	rb(i,1,n){
		if(p[i].x-p[i].y!=is){
			is=p[i].x-p[i].y;
			if(p[i].SEC=='U'){
				las=p[i].x;
			}
			else las=-1;
			if(p[i].SEC=='R'){
				las2=p[i].x;
			}
			else{
				las2=-1;
			}
		}
		else{
			if(las!=-1){
				if(p[i].SEC=='L'){
					res=min(res,(p[i].x-las)*10);
				}
			}
			if(las2!=-1){
				if(p[i].SEC=='D'){
					res=min(res,(p[i].x-las2)*10);
				}
			}if(p[i].SEC=='U'){
				las=p[i].x;
			}
			if(p[i].SEC=='R'){
				las2=p[i].x;
			}
		}
	}
	if(res==INF){
		cout<<"SAFE"<<endl;
	}
	else
	cout<<res<<endl;
	return 0;
}


Measured memory_bytes: 5840