#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    int n, ans = -1;
    cin >> n;
    vector <int> x(n + 1), y(n + 1);
    vector < vector <int> > A(200002), B(200002);
    vector <char> c(n + 1);
    map <int, vector < pair <int, int> > > m1, m2;
    for (int i = 1; i <= n; i++)
    {
        cin >> x[i] >> y[i] >> c[i];
        A[x[i]].push_back(i);
        B[y[i]].push_back(i);
        m1[x[i] + y[i]].emplace_back( x[i], i );
        m2[x[i] - y[i]].emplace_back( x[i], i );
    }

    for (int i = 0; i <= 200000; i++)
    {
        sort(B[i].begin(), B[i].end(), [&](int a, int b)
        {
                return x[a] < x[b];
        });
        vector <int> v;
        for (int id : B[i])
        {
            if (c[id] == 'L') v.push_back(id);
        }
        reverse(v.begin(), v.end());
        for (int id : B[i])
        {
            if (c[id] == 'L') v.pop_back();
            if (c[id] == 'R' && !v.empty())
            {
                int ret = ((x[v.back()] - x[id]) * 10 + 1) / 2;
                if (ans == -1) ans = ret;
                else ans = min(ans, ret);
            }
        }
        v.clear();
        sort(A[i].begin(), A[i].end(), [&](int a, int b)
        {
                return y[a] < y[b];
        });
        for (int id : A[i])
        {
            if (c[id] == 'D') v.push_back(id);
        }
        reverse(v.begin(), v.end());
        for (int id : A[i])
        {
            if (c[id] == 'D') v.pop_back();
            if (c[id] == 'U' && !v.empty())
            {
                int ret = ((y[v.back()] - y[id]) * 10 + 1) / 2;
                if (ans == -1) ans = ret;
                else ans = min(ans, ret);
            }
        }
    }

    for (auto i : m1)
    {
        vector < pair <int, int> > v = i.second, v2;
        sort(v.begin(), v.end());
        for (int i = (int)v.size() - 1; i >= 0; i--)
        {
            if (c[v[i].second] == 'L') v2.push_back(v[i]);
        }
        for (int i = 0; i < v.size(); i++)
        {
            if (c[v[i].second] == 'L') v2.pop_back();
            if (c[v[i].second] == 'D' && !v2.empty())
            {
                int ret = (y[v[i].second] - y[v2.back().second]) * 10;
                if (ans == -1) ans = ret;
                else ans = min(ans, ret);
            }
        }
        v2.clear();
        for (int i = (int)v.size() - 1; i >= 0; i--)
        {
            if (c[v[i].second] == 'U') v2.push_back(v[i]);
        }
        for (int i = 0; i < v.size(); i++)
        {
            if (c[v[i].second] == 'U') v2.pop_back();
            if (c[v[i].second] == 'R' && !v2.empty())
            {
                int ret = (y[v[i].second] - y[v2.back().second]) * 10;
                if (ans == -1) ans = ret;
                else ans = min(ans, ret);
            }
        }
    }

    for (auto i : m2)
    {
        vector < pair <int, int> > v = i.second, v2;
        sort(v.begin(), v.end());
        for (int i = (int)v.size() - 1; i >= 0; i--)
        {
            if (c[v[i].second] == 'D') v2.push_back(v[i]);
        }
        for (int i = 0; i < v.size(); i++)
        {
            if (c[v[i].second] == 'D') v2.pop_back();
            if (c[v[i].second] == 'R' && !v2.empty())
            {
                int ret = (y[v2.back().second] - y[v[i].second]) * 10;
                if (ans == -1) ans = ret;
                else ans = min(ans, ret);
            }
        }
        v2.clear();
        for (int i = (int)v.size() - 1; i >= 0; i--)
        {
            if (c[v[i].second] == 'L') v2.push_back(v[i]);
        }
        for (int i = 0; i < v.size(); i++)
        {
            if (c[v[i].second] == 'L') v2.pop_back();
            if (c[v[i].second] == 'U' && !v2.empty())
            {
                int ret = (y[v2.back().second] - y[v[i].second]) * 10;
                if (ans == -1) ans = ret;
                else ans = min(ans, ret);
            }
        }
    }

    if (ans == -1)
    {
        cout << "SAFE\n";
        return 0;
    }
    
    cout << ans << endl;
}

Measured memory_bytes: 65336