#include <bits/stdc++.h>

using namespace std;

const int inf = 1000000000;

int n;
vector< pair<int,int> > X[202020];
vector< pair<int,int> > Y[202020];

vector< pair<int,int> > V[2][402020];
vector< pair<int,int> > W[2][402020];

int ans = inf;

int pos[202020];

bool cmp(const pair<int,int> &fr, const pair<int,int> &sc) {
	return fr.first < sc.first;
}

void check( vector< pair<int,int> >  &v,int mul) {
	if (v.size() <= 1) return ;
	sort(v.begin(),v.end(), cmp );

	pos[ v.size() ] = -1;
	for (int i = v.size() - 1; i >= 0; i--) 
	{
		if (v[i].second == -1)  pos[i] = i;
		else pos[i] = pos[i + 1];
	}

	for (int i = 0; i < v.size(); i++) if (v[i].second == 1) {
		if (pos[i] == -1) continue;
		else
			ans = min(ans, (-v[i].first + v[ pos[i] ].first)* mul );
	}
}

int main() {

	scanf("%d",&n);
	int xx,yy;
	char op[10];
	for (int i = 1; i <= n; i++) {
		scanf("%d%d%s",&xx,&yy,op);
		if (op[0] == 'U') {
			Y[xx].push_back( make_pair(yy,1) );
			V[1][xx + yy].push_back( make_pair(yy,1) );
			W[0][yy - xx + 200000].push_back( make_pair(yy,1) );
		}
		else if (op[0] == 'R') {
			X[yy].push_back( make_pair(xx,1) );
			V[1][xx + yy].push_back( make_pair(yy,-1) );
			W[1][yy - xx + 200000].push_back( make_pair(yy,1) );
		}
		else if (op[0] == 'D') {
			Y[xx].push_back( make_pair(yy,-1) );
			V[0][xx + yy].push_back( make_pair(yy,-1) );
			W[1][yy - xx + 200000].push_back( make_pair(yy,-1) );
		}
		else if (op[0] == 'L') {
			X[yy].push_back( make_pair(xx,-1) );
			V[0][xx + yy].push_back( make_pair(yy,1) );
			W[0][yy - xx + 200000].push_back( make_pair(yy,-1) );
		}
	}

	for (int i = 0; i <= 200000; i++) {
		check( X[i],5);
		check( Y[i],5);
	}
	for (int i = 0; i <= 400000; i++) {
		check(V[0][i],10);
		check(W[0][i],10);
		check(V[1][i],10);
		check(W[1][i],10);
	}

	if (ans == inf) 
		printf("SAFE\n");
	else
		printf("%d\n",ans);
	return 0;
}
