//#include "bits/stdc++.h"

#define _USE_MATH_DEFINES

#include <iostream>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <functional>
#include <utility>
#include <tuple>
#include <vector>
#include <string>
#include <list>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <queue>
#include <deque>
#include <stack>
#include <iterator>
#include <bitset>
#include <complex>
#include <limits>
#include <random>


using namespace std;

#define rep(i,a,b) for(int i=(a), i##_len=(b);i<i##_len;i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)

#define int ll
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<double, double> pdd;
typedef vector< vector<int> > mat;

template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; }

const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;

map<pii, vector<int>> DL, DR, H, V;

signed main()
{
	cin.tie(0);
	ios::sync_with_stdio(false);

	int N;
	cin >> N;

	char c;
	int x, y, u;
	rep(i, 0, N)
	{
		cin >> x >> y >> c;
		if (c == 'R')u = 0;
		if (c == 'L')u = 1;
		if (c == 'D')u = 2;
		if (c == 'U')u = 3;
		if (u <= 1)H[mp(y, u)].push_back(x);
		else V[mp(x,u - 2)].push_back(y);
		int ly = y + x, ry = y + (200000 - x);
		DL[mp(ly,u)].push_back(x);
		DR[mp(ry,u)].push_back(x);
	}

	int ans = INF;

	for (auto& p : DL)sort(all(p.second));
	for (auto& p : DR)sort(all(p.second));
	for (auto& p : H)sort(all(p.second));
	for (auto& p : V)sort(all(p.second));

	for (auto& p : DL)
	{
		if (p.first.second == 2)
		{
			if (DL.find(mp(p.first.first, 1)) == DL.end())continue;
			for (auto v : p.second)
			{
				auto idx = lower_bound(all(DL[mp(p.first.first, 1)]), v);
				if (idx == DL[mp(p.first.first, 1)].end())continue;
				chmin(ans, abs((*idx) - v) * 10);
			}
		}
		if (p.first.second == 0)
		{
			if (DL.find(mp(p.first.first, 3)) == DL.end())continue;
			for (auto v : p.second)
			{
				auto idx = lower_bound(all(DL[mp(p.first.first, 3)]), v);
				if (idx == DL[mp(p.first.first, 3)].end())continue;
				chmin(ans, abs((*idx) - v) * 10);
			}
		}
	}
	for (auto& p : DR)
	{
		if (p.first.second == 0)
		{
			if (DR.find(mp(p.first.first, 2)) == DR.end())continue;
			for (auto v : p.second)
			{
				auto idx = lower_bound(all(DR[mp(p.first.first, 2)]), v);
				if (idx == DR[mp(p.first.first, 2)].end())continue;
				chmin(ans, abs((*idx) - v) * 10);
			}
		}
		if (p.first.second == 3)
		{
			if (DR.find(mp(p.first.first, 1)) == DR.end())continue;
			for (auto v : p.second)
			{
				auto idx = lower_bound(all(DR[mp(p.first.first, 1)]), v);
				if (idx == DR[mp(p.first.first, 1)].end())continue;
				chmin(ans, abs((*idx) - v) * 10);
			}
		}
	}
	for (auto& p : H)
	{
		if (p.first.second == 0)
		{
			if (H.find(mp(p.first.first, 1)) == H.end())continue;
			for (auto v : p.second)
			{
				auto idx = lower_bound(all(H[mp(p.first.first, 1)]), v);
				if (idx == H[mp(p.first.first, 1)].end())continue;
				chmin(ans, abs((*idx) - v) * 5);
			}
		}
	}
	for (auto& p : V)
	{
		if (p.first.second == 1)
		{
			if (V.find(mp(p.first.first, 0)) == V.end())continue;
			for (auto v : p.second)
			{
				auto idx = lower_bound(all(V[mp(p.first.first, 0)]), v);
				if (idx == V[mp(p.first.first, 0)].end())continue;
				chmin(ans, abs((*idx) - v) * 5);
			}
		}
	}

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

	return 0;
}

Measured memory_bytes: 69188