#pragma GCC optimize("O3")
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<functional>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>
using namespace std;
#define MOD 1000000007
#define f(i,n) for(int i=0;i<int(n);i++)
#define N 200000






int main() {
	vector<pair<pair<int, int>, int>>a;
	vector<pair<int, int> >b1;
	vector<pair<int, int> >b2;
	vector<pair<int, int> >b3;
	vector<pair<int, int> >b4;
	int sz;
	int n, k;
	int x, y, z;
	int s, ans;
	ans = MOD;
	char c[2];
	scanf("%d", &n);
	f(i, n) {
		scanf("%d %d", &x, &y);
		c[0] = 0;
		c[1] = 0;
		scanf("%s", c);
		if (c[0] == 'U')b1.push_back(make_pair(x, y));
		if (c[0] == 'R')b2.push_back(make_pair(x, y));
		if (c[0] == 'D')b3.push_back(make_pair(x, y));
		if (c[0] == 'L')b4.push_back(make_pair(x, y));
	}

	a.clear();
	sz = b1.size();
	f(i, sz)a.push_back(make_pair(make_pair(b1[i].first, b1[i].second), 0));
	sz = b3.size();
	f(i, sz)a.push_back(make_pair(make_pair(b3[i].first, b3[i].second), 1));
	sort(a.begin(), a.end());
	sz = a.size();
	x = -1;
	y = -1;
	f(i,sz) {
		if (x < a[i].first.first) {
			x = a[i].first.first;
			y = -1;
		}
		if (a[i].second == 0)y = a[i].first.second;
		else if (y >= 0) {
			z = a[i].first.second - y;
			ans = min(ans, z * 5);
		}
	}

	a.clear();
	sz = b2.size();
	f(i, sz)a.push_back(make_pair(make_pair(b2[i].second, b2[i].first), 0));
	sz = b4.size();
	f(i, sz)a.push_back(make_pair(make_pair(b4[i].second, b4[i].first), 1));
	sort(a.begin(), a.end());
	sz = a.size();
	x = -1;
	y = -1;
	f(i, sz) {
		if (x < a[i].first.first) {
			x = a[i].first.first;
			y = -1;
		}
		if (a[i].second == 0)y = a[i].first.second;
		else if (y >= 0) {
			z = a[i].first.second - y;
			ans = min(ans, z * 5);
		}
	}

	a.clear();
	sz = b2.size();
	f(i, sz)a.push_back(make_pair(make_pair(b2[i].first + b2[i].second, b2[i].first - b2[i].second), 0));
	sz = b1.size();
	f(i, sz)a.push_back(make_pair(make_pair(b1[i].first + b1[i].second, b1[i].first - b1[i].second), 1));
	sort(a.begin(), a.end());
	sz = a.size();
	x = -1000000;
	y = -1000000;
	f(i, sz) {
		if (x != a[i].first.first) {
			x = a[i].first.first;
			y = -1000000;
		}
		if (a[i].second == 0)y = a[i].first.second;
		else if (y > -1000000) {
			z = a[i].first.second - y;
			ans = min(ans, z * 5);
		}
	}

	a.clear();
	sz = b3.size();
	f(i, sz)a.push_back(make_pair(make_pair(b3[i].first + b3[i].second, b3[i].first - b3[i].second), 0));
	sz = b4.size();
	f(i, sz)a.push_back(make_pair(make_pair(b4[i].first + b4[i].second, b4[i].first - b4[i].second), 1));
	sort(a.begin(), a.end());
	sz = a.size();
	x = -1000000;
	y = -1000000;
	f(i, sz) {
		if (x != a[i].first.first) {
			x = a[i].first.first;
			y = -1000000;
		}
		if (a[i].second == 0)y = a[i].first.second;
		else if (y > -1000000) {
			z = a[i].first.second - y;
			ans = min(ans, z * 5);
		}
	}

	a.clear();
	sz = b1.size();
	f(i, sz)a.push_back(make_pair(make_pair(b1[i].first - b1[i].second, b1[i].first + b1[i].second), 0));
	sz = b4.size();
	f(i, sz)a.push_back(make_pair(make_pair(b4[i].first - b4[i].second, b4[i].first + b4[i].second), 1));
	sort(a.begin(), a.end());
	sz = a.size();
	x = -1000000;
	y = -1000000;
	f(i, sz) {
		if (x != a[i].first.first) {
			x = a[i].first.first;
			y = -1000000;
		}
		if (a[i].second == 0)y = a[i].first.second;
		else if (y > -1000000) {
			z = a[i].first.second - y;
			ans = min(ans, z * 5);
		}
	}

	a.clear();
	sz = b2.size();
	f(i, sz)a.push_back(make_pair(make_pair(b2[i].first - b2[i].second, b2[i].first + b2[i].second), 0));
	sz = b3.size();
	f(i, sz)a.push_back(make_pair(make_pair(b3[i].first - b3[i].second, b3[i].first + b3[i].second), 1));
	sort(a.begin(), a.end());
	sz = a.size();
	x = -1000000;
	y = -1000000;
	f(i, sz) {
		if (x != a[i].first.first) {
			x = a[i].first.first;
			y = -1000000;
		}
		if (a[i].second == 0)y = a[i].first.second;
		else if (y > -1000000) {
			z = a[i].first.second - y;
			ans = min(ans, z * 5);
		}
	}

	if (ans > 1000000000)printf("SAFE\n");
	else printf("%d\n", ans);


	return 0;
}

Measured memory_bytes: 8768