#include <iostream>
#include <iomanip>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <array>
#include <tuple>
#include <string>
#include <algorithm>
#include <functional> // greater など
#include <cmath>      // abs など
#include <numeric>    // accumulate, gcd など
#include <cassert>    // assert
#include <stdexcept>
#include <bitset>
using namespace std;

// #include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
// typedef int128_t ll;
// typedef cpp_int ll;

// GCC, 配列のアクセスなど
#define _GLIBCXX_DEBUG
// Clang, 配列のアクセスなど
#define _LIBCPP_DEBUG 0

//////////////// //////////////// //////////////// //////////////// ////////////////
//////////////// //////////////// //////////////// //////////////// ////////////////

// constexpr inline ll mod = 1'000'000'007;
constexpr int INF = 10000'000;
constexpr int max_n = 200'000 + 5;

int n;
vector<int> xs(max_n), ys(max_n), us(max_n);
// 向き -> x座標 -> y座標
vector<map<int, vector<int>>> pts(8);
// 縦方向
vector<map<int, vector<int>>> ptsv(4);
// u,r,d,l -> y = x + b
vector<int> pl = { 6, 5, 2, 1 };
// u,r,d,l -> y = -x + b
vector<int> mi = { 3, 0, 7, 4 };

int main()
{
    // 高速な入出力
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << setprecision(15); // 浮動小数の表示の精度

    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> xs[i] >> ys[i];
        char c; cin >> c;
        switch (c)
        {
        case 'U':
            us[i] = 0;
            break;
        case 'R':
            us[i] = 1;
            break;
        case 'D':
            us[i] = 2;
            break;
        case 'L':
            us[i] = 3;
            break;
        }
    }

    for (int i = 0; i < n; i++) {
        int x = (us[i] & 1) ? ys[i] : xs[i];
        int y = (us[i] & 1) ? xs[i] : ys[i];
        ptsv[us[i]][x].push_back(y);
    }
    for (int i = 0; i < n; i++) {
        int idx = pl[us[i]];
        int b = ys[i] - xs[i];
        pts[idx][b].push_back(ys[i]);

        idx = mi[us[i]];
        b = ys[i] + xs[i];
        pts[idx][b].push_back(ys[i]);
    }
    for (int j = 0; j < 4; j++) {
        for (auto &tup : ptsv[j]) {
            auto &vec = tup.second;
            sort(vec.begin(), vec.end());
        }
    }
    for (int j = 0; j < 8; j++)
    {
        for (auto &tup : pts[j])
        {
            auto &vec = tup.second;
            sort(vec.begin(), vec.end());
        }
    }

    int tmpres = INF;
    for (int i = 0; i < n; i++) {
        int j = (us[i] + 2) % 4;
        int sgn = (us[i] < 2) ? 1 : -1;
        int x = (us[i] & 1) ? ys[i] : xs[i];
        int y = (us[i] & 1) ? xs[i] : ys[i];
        auto &vec = ptsv[j][x];
        if (sgn == 1) {
            auto it = lower_bound(vec.begin(), vec.end(), y + sgn);
            if (it != vec.end()) {
                tmpres = min(tmpres, (*it - y) *5);
            }
        } else {
            auto it = lower_bound(vec.rbegin(), vec.rend(), y + sgn, [](int c, int d) { return c > d; });
            if (it != vec.rend())
            {
                tmpres = min(tmpres, (y - *it) *5);
            }
        }

        j = 2 * us[i];
        sgn = ((j + 1) % 8 < 4) ? 1 : -1;
        y = ys[i];
        int b = ys[i] - sgn * xs[i];
        auto &vec2 = pts[j][b];
        if (sgn == 1)
        {
            auto it = lower_bound(vec2.begin(), vec2.end(), y + sgn);
            if (it != vec2.end())
            {
                tmpres = min(tmpres, (*it - y) * 10);
            }
        }
        else
        {
            auto it = lower_bound(vec2.rbegin(), vec2.rend(), y + sgn, [](int c, int d){return c > d;});
            if (it != vec2.rend())
            {
                tmpres = min(tmpres, (y - *it) * 10);
            }
        }

        j = 2 * us[i] + 1;
        sgn = ((j + 1) % 8 < 4) ? 1 : -1;
        y = ys[i];
        b = ys[i] - sgn * xs[i];
        auto &vec3 = pts[j][b];
        if (sgn == 1)
        {
            auto it = lower_bound(vec3.begin(), vec3.end(), y + sgn);
            if (it != vec3.end())
            {
                tmpres = min(tmpres, (*it - y) * 10);
            }
        }
        else
        {
            auto it = lower_bound(vec3.rbegin(), vec3.rend(), y + sgn, [](int c, int d) { return c > d; });
            if (it != vec3.rend())
            {
                tmpres = min(tmpres, (y - *it) * 10);
            }
        }
    }
    if (tmpres == INF) {
        cout << "SAFE\n";
    } else {
        cout << tmpres << endl;
    }

    return 0;
}


Measured memory_bytes: 116916