#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <functional>
#include <vector>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <cassert>
#define InfL 2000000000
#define InfLL 4000000000000000000LL
#define mod 1000000007
#define rep(i,n) for(int i=0;i<n;i++)
#define rrep(i,n) for(int i=(n-1);i>=0;i--)
using namespace std;
typedef long long ll;
typedef double db;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<bool> vb;
typedef vector<db> vd;

int main() {
	int MAX_L = 400001;
	int MAX_d = 200000;
	vector<vi> UR_D(MAX_L);
	vector<vi> UR_L(MAX_L);
	vector<vi> UL_R(MAX_L);
	vector<vi> UL_D(MAX_L);
	vector<vi> RD_U(MAX_L);
	vector<vi> RD_L(MAX_L);
	vector<vi> DL_U(MAX_L);
	vector<vi> DL_R(MAX_L);
	vector<vi> U(MAX_L);
	vector<vi> R(MAX_L);
	vector<vi> D(MAX_L);
	vector<vi> L(MAX_L);

	int ans = InfL;
	int N;
	cin >> N;
	vi X(N), Y(N);
	vector<char> dir_(N);
	rep(i, N) {
		int x, y;
		char dir;
		cin >> x >> y >> dir;
		X[i] = x;
		Y[i] = y;
		dir_[i] = dir;
		if (dir == 'U') {
			RD_U[x + y].push_back(x);
			DL_U[-x + y + MAX_d].push_back(-x);
			U[x].push_back(-y);
		}
		if (dir == 'R') {
			UL_R[-x - y + MAX_d * 2].push_back(-x);
			DL_R[-x + y + MAX_d].push_back(-x);
			R[y].push_back(-x);
		}
		if (dir == 'D') {
			UR_D[x - y + MAX_d].push_back(x);
			UL_D[-x - y + MAX_d * 2].push_back(-x);
			D[x].push_back(y);
		}
		if (dir == 'L') {
			UR_L[x - y + MAX_d].push_back(x);
			RD_L[x + y].push_back(x);
			L[y].push_back(x);
		}
	}
	rep(i, MAX_L) {
		sort(RD_U[i].begin(), RD_U[i].end(), less<int>());
		sort(DL_U[i].begin(), DL_U[i].end(), less<int>());
		sort(UL_R[i].begin(), UL_R[i].end(), less<int>());
		sort(DL_R[i].begin(), DL_R[i].end(), less<int>());
		sort(UR_D[i].begin(), UR_D[i].end(), less<int>());
		sort(UL_D[i].begin(), UL_D[i].end(), less<int>());
		sort(UR_L[i].begin(), UR_L[i].end(), less<int>());
		sort(RD_L[i].begin(), RD_L[i].end(), less<int>());
	}
	rep(i, MAX_d) {
		sort(U[i].begin(), U[i].end(), less<int>());
		sort(D[i].begin(), D[i].end(), less<int>());
		sort(L[i].begin(), L[i].end(), less<int>());
		sort(R[i].begin(), R[i].end(), less<int>());
	}
	rep(i, N) {
		int x = X[i];
		int y = Y[i];
		char dir = dir_[i];
		if (dir == 'U') {
			int i1 = x - y + MAX_d;
			auto it1 = upper_bound(UR_L[i1].begin(), UR_L[i1].end(), x);
			if (it1 != UR_L[i1].end())
				ans = min(ans, (*it1 - x) * 2);
			int i2 = -x - y + MAX_d * 2;
			auto it2 = upper_bound(UL_R[i2].begin(), UL_R[i2].end(), -x);
			if (it2 != UL_R[i2].end())
				ans = min(ans, (*it2 + x) * 2);
			auto it3 = upper_bound(D[x].begin(), D[x].end(), y);
			if (it3 != D[x].end())
				ans = min(ans, *it3 - y);
		}
		if (dir == 'R') {
			int i1 = x - y + MAX_d;
			auto it1 = upper_bound(UR_D[i1].begin(), UR_D[i1].end(), x);
			if (it1 != UR_D[i1].end())
				ans = min(ans, (*it1 - x) * 2);
			int i2 = x + y;
			auto it2 = upper_bound(RD_U[i2].begin(), RD_U[i2].end(), x);
			if (it2 != RD_U[i2].end())
				ans = min(ans, (*it2 - x) * 2);
			auto it3 = upper_bound(L[y].begin(), L[y].end(), x);
			if (it3 != L[y].end())
				ans = min(ans, *it3 - x);
		}
		if (dir == 'D') {
			int i1 = x + y;
			auto it1 = upper_bound(RD_L[i1].begin(), RD_L[i1].end(), x);
			if (it1 != RD_L[i1].end())
				ans = min(ans, (*it1 - x) * 2);
			int i2 = -x + y + MAX_d;
			auto it2 = upper_bound(DL_R[i2].begin(), DL_R[i2].end(), -x);
			if (it2 != DL_R[i2].end())
				ans = min(ans, (*it2 + x) * 2);
			auto it3 = upper_bound(U[x].begin(), U[x].end(), -y);
			if (it3 != U[x].end())
				ans = min(ans, *it3 + y);
		}
		if (dir == 'L') {
			int i1 = -x - y + MAX_d * 2;
			auto it1 = upper_bound(UL_D[i1].begin(), UL_D[i1].end(), -x);
			if (it1 != UL_D[i1].end())
				ans = min(ans, (*it1 + x) * 2);
			int i2 = -x + y + MAX_d;
			auto it2 = upper_bound(DL_U[i2].begin(), DL_U[i2].end(), -x);
			if (it2 != DL_U[i2].end())
				ans = min(ans, (*it2 + x) * 2);
			auto it3 = upper_bound(R[y].begin(), R[y].end(), -x);
			if (it3 != R[y].end())
				ans = min(ans, *it3 + x);
		}
	}
	if (ans == InfL) {
		cout << "SAFE" << endl;
	}
	else {
		ans *= 5;
		cout << ans << endl;
	}
	return 0;
}

Measured memory_bytes: 136084