#include <iostream>
#include <vector>
#include <array>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <string>
#include <sstream>
#include <algorithm>
#include <random>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>
#include <cassert>
#include <climits>
#include <bitset>
#include <functional>
#include <iomanip>
#include <random>

#define FOR_LT(i, beg, end) for (int i = (int)(beg); i < (int)(end); i++)
#define FOR_LE(i, beg, end) for (int i = (int)(beg); i <= (int)(end); i++)
#define FOR_DW(i, beg, end) for (int i = (int)(beg); (int)(end) <= i; i--)
#define REP(n)              for (int repeat_index = 0; repeat_index < (int)n; repeat_index++)

using namespace std;

struct P {

};

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout << fixed << setprecision(20);

	int n; cin >> n;

	// R0 L1 U2 D3
	map<int64_t, set<int64_t>> danger[4][3];

	int64_t ans = INT64_MAX;
	REP(n) {
		int64_t x, y; cin >> x >> y;
		char c; cin >> c;
		int d = -1;
		switch(c) {
		case 'R': d = 0; break;
		case 'L': d = 1; break;
		case 'U': d = 2; break;
		case 'D': d = 3; break;
		}

		int64_t v = x + y;
		int64_t w = x - y;

		if (d == 0) {
			{
				auto it = danger[d][0].find(y);
				if (it != danger[d][0].end()) {
					set<int64_t>& s = it->second;
					auto it2 = s.lower_bound(x);
					if (it2 != s.end()) {
						ans = min(ans, abs(*it2 - x) * 5);
					}
				}
			}
			{
				auto it = danger[d][1].find(w);
				if (it != danger[d][1].end()) {
					set<int64_t>& s = it->second;
					auto it2 = s.lower_bound(v);
					if (it2 != s.end()) {
						ans = min(ans, abs(*it2 - v) * 5);
					}
				}
			}
			{
				auto it = danger[d][2].find(v);
				if (it != danger[d][2].end()) {
					set<int64_t>& s = it->second;
					auto it2 = s.lower_bound(w);
					if (it2 != s.end()) {
						ans = min(ans, abs(w - *it2) * 5);
					}
				}
			}
		}

		if (d == 1) {
			{
				auto it = danger[d][0].find(y);
				if (it != danger[d][0].end()) {
					set<int64_t>& s = it->second;
					auto it2 = s.lower_bound(x);
					if (it2 != s.begin()) {
						it2--;
						ans = min(ans, abs(x - *it2) * 5);
					}
				}
			}
			{
				auto it = danger[d][1].find(w);
				if (it != danger[d][1].end()) {
					set<int64_t>& s = it->second;
					auto it2 = s.lower_bound(v);
					if (it2 != s.begin()) {
						it2--;
						ans = min(ans, abs(v - *it2) * 5);
					}
				}
			}
			{
				auto it = danger[d][2].find(v);
				if (it != danger[d][2].end()) {
					set<int64_t>& s = it->second;
					auto it2 = s.lower_bound(w);
					if (it2 != s.begin()) {
						it2--;
						ans = min(ans, abs(w - *it2) * 5);
					}
				}
			}
		}
		if (d == 2) {
			{
				auto it = danger[d][0].find(x);
				if (it != danger[d][0].end()) {
					set<int64_t>& s = it->second;
					auto it2 = s.lower_bound(y);
					if (it2 != s.end()) {
						ans = min(ans, abs(y - *it2) * 5);
					}
				}
			}
			{
				auto it = danger[d][1].find(w);
				if (it != danger[d][1].end()) {
					set<int64_t>& s = it->second;
					auto it2 = s.lower_bound(v);
					if (it2 != s.end()) {
						ans = min(ans, abs(v - *it2) * 5);
					}
				}
			}
			{
				auto it = danger[d][2].find(v);
				if (it != danger[d][2].end()) {
					set<int64_t>& s = it->second;
					auto it2 = s.lower_bound(w);
					if (it2 != s.begin()) {
						it2--;
						ans = min(ans, abs(w - *it2) * 5);
					}
				}
			}
		}
		if (d == 3) {
			{
				auto it = danger[d][0].find(x);
				if (it != danger[d][0].end()) {
					set<int64_t>& s = it->second;
					auto it2 = s.lower_bound(y);
					if (it2 != s.begin()) {
						it2--;
						ans = min(ans, abs(y - *it2) * 5);
					}
				}
			}
			{
				auto it = danger[d][1].find(w);
				if (it != danger[d][1].end()) {
					set<int64_t>& s = it->second;
					auto it2 = s.lower_bound(v);
					if (it2 != s.begin()) {
						it2--;
						ans = min(ans, abs(v - *it2) * 5);
					}
				}
			}
			{
				auto it = danger[d][2].find(v);
				if (it != danger[d][2].end()) {
					set<int64_t>& s = it->second;
					auto it2 = s.lower_bound(w);
					if (it2 != s.end()) {
						ans = min(ans, abs(w - *it2) * 5);
					}
				}
			}
		}


		if (d == 0) {
			danger[1][0][y].insert(x);
			danger[2][2][v].insert(w);
			danger[3][1][w].insert(v);
		}
		if (d == 1) {
			danger[0][0][y].insert(x);
			danger[2][1][w].insert(v);
			danger[3][2][v].insert(w);
		}
		if (d == 2) {
			danger[3][0][x].insert(y);
			danger[0][2][v].insert(w);
			danger[1][1][w].insert(v);
		}
		if (d == 3) {
			danger[2][0][x].insert(y);
			danger[0][1][w].insert(v);
			danger[1][2][v].insert(w);
		}
	}


	if (ans == INT64_MAX) {
		cout << "SAFE" << endl;
	}
	else {
		cout << ans << endl;
	}

	return 0;
}


Measured memory_bytes: 87888