#pragma GCC optimize ("O3")
// #pragma GCC target ("avx")
#include<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<string.h>
#include<map>

#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define NDEBUG
#define eprintf(...) do {} while (0)
#endif
#include<cassert>

using namespace std;

typedef long long LL;
typedef vector<int> VI;

#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i)

template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; }
template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; }
template<class Iter> void rprintf(const char *fmt, Iter begin, Iter end) {
    for (bool sp=0; begin!=end; ++begin) { if (sp) putchar(' '); else sp = true; printf(fmt, *begin); }
    putchar('\n');
}

const LL INF = 1LL<<60;

int N;

LL solveDU(vector<pair<LL, LL> > &D, vector<pair<LL, LL> > &U) {
    LL ret = INF;

    sort(D.begin(), D.end());
    sort(U.begin(), U.end());
    map<LL, vector<LL> > mp;
    
    REP (i, D.size()) {
	LL x = D[i].first;
	LL y = D[i].second;
	mp[x].push_back(y);
    }
    EACH (e, mp) sort(e->second.begin(), e->second.end());

    REP (i, U.size()) {
	LL x = U[i].first;
	LL y = U[i].second;
	auto it = mp.find(x);
	if (it != mp.end()) {
	    vector<LL> &v = it->second;
	    auto lb = lower_bound(v.begin(), v.end(), y);
	    if (lb != v.end()) {
		amin(ret, (*lb - y) / 2);
	    }
	}
    }

    return ret;
}

LL solveRU(vector<pair<LL, LL> > &R, vector<pair<LL, LL> > &U) {
    LL ret = INF;
    sort(R.begin(), R.end());
    sort(U.begin(), U.end());

    map<LL, LL> mp;

    int idx = 0;
    REP (i, U.size()) {
	LL ux = U[i].first;
	LL uy = U[i].second;
	while (idx < (int)R.size() && R[idx].first <= ux) {
	    LL rx = R[idx].first;
	    LL ry = R[idx].second;

	    LL key = rx + ry;
	    if (mp.count(key)) amax(mp[key], rx);
	    else mp[key] = rx;
	    idx++;
	}

	{
	    LL key = ux + uy;
	    if (mp.count(key)) {
		LL rx = mp[key];
		amin(ret, ux - rx);
	    }
	}
    }

    return ret;
}

vector<pair<LL, LL> > U, R, D, L;


void conv_x(vector<pair<LL, LL> > & v) {
    EACH (e, v) {
	e->first= -e->first;
    }
}
void conv_y(vector<pair<LL, LL> > & v) {
    EACH (e, v) {
	e->second = -e->second;
    }
}
void flip(vector<pair<LL, LL> > & v) {
    EACH (e, v) {
	swap(e->first, e->second);
    }
}

void MAIN() {
    scanf("%d", &N);
    REP (i, N) {
	int x, y;
	char buf[11];
	scanf("%d%d%s", &x, &y, buf);
	x *= 10;
	y *= 10;
	if (*buf == 'U') U.emplace_back(x, y);
	if (*buf == 'D') D.emplace_back(x, y);
	if (*buf == 'L') L.emplace_back(x, y);
	if (*buf == 'R') R.emplace_back(x, y);
    }

    LL ans = INF;
    amin(ans, solveDU(D, U));

    flip(R);
    flip(L);
    amin(ans, solveDU(L, R));
    flip(R);
    flip(L);


    amin(ans, solveRU(R, U));

    conv_y(R);
    conv_y(D);
    amin(ans, solveRU(R, D));
    conv_y(R);
    conv_y(D);

    conv_x(L);
    conv_x(U);
    conv_x(D);

    amin(ans, solveRU(L, U));
    conv_y(L);
    conv_y(D);
    amin(ans, solveRU(L, D));
    conv_y(L);
    conv_y(D);

    if (ans == INF) {
	puts("SAFE");
    } else {
	printf("%lld\n", ans);
    }
}

int main() {
    int TC = 1;
//    scanf("%d", &TC);
    REP (tc, TC) MAIN();
    return 0;
}



Measured memory_bytes: 27784