#include <iostream>
#include <vector>
#include <algorithm>
#include <set>

using namespace std;

#define x first
#define y second

const int INF = 1000000000;

bool por1(pair<int, int> a, pair<int, int> b)
{
    if (a.y < b.y)
        return true;
    if (a.y > b.y)
        return false;
    return a.x < b.x;
}

bool por2(pair<int, int> a, pair<int, int> b)
{
    if (a.y > b.y)
        return true;
    if (a.y < b.y)
        return false;
    return a.x < b.x;
}

int main()
{
    ios_base::sync_with_stdio(0);
    int n;
    cin >> n;
    vector< pair<int, int> > L, P, G, D;
    for (int i = 0; i < n; ++i)
    {
        int X, Y;
        char k;
        cin >> X >> Y >> k;
        if (k == 'U')
            G.push_back(make_pair(X, Y));
        if (k == 'D')
            D.push_back(make_pair(X, Y));
        if (k == 'L')
            L.push_back(make_pair(X, Y));
        if (k == 'R')
            P.push_back(make_pair(X, Y));
    }
    int odp = INF;
    sort(D.begin(), D.end(), por1);
    sort(P.begin(), P.end(), por1);
    set< pair<int, int> > S;
    int j = 0;
    for (int i = 0; i < D.size(); ++i)
    {
        while (j < P.size() && P[j].y <= D[i].y)
        {
            S.insert(make_pair(P[j].x - P[j].y, -P[j].y));
            ++j;
        }
        if (S.lower_bound(make_pair(D[i].x - D[i].y, -INF)) != S.end())
        {
            pair<int, int> akt = *S.lower_bound(make_pair(D[i].x - D[i].y, -INF));
            if (akt.first == D[i].x - D[i].y)
                odp = min(odp, (D[i].y - -akt.y) * 10);
        }
    }
    sort(L.begin(), L.end(), por1);
    S.clear();
    j = 0;
    for (int i = 0; i < D.size(); ++i)
    {
        while (j < L.size() && L[j].y <= D[i].y)
        {
            S.insert(make_pair(L[j].x + L[j].y, -L[j].y));
            ++j;
        }
        if (S.lower_bound(make_pair(D[i].x + D[i].y, -INF)) != S.end())
        {
            pair<int, int> akt = *S.lower_bound(make_pair(D[i].x + D[i].y, -INF));
            if (akt.first == D[i].x + D[i].y)
                odp = min(odp, (D[i].y - -akt.y) * 10);
        }
    }
    sort(G.begin(), G.end(), por2);
    sort(P.begin(), P.end(), por2);
    S.clear();
    j = 0;
    for (int i = 0; i < G.size(); ++i)
    {
        while (j < P.size() && P[j].y >= G[i].y)
        {
            S.insert(make_pair(P[j].x + P[j].y, P[j].y));
            ++j;
        }
        if (S.lower_bound(make_pair(G[i].x + G[i].y, -INF)) != S.end())
        {
            pair<int, int> akt = *S.lower_bound(make_pair(G[i].x + G[i].y, -INF));
            if (akt.first == G[i].x + G[i].y)
                odp = min(odp, (akt.y - G[i].y) * 10);
        }
    }
    sort(L.begin(), L.end(), por2);
    S.clear();
    j = 0;
    for (int i = 0; i < G.size(); ++i)
    {
        while (j < L.size() && L[j].y >= G[i].y)
        {
            S.insert(make_pair(L[j].x - L[j].y, L[j].y));
            ++j;
        }
        if (S.lower_bound(make_pair(G[i].x - G[i].y, -INF)) != S.end())
        {
            pair<int, int> akt = *S.lower_bound(make_pair(G[i].x - G[i].y, -INF));
            if (akt.first == G[i].x - G[i].y)
                odp = min(odp, (akt.y - G[i].y) * 10);
        }
    }
    vector< vector<int> > yG(200001), yD(200001);
    for (int i = 0; i < G.size(); ++i)
        yG[G[i].x].push_back(G[i].y);
    for (int i = 0; i < D.size(); ++i)
        yD[D[i].x].push_back(D[i].y);
    for (int i = 0; i < 200001; ++i)
    {
        sort(yG[i].begin(), yG[i].end());
        sort(yD[i].begin(), yD[i].end());
        int k = 0;
        for (int j = 0; j < yD[i].size(); ++j)
        {
            k = max(k, 0);
            while (k < yG[i].size() && yG[i][k] <= yD[i][j])
                ++k;
            --k;
            if (k < yG[i].size() && k >= 0)
                odp = min(odp, (yD[i][j] - yG[i][k]) * 5);
        }
    }
    vector< vector<int> > xP(200001), xL(200001);
    for (int i = 0; i < P.size(); ++i)
        xP[P[i].y].push_back(P[i].x);
    for (int i = 0; i < L.size(); ++i)
        xL[L[i].y].push_back(L[i].x);
    for (int i = 0; i < 200001; ++i)
    {
        sort(xP[i].begin(), xP[i].end());
        sort(xL[i].begin(), xL[i].end());
        int k = 0;
        for (int j = 0; j < xL[i].size(); ++j)
        {
            k = max(k, 0);
            while (k < xP[i].size() && xP[i][k] <= xL[i][j])
                ++k;
            --k;
            if (k < xP[i].size() && k >= 0)
                odp = min(odp, (xL[i][j] - xP[i][k]) * 5);
        }
    }
    if (odp == INF)
        cout << "SAFE";
    else
        cout << odp;
    return 0;
}


Measured memory_bytes: 32124