#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
#define all(x) (x).begin(),(x).end()
const int INF=1e9+5;
const int N=2e5+5;
const int mod=1e9+7;


int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n;
	cin>>n;
	vector<pair<int,int>> U,L,R,D;
	for(int i=0;i<n;++i){
		int x,y;
		char dir;
		cin>>x>>y>>dir;
		if(dir=='U') U.emplace_back(x,y);
		if(dir=='D') D.emplace_back(x,y);
		if(dir=='R') R.emplace_back(x,y);
		if(dir=='L') L.emplace_back(x,y);
	}
	int ans=1e9;
	//~ U vs D
	vector<vector<int>> line1(200005);
	for(int i=0;i<(int)U.size();++i){
		int x=U[i].first;
		int y=U[i].second;
		line1[x].push_back(y);
	}
	for(int i=0;i<200005;++i){
		if(!(int)line1[i].size()) continue;
		sort(all(line1[i]));
	}
	for(int i=0;i<(int)D.size();++i){
		int x=D[i].first;
		int y=D[i].second;
		if(!(int)line1[x].size()) continue;
		int pos=lower_bound(all(line1[x]),y)-line1[x].begin()-1;
		if(pos<0) continue;
		ans=min(ans,(y-line1[x][pos])*5);
	}
	//~ L vs R
	vector<vector<int>> line2(200005);
	for(int i=0;i<(int)R.size();++i){
		int x=R[i].first;
		int y=R[i].second;
		line2[y].push_back(x);
	}
	for(int i=0;i<200005;++i){
		if(!(int)line2[i].size()) continue;
		sort(all(line2[i]));
	}
	for(int i=0;i<(int)L.size();++i){
		int x=L[i].first;
		int y=L[i].second;
		if(!(int)line2[y].size()) continue;
		int pos=lower_bound(all(line2[y]),x)-line2[y].begin()-1;
		if(pos<0) continue;
		ans=min(ans,(x-line2[y][pos])*5);
	}
	//~ U vs R
	vector<vector<int>> cross1(400005);
	for(int i=0;i<(int)U.size();++i){
		int x=U[i].first;
		int y=U[i].second;
		cross1[x+y].push_back(y);
	}
	for(int i=0;i<400005;++i){
		if(!(int)cross1[i].size()) continue;
		sort(all(cross1[i]));
	}
	for(int i=0;i<(int)R.size();++i){
		int x=R[i].first;
		int y=R[i].second;
		if(!(int)cross1[x+y].size()) continue;
		int pos=lower_bound(all(cross1[x+y]),y)-cross1[x+y].begin()-1;
		if(pos<0) continue;
		ans=min(ans,(y-cross1[x+y][pos])*10);
	}
	//~ U vs L 
	vector<vector<int>> cross2(400005);
	for(int i=0;i<(int)U.size();++i){
		int x=U[i].first;
		int y=U[i].second;
		cross2[200001-x+y].push_back(y);
	}
	for(int i=0;i<400005;++i){
		if(!(int)cross2[i].size()) continue;
		sort(all(cross2[i]));
	}
	for(int i=0;i<(int)L.size();++i){
		int x=L[i].first;
		int y=L[i].second;
		if(!(int)cross2[200001-x+y].size()) continue;
		int pos=lower_bound(all(cross2[200001-x+y]),y)-cross2[200001-x+y].begin()-1;
		if(pos<0) continue;
		ans=min(ans,(y-cross2[200001-x+y][pos])*10);
	}
	//~ D vs R 
	vector<vector<int>> cross3(400005);
	for(int i=0;i<(int)R.size();++i){
		int x=R[i].first;
		int y=R[i].second;
		cross3[200001-x+y].push_back(y);
	}
	for(int i=0;i<400005;++i){
		if(!(int)cross3[i].size()) continue;
		sort(all(cross3[i]));
	}
	for(int i=0;i<(int)D.size();++i){
		int x=D[i].first;
		int y=D[i].second;
		if(!(int)cross3[200001-x+y].size()) continue;
		int pos=lower_bound(all(cross3[200001-x+y]),y)-cross3[200001-x+y].begin()-1;
		if(pos<0) continue;
		ans=min(ans,(y-cross3[200001-x+y][pos])*10);
	}
	//~ D vs L CONTINUE IN HERE
	vector<vector<int>> cross4(400005);
	for(int i=0;i<(int)L.size();++i){
		int x=L[i].first;
		int y=L[i].second;
		cross4[x+y].push_back(y);
	}
	for(int i=0;i<400005;++i){
		if(!(int)cross4[i].size()) continue;
		sort(all(cross4[i]));
	}
	for(int i=0;i<(int)D.size();++i){
		int x=D[i].first;
		int y=D[i].second;
		if(!(int)cross4[x+y].size()) continue;
		int pos=lower_bound(all(cross4[x+y]),y)-cross4[x+y].begin()-1;
		if(pos<0) continue;
		ans=min(ans,(y-cross4[x+y][pos])*10);
	}
	if(ans==1e9) cout<<"SAFE";
	else cout<<ans;
	return 0;
}


Measured memory_bytes: 63328