#include <iostream>
#include <algorithm>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <string>
#include <queue>
#include <deque>
#include <list>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <bitset>
#include <random>
#include <complex>
#include <assert.h>

using namespace std;
typedef long long ll;
#define endl '\n'

template<typename T>
inline bool chmin(T& x, T a) {
	if (x >= a) { x = a; return true; }
	return false;
}

template<typename T>
inline bool chmax(T& x, T a) {
	if (x <= a) { x = a; return true; }
	return false;
}

int main() {
	ios::sync_with_stdio(false);
	std::cin.tie(0);
	
	int N; cin >> N;
	vector<pair<int, int>> up, down, left, right;
	for (int i = 0; i < N; i++) {
		int x, y; cin >> x >> y;
		char u; cin >> u;
		pair<int, int> p = { x,y };
		if (u == 'U') { up.push_back(p); }
		if (u == 'D') { down.push_back(p); }
		if (u == 'L') { left.push_back(p); }
		if (u == 'R') { right.push_back(p); }
	}

	int ans = 100000000;

	// up - down
	{
		sort(up.begin(), up.end());
		sort(down.begin(), down.end());

		auto itr1 = up.begin();
		auto itr2 = down.begin();
		for (int X = 0; X <= 200000; X++) {
			vector<int> v1, v2;
			while (itr1 != up.end() && itr1->first < X) { itr1++; }
			while (itr1 != up.end() && itr1->first == X) { v1.push_back(itr1->second); itr1++; }
			while (itr2 != down.end() && itr2->first < X) { itr2++; }
			while (itr2 != down.end() && itr2->first == X) { v2.push_back(itr2->second); itr2++; }

			if (v1.size() == 0 || v2.size() == 0) { continue; }
			int c = 0;
			for (int i = 0; i < v1.size(); i++) {
				while (c < v2.size() && v2[c] < v1[i]) { c++; }
				if (c == v2.size()) { break; }
				int k = v2[c] - v1[i];
				chmin(ans, k * 5);
			}
		}
	}
	// right - left
	{
		for (int i = 0; i < right.size(); i++) {
			int c = right[i].first;
			right[i].first = right[i].second;
			right[i].second = c;
		}
		for (int i = 0; i < left.size(); i++) {
			int c = left[i].first;
			left[i].first = left[i].second;
			left[i].second = c;
		}
		sort(right.begin(), right.end());
		sort(left.begin(), left.end());

		auto itr1 = right.begin();
		auto itr2 = left.begin();
		for (int X = 0; X <= 200000; X++) {
			vector<int> v1, v2;
			while (itr1 != right.end() && itr1->first < X) { itr1++; }
			while (itr1 != right.end() && itr1->first == X) { v1.push_back(itr1->second); itr1++; }
			while (itr2 != left.end() && itr2->first < X) { itr2++; }
			while (itr2 != left.end() && itr2->first == X) { v2.push_back(itr2->second); itr2++; }

			if (v1.size() == 0 || v2.size() == 0) { continue; }
			int c = 0;
			for (int i = 0; i < v1.size(); i++) {
				while (c < v2.size() && v2[c] < v1[i]) { c++; }
				if (c == v2.size()) { break; }
				int k = v2[c] - v1[i];
				chmin(ans, k * 5);
			}
		}

		for (int i = 0; i < right.size(); i++) {
			int c = right[i].first;
			right[i].first = right[i].second;
			right[i].second = c;
		}
		for (int i = 0; i < left.size(); i++) {
			int c = left[i].first;
			left[i].first = left[i].second;
			left[i].second = c;
		}
	}

	// right - up -> (x+y, x-y)
	{
		vector<pair<int, int>> _right, _up;
		for (int i = 0; i < right.size(); i++) {
			int x = right[i].first, y = right[i].second;
			_right.push_back({ x + y, x - y });
		}
		for (int i = 0; i < up.size(); i++) {
			int x = up[i].first, y = up[i].second;
			_up.push_back({ x + y, x - y });
		}

		sort(_right.begin(), _right.end());
		sort(_up.begin(), _up.end());

		auto itr1 = _right.begin();
		auto itr2 = _up.begin();
		for (int X = 0; X <= 400000; X++) {
			vector<int> v1, v2;
			while (itr1 != _right.end() && itr1->first < X) { itr1++; }
			while (itr1 != _right.end() && itr1->first == X) { v1.push_back(itr1->second); itr1++; }
			while (itr2 != _up.end() && itr2->first < X) { itr2++; }
			while (itr2 != _up.end() && itr2->first == X) { v2.push_back(itr2->second); itr2++; }

			if (v1.size() == 0 || v2.size() == 0) { continue; }
			int c = 0;
			for (int i = 0; i < v1.size(); i++) {
				while (c < v2.size() && v2[c] < v1[i]) { c++; }
				if (c == v2.size()) { break; }
				int k = v2[c] - v1[i];
				chmin(ans, k * 5);
			}
		}
	}
	// left - up -> (_x+y, _x-y)
	{
		vector<pair<int, int>> _right, _up;
		for (int i = 0; i < left.size(); i++) {
			int x = 200000 - left[i].first, y = left[i].second;
			_right.push_back({ x + y, x - y });
		}
		for (int i = 0; i < up.size(); i++) {
			int x = 200000 - up[i].first, y = up[i].second;
			_up.push_back({ x + y, x - y });
		}

		sort(_right.begin(), _right.end());
		sort(_up.begin(), _up.end());

		auto itr1 = _right.begin();
		auto itr2 = _up.begin();
		for (int X = 0; X <= 400000; X++) {
			vector<int> v1, v2;
			while (itr1 != _right.end() && itr1->first < X) { itr1++; }
			while (itr1 != _right.end() && itr1->first == X) { v1.push_back(itr1->second); itr1++; }
			while (itr2 != _up.end() && itr2->first < X) { itr2++; }
			while (itr2 != _up.end() && itr2->first == X) { v2.push_back(itr2->second); itr2++; }

			if (v1.size() == 0 || v2.size() == 0) { continue; }
			int c = 0;
			for (int i = 0; i < v1.size(); i++) {
				while (c < v2.size() && v2[c] < v1[i]) { c++; }
				if (c == v2.size()) { break; }
				int k = v2[c] - v1[i];
				chmin(ans, k * 5);
			}
		}
	}
	// left - down -> (_x+_y, _x-_y)
	{
		vector<pair<int, int>> _right, _up;
		for (int i = 0; i < left.size(); i++) {
			int x = 200000 - left[i].first, y = 200000 - left[i].second;
			_right.push_back({ x + y, x - y });
		}
		for (int i = 0; i < down.size(); i++) {
			int x = 200000 - down[i].first, y = 200000 - down[i].second;
			_up.push_back({ x + y, x - y });
		}

		sort(_right.begin(), _right.end());
		sort(_up.begin(), _up.end());

		auto itr1 = _right.begin();
		auto itr2 = _up.begin();
		for (int X = 0; X <= 400000; X++) {
			vector<int> v1, v2;
			while (itr1 != _right.end() && itr1->first < X) { itr1++; }
			while (itr1 != _right.end() && itr1->first == X) { v1.push_back(itr1->second); itr1++; }
			while (itr2 != _up.end() && itr2->first < X) { itr2++; }
			while (itr2 != _up.end() && itr2->first == X) { v2.push_back(itr2->second); itr2++; }

			if (v1.size() == 0 || v2.size() == 0) { continue; }
			int c = 0;
			for (int i = 0; i < v1.size(); i++) {
				while (c < v2.size() && v2[c] < v1[i]) { c++; }
				if (c == v2.size()) { break; }
				int k = v2[c] - v1[i];
				chmin(ans, k * 5);
			}
		}
	}
	// right - down -> (x+_y, x-_y)
	{
		vector<pair<int, int>> _right, _up;
		for (int i = 0; i < right.size(); i++) {
			int x = right[i].first, y = 200000 - right[i].second;
			_right.push_back({ x + y, x - y });
		}
		for (int i = 0; i < down.size(); i++) {
			int x = down[i].first, y = 200000 - down[i].second;
			_up.push_back({ x + y, x - y });
		}

		sort(_right.begin(), _right.end());
		sort(_up.begin(), _up.end());

		auto itr1 = _right.begin();
		auto itr2 = _up.begin();
		for (int X = 0; X <= 400000; X++) {
			vector<int> v1, v2;
			while (itr1 != _right.end() && itr1->first < X) { itr1++; }
			while (itr1 != _right.end() && itr1->first == X) { v1.push_back(itr1->second); itr1++; }
			while (itr2 != _up.end() && itr2->first < X) { itr2++; }
			while (itr2 != _up.end() && itr2->first == X) { v2.push_back(itr2->second); itr2++; }

			if (v1.size() == 0 || v2.size() == 0) { continue; }
			int c = 0;
			for (int i = 0; i < v1.size(); i++) {
				while (c < v2.size() && v2[c] < v1[i]) { c++; }
				if (c == v2.size()) { break; }
				int k = v2[c] - v1[i];
				chmin(ans, k * 5);
			}
		}
	}

	if (ans == 100000000) {
		cout << "SAFE" << endl;
	}
	else {
		cout << ans << endl;
	}
	return 0;
}

Measured memory_bytes: 8412