#include <bits/stdc++.h>
#define int long long
using pii=std::pair<int,int>;
using namespace std;

const int maxn = 2e5 + 25, reqshift = 2e5 + 5;

int n, x[maxn], y[maxn];
char u[maxn];
vector<int> heading[4][maxn];
vector<int> diag[2][4][2 * maxn];  // {UL, LD, DR, RU}
int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> n;
    for(int i = 0; i < n; i++)
    {
        cin >> x[i] >> y[i] >> u[i];
        if(u[i] == 'U')
        {
            heading[0][x[i]].push_back(y[i]);
            diag[0][0][y[i] - x[i] + reqshift].push_back(x[i]);
            diag[1][3][x[i] + y[i]].push_back(-y[i]);
        }
        else if(u[i] == 'D')
        {
            heading[1][x[i]].push_back(y[i]);
            diag[0][2][y[i] - x[i] + reqshift].push_back(-x[i]);
            diag[1][1][x[i] + y[i]].push_back(y[i]);
        }
        else if(u[i] == 'R')
        {
            heading[2][y[i]].push_back(x[i]);
            diag[0][3][x[i] + y[i]].push_back(-y[i]);
            diag[1][2][y[i] - x[i] + reqshift].push_back(-x[i]);
        }
        else if(u[i] == 'L')
        {
            heading[3][y[i]].push_back(x[i]);
            diag[0][1][x[i] + y[i]].push_back(y[i]);
            diag[1][0][y[i] - x[i] + reqshift].push_back(x[i]);
        }
    }
    int ans = 2e18;
    for(int i = 0; i < maxn; i++)
        if(!heading[0][i].empty() && !heading[1][i].empty())
        {
            sort(heading[0][i].begin(), heading[0][i].end());
            sort(heading[1][i].begin(), heading[1][i].end());
            for(auto x : heading[0][i])
            {
                auto y = upper_bound(heading[1][i].begin(), heading[1][i].end(), x);
                if(y != heading[1][i].end())
                    ans = min(ans, 5 * abs(*y - x));
            }
        }
    for(int i = 0; i < maxn; i++)
        if(!heading[2][i].empty() && !heading[3][i].empty())
        {
            sort(heading[2][i].begin(), heading[2][i].end());
            sort(heading[3][i].begin(), heading[3][i].end());
            for(auto x : heading[2][i])
            {
                auto y = upper_bound(heading[3][i].begin(), heading[3][i].end(), x);
                if(y != heading[3][i].end())
                    ans = min(ans, 5 * abs(*y - x));
            }
        }
    for(int i = 0; i < 2 * maxn; i++)
        if(!diag[0][0][i].empty() && !diag[1][0][i].empty())
        {
            sort(diag[0][0][i].begin(), diag[0][0][i].end());
            sort(diag[1][0][i].begin(), diag[1][0][i].end());
            for(auto x : diag[0][0][i])
            {
                auto y = upper_bound(diag[1][0][i].begin(),diag[1][0][i].end(), x);
                if(y != diag[1][0][i].end())
                    ans = min(ans, 10 * abs(*y - x));
            }
        }
    for(int i = 0; i < 2 * maxn; i++)
        if(!diag[0][1][i].empty() && !diag[1][1][i].empty())
        {
            sort(diag[0][1][i].begin(), diag[0][1][i].end());
            sort(diag[1][1][i].begin(), diag[1][1][i].end());
            for(auto x : diag[0][1][i])
            {
                auto y = upper_bound(diag[1][1][i].begin(),diag[1][1][i].end(), x);
                if(y != diag[1][1][i].end())
                    ans = min(ans, 10 * abs(*y - x));
            }
        }
    for(int i = 0; i < 2 * maxn; i++)
        if(!diag[0][2][i].empty() && !diag[1][2][i].empty())
        {
            sort(diag[0][2][i].begin(), diag[0][2][i].end());
            sort(diag[1][2][i].begin(), diag[1][2][i].end());
            for(auto x : diag[0][2][i])
            {
                auto y = upper_bound(diag[1][2][i].begin(),diag[1][2][i].end(), x);
                if(y != diag[1][2][i].end())
                    ans = min(ans, 10 * abs(*y - x));
            }
        }
    for(int i = 0; i < 2 * maxn; i++)
        if(!diag[0][3][i].empty() && !diag[1][3][i].empty())
        {
            sort(diag[0][3][i].begin(), diag[0][3][i].end());
            sort(diag[1][3][i].begin(), diag[1][3][i].end());
            for(auto x : diag[0][3][i])
            {
                auto y = upper_bound(diag[1][3][i].begin(),diag[1][3][i].end(), x);
                if(y != diag[1][3][i].end())
                    ans = min(ans, 10 * abs(*y - x));
            }
        }
    if(ans < 1e18)
        cout << ans << "\n";
    else
        cout << "SAFE\n";
    return 0;
}

Measured memory_bytes: 119372