#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
using P = pair<int,int>;
int n, x, y, u, ans = 1e9, pa[][2] = {{0,2},{4,7},{8,9},{6,5},{10,11},{3,1}};
char us[] = "RLUD", uc;
set<P> st[12];
void chmin(int &a, int b){if(a>b)a=b;}
int main(){
	scanf("%d", &n);
	rep(i,n){
		scanf("\n%d %d %c", &x, &y, &uc);
		rep(i,4) if(us[i] == uc) u = i;
		st[u].emplace(x+y,x*2);
		st[u+4].emplace(x-y,x*2);
		st[u+8].emplace(u<2?y:x,u<2?x:y);
	}
	for(auto [l, r]: pa) for(auto li: st[l]){
		auto it = st[r].lower_bound(li);
		if(it != st[r].end() && it->first == li.first) chmin(ans,it->second-li.second);
	}
	if(ans != 1e9) printf("%d\n", ans*5);
	else puts("SAFE");
}