#include <bits/stdc++.h>
using namespace std;

const int MAXN = 200005;
const int MAXM = 400015;

int n, xi, yi;
char di;

vector <int> a[6][MAXM], b[6][MAXM];
int calc(int ind, int x){
	sort(a[ind][x].begin(), a[ind][x].end());
	sort(b[ind][x].begin(), b[ind][x].end());
	
	int j = 0, res = -1;
	for(int i = 0; i < (int)b[ind][x].size(); i++){
		while(j < (int)a[ind][x].size() && a[ind][x][j] <= b[ind][x][i]){
			j++;
		}
		
		if(j > 0){
			int cur = 5 * (b[ind][x][i] - a[ind][x][j - 1]);
			if(res == -1 || cur < res)res = cur;
		}
	}
	return res;
}

int main(){
	scanf("%d", &n);
	for(int i = 0; i < n; i++){
		scanf("%d %d %c", &xi, &yi, &di);
		
		//(x + y, x - y)
		if(di == 'U'){//(1, -1)
			a[0][xi].push_back(yi);
			b[2][xi + yi].push_back(xi - yi);
			a[4][xi - yi + MAXN].push_back(xi + yi);
		}else if(di == 'R'){//(1, 1)
			a[1][yi].push_back(xi);
			a[2][xi + yi].push_back(xi - yi);
			a[5][xi - yi + MAXN].push_back(xi + yi);
		}else if(di == 'L'){//(-1, -1)
			b[1][yi].push_back(xi);
			b[3][xi + yi].push_back(xi - yi);
			b[4][xi - yi + MAXN].push_back(xi + yi);
		}else{//(-1, 1)
			b[0][xi].push_back(yi);
			a[3][xi + yi].push_back(xi - yi);
			b[5][xi - yi + MAXN].push_back(xi + yi);
		}
	}
	
	long long res = -1;
	for(int i = 0; i < 6; i++)
	for(int j = 0; j < MAXM; j++){
		long long cur = calc(i, j);
		if(cur != -1 && (res == -1 || cur < res))res = cur;
	}
	
	if(res == -1)printf("SAFE\n");
	else printf("%lld\n", res);
	
	return 0;
}
