//#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>

using namespace std;

//vector string deque break continue
#define forn(i, s, f) for (int i = (int)s; i < (int)f; i++)
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair <int, int>
#define fs first
#define sc second
#define pf push_front
#define pb push_back
#define pop_f pop_front
#define pop_b pop_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()

#ifdef DEBUG
#else
    #define cerr if (false) cerr
#endif

template <typename T> istream& operator>>(istream& in, vector <T>& a) {for (auto& i : a) in >> i; return in;}
template <typename T> ostream& operator<<(ostream& out, vector <T>& a)  {for (auto& i : a) out << i << " "; return out;}
template <typename T, typename U> void chkmin(T& a, U b) {if (a > b) a = b;}
template <typename T, typename U> void chkmax(T& a, U b) {if (a < b) a = b;}

#define x first
#define y second

const int INF = 1e9;

int main() {
    ios_base::sync_with_stdio(0), cin.tie(0);
    int n;
    cin >> n;
    vector <pii> cords(n);
    vector <char> dir(n);
    map <int, vector <int>> a, b;
    forn (i, 0, n) {
        cin >> cords[i].x >> cords[i].y >> dir[i];
        int x = cords[i].x, y = cords[i].y;
        a[x + y].pb(i), b[x - y].pb(i);
    }
    int ans = INF;
    for (auto& [sum, points] : a) {
        cerr << sum << " " << points << "\n";
        vector <int> l, r;
        set <int> u, d;
        for (auto i : points) {
            int x = cords[i].x;
            if (dir[i] == 'R')
                r.pb(x);
            if (dir[i] == 'L')
                l.pb(x);
            if (dir[i] == 'U')
                u.insert(x);
            if (dir[i] == 'D')
                d.insert(x);
        }
        for (auto x : l) {
            auto i = d.lower_bound(x);
            if (i == d.begin())
                continue;
            --i;
            chkmin(ans, 10 * (x - *i));
        }
        for (auto x : r) {
            auto i = u.upper_bound(x);
            if (i == u.end())
                continue;
            chkmin(ans, 10 * (*i - x));
        }
    }
    cerr << "\n" << ans << "\n";;
    for (auto& [sum, points] : b) {
        cerr << sum << " " << points << "\n";
        vector <int> l, r;
        set <int> u, d;
        for (auto i : points) {
            int x = cords[i].x;
            if (dir[i] == 'R')
                r.pb(x);
            if (dir[i] == 'L')
                l.pb(x);
            if (dir[i] == 'U')
                u.insert(x);
            if (dir[i] == 'D')
                d.insert(x);
        }
        for (auto x : r) {
            auto i = d.upper_bound(x);
            if (i == d.end())
                continue;
            chkmin(ans, 10 * (*i - x));
        }

        for (auto x : l) {
            auto i = u.lower_bound(x);
            if (i == u.begin())
                continue;
            --i;
            chkmin(ans, 10 * (x - *i));
        }
    }

    map <int, set <int>> u, r, l, d;
    forn (i, 0, n) {
        int x = cords[i].x, y = cords[i].y;
        if (dir[i] == 'R')
            r[y].insert(x);
        if (dir[i] == 'L')
            l[y].insert(x);
        if (dir[i] == 'U')
            u[x].insert(y);
        if (dir[i] == 'D')
            d[x].insert(y);
    }
    forn (i, 0, n) {
        int x = cords[i].x, y = cords[i].y;
        if (dir[i] == 'R') {
            auto j = l[y].upper_bound(x);
            if (j != l[y].end())
                chkmin(ans, 5 * (*j - x));
        }
        if (dir[i] == 'L') {
            auto j = r[y].lower_bound(x);
            if (j != r[y].begin())
                chkmin(ans, 5 * (x - *--j));
        }
        if (dir[i] == 'U') {
            auto j = d[x].upper_bound(y);
            if (j != d[x].end())
                chkmin(ans, 5 * (*j - y));
        }
        if (dir[i] == 'D') {
            auto j = u[x].lower_bound(y);
            if (j != u[x].begin())
                chkmin(ans, 5 * (y - *--j));
        }
    }
    if (ans == INF) 
        cout << "SAFE";
    else
        cout << ans;
    return 0;
}

Measured memory_bytes: 94796