#include<bits/stdc++.h>

using namespace std;

#define int long long

struct plane{
	int x, y, d;
};

typedef pair<int, int> ii;

signed main(){
	int n; cin>>n;
	vector<plane> a(n);
	for(int i = 0; i < n; i++){
		char dir; 
		cin>>a[i].x>>a[i].y>>dir;
		vector<char> lol = {'U', 'R', 'D', 'L'};
		a[i].d = find(lol.begin(), lol.end(), dir) - lol.begin();
	}
	
	bool coll = false;
	int time = INT_MAX;
	{
		map<int, vector<ii>> ru;
		for(int i = 0; i < n; i++){
			if(a[i].d == 1){
				ru[a[i].x + a[i].y].push_back(make_pair(a[i].x, a[i].y));	
			}
		}
		
		for(auto& c : ru){
			sort(c.second.begin(), c.second.end());
		}
		
		
		for(int i = 0; i < n; i++){
			if(a[i].d == 0){
				int v = a[i].x + a[i].y;
				
				auto& vv = ru[v];
				if(vv.size() == 0) continue;
			
				
				auto it = lower_bound(vv.begin(), vv.end(), make_pair(a[i].x, a[i].y));
				if(it != vv.begin()){
					coll = true;
					auto prev = it; prev--;
					time = min(time, abs(prev->first - a[i].x));
				}
		
			}
		}
	}
	
	
	{
		map<int, vector<ii>> ru;
		for(int i = 0; i < n; i++){
			if(a[i].d == 3){
				ru[a[i].x - a[i].y].push_back(make_pair(a[i].x, a[i].y));	
			}
		}
		
		for(auto& c : ru){
			sort(c.second.begin(), c.second.end());
		}
		
		
		for(int i = 0; i < n; i++){
			if(a[i].d == 0){
				int v = a[i].x - a[i].y;
				
				auto& vv = ru[v];
				if(vv.size() == 0) continue;
			
				
				auto it = lower_bound(vv.begin(), vv.end(), make_pair(a[i].x, a[i].y));
				
				if(it != vv.end()){
						coll = true;
					time = min(time, abs(it->first - a[i].x));
				}
			}
		}
	}
	
	{
		map<int, vector<ii>> ru;
		for(int i = 0; i < n; i++){
			if(a[i].d == 1){
				ru[a[i].x - a[i].y].push_back(make_pair(a[i].x, a[i].y));	
			}
		}
		
		for(auto& c : ru){
			sort(c.second.begin(), c.second.end());
		}
		
		
		for(int i = 0; i < n; i++){
			if(a[i].d == 2){
				int v = a[i].x - a[i].y;
				
				auto& vv = ru[v];
				if(vv.size() == 0) continue;
				
				
				auto it = lower_bound(vv.begin(), vv.end(), make_pair(a[i].x, a[i].y));
				if(it != vv.begin()){
					coll = true;
					auto prev = it; prev--;
					time = min(time, abs(prev->first - a[i].x));
				}
			}
		}
	}
	{
		map<int, vector<ii>> ru;
		for(int i = 0; i < n; i++){
			if(a[i].d == 3){
				ru[a[i].x + a[i].y].push_back(make_pair(a[i].x, a[i].y));	
			}
		}
		
		for(auto& c : ru){
			sort(c.second.begin(), c.second.end());
		}
		
		
		for(int i = 0; i < n; i++){
			if(a[i].d == 2){
				int v = a[i].x + a[i].y;
				
				auto& vv = ru[v];
				if(vv.size() == 0) continue;
			
				
				auto it = lower_bound(vv.begin(), vv.end(), make_pair(a[i].x, a[i].y));
				if(it != vv.end()){
						coll = true;
					time = min(time, abs(it->first - a[i].x));
				}
			}
		}
	}
	
	if(coll) time *= 10;

	{
		map<int, set<int> > left;
		for(int i = 0; i < n; i++){
			if(a[i].d == 3){
				left[a[i].y].insert(a[i].x);
			}
		}
		
		for(int i = 0; i < n; i++){
			if(a[i].d == 1){
				set<int>& ss = left[a[i].y];
				if(ss.size() == 0) continue;
			
				auto it = ss.lower_bound(a[i].x);
				if(it != ss.end()){
					coll = true;
					time = min(time, 5 * abs(a[i].x - *it));
				}
			}
		}
	}

	{
		map<int, set<int> > left;
		for(int i = 0; i < n; i++){
			if(a[i].d == 2){
				left[a[i].x].insert(a[i].y);
			}
		}
		
		for(int i = 0; i < n; i++){
			if(a[i].d == 0){
				set<int>& ss = left[a[i].x];
				if(ss.size() == 0) continue;
				auto it = ss.lower_bound(a[i].y);
				if(it != ss.end()){
					coll = true;
					time = min(time, 5 * abs(a[i].y - *it));
				}
			}
		}
	}
	
	if(!coll){
		cout<<"SAFE"<<endl;
	}else{
		cout<<time<<endl;
	}
	//cout<<time<<endl;
}

Measured memory_bytes: 35564