#include <iostream>
#include <utility>
#include <tuple>
#include <vector>
#include <string>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <algorithm>
#include <functional>
#include <climits>
#include <numeric>
#include <queue>
#include <cmath>
#include <iomanip>
#include <array>
#include <string>
#include <stack>
#include <cassert>
#include <memory>
#include <random>
#include <fstream>
#include <cfloat>
#include <complex>
using namespace std;
struct Point {
	int x, y;
	int pos() const { return y - x; };
	int neg() const { return y + x; };
};
enum class Dir {
	Up, Right, Down, Left
};
struct Plane {
	Point position;
	Dir dir;
};
int main() {
	int n; cin >> n;
	vector<Plane> planes(n);
	for (auto& p : planes) {
		char d; cin >> p.position.x >> p.position.y >> d;
		if (d == 'U') p.dir = Dir::Up;
		else if (d == 'R') p.dir = Dir::Right;
		else if (d == 'D') p.dir = Dir::Down;
		else if (d == 'L') p.dir = Dir::Left;
		else throw 0;
	}
	vector<Plane> up45, right45, down45, left45;
	for (const auto p : planes) {
		switch (p.dir) {
		case Dir::Up: up45.push_back(p); break;
		case Dir::Right: right45.push_back(p); break;
		case Dir::Down: down45.push_back(p); break;
		case Dir::Left: left45.push_back(p); break;
		}
	}
	map<int, vector<Plane>> down_by_x, left_by_y;
	for (const auto d : down45) {
		down_by_x[d.position.x].push_back(d);
	}
	for (const auto l : left45) {
		left_by_y[l.position.y].push_back(l);
	}
	for (auto& d : down_by_x) {
		sort(d.second.begin(), d.second.end(), [](const Plane& a, const Plane& b) {return a.position.y < b.position.y; });
	}
	for (auto& l : left_by_y) {
		sort(l.second.begin(), l.second.end(), [](const Plane& a, const Plane& b) {return a.position.x < b.position.x; });
	}
	auto up135 = up45;
	auto right135 = right45;
	auto down135 = down45;
	auto left135 = left45;
	const auto comp45 = [](const Plane& a, const Plane& b) {return a.position.pos() == b.position.pos() ? a.position.x < b.position.x : a.position.pos() < b.position.pos(); };
	const auto comp135 = [](const Plane& a, const Plane& b) {return a.position.neg() == b.position.neg() ? a.position.x < b.position.x : a.position.neg() < b.position.neg(); };
	sort(up45.begin(), up45.end(), comp45);
	sort(right45.begin(), right45.end(), comp45);
	sort(down45.begin(), down45.end(), comp45);
	sort(left45.begin(), left45.end(), comp45);
	sort(up135.begin(), up135.end(), comp135);
	sort(right135.begin(), right135.end(), comp135);
	sort(down135.begin(), down135.end(), comp135);
	sort(left135.begin(), left135.end(), comp135);
	int min_time = INT_MAX;
	for (const auto up : up45) {
		const auto right_max = upper_bound(right135.begin(), right135.end(), up, comp135);
		if (right_max != right135.begin() && prev(right_max)->position.neg() == up.position.neg()) {
			const auto first = prev(right_max)->position;
			min_time = min(min_time, (up.position.x - first.x) << 1);
		}
		const auto left_max = lower_bound(left45.begin(), left45.end(), up, comp45);
		if (left_max != left45.end() && left_max->position.pos() == up.position.pos()) {
			min_time = min(min_time, (left_max->position.x - up.position.x) << 1);
		}
		const auto find = down_by_x.find(up.position.x);
		if (find != down_by_x.end()) {
			const auto first = lower_bound(find->second.begin(), find->second.end(), up, comp45);
			if (first != find->second.end()) {
				min_time = min(min_time, first->position.y - up.position.y);
			}
		}
	}
	for (const auto down : down45) {
		const auto right_max = upper_bound(right45.begin(), right45.end(), down, comp45);
		if (right_max != right45.begin() && prev(right_max)->position.pos() == down.position.pos()) {
			const auto first = prev(right_max)->position;
			min_time = min(min_time, (down.position.x - first.x) << 1);
		}
		const auto left_max = lower_bound(left135.begin(), left135.end(), down, comp135);
		if (left_max != left135.end() && left_max->position.neg() == down.position.neg()) {
			min_time = min(min_time, (left_max->position.x - down.position.x) << 1);
		}
	}
	for (const auto right : right45) {
		const auto find = left_by_y.find(right.position.y);
		if (find != left_by_y.end()) {
			const auto first = lower_bound(find->second.begin(), find->second.end(), right, comp135);
			if (first != find->second.end()) {
				min_time = min(min_time, first->position.x - right.position.x);
			}
		}
	}
	if (min_time == INT_MAX) {
		cout << "SAFE\n";
	}
	else {
		cout << min_time * 5 << '\n';
	}
}

Measured memory_bytes: 31732