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

#ifdef LOCAL
#include "debug_print.hpp"
#else
void debug(...) {}
#endif

struct Info
{
    int a, b;
    char u;
    Info() : a(-1), b(-1), u('N') {}
    Info(int a, int b, char u) : a(a), b(b), u(u) {}
};

bool operator<(const Info& info1, const Info& info2){
    if(info1.a!=info2.a) return info1.a<info2.a;
    if(info1.b!=info2.b) return info1.b<info2.b;
    return info1.u<info2.u;
}

template<class T> inline bool chmin(T &a, T b)
{
    if(a > b)
    {
        a = b;
        return true;
    }
    return false;
}

struct Diagonal
{
    int a, b;
    int c;
    char u;
    Diagonal() : a(-1), b(-1), c(-1LL), u('N') {}
    Diagonal(int a, int b, char u) : a(a), b(b), c(a + b), u(u) {}
};

bool operator<(const Diagonal& info1, const Diagonal& info2){
    if(info1.c!=info2.c) return info1.c<info2.c;
    if(info1.a!=info2.a) return info1.a<info2.a;
    if(info1.b!=info2.b) return info1.b<info2.b;
    return info1.u<info2.u;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int N;
    cin >> N;
    vector<int> X(N), Y(N);
    vector<char> U(N);
    for(int i=0; i<N; i++) cin >> X[i] >> Y[i] >> U[i];

    set<Info> H; // x, y, u
    set<Info> W; // y, x, u
    for(int i=0; i<N; i++)
    {
        H.emplace(X[i], Y[i], U[i]);
        W.emplace(Y[i], X[i], U[i]);
    }

    bool is_safe = true;
    int ans = 1<<30;
    Info pre;
    for(auto itr = H.begin(); itr != H.end(); itr++)
    {
        if(pre.a == itr->a)
        {
            if(pre.b < itr->b && pre.u == 'U' && itr->u == 'D')
            {
                is_safe = false;
                chmin(ans, (itr->b - pre.b) * 5);
            }
            else if(pre.b > itr->b && pre.u == 'D' && itr->u == 'U')
            {
                is_safe = false;
                chmin(ans, (pre.b - itr->b) * 5);
            }
        }
        pre = *itr;
    }
    pre = Info();
    for(auto itr = W.begin(); itr != W.end(); itr++)
    {
        if(pre.a == itr->a)
        {
            if(pre.b < itr->b && pre.u == 'R' && itr->u == 'L')
            {
                is_safe = false;
                chmin(ans, (itr->b - pre.b) * 5);
            }
            else if(pre.b > itr->b && pre.u == 'L' && itr->u == 'R')
            {
                is_safe = false;
                chmin(ans, (pre.b - itr->b) * 5);
            }
        }
        pre = *itr;
    }

    set<Diagonal> P;
    set<Diagonal> M;
    for(int i=0; i<N; i++)
    {
        P.emplace(X[i], Y[i], U[i]);
        M.emplace(200000 - X[i], Y[i], U[i]);
    }

    Diagonal temp;
    for(auto itr = P.begin(); itr != P.end(); itr++)
    {
        if(temp.c == itr->c)
        {
            if(temp.a < itr->a && ((temp.u == 'R' && itr->u == 'U') || (temp.u == 'D' && itr->u == 'L')))
            {
                is_safe = false;
                chmin(ans, (itr->a - temp.a) * 10);
            }
            else if(temp.a > itr->a && ((temp.u == 'U' && itr->u == 'R') || (temp.u == 'L' && itr->u == 'D')))
            {
                is_safe = false;
                chmin(ans, (temp.a - itr->a) * 10);
            }
        }
        temp = *itr;
    }
    temp = Diagonal();
    for(auto itr = M.begin(); itr != M.end(); itr++)
    {
        if(temp.c == itr->c)
        {
            if(temp.a > itr->a && ((temp.u == 'R' && itr->u == 'D') || (temp.u == 'U' && itr->u == 'L')))
            {
                is_safe = false;
                chmin(ans, (temp.a - itr->a) * 10);
                debug(temp.a, temp.b, temp.u, itr->a, itr->b, itr->u);
            }
            else if(temp.a < itr->a && ((temp.u == 'D' && itr->u == 'R') || (temp.u == 'L' && itr->u == 'U')))
            {
                is_safe = false;
                chmin(ans, (itr->a - temp.a) * 10);
                debug(temp.a, temp.b, temp.u, itr->a, itr->b, itr->u);
            }
        }
        temp = *itr;
    }


    if(is_safe) cout << "SAFE" << endl;
    else cout << ans << endl;

    return 0;
}


Measured memory_bytes: 54948