#include<bits/stdc++.h>
#pragma GCC target ("avx2")
#pragma GCC optimization ("Ofast")
#pragma GCC optimization ("unroll-loops")
#pragma comment(linker, "/stack:200000000")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
#define int long long 
typedef pair<int,int>pi;
typedef vector<int>vi;

const int MAXN=400005;
const int MOD=1000000007;

#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define DEC(i,a,b) for(int i=(a);i>=(b);i--)
#define pb push_back
#define mp make_pair
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(),(v).end()
#define db(x) cerr<<#x<<" = "<<x<<"\n"
#define LOW(v,x) (lower_bound(all(v),(x))-(v).begin())
#define UP(v,x) (upper_bound(all(v),(x))-(v).begin())

int N, ans, X[MAXN], Y[MAXN];
char dir[MAXN];
vi low[2*MAXN], up[2*MAXN];

void UD(){
	FOR(i,0,2*MAXN-1){
		low[i].clear();
		up[i].clear();
	}
	FOR(i,1,N){
		if (dir[i] == 'U'){
			low[X[i]].pb(Y[i]);
		}
		if (dir[i] == 'D'){
			up[X[i]].pb(Y[i]);
		}
	}
	FOR(i,0,2*MAXN-1){
		if (low[i].empty() || up[i].empty()) continue;
		sort(all(low[i]));
		sort(all(up[i]));
		for (auto j:low[i]){
			int idx = LOW(up[i], j);
			if (idx < sz(up[i])){
				ans = min(ans, 5 * (up[i][idx] - j));
			}
		}
	}
}

void LR(){
	FOR(i,0,2*MAXN-1){
		low[i].clear();
		up[i].clear();
	}
	FOR(i,1,N){
		if (dir[i] == 'R'){
			low[Y[i]].pb(X[i]);
		}
		if (dir[i] == 'L'){
			up[Y[i]].pb(X[i]);
		}
	}
	FOR(i,0,2*MAXN-1){
		if (low[i].empty() || up[i].empty()) continue;
		sort(all(low[i]));
		sort(all(up[i]));
		for (auto j:low[i]){
			int idx = LOW(up[i], j);
			if (idx < sz(up[i])){
				ans = min(ans, 5 * (up[i][idx] - j));
			}
		}
	}
}

void UR(){
	FOR(i,0,2*MAXN-1){
		low[i].clear();
		up[i].clear();
	}
	FOR(i,1,N){
		if (dir[i] == 'U'){
			low[X[i] + Y[i]].pb(Y[i]);
		}
		if (dir[i] == 'R'){
			up[X[i] + Y[i]].pb(Y[i]);
		}
	}
	FOR(i,0,2*MAXN-1){
		if (low[i].empty() || up[i].empty()) continue;
		sort(all(low[i]));
		sort(all(up[i]));
		for (auto j:low[i]){
			int idx = LOW(up[i], j);
			if (idx < sz(up[i])){
				ans = min(ans, 10 * (up[i][idx] - j));
			}
		}
	}
}

void DL(){
	FOR(i,0,2*MAXN-1){
		low[i].clear();
		up[i].clear();
	}
	FOR(i,1,N){
		if (dir[i] == 'L'){
			low[X[i] + Y[i]].pb(Y[i]);
		}
		if (dir[i] == 'D'){
			up[X[i] + Y[i]].pb(Y[i]);
		}
	}
	FOR(i,0,2*MAXN-1){
		if (low[i].empty() || up[i].empty()) continue;
		sort(all(low[i]));
		sort(all(up[i]));
		for (auto j:low[i]){
			int idx = LOW(up[i], j);
			if (idx < sz(up[i])){
				ans = min(ans, 10 * (up[i][idx] - j));
			}
		}
	}
}

void UL(){
	FOR(i,0,2*MAXN-1){
		low[i].clear();
		up[i].clear();
	}
	FOR(i,1,N){
		if (dir[i] == 'U'){
			low[X[i] - Y[i] + MAXN].pb(Y[i]);
		}
		if (dir[i] == 'L'){
			up[X[i] - Y[i] + MAXN].pb(Y[i]);
		}
	}
	FOR(i,0,2*MAXN-1){
		if (low[i].empty() || up[i].empty()) continue;
		sort(all(low[i]));
		sort(all(up[i]));
		for (auto j:low[i]){
			int idx = LOW(up[i], j);
			if (idx < sz(up[i])){
				ans = min(ans, 10 * (up[i][idx] - j));
			}
		}
	}
}

void DR(){
	FOR(i,0,MAXN-1){
		low[i].clear();
		up[i].clear();
	}
	FOR(i,1,N){
		if (dir[i] == 'R'){
			low[X[i] - Y[i] + MAXN].pb(Y[i]);
		}
		if (dir[i] == 'D'){
			up[X[i] - Y[i] + MAXN].pb(Y[i]);
		}
	}
	FOR(i,0,2*MAXN-1){
		if (low[i].empty() || up[i].empty()) continue;
		sort(all(low[i]));
		sort(all(up[i]));
		for (auto j:low[i]){
			int idx = LOW(up[i], j);
			if (idx < sz(up[i])){
				ans = min(ans, 10 * (up[i][idx] - j));
			}
		}
	}
}
main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	ans = 1e18;
	cin>>N;
	FOR(i,1,N){
		cin>>X[i]>>Y[i]>>dir[i];
	}
	UD();
	LR();
	UR(), DR();
	
	UL();
	DL();
	if (ans == 1e18) {cout<<"SAFE";}
	else cout<<ans;
}


Measured memory_bytes: 60280