#include <algorithm>
#include <map>
#include <vector>
#include <iostream>
using namespace std;

int nearest(vector<int> &L, vector<int> &R) {
    if (L.empty()) return 1<<25;
    int i = 0, j = 0;
    int ans = 1<<25;
    while (i < L.size() && j < R.size()) {
        while (j < R.size() && L[i] >= R[j]) j++;
        if (j < R.size())
            ans = min(ans, R[j] - L[i]);
        i++;
    }
    return ans;
}


int main() {
    int n; cin >> n;
    int x[n], y[n];
    char u[n];
    map<int, map<char, vector<int> > > xmy, xpy, xx, yy;
    for (int i = 0; i < n; i++) {
        cin >> x[i] >> y[i] >> u[i];
        xmy[x[i] - y[i]][u[i]].push_back(x[i]);
        xpy[x[i] + y[i]][u[i]].push_back(x[i]);
        if (u[i] == 'U' || u[i] == 'D')
            xx[x[i]][u[i]].push_back(y[i]);
        else
            yy[y[i]][u[i]].push_back(x[i]);
    }
    int ans = 1<<30;

    for (auto &[k, ps] : xpy) {
        for (auto &[k1, v] : ps) sort(v.begin(), v.end());
        ans = min(ans, nearest(ps['R'], ps['U']) * 10);
        ans = min(ans, nearest(ps['D'], ps['L']) * 10);
    }

    for (auto &[k, ps] : xmy) {
        for (auto &[k1, v] : ps) sort(v.begin(), v.end());
        ans = min(ans, nearest(ps['R'], ps['D']) * 10);
        ans = min(ans, nearest(ps['U'], ps['L']) * 10);
    }

    for (auto &[k, ps] : yy) {
        for (auto &[k1, v] : ps) sort(v.begin(), v.end());
        ans = min(ans, nearest(ps['R'], ps['L']) * 5);
    }

    for (auto &[k, ps] : xx) {
        for (auto &[k1, v] : ps) sort(v.begin(), v.end());
        ans = min(ans, nearest(ps['U'], ps['D']) * 5);
    }

    if (ans > 1<<25) cout << "SAFE\n";
    else cout << ans << endl;
}
