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

using ll = long long int;

int main()
{
	ios::sync_with_stdio(0); cin.tie(0);
	mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

	int n; cin >> n;
	const int lim = 6e5 + 100, offset = 2e5;
	vector<vector<array<int, 2>>> vert(200010), hor(200010);
	vector<vector<int>> left1(lim), right1(lim), left2(lim), right2(lim);
	vector<array<int, 2>> up, down;
	for (int i = 0; i < n; ++i) {
		int x, y; char dir;
		cin >> x >> y >> dir;
		if (dir == 'U') {vert[x].push_back({y, 0}); up.push_back({x, y});}
		else if (dir == 'D') {vert[x].push_back({y, 1}); down.push_back({x, y});}
		else if (dir == 'L') {
			hor[y].push_back({x, 0});
			left1[x+y+offset].push_back(y);
			left2[x-y+offset].push_back(y);
		}
		else {
			hor[y].push_back({x, 1});
			right1[x+y+offset].push_back(y);
			right2[x-y+offset].push_back(y);
		}
	}
	for (auto &vec : hor)
		sort(begin(vec), end(vec));
	for (auto &vec : vert)
		sort(begin(vec), end(vec));
	for (auto &vec : left1)
		sort(begin(vec), end(vec));
	for (auto &vec : left2)
		sort(begin(vec), end(vec));
	for (auto &vec : right1)
		sort(begin(vec), end(vec));
	for (auto &vec : right2)
		sort(begin(vec), end(vec));
	int ans = 2e9;
	for (auto &vec : hor) {
		int N = vec.size();
		for (int i = 0; i+1 < N; ++i) {
			if (vec[i][1] == 1 && vec[i+1][1] == 0)
				ans = min(ans, 5*vec[i+1][0] - 5*vec[i][0]);
			if (vec[i][0] == vec[i+1][0])
				ans = 0;
		}
	}
	for (auto &vec : vert) {
		int N = vec.size();
		for (int i = 0; i+1 < N; ++i) {
			if (vec[i][1] == 0 && vec[i+1][1] == 1)
				ans = min(ans, 5*vec[i+1][0] - 5*vec[i][0]);
			if (vec[i][0] == vec[i+1][0])
				ans = 0;
		}
	}
	for (auto [x, y] : up) {
		int sum = x+y+offset, dif = x-y+offset;
		auto it = upper_bound(begin(right1[sum]), end(right1[sum]), y);
		if (it != end(right1[sum]))
			ans = min(ans, 10*abs(y-*it));
		it = upper_bound(begin(left2[dif]), end(left2[dif]), y);
		if (it != end(left2[dif]))
			ans = min(ans, 10*abs(y-*it));
	}
	for (auto [x, y] : down) {
		int sum = x+y+offset, dif = x-y+offset;
		auto it = lower_bound(begin(left1[sum]), end(left1[sum]), y);
		if (it != begin(left1[sum])) {
			--it;
			ans = min(ans, 10*abs(y-*it));
		}
		it = lower_bound(begin(right2[dif]), end(right2[dif]), y);
		if (it != begin(right2[dif])) {
			--it;
			ans = min(ans, 10*abs(y-*it));
		}
	}
	if (ans < 2e9) cout << ans << '\n';
	else cout << "SAFE\n";
}