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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)

typedef long long Int;
typedef pair<int, int> P;
typedef tuple<int,int,int> T;



int main() {
    int N;
    cin >> N;

    int ans = 500000000;
    bool flag = false;

    vector<int> X(N), Y(N);
    vector<char> U(N);
    vector<T> tplus(N), tminus(N), xp(N), yp(N);
    rep(i,N) {
        cin >> X[i] >> Y[i] >> U[i];
        tplus[i] = make_tuple(X[i] + Y[i], X[i], i);
        tminus[i] = make_tuple(X[i] - Y[i], X[i], i);
        xp[i] = make_tuple(X[i],Y[i],i);
        yp[i] = make_tuple(Y[i],X[i],i);
    }

    sort(tplus.begin(),tplus.end());
    sort(tminus.begin(),tminus.end());
    sort(xp.begin(),xp.end());
    sort(yp.begin(),yp.end());

    int i = 0;
    while(i < N - 1) {
        if (get<0>(tplus[i]) < get<0>(tplus[i+1])) {
            i++;
            continue;
        }
        int tmp = get<0>(tplus[i]);
        int j = i + 1;
        for (int k = i+1; k<N; k++) {
            if (get<0>(tplus[k]) == tmp) {
                j = k;
            }
            else {
                break;
            }
        }
        vector<P> ur(0), dl(0);
        for (int k = i; k <= j; k++) {
            if (U[get<2>(tplus[k])] == 'U') ur.push_back(make_pair(get<1>(tplus[k]), 0));
            if (U[get<2>(tplus[k])] == 'R') ur.push_back(make_pair(get<1>(tplus[k]), 1));
            if (U[get<2>(tplus[k])] == 'D') dl.push_back(make_pair(get<1>(tplus[k]), 0));
            if (U[get<2>(tplus[k])] == 'L') dl.push_back(make_pair(get<1>(tplus[k]), 1));
        }
        int nur = ur.size();
        if (nur > 1) {
            rep(k,nur - 1) {
                if (ur[k].second == 1 && ur[k+1].second == 0) {
                    ans = min(10 * (ur[k+1].first - ur[k].first), ans);
                    flag = true;
                }
            }
        }
        int ndl = dl.size();
        if (ndl > 1) {
            rep(k,ndl - 1) {
                if (dl[k].second == 0 && dl[k+1].second == 1) {
                    ans = min(10 * (dl[k+1].first - dl[k].first), ans);
                    flag = true;
                }
            }
        }

        i = j + 1;
    }

    i = 0;
    while(i < N - 1) {
        if (get<0>(tminus[i]) < get<0>(tminus[i+1])) {
            i++;
            continue;
        }
        int tmp = get<0>(tminus[i]);
        int j = i + 1;
        for (int k = i+1; k<N; k++) {
            if (get<0>(tminus[k]) == tmp) {
                j = k;
            }
            else {
                break;
            }
        }

        vector<P> ul(0), dr(0);
        for (int k = i; k <= j; k++) {
            if (U[get<2>(tminus[k])] == 'U') ul.push_back(make_pair(get<1>(tminus[k]), 0));
            if (U[get<2>(tminus[k])] == 'R') dr.push_back(make_pair(get<1>(tminus[k]), 1));
            if (U[get<2>(tminus[k])] == 'D') dr.push_back(make_pair(get<1>(tminus[k]), 0));
            if (U[get<2>(tminus[k])] == 'L') ul.push_back(make_pair(get<1>(tminus[k]), 1));
        }
        int nul = ul.size();
        if (nul > 1) {
            rep(k,nul - 1) {
                if (ul[k].second == 0 && ul[k+1].second == 1) {
                    ans = min(10 * (ul[k+1].first - ul[k].first), ans);
                    flag = true;
                }
            }
        }
        int ndr = dr.size();
        if (ndr > 1) {
            rep(k,ndr - 1) {
                if (dr[k].second == 1 && dr[k+1].second == 0) {
                    ans = min(10 * (dr[k+1].first - dr[k].first), ans);
                    flag = true;
                }
            }
        }



        i = j + 1;
    }

    i = 0;
    while(i < N - 1) {
        if (get<0>(xp[i]) < get<0>(xp[i+1])) {
            i++;
            continue;
        }
        int tmp = get<0>(xp[i]);
        int j = i + 1;
        for (int k = i+1; k<N; k++) {
            if (get<0>(xp[k]) == tmp) {
                j = k;
            }
            else {
                break;
            }
        }

        vector<P> ud;
        for (int k = i; k <= j; k++) {
            if (U[get<2>(xp[k])] == 'U') ud.push_back(make_pair(get<1>(xp[k]), 0));
            if (U[get<2>(xp[k])] == 'D') ud.push_back(make_pair(get<1>(xp[k]), 1));
        }
        int nud = ud.size();
        if (nud > 1) {
            rep(k,nud - 1) {
                if (ud[k].second == 0 && ud[k+1].second == 1) {
                    ans = min(5 * (ud[k+1].first - ud[k].first), ans);
                    flag = true;
                }
            }
        }

        i = j + 1;
    }

    i = 0;
    while(i < N - 1) {
        if (get<0>(yp[i]) < get<0>(yp[i+1])) {
            i++;
            continue;
        }
        int tmp = get<0>(yp[i]);
        int j = i + 1;
        for (int k = i+1; k<N; k++) {
            if (get<0>(yp[k]) == tmp) {
                j = k;
            }
            else {
                break;
            }
        }

        vector<P> lr;
        for (int k = i; k <= j; k++) {
            if (U[get<2>(yp[k])] == 'L') lr.push_back(make_pair(get<1>(yp[k]), 0));
            if (U[get<2>(yp[k])] == 'R') lr.push_back(make_pair(get<1>(yp[k]), 1));
        }
        int nlr = lr.size();
        if (nlr > 1) {
            rep(k,nlr - 1) {
                if (lr[k].second == 1 && lr[k+1].second == 0) {
                    ans = min(5 * (lr[k+1].first - lr[k].first), ans);
                    flag = true;
                }
            }
        }


        i = j + 1;
    }

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




}


Measured memory_bytes: 16240