#include <bits/stdc++.h>

using namespace std;

using ll = long long;

constexpr char newl = '\n';
constexpr int INF = 1e9;
const string dir = "URDL";

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int n;
    cin >> n;

    vector<int> x(n), y(n), d(n);
    map<int, set<int> > su, sr, sd, sl;
    for (int i = 0; i < n; i++) {
        char u;
        cin >> x[i] >> y[i] >> u;
        d[i] = dir.find(u);
        
        if (u == 'U' || u == 'D') {
            (u == 'U' ? su : sd)[x[i]].insert(y[i]);
        } else {
            (u == 'R' ? sr : sl)[y[i]].insert(x[i]);
        }
    }

    int ans = INF;

    // 正面から激突
    for (int i = 0; i < n; i++) {
        // 正の方向に進むもののみチェック
        if (d[i] > 1) continue;

        if (d[i] == 0) { // U_i == 'U'
            if (sd.find(x[i]) == sd.end()) continue;

            auto it = sd[x[i]].lower_bound(y[i]);
            if (it == sd[x[i]].end()) continue;

            ans = min(ans, (*it - y[i]) * 5);
        } else { // U_i == 'R'
            if (sl.find(y[i]) == sl.end()) continue;

            auto it = sl[y[i]].lower_bound(x[i]);
            if (it == sl[y[i]].end()) continue;

            ans = min(ans, (*it - x[i]) * 5);
        }
    }

    // xに関してソート
    vector<int> ids(n);
    iota(ids.begin(), ids.end(), 0);
    sort(ids.begin(), ids.end(), [&](int i1, int i2){
        return x[i1] < x[i2];
    });

    // 交差して衝突（片方はL）
    // if U_i == 'L':
    //   if U_j == 'U':
    //     x_i > x_j && y_i > y_j && x_i - x_j == y_i - y_j
    //   elif U_j == 'D':
    //     x_i > x_j && y_i < y_j && x_i - x_j == y_j - y_i
    {
        map<int, int> sum_to_x, diff_to_x;
        for (int i : ids) {
            if (d[i] == 1) continue;
            if (d[i] != 3) {
                if (d[i] == 0) {
                    diff_to_x[x[i] - y[i]] = x[i];
                } else {
                    sum_to_x[x[i] + y[i]] = x[i];
                }
                continue;
            }

            if (sum_to_x.find(x[i] + y[i]) != sum_to_x.end()) {
                ans = min(ans, (x[i] - sum_to_x[x[i] + y[i]]) * 10);
            }

            if (diff_to_x.find(x[i] - y[i]) != diff_to_x.end()) {
                ans = min(ans, (x[i] - diff_to_x[x[i] - y[i]]) * 10);
            }
        }
    }
    
    reverse(ids.begin(), ids.end());

    // 交差して衝突（片方はR）
    // if U_i == 'R':
    //   if U_j == 'U':
    //     x_i < x_j && y_i > y_j && x_j - x_i == y_i - y_j
    //   elif U_j == 'D':
    //     x_i < x_j && y_i < y_j && x_j - x_i == y_j - y_i
    {
        map<int, int> sum_to_x, diff_to_x;
        for (int i : ids) {
            if (d[i] == 3) continue;
            if (d[i] != 1) {
                if (d[i] == 2) {
                    diff_to_x[x[i] - y[i]] = x[i];
                } else {
                    sum_to_x[x[i] + y[i]] = x[i];
                }
                continue;
            }

            if (sum_to_x.find(x[i] + y[i]) != sum_to_x.end()) {
                ans = min(ans, (sum_to_x[x[i] + y[i]] - x[i]) * 10);
            }

            if (diff_to_x.find(x[i] - y[i]) != diff_to_x.end()) {
                ans = min(ans, (diff_to_x[x[i] - y[i]] - x[i]) * 10);
            }
        }
    }

    if (ans >= INF) cout << "SAFE\n";
    else cout << ans << newl;
    return 0;
}

Measured memory_bytes: 43480