#include <bits/stdc++.h>
using namespace std;

template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl





using ll = long long;

const ll mod = 1e9+7;
const int maxn = 1e6 + 5;
const ll inf = 1e18;

using pt = pair<pair<ll,ll>, char>;

int n;
ll x[maxn], y[maxn];
char u[maxn];




ll solveX(vector<pt> a) {
    int n = a.size();
    ll res = inf;
    vector<pair<int,char>> v;
    for (auto p: a) {
	v.push_back({p.first.second, p.second});
    }

    sort(v.begin(), v.end());
    int j=0;
    for (int i=0; i<n && j<n; i++) {
	if (v[i].second!='U') continue;
	if (j<i) j=i;
	while (j<n && v[j].second!='D') j++;
	if (j==n) break;
	ll cur = (v[j].first-v[i].first)*10/2;
	res = min(res, cur);
    }

    return res;
}

ll solveY(vector<pt> a) {
    int n = a.size();
    ll res = inf;
    vector<pair<int,char>> v;
    for (auto p: a) {
	v.push_back({p.first.first, p.second});
    }

    sort(v.begin(), v.end());
    int j=0;
    for (int i=0; i<n && j<n; i++) {
	if (v[i].second!='R') continue;
	if (j<i) j=i;
	while (j<n && v[j].second!='L') j++;
	if (j==n) break;
	ll cur = (v[j].first-v[i].first)*10/2;
	res = min(res, cur);
    }

    return res;
}



ll solveD1(vector<pt> a) {
    int n = a.size();
    ll res = inf;
    sort(a.begin(), a.end());
    // D, L
    {
	int j=0;
	for (int i=0; i<n && j<n; i++) {
	    if (a[i].second!='D') continue;
	    if (j<i) j=i;
	    while (j<n && a[j].second!='L') j++;
	    if (j==n) break;
	    ll dy = a[i].first.second - a[j].first.second;
	    assert(dy>=0);
	    ll cur = dy*10;
	    res = min(res, cur);
	}
    }

    // R, U
    {
	int j=0;
	for (int i=0; i<n && j<n; i++) {
	    if (a[i].second!='R') continue;
	    if (j<i) j=i;
	    while (j<n && a[j].second!='U') j++;
	    if (j==n) break;
	    ll dy = a[i].first.second - a[j].first.second;
	    assert(dy>=0);
	    ll cur = dy*10;
	    res = min(res, cur);
	}
    }

    return res;
}


ll solveD2(vector<pt> a) {
    int n = a.size();
    ll res = inf;
    sort(a.begin(), a.end());
    // R, D
    {
	int j=0;
	for (int i=0; i<n && j<n; i++) {
	    if (a[i].second!='R') continue;
	    if (j<i) j=i;
	    while (j<n && a[j].second!='D') j++;
	    if (j==n) break;
	    ll dy = a[j].first.second - a[i].first.second;
	    assert(dy>=0);
	    ll cur = dy*10;
	    res = min(res, cur);
	}
    }

    // U, L
    {
	int j=0;
	for (int i=0; i<n && j<n; i++) {
	    if (a[i].second!='U') continue;
	    if (j<i) j=i;
	    while (j<n && a[j].second!='L') j++;
	    if (j==n) break;
	    ll dy = a[j].first.second - a[i].first.second;
	    assert(dy>=0);
	    ll cur = dy*10;
	    res = min(res, cur);
	}
    }

    return res;
}



map<int, vector<pt>> diag1, diag2, xline, yline;

ll res = inf;

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);  cout.tie(0);

    cin>>n;
    for (int i=0; i<n; i++) {
	cin>>x[i]>>y[i]>>u[i];
	pt cur = {{x[i],y[i]}, u[i]};
	diag1[x[i]+y[i]].push_back(cur);
	diag2[x[i]-y[i]].push_back(cur);
	xline[x[i]].push_back(cur);
	yline[y[i]].push_back(cur);
    }

    for (auto p: xline) {
	res = min(res, solveX(p.second));
    }

    for (auto p: yline) {
	res = min(res, solveY(p.second));
    }

    for (auto p: diag1) {
	res = min(res, solveD1(p.second));
    }

    for (auto p: diag2) {
	res = min(res, solveD2(p.second));
    }
    

    if (res==inf) out("SAFE");
    out(res);
    
    return 0;
}


Measured memory_bytes: 89024