// Problem Link: 
// Tutorial Link: 

#include<bits/stdc++.h>
 
using namespace std;
 
#define LL long long
#define ll  long long
#define all(v) v.begin(), v.end()
#define pb push_back
#define pLL pair<LL,LL>
#define ff first
#define ss second
#define rep(i,a,b) for(LL i=a;i<b;++i)
#define repb(i,a,b) for(LL i=a;i>b;--i)
#define ld double
#define mp make_pair
#define vLL vector<LL>
#define vpLL vector<pLL>
#define vld vector<ld>
#define pld pair<ld,ld>
#define vpld vector<pld>
#define SLL set<LL>
#define SpLL set<pLL>
#define FIO ios::sync_with_stdio(false)
#define mod 1000000007

LL n;
map<LL, map<string, vLL> > sum, dif, xx, yy;

void inline sortmm(map<LL, map<string, vLL> >& mm) {
	for(auto it = mm.begin(); it != mm.end(); it++) 
		for(auto it2 = it->ss.begin(); it2 != it->ss.end(); it2++) 
			sort(it2->ss.begin(), it2->ss.end());
}

LL inline find_collide(vLL& v, LL val, LL mode) {
	if(v.empty()) return mod;
	if(mode == 0) {
		auto pos = lower_bound(v.begin(), v.end(), val);
		if(pos == v.begin()) return mod;
		pos--;
		return 10 * (val - *pos);
	}
	else {
		auto pos = upper_bound(v.begin(), v.end(), val);
		if(pos == v.end()) return mod;
		return 10 * (*pos - val);
	}
}

int main()
{
	FIO;
	cin >> n;
	rep(i,1,n+1) {
		LL x, y;
		string c;
		cin >> x >> y >> c; 
		sum[x+y][c].pb(x);
		dif[x-y][c].pb(x);
		xx[x][c].pb(y);
		yy[y][c].pb(x);
	}
	LL ans = mod;
	sortmm(sum);
	sortmm(dif);
	sortmm(xx);
	sortmm(yy);
	for(auto it = sum.begin(); it != sum.end(); it++) {
		for(auto it2 = it->ss.begin(); it2 != it->ss.end(); it2++) {
			string cur = it2->ff, opp;
			LL val;
			if(cur == "U") {
				opp = "R";
				val = 0;
			}
			else if(cur == "D") {
				opp = "L";
				val = 1;
			}
			else if(cur == "L") {
				opp = "D";
				val = 0;
			}
			else if(cur == "R") {
				opp = "U";
				val = 1;
			}
			for(LL x: it2->ss) {
				LL collide = find_collide(it->ss[opp], x, val);
				ans = min(ans, collide);
			}
		}	
	}
	for(auto it = dif.begin(); it != dif.end(); it++) {
		for(auto it2 = it->ss.begin(); it2 != it->ss.end(); it2++) {
			string cur = it2->ff, opp;
			LL val;
			if(cur == "U") {
				opp = "L";
				val = 1;
			}
			else if(cur == "D") {
				opp = "R";
				val = 0;
			}
			else if(cur == "L") {
				opp = "U";
				val = 0;
			}
			else if(cur == "R") {
				opp = "D";
				val = 1;
			}
			for(LL x: it2->ss) {
				LL collide = find_collide(it->ss[opp], x, val);
				ans = min(ans, collide);
			}
		}
	}
	for(auto it = xx.begin(); it != xx.end(); it++) {
		for(auto it2 = it->ss.begin(); it2 != it->ss.end(); it2++) {
			string cur = it2->ff;
			if(cur == "L" || cur == "R" || cur == "U") continue;
			LL val = 0;
			string opp = "U";
			for(LL x: it2->ss) {
				LL collide = find_collide(it->ss[opp], x, val);
				if(collide != mod) collide /= 2;
				ans = min(ans, collide);
			}
		}
	}
	for(auto it = yy.begin(); it != yy.end(); it++) {
		for(auto it2 = it->ss.begin(); it2 != it->ss.end(); it2++) {
			string cur = it2->ff;
			if(cur == "R" || cur == "D" || cur == "U") continue;
			LL val = 0;
			string opp = "R";
			for(LL x: it2->ss) {
				LL collide = find_collide(it->ss[opp], x, val);
				if(collide != mod) collide /= 2;
				ans = min(ans, collide);
			}
		}
	}
	if(ans == mod) cout << "SAFE\n";
	else cout << ans << "\n";
}

Measured memory_bytes: 190500