#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)
#define VL vector<ll>
#define VS vector<string>
#define VB vector<bool>
#define VP vector<pair<ll,ll>>
#define VVL vector<vector<ll>>
#define VVP vector<vector<pair<ll,ll>>>
#define PL pair<ll,ll>
#define ALL(v) (v).begin(), (v).end()
ll d1[4] = {1, -1, 0, 0};
ll d2[4] = {0, 0, 1, -1};

typedef struct flare {
    ll x, y;
    char u;
    flare () {;};
    flare (ll _x, ll _y, char _u) : x(_x), y(_y), u(_u) {;};
} flare;

int main(){
    ll n;
    cin >> n;
    vector<flare> fl(n);
    rep(i, 0, n) cin >> fl[i].x >> fl[i].y >> fl[i].u;

    ll conflict = 1e18;

    // -> <- のパターン
    {
        map<ll, vector<flare>> mp;
        rep(i, 0, n) {
            mp[fl[i].y].push_back(fl[i]);
        }
        for(auto &e : mp) {
            sort(e.second.begin(), e.second.end(), [](flare l, flare r) {
                if(l.x < r.x) return true;
                else return false;
            });
        }
        for(auto e : mp) {
            ll rightest = -1;
            auto v = e.second;
            rep(i, 0, v.size()) {
                if(v[i].u == 'R') rightest = v[i].x;
                else if(v[i].u == 'L' && rightest != -1) conflict = min(conflict, (10 / 2) * abs(v[i].x - rightest));
            }
        }
    }
    /*
        |
        v

        ^
        |
        のパターン
    */
    {
        map<ll, vector<flare>> mp;
        rep(i, 0, n) {
            mp[fl[i].x].push_back(fl[i]);
        }
        for(auto &e : mp) {
            sort(e.second.begin(), e.second.end(), [](flare l, flare r) {
                if(l.y < r.y) return true;
                else return false;
            });
        }
        for(auto e : mp) {
            ll downest = -1;
            auto v = e.second;
            rep(i, 0, v.size()) {
                if(v[i].u == 'U') downest = v[i].y;
                else if(v[i].u == 'D' && downest != -1) conflict = min(conflict, (10 / 2) * abs(v[i].y - downest));
            }
        }
    }
    /*
    -> 
        ^
        |
        のパターン　y = -x + b ==> b = x + y
    */
    {
        map<ll, vector<flare>> mp;
        rep(i, 0, n) {
            mp[fl[i].x + fl[i].y].push_back(fl[i]);
        }
        for(auto &e : mp) {
            sort(e.second.begin(), e.second.end(), [](flare l, flare r) {
                if(l.y > r.y) return true;
                else return false;
            });
        }
        for(auto e : mp) {
            flare rmax = flare(-1, -1, -1), dmax = flare(-1, -1, -1);
            auto v = e.second;
            rep(i, 0, v.size()) {
                if(v[i].u == 'D') dmax = v[i];
                else if(v[i].u == 'R') rmax = v[i];
                else if(v[i].u == 'U' && rmax.x != -1) conflict = min (conflict, (10) * abs(-v[i].y + rmax.y));
                else if(v[i].u == 'L' && dmax.x != -1) conflict = min (conflict, (10) * abs(-v[i].y + dmax.y));
            }
        }
    }
    /*
          < - 
        ^
        |
        のパターン　y = x + b ==> b = -x + y
    */
    {
        map<ll, vector<flare>> mp;
        rep(i, 0, n) {
            mp[-fl[i].x + fl[i].y].push_back(fl[i]);
        }
        for(auto &e : mp) {
            sort(e.second.begin(), e.second.end(), [](flare l, flare r) {
                if(l.x < r.x) return true;
                else return false;
            });
        }
        for(auto e : mp) {
            flare rmax = flare(-1, -1, -1), umax = flare(-1, -1, -1);
            auto v = e.second;
            rep(i, 0, v.size()) {
                if(v[i].u == 'U') umax = v[i];
                else if(v[i].u == 'R') rmax = v[i];
                else if(v[i].u == 'L' && umax.x != -1) conflict = min (conflict, (10) * abs(-v[i].x + umax.x));
                else if(v[i].u == 'D' && rmax.x != -1) conflict = min (conflict, (10) * abs(-v[i].x + rmax.x));
            }
        }
    }
    
    if(conflict == (ll)1e18) cout << "SAFE" << endl;
    else cout << conflict << endl;
    return 0;
}

Measured memory_bytes: 29496