#include <bits/stdc++.h>

using namespace std;

int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};

int main() {
    ios_base::sync_with_stdio(false);
    cout.precision(12);
    cout << fixed;
    cin.tie(0);

    int n;
    cin >> n;
    vector<vector<int> > vx;
    vector<vector<int> > vy;
    for (int i = 0; i < n; i++) {
        int x, y, dir;
        char c;
        cin >> x >> y >> c;
        if (c == 'U') {
            dir = 3;
        } else if (c == 'D') {
            dir = 1;
        } else if (c == 'L') {
            dir = 0;
        } else if (c == 'R') {
            dir = 2;
        }
        vx.push_back({x,y,dir});
        vy.push_back({y,x,dir});
    }
    sort(vx.begin(), vx.end());
    sort(vy.begin(), vy.end());

    map<int, int> side;
    map<int, int> dp;
    map<int, int> dn;
    int ans = 1e9;

    side.clear();
    for (int i = 0; i < vx.size(); i++) {
        const vector<int> & v = vx[i];
        const int & x = v[0];
        const int & y = v[1];
        const int & dir = v[2];
        if (dir == 2) {
            side[y] = x;
        }
        if (dir == 0) {
            if (side.find(y) != side.end()) {
                int cand = x - side[y];
                ans = min(ans, cand * 5);
            }
        }
    }

    side.clear();
    for (int i = 0; i < vy.size(); i++) {
        const vector<int> & v = vy[i];
        const int & y = v[0];
        const int & x = v[1];
        const int & dir = v[2];
        if (dir == 3) {
            side[x] = y;
        }
        if (dir == 1) {
            if (side.find(x) != side.end()) {
                int cand = y - side[x];
                ans = min(ans, cand * 5);
            }
        }
    }

    dp.clear();
    dn.clear();
    for (int i = 0; i < vx.size(); i++) {
        const vector<int> & v = vx[i];
        const int & x = v[0];
        const int & y = v[1];
        const int & dir = v[2];
        int cdp = x + y;
        int cdn = x - y;
        if (dir == 1) {
            if (dn.find(cdn) != dn.end()) {
                int cand = x - dn[cdn];
                ans = min(ans, cand * 10);
            }
        }
        if (dir == 3) {
            if (dp.find(cdp) != dp.end()) {
                int cand = x - dp[cdp];
                ans = min(ans, cand * 10);
            }
        }
        if (dir == 2) {
            dp[cdp] = x;
            dn[cdn] = x;
        }
    }

    dp.clear();
    dn.clear();
    for (int i = vx.size() - 1; i >= 0; i--) {
        const vector<int> & v = vx[i];
        const int & x = v[0];
        const int & y = v[1];
        const int & dir = v[2];
        int cdp = x + y;
        int cdn = x - y;
        if (dir == 3) {
            if (dn.find(cdn) != dn.end()) {
                int cand = dn[cdn] - x;
                ans = min(ans, cand * 10);
            }
        }
        if (dir == 1) {
            if (dp.find(cdp) != dp.end()) {
                int cand = dp[cdp] - x;
                ans = min(ans, cand * 10);
            }
        }
        if (dir == 0) {
            dp[cdp] = x;
            dn[cdn] = x;
        }
    }

    if (ans == 1e9) {
        cout << "SAFE\n";
    } else {
        cout << ans << endl;
    }

    return 0;
}


Measured memory_bytes: 34280