#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//typedef __int128_t LL;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define F first
#define S second
#define pb push_back
mt19937 rnd;

struct Point {
    int x, y, t;
    bool operator<(const Point& ot) const {
        return std::tie(x, y) < std::tie(ot.x, ot.y);
    }
};

const int N = 2e5 + 10;
const int M = N + N + 5;
const int inf = 1e9 + 10;
vector<char> vch = {'*', 'U', 'R', 'D', 'L'};
Point a[N];
vector<Point> ver[N], hor[N];
vector<Point> rig_up[M], rig_down[M];
vector<Point> lef_up[M], lef_down[M];

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
#endif

    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i].x >> a[i].y;
        char ch;
        cin >> ch;
        for (int j = 1; j <= 4; j++) {
            if (ch == vch[j]) {
                a[i].t = j;
                break;
            }
        }
    }
    for (int i = 1; i <= n; i++) {
        int r = a[i].x - a[i].y + N;
        int l = a[i].x + a[i].y;
        if (a[i].t & 1) {
            ver[a[i].x].pb(a[i]);
            if (a[i].t == 1) {
                rig_up[r].pb(a[i]);
                lef_up[l].pb(a[i]);
            }
            else {
                rig_down[r].pb(a[i]);
                lef_down[l].pb(a[i]);
            }
        }
        else {
            hor[a[i].y].pb(a[i]);
            if (a[i].t == 2) {
                rig_down[r].pb(a[i]);
                lef_up[l].pb(a[i]);
            }
            else {
                rig_up[r].pb(a[i]);
                lef_down[l].pb(a[i]);
            }
        }
    }
    int ans = inf;
    for (int i = 0; i < N; i++) {
        sort(hor[i].begin(), hor[i].end());
        sort(ver[i].begin(), ver[i].end());
        for (int j = 0; j < int(ver[i].size()) - 1; j++) {
            if (ver[i][j].t == 1 && ver[i][j + 1].t == 3) {
                ans = min(ans, 5 * (ver[i][j + 1].y - ver[i][j].y));
            }
        }
        for (int j = 0; j < int(hor[i].size()) - 1; j++) {
            if (hor[i][j].t == 2 && hor[i][j + 1].t == 4) {
                ans = min(ans, 5 * (hor[i][j + 1].x - hor[i][j].x));
            }
        }
    }
    for (int i = 0; i < M; i++) {
        sort(rig_up[i].begin(), rig_up[i].end());
        sort(rig_down[i].begin(), rig_down[i].end());
        sort(lef_up[i].begin(), lef_up[i].end());
        sort(lef_down[i].begin(), lef_down[i].end());
        for (int j = 0; j < int(rig_up[i].size()) - 1; j++) {
            if (rig_up[i][j].t == 1 && rig_up[i][j + 1].t == 4) {
                ans = min(ans, 10 * (rig_up[i][j + 1].x - rig_up[i][j].x));
            }
        }
        for (int j = 0; j < int(rig_down[i].size()) - 1; j++) {
            if (rig_down[i][j].t == 2 && rig_down[i][j + 1].t == 3) {
                ans = min(ans, 10 * (rig_down[i][j + 1].x - rig_down[i][j].x));
            }
        }
        for (int j = 0; j < int(lef_up[i].size()) - 1; j++) {
            if (lef_up[i][j].t == 2 && lef_up[i][j + 1].t == 1) {
                ans = min(ans, 10 * (lef_up[i][j + 1].x - lef_up[i][j].x));
            }
        }
        for (int j = 0; j < int(lef_down[i].size()) - 1; j++) {
            if (lef_down[i][j].t == 3 && lef_down[i][j + 1].t == 4) {
                ans = min(ans, 10 * (lef_down[i][j + 1].x - lef_down[i][j].x));
            }
        }
    }
    if (ans == inf) {
        cout << "SAFE\n";
    }
    else {
        cout << ans << "\n";
    }

#ifdef LOCAL
    cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
    return 0;
}


Measured memory_bytes: 71268