#include <bits/stdc++.h>

using namespace std;

typedef unsigned uint;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ldbl;
typedef pair<int, int> pii;
typedef pair<uint, uint> puu;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pull;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<uint> vu;
typedef vector<ll> vll;
typedef vector<ull> vull;
typedef vector<pii> vpii;
typedef vector<puu> vpuu;
typedef vector<pll> vpll;
typedef vector<pull> vpull;
typedef vector<string> vstr;
typedef vector<double> vdbl;
typedef vector<ldbl> vldbl;
#define pb push_back
#define ppb pop_back
#define pfr push_front
#define ppfr pop_front
#define emp emplace
#define empb emplace_back
#define be begin
#define rbe rbegin
#define all(x) (x).be(), (x).end()
#define rall(x) (x).rbe(), (x).rend()
#define fir first
#define sec second
#define mkp make_pair
#define brif(cond) if (cond) break
#define ctif(cond) if (cond) continue
#define retif(cond) if (cond) return
void canhazfast() {ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);}
template<typename T> T gcd(T a, T b) {return b ? gcd(b, a%b) : a;}
template<typename T> T extgcd(T a, T b, T &x, T &y)
{
    T x0 = 1, y0 = 0, x1 = 0, y1 = 1;
    while (b) {
        T q = a/b; a %= b; swap(a, b);
        x0 -= q*x1; swap(x0, x1);
        y0 -= q*y1; swap(y0, y1);
    }
    x = x0; y = y0; return a;
}
int ctz(uint x) {return __builtin_ctz(x);}
int ctzll(ull x) {return __builtin_ctzll(x);}
int clz(uint x) {return __builtin_clz(x);}
int clzll(ull x) {return __builtin_clzll(x);}
int popcnt(uint x) {return __builtin_popcount(x);}
int popcntll(ull x) {return __builtin_popcountll(x);}
int bsr(uint x) {return 31^clz(x);}
int bsrll(ull x) {return 63^clzll(x);}

typedef pair<int, char> pic;
typedef vector<pic> vpic;

#define MX 200000
#define INF 123456789

vpic atx[MX+1];
vpic aty[MX+1];
vpic ats[2*MX+1];
vpic atd[2*MX+1];

template<typename T>
void umin(T &a, T b) { a = min(a, b); }

int main()
{
    canhazfast();

    int n, ans = INF;

    cin >> n;
    for (int i = 0; i < n; ++i) {
        int x, y, s, d;
        char dir;

        cin >> x >> y >> dir;
        s = x+y;
        d = x-y+MX;
        atx[x].empb(y, dir);
        aty[y].empb(x, dir);
        ats[s].empb(d, dir);
        atd[d].empb(s, dir);
    }
    /// eq x -> UD
    for (int i = 0; i <= MX; ++i) {
        sort(all(atx[i]));
        for (size_t j = 1; j < atx[i].size(); ++j) {
            pic pre = atx[i][j-1], nxt = atx[i][j];
            if (pre.sec == 'U' && nxt.sec == 'D') umin(ans, nxt.fir-pre.fir);
        }
    }
    /// eq y -> RL
    for (int i = 0; i <= MX; ++i) {
        sort(all(aty[i]));
        for (size_t j = 1; j < aty[i].size(); ++j) {
            pic pre = aty[i][j-1], nxt = aty[i][j];
            if (pre.sec == 'R' && nxt.sec == 'L') umin(ans, nxt.fir-pre.fir);
        }
    }
    /// eq s -> RU, DL
    for (int i = 0; i <= 2*MX; ++i) {
        sort(all(ats[i]));
        int lastR = -INF, lastD = -INF;
        for (pic p : ats[i]) {
            switch (p.sec) {
            case 'D':
                lastD = p.fir;
                break;
            case 'L':
                umin(ans, p.fir-lastD);
                break;
            case 'R':
                lastR = p.fir;
                break;
            case 'U':
                umin(ans, p.fir-lastR);
                break;
            }
        }
    }
    /// eq d -> RD, UL
    for (int i = 0; i <= 2*MX; ++i) {
        sort(all(atd[i]));
        int lastR = -INF, lastU = -INF;
        for (pic p : atd[i]) {
            switch (p.sec) {
            case 'D':
                umin(ans, p.fir-lastR);
                break;
            case 'L':
                umin(ans, p.fir-lastU);
                break;
            case 'R':
                lastR = p.fir;
                break;
            case 'U':
                lastU = p.fir;
                break;
            }
        }
    }
    if (ans < INF) cout << 5*ans << '\n';
    else cout << "SAFE\n";

    return 0;
}


Measured memory_bytes: 51284