#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

#define $(x) (int)((x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define endl '\n'
#define x first
#define y second
#define int long long
#define double long double
#define pii pair<int, int>
#define pb push_back
#define vec vector
#define beg begin
#define dbg(x) cout << #x << " = " << x << endl;

template<class T> ostream& operator<<(ostream &str, vector<T> &a) {
    for (auto &i : a) {
        str << i << " ";
    }
    return str;
}

template<class T> istream& operator>>(istream &str, vector<T> &a) {
    for (auto &i : a) {
        str >> i;
    }
    return str;
}

template<class T> ostream& operator<<(ostream &str, pair<T, T> &a) {
    str << a.first << " " << a.second;
    return str;
}

template<class T> istream& operator>>(istream &str, pair<T, T> &a) {
    str >> a.first >> a.second;
    return str;
}

void solve(); signed main() {
#ifdef LOCAL
    freopen("C:\\Users\\artma\\CLionProjects\\problems\\input.txt", "r", stdin);
#endif
    srand(time(0));
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cout.setf(ios::fixed); cout.precision(6);
    solve();
    return 0;
}

const int MOD = 1e9 + 7, MAXN = 1e6 + 10, INF = 1e18 + 10, BASE = 37;

void solve() {
    int n;
    cin >> n;
    vec<vec<int>> p(n);
    for (int i = 0; i < n; ++i) {
        int x, y;
        char ch;
        cin >> x >> y >> ch;
        if (ch == 'U') {
            p[i] = {x, y, 0};
        }
        else if (ch == 'R') {
            p[i] = {x, y, 1};
        }
        else if (ch == 'D') {
            p[i] = {x, y, 2};
        }
        else {
            p[i] = {x, y, 3};
        }
    }

    map<pii, vec<vec<int>>> mp;
    for (auto it : p) {
        int x = it[0], y = it[1];
        mp[{1, y - x}].pb(it);
        mp[{-1, y + x}].pb(it);
    }

    int ans = INF;
    map<int, vec<vec<int>>> X, Y;
    for (auto it : p) {
        if (it[2] == 0 || it[2] == 2) {
            X[it[0]].pb(it);
        }
    }
    for (auto it : p) {
        if (it[2] == 1 || it[2] == 3) {
            Y[it[1]].pb({it[1], it[0], it[2]});
        }
    }

    for (auto &it : X) {
        sort(all(it.y));
        int last = -INF;
        for (int i = 0; i < $(it.y); ++i) {
            if (it.y[i][2] == 0) {
                last = it.y[i][1];
                break;
            }
        }
        for (int i = 0; i < $(it.y); ++i) {
            if (it.y[i][2] == 2) {
                if (it.y[i][1] - last < 0) continue;
                ans = min(ans, it.y[i][1] - last);
            }
            else {
                last = max(last, it.y[i][1]);
            }
        }
    }
    for (auto &it : Y) {
        sort(all(it.y));
        int last = -INF;
        for (int i = 0; i < $(it.y); ++i) {
            if (it.y[i][2] == 1) {
                last = it.y[i][1];
                break;
            }
        }
        for (int i = 0; i < $(it.y); ++i) {
            if (it.y[i][2] == 3) {
                if (it.y[i][1] - last < 0) continue;
                ans = min(ans, it.y[i][1] - last);
            }
            else {
                last = max(last, it.y[i][1]);
            }
        }
    }

    for (auto line : mp) {
        if (line.x.x == -1) {
            vec<pii> UR, DL;
            for (auto it : line.y) {
                if (it[2] == 0 || it[2] == 1) {
                    UR.pb({it[0], it[2]});
                }
                else {
                    DL.pb({it[0], it[2]});
                }
            }
            sort(all(UR));
            sort(all(DL));
            int last = -INF;
            for (auto it : UR) {
                if (it.y == 1) {
                    last = it.x;
                    break;
                }
            }
            for (auto it : UR) {
                if (it.y == 1) {
                    last = max(it.x, last);
                }
                else {
                    if (it.x - last < 0) continue;
                    ans = min(ans, 2 * (it.x - last));
                }
            }
            last = -INF;
            for (auto it : DL) {
                if (it.y == 2) {
                    last = it.x;
                    break;
                }
            }
            for (auto it : DL) {
                if (it.y == 2) {
                    last = max(it.x, last);
                }
                else {
                    if (it.x - last < 0) continue;
                    ans = min(ans, 2 * (it.x - last));
                }
            }
        }
        else {
            vec<pii> UL, DR;
            for (auto it : line.y) {
                if (it[2] == 0 || it[2] == 3) {
                    UL.pb({it[0], it[2]});
                }
                else {
                    DR.pb({it[0], it[2]});
                }
            }
            sort(all(UL));
            sort(all(DR));
            int last = -INF;
            for (auto it : UL) {
                if (it.y == 0) {
                    last = it.x;
                    break;
                }
            }
            for (auto it : UL) {
                if (it.y == 0) {
                    last = max(it.x, last);
                }
                else {
                    if (it.x - last < 0) continue;
                    ans = min(ans, 2 * (it.x - last));
                }
            }
            last = -INF;
            for (auto it : DR) {
                if (it.y == 1) {
                    last = it.x;
                    break;
                }
            }
            for (auto it : DR) {
                if (it.y == 1) {
                    last = max(it.x, last);
                }
                else {
                    if (it.x - last < 0) continue;
                    ans = min(ans, 2 * (it.x - last));
                }
            }
        }
    }

    if (ans == INF) cout << "SAFE" << endl;
    else cout << ans * 5 << endl;
}

Measured memory_bytes: 98620