//2020年4月17日現在

#include<stdio.h>
#include<cmath>
#include<math.h>
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<complex>
#include<queue>
#include<set>
#include<utility>
#include<iomanip>
#include<limits>
#include<thread>
#include<map>
#include<stack>
#include<numeric>
#include<cassert>
#include<random>
#include<chrono>
#include<unordered_set>
#include<unordered_map>
#include<fstream>
#include<list>
#include<functional>
#include<bitset>
#include<tuple>
#include <cstdio>

using namespace std;

template <class... A>
void prints() { cout << "\n"; }
template <class... A>
void prints_rest() { cout << "\n"; }
template <class T, class... A>
void prints_rest(const T &first, const A &... rest) { cout << " " << first; prints_rest(rest...); }
template <class T, class... A>
void prints(const T &first, const A &... rest) { cout << first; prints_rest(rest...); }
template <class T, class... A>
void printd(const T &first, const A &... rest) { cout << fixed << setprecision(15) << first; prints_rest(rest...); }


namespace {
	// printElems(&a[0], &a[n]);　みたいな感じで使う。
	void printElems(const int* begin, const int* end)
	{
		for (const int* p = begin; p != end; ++p) {
			cout << *p << ' ';
		}
		cout << endl;
	}
}

#define rep(i,n) for(ll i=0; i<n; i++)
#define rep1(i,n) for(ll i=1; i<n+1; i++)
#define repr(i,n) for(ll i=n-1; i>=0; i--)
#define repr1(i,n) for(ll i=n; i>0; i--)
#define FOR(i,j,n) for(ll i=j; i < n; i++)
#define souatari(i,j,n) rep1(i,n-1)rep(j,i)
#define souatari1(i,j,n) for(ll i=2; i<n+1; i++)rep1(j,i-1)
#define mp make_pair
#define F first
#define S second
#define elif else if

typedef long long ll;
typedef vector<int>  vi;
typedef vector<vi>  vvi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vpii;
typedef array<int,3> arr3;
typedef long double D;


const ll llinf =  numeric_limits<ll>::max();
const ll _llinf = numeric_limits<ll>::lowest();
const int inf = numeric_limits<int>::max();
const int _inf = numeric_limits<int>::lowest();
const double dinf = numeric_limits<double>::infinity();
const ll MOD = 1000000007;
//const ll MOD = 998244353;
const ll BIG = 1LL<<60;
const string yes = "Yes";
const string no = "No";

//----------------------------------------------

priority_queue<pii> vir[200005];
priority_queue<pii> hor[200005];
priority_queue<pii> negl[400005];
priority_queue<pii> negr[400005];
priority_queue<pii> posl[400005];
priority_queue<pii> posr[400005];



int main(){
	int n;
	cin >> n;
	int ans = inf;
	rep(i,n){
		int x,y;
		cin >> x >> y;
		string u;
		cin >> u;
		if(u == "U"){vir[x].push(mp(y,1));negr[x+y].push(mp(y,1));posl[y-x+200000].push(mp(y,1));}
		if(u == "D"){vir[x].push(mp(y,3));negl[x+y].push(mp(y,3));posr[y-x+200000].push(mp(y,3));}
		if(u == "R"){hor[y].push(mp(x,2));negr[x+y].push(mp(y,2));posr[y-x+200000].push(mp(y,2));}
		if(u == "L"){hor[y].push(mp(x,4));negl[x+y].push(mp(y,4));posl[y-x+200000].push(mp(y,4));}
	}
	rep(i,200005){
		int tmpmax = -1;

		while(!vir[i].empty()){
			pii tmp = vir[i].top();
			vir[i].pop();
			if(tmp.S == 1 && tmpmax != -1) ans = min(ans,(tmpmax-tmp.F)*5);
			if(tmp.S == 3) tmpmax = tmp.F;
		}
		tmpmax = -1;
		while(!hor[i].empty()){
			pii tmp = hor[i].top();
			hor[i].pop();
			if(tmp.S == 2 && tmpmax != -1) ans = min(ans,(tmpmax-tmp.F)*5);
			if(tmp.S == 4) tmpmax = tmp.F;
		}
	}
	rep(i,400005){
		int tmpmax = -1;

		while(!negr[i].empty()){
			pii tmp = negr[i].top();
			negr[i].pop();
			if(tmp.S == 1 && tmpmax != -1) ans = min(ans,(tmpmax-tmp.F)*10);
			if(tmp.S == 2) tmpmax = tmp.F;
		}
		tmpmax = -1;
		while(!negl[i].empty()){
			pii tmp = negl[i].top();
			negl[i].pop();
			if(tmp.S == 4 && tmpmax != -1) ans = min(ans,(tmpmax-tmp.F)*10);
			if(tmp.S == 3) tmpmax = tmp.F;
		}

		tmpmax = -1;
		while(!posl[i].empty()){
			pii tmp = posl[i].top();
			posl[i].pop();
			if(tmp.S == 1 && tmpmax != -1) ans = min(ans,(tmpmax-tmp.F)*10);
			if(tmp.S == 4) tmpmax = tmp.F;
		}
		tmpmax = -1;
		while(!posr[i].empty()){
			pii tmp = posr[i].top();
			posr[i].pop();
			if(tmp.S == 2 && tmpmax != -1) ans = min(ans,(tmpmax-tmp.F)*10);
			if(tmp.S == 3) tmpmax = tmp.F;
		}
	}
	if(ans == inf){prints("SAFE");}
	else{prints(ans);}


	return 0;
}




Measured memory_bytes: 84440