//#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;

vector<int> DL[400010][4];
vector<int> DR[400010][4];
vector<int> H[200010][2];
vector<int> V[200010][2];

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[y][u].push_back(x);
		else V[x][u - 2].push_back(y);
		int ly = y + x, ry = y + (200000 - x);
		DL[ly][u].push_back(x);
		DR[ry][u].push_back(x);
	}

	int ans = INF;

	rep(i, 0, 400010)
	{
		rep(j, 0, 4)
		{
			sort(all(DL[i][j]));
			sort(all(DR[i][j]));
		}
		for (auto v : DL[i][0])
		{
			auto idx = lower_bound(all(DL[i][3]), v);
			if (idx == DL[i][3].end())continue;
			chmin(ans, abs((*idx) - v) * 10);
		}
		for (auto v : DL[i][1])
		{
			auto idx = lower_bound(all(DL[i][2]), v);
			if (idx == DL[i][2].begin())continue;
			idx--;
			chmin(ans, abs((*idx) - v) * 10);
		}
		for (auto v : DL[i][2])
		{
			auto idx = lower_bound(all(DL[i][1]), v);
			if (idx == DL[i][1].end())continue;
			chmin(ans, abs((*idx) - v) * 10);
		}
		for (auto v : DL[i][3])
		{
			auto idx = lower_bound(all(DL[i][0]), v);
			if (idx == DL[i][0].begin())continue;
			idx--;
			chmin(ans, abs((*idx) - v) * 10);
		}

		for (auto v : DR[i][0])
		{
			auto idx = lower_bound(all(DR[i][2]), v);
			if (idx == DR[i][2].end())continue;
			chmin(ans, abs((*idx) - v) * 10);
		}
		for (auto v : DR[i][1])
		{
			auto idx = lower_bound(all(DR[i][3]), v);
			if (idx == DR[i][3].begin())continue;
			idx--;
			chmin(ans, abs((*idx) - v) * 10);
		}
		for (auto v : DR[i][2])
		{
			auto idx = lower_bound(all(DR[i][0]), v);
			if (idx == DR[i][0].begin())continue;
			idx--;
			chmin(ans, abs((*idx) - v) * 10);
		}
		for (auto v : DR[i][3])
		{
			auto idx = lower_bound(all(DR[i][1]), v);
			if (idx == DR[i][1].end())continue;
			chmin(ans, abs((*idx) - v) * 10);
		}
	}

	rep(i, 0, 200010)
	{
		rep(j, 0, 2)
		{
			sort(all(H[i][j]));
			sort(all(V[i][j]));
		}

		for (auto v : H[i][0])
		{
			auto idx = lower_bound(all(H[i][1]), v);
			if (idx == H[i][1].end())continue;
			chmin(ans, abs((*idx) - v) * 5);
		}
		for (auto v : V[i][1])
		{
			auto idx = lower_bound(all(V[i][0]), v);
			if (idx == V[i][0].end())continue;
			chmin(ans, abs((*idx) - v) * 5);
		}
	}

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

	return 0;
}

Measured memory_bytes: 116076