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

constexpr int MAX = 200010;
int n;
int x[MAX], y[MAX], u[MAX];
int ans = 1e9;

map<int, vector<int>> a, b;

void f() {
	for(auto& [_, v] : a) {
		v.clear();
	}
	for(auto& [_, v] : b) {
		v.clear();
	}
	for(int i = 0; i < n; ++i) {
		if(u[i] == 0) {
			b[x[i]].push_back(y[i]);
		}
		if(u[i] == 2) {
			a[x[i]].push_back(y[i]);
		}
	}
	for(auto& [_, v] : a) {
		sort(v.begin(), v.end());
	}
	for(auto& [x, v] : b) {
		for(int y : v) {
			auto itr = lower_bound(a[x].begin(), a[x].end(), y);
			if(itr == a[x].end()) continue;
			ans = min(ans, (*itr - y) * 10 / 2);
		}
	}
}

void g() {
	for(auto& [_, v] : a) {
		v.clear();
	}
	for(auto& [_, v] : b) {
		v.clear();
	}
	for(int i = 0; i < n; ++i) {
		if(u[i] == 0) {
			a[x[i] + y[i]].push_back(x[i]);
		}
		if(u[i] == 3) {
			b[x[i] + y[i]].push_back(x[i]);
		}
	}
	for(auto& [_, v] : a) {
		sort(v.begin(), v.end());
	}
	for(auto& [x, v] : b) {
		for(int y : v) {
			auto itr = upper_bound(a[x].begin(), a[x].end(), y);
			if(itr == a[x].end()) continue;
			ans = min(ans, (*itr - y) * 10);
		}
	}
}

int main() {
	cin >> n;
	constexpr char c[] = {'U', 'L', 'D', 'R'};
	for(int i = 0; i < n; ++i) {
		cin >> x[i] >> y[i];
		char cc; cin >> cc;
		for(int j = 0; j < 4; ++j) {
			if(c[j] == cc) {
				u[i] = j;
			}
		}
	}
	for(int i = 0; i < 4; ++i) {
		f();
		g();
		for(int j = 0; j < n; ++j) {
			u[j] = (u[j] + 1) % 4;
			swap(x[j], y[j]);
			x[j] = -x[j];
		}
	}
	if(ans == 1e9) {
		cout << "SAFE" << '\n';
		return 0;
	}
	cout << ans << '\n';
	return 0;
}