// F.

#include <iostream>
#include <sstream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>

using namespace std;
typedef long long LL;

const int INF = 1 << 30;

int solve(vector<int> x, vector<int> y, vector<string> dir) {
	int ans = INF;
	int n = (int)x.size();
	map<int, set<int>> a, b, v, h;
	for (int i = 0; i < n; ++i) {
		if (dir[i] == "L") {
			a[x[i] - y[i]].insert(x[i]);
			h[y[i]].insert(x[i]);
		}
		if (dir[i] == "R") {
			b[x[i] + y[i]].insert(x[i]);
		}
		if (dir[i] == "D") {
			v[x[i]].insert(y[i]);
		}
	}
	for (int i = 0; i < n; ++i) {
		if (dir[i] == "R") {
			if (h.find(y[i]) != h.end()) {
				set<int>& s = h[y[i]];
				auto it = s.lower_bound(x[i]);
				if (it != s.end()) {
					ans = min(ans, (*it - x[i]) * 5);
				}
			}
		}
		if (dir[i] != "U") {
			continue;
		}
		int key = x[i] - y[i];
		if (a.find(key) != a.end()) {
			set<int>& s = a[key];
			auto it = s.lower_bound(x[i]);
			if (it != s.end()) {
				ans = min(ans, (*it - x[i]) * 10);
			}
		}
		key = x[i] + y[i];
		if (b.find(key) != b.end()) {
			set<int>& s = b[key];
			auto it = s.lower_bound(x[i]);
			if (it != s.begin()) {
				--it;
				ans = min(ans, (x[i] - *it) * 10);
			}
		}
		if (v.find(x[i]) != v.end()) {
			set<int>& s = v[x[i]];
			auto it = s.lower_bound(y[i]);
			if (it != s.end()) {
				ans = min(ans, (*it - y[i]) * 5);
			}
		}
	}
	return ans;
}

int main(int argc, char* argv[]) {
	cout.precision(20);
#ifdef _MSC_VER
	while (true)
#endif
	{
		int ans = INF;
		LL n = -1;
		cin >> n;
		if (n < 0) return 0;
		vector<int> x(n), y(n);
		vector<string> dir(n);
		for (int i = 0; i < n; ++i) {
			cin >> x[i] >> y[i] >> dir[i];
		}
		ans = solve(x, y, dir);
		map<string, string> t;
		t["D"] = "U";
		t["U"] = "D";
		t["L"] = "R";
		t["R"] = "L";
		for (int i = 0; i < n; ++i) {
			x[i] = -x[i];
			y[i] = -y[i];
			dir[i] = t[dir[i]];
		}
		ans = min(ans, solve(x, y, dir));
		if (ans == INF) {
			cout << "SAFE" << endl;
		} else {
			cout << ans << endl;
		}
	}
	return 0;
}

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (int)(n); i ++)
#define irep(i,n) for (int i = (int)(n) - 1;i >= 0;--i)
#define rep2(i,a,b) for (int i = (int)(a); i < (int)(b); ++i)
using namespace std;
using ll = long long;
using PL = pair<ll,ll>;
using P = pair<int,int>;
constexpr ll INF = 1000000000;
constexpr long long HINF = 1000000000000000;
constexpr long long MOD = 1000000007;// = 998244353;
constexpr double EPS = 1e-4;
constexpr double PI = 3.14159265358979;
const int MAXD = 200010;

ll compare(vector<vector<int>> &A,vector<vector<int>> &B,int sz = MAXD * 2) {
    int ans = INF;
    rep(i,sz) {
        sort(B[i].begin(),B[i].end());
        for (int a:A[i]) {
            int idx = lower_bound(B[i].begin(),B[i].end(),a + 1) - B[i].begin();
            if (idx >= (int)B[i].size()) continue;
            ans = min(ans,B[i][idx] - a);
        }
    }
    return ans;
}

int main() {
    int N; cin >> N;
    vector<vector<int>> XU(MAXD),XD(MAXD),YR(MAXD),YL(MAXD),mR(2*MAXD),mD(2*MAXD),mU(2*MAXD),mL(2*MAXD),aR(2*MAXD),aU(2*MAXD),aD(2*MAXD),aL(2*MAXD);
    rep(i,N) {
        int x,y; char s; 
        cin >> x >> y >> s;
        if (s == 'U') {
            XU[x].push_back(y);
            mU[y - x + MAXD].push_back(x);
            aU[y + x].push_back(x);
        } else if (s == 'R'){
            YR[y].push_back(x);
            mR[y - x + MAXD].push_back(x);
            aR[y + x].push_back(x);
        } else if (s == 'D') {
            XD[x].push_back(y);
            mD[y - x + MAXD].push_back(x);
            aD[y + x].push_back(x);
        } else {
            YL[y].push_back(x);
            mL[y - x + MAXD].push_back(x);
            aL[y + x].push_back(x);
        }
    }
    ll ans = INF;
    ans = min(ans,compare(XU,XD,MAXD)*5LL);
    ans = min(ans,compare(YR,YL,MAXD)*5LL);
    ans = min(ans,compare(mR,mD)*10LL);
    ans = min(ans,compare(mU,mL)*10LL);
    ans = min(ans,compare(aR,aU)*10LL);
    ans = min(ans,compare(aD,aL)*10LL);
    if (ans == INF) cout << "SAFE" << '\n';
    else cout << ans << '\n';
    return 0;
}