#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <unordered_map>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <assert.h>
#include <unordered_map>
#include <fstream>
#include <time.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 2020000;
ll dy[8] = {1,-1,0,0,1,-1,1,-1};
ll dx[8] = {0,0,1,-1,1,-1,-1,1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    rep(i,a.size()) cout << a[i] << " ";
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){cout << a << " " << b << "\n";}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << a << " " << b << " " << c << "\n";
}
const int mod = 1e9 + 7;
//const int mod = 998244353;

bool aa(P a, P b){
	if(a.first+a.second != b.first+b.second){
		return a.first+a.second < b.first+b.second;
	}else{
		a.second < b.second;
	}
}
bool bb(P a, P b){
	if(a.second-a.first != b.second-b.first){
		return a.second-a.first < b.second-b.first;
	}else{
		a.second < b.second;
	}
}


int main(){
	ll n; cin >> n;
	vector<vpl> v(4);
	map<char,ll> mp;
	mp['U'] = 0, mp['D'] = 1, mp['R'] = 2, mp['L'] = 3;
	rep(i,n){
		ll x,y; char u; cin >> x >> y >> u;
		v[mp[u]].emplace_back(x,y);
	}
	sort(all(v[0])); sort(all(v[1]));
	ll mn = linf;
	for(auto t : v[0]){
		auto itr = lower_bound(all(v[1]), P(t.first,t.second));
		if(itr != v[1].end() && (*itr).first == t.first){
			chmin(mn,((*itr).second - t.second)*5);
		}
	}
	vpl p,q;
	for(auto t : v[2]) p.emplace_back(t.second,t.first);
	for(auto t : v[3]) q.emplace_back(t.second,t.first);
	sort(all(p)); sort(all(q));
	for(auto t : p){
		auto itr = lower_bound(all(q), P(t.first,t.second));
		if(itr != q.end() && (*itr).first == t.first){
			chmin(mn,((*itr).second - t.second)*5);
		}
	}
	vector<P> a,b;
	for(auto t : v[2]) a.emplace_back(t.first+t.second,t.second);
	for(auto t : v[3]) b.emplace_back(t.second-t.first,t.second);
	sort(all(a));
	sort(all(b));
	for(auto t : v[0]){
		auto itr = lower_bound(all(a),P(t.first+t.second,t.second));
		if(itr != a.end() && (*itr).first == t.first+t.second){
			chmin(mn,((*itr).second-t.second)*10);
			//print2((*itr).first,(*itr).second);
			//print2(t.first,t.second);
		}
		itr = lower_bound(all(b),P(t.second-t.first,t.second));
		if(itr != b.end() && (*itr).first == t.second-t.first){
			chmin(mn,((*itr).second-t.second)*10);
		}
	}
	a.clear(); b.clear();
	for(auto t : v[2]) a.emplace_back(t.second-t.first,t.second);
	for(auto t : v[3]) b.emplace_back(t.first+t.second,t.second);
	sort(all(a));
	sort(all(b));
	for(auto t : v[1]){
		auto itr = lower_bound(all(b),P(t.first+t.second,t.second));
		if(itr != b.begin()){
			itr--;
			//print2((*itr).first,(*itr).second);
			//print2(t.first,t.second);
			if((*itr).first == t.first+t.second){
				chmin(mn,(t.second-(*itr).second)*10);
			}
		}
		itr = lower_bound(all(a),P(t.second-t.first,t.second));
		if(itr != a.begin()){
			itr--;
			if((*itr).first == t.second-t.first){
				chmin(mn,(t.second-(*itr).second)*10);
			}
		}
	}
	if(mn == linf) puts("SAFE");
	else cout << mn << "\n";
}

Measured memory_bytes: 14112