#include <bits/stdc++.h>
using namespace std;
#define all(n) begin(n), end(n)
const long long INF = numeric_limits<long long>::max();
typedef long long ll;
typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef unsigned long long ull;
template <class T>
bool chmax(T &a, const T &b)
{
    if (a < b)
    {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
bool chmin(T &a, const T &b)
{
    if (b < a)
    {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T>
vector<T> make_v(size_t a) { return vector<T>(a); }

template <typename T, typename... Ts>
auto make_v(size_t a, Ts... ts)
{
    return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type
fill_v(T &t, const V &v) { t = v; }

template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type
fill_v(T &t, const V &v)
{
    for (auto &e : t)
        fill_v(e, v);
}
const int MAX = 200010;
int main()
{
    int N;
    cin >> N;
    map<char, map<int, set<int>>> vert, hor, dvert, dhor;
    for (size_t i = 0; i < N; i++)
    {
        int x, y;
        char u;
        cin >> x >> y >> u;
        if (u == 'U' or u == 'D')
        {
            vert[u][x].insert(y);
        }
        else
        {
            hor[u][y].insert(x);
        }
        int x2 = x + y, y2 = -x + y;
        dvert[u][x2].insert(y2);
        dhor[u][y2].insert(x2);
    }
    int t = INT_MAX;
    for (auto &&i : vert['U'])
    {
        int x = i.first;
        for (auto &&y : i.second)
        {
            auto itr = vert['D'][x].lower_bound(y);
            if (itr == vert['D'][x].end())
                continue;
            int y2 = *itr;
            chmin(t, (y2 - y) * 5);
        }
    }
    for (auto &&i : hor['R'])
    {
        int y = i.first;
        for (auto &&x : i.second)
        {
            auto itr = hor['L'][y].lower_bound(x);
            if (itr == hor['L'][y].end())
                continue;
            int x2 = *itr;
            chmin(t, (x2 - x) * 5);
        }
    }
/*    for (auto &&i : dvert['R'])
    {
        int dx = i.first;
        for (auto &&dy : i.second)
        {
            auto itr = dvert['D'][dx].lower_bound(dy);
            if (itr == dvert['D'][dx].end())
                continue;
            int dy2 = *itr;
            chmin(t, (dy2 - dy) * 5);
        }
        for (auto &&dy : i.second)
        {
            auto itr = dvert['U'][dx].lower_bound(dy);
            if (itr == dvert['U'][dx].end())
                continue;
            int dy2 = *itr;
            chmin(t, (dy2 - dy) * 5);
        }
    }
*/
    for (auto &&i : dvert['L'])
    {
        int dx = i.first;
        for (auto &&dy : i.second)
        {
            auto itr = dvert['D'][dx].lower_bound(dy);
            if (itr == dvert['D'][dx].end())
                continue;
            int dy2 = *itr;
            chmin(t, (dy2 - dy) * 5);
        }
/*        for (auto &&dy : i.second)
        {
            auto itr = dvert['U'][dx].lower_bound(dy);
            if (itr == dvert['U'][dx].end())
                continue;
            int dy2 = *itr;
            chmin(t, (dy2 - dy) * 5);
        }*/
    }
    for (auto &&i : dvert['U'])
    {
        int dx = i.first;
/*        for (auto &&dy : i.second)
        {
            auto itr = dvert['L'][dx].lower_bound(dy);
            if (itr == dvert['L'][dx].end())
                continue;
            int dy2 = *itr;
            chmin(t, (dy2 - dy) * 5);
        }*/
        for (auto &&dy : i.second)
        {
            auto itr = dvert['R'][dx].lower_bound(dy);
            if (itr == dvert['R'][dx].end())
                continue;
            int dy2 = *itr;
            chmin(t, (dy2 - dy) * 5);
        }
    }
/*    for (auto &&i : dvert['D'])
    {
        int dx = i.first;
        for (auto &&dy : i.second)
        {
            auto itr = dvert['L'][dx].lower_bound(dy);
            if (itr == dvert['L'][dx].end())
                continue;
            int dy2 = *itr;
            chmin(t, (dy2 - dy) * 5);
        }
        for (auto &&dy : i.second)
        {
            auto itr = dvert['R'][dx].lower_bound(dy);
            if (itr == dvert['R'][dx].end())
                continue;
            int dy2 = *itr;
            chmin(t, (dy2 - dy) * 5);
        }
    }*/
    for (auto &&i : dhor['U'])
    {
        int dy = i.first;
        for (auto &&dx : i.second)
        {
            auto itr = dhor['L'][dy].lower_bound(dx);
            if (itr == dhor['L'][dy].end())
                continue;
            int dx2 = *itr;
            chmin(t, (dx2 - dx) * 5);
        }
/*        for (auto &&dx : i.second)
        {
            auto itr = dhor['R'][dy].lower_bound(dx);
            if (itr == dhor['R'][dy].end())
                continue;
            int dx2 = *itr;
            chmin(t, (dx2 - dx) * 5);
        }*/
    }
/*    for (auto &&i : dhor['D'])
    {
        int dy = i.first;
        for (auto &&dx : i.second)
        {
            auto itr = dhor['L'][dy].lower_bound(dx);
            if (itr == dhor['L'][dy].end())
                continue;
            int dx2 = *itr;
            chmin(t, (dx2 - dx) * 5);
        }
        for (auto &&dx : i.second)
        {
            auto itr = dhor['R'][dy].lower_bound(dx);
            if (itr == dhor['R'][dy].end())
                continue;
            int dx2 = *itr;
            chmin(t, (dx2 - dx) * 5);
        }
    }*/
    for (auto &&i : dhor['R'])
    {
        int dy = i.first;
        for (auto &&dx : i.second)
        {
            auto itr = dhor['D'][dy].lower_bound(dx);
            if (itr == dhor['D'][dy].end())
                continue;
            int dx2 = *itr;
            chmin(t, (dx2 - dx) * 5);
        }
/*        for (auto &&dx : i.second)
        {
            auto itr = dhor['U'][dy].lower_bound(dx);
            if (itr == dhor['U'][dy].end())
                continue;
            int dx2 = *itr;
            chmin(t, (dx2 - dx) * 5);
        }*/
    }
/*    for (auto &&i : dhor['L'])
    {
        int dy = i.first;
        for (auto &&dx : i.second)
        {
            auto itr = dhor['D'][dy].lower_bound(dx);
            if (itr == dhor['D'][dy].end())
                continue;
            int dx2 = *itr;
            chmin(t, (dx2 - dx) * 5);
        }
        for (auto &&dx : i.second)
        {
            auto itr = dhor['U'][dy].lower_bound(dx);
            if (itr == dhor['U'][dy].end())
                continue;
            int dx2 = *itr;
            chmin(t, (dx2 - dx) * 5);
        }
    }*/

    if (t == INT_MAX)
        cout << "SAFE";
    else
        cout << t;
    return 0;
}

Measured memory_bytes: 106596