#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<int(n);++i)
#define all(a) (a).begin(),(a).end()
typedef long long ll;
#define rep1(i,n) for(int i=1;i<=int(n);++i)
#define lb(s,x) (lower_bound(all(s),x)-s.begin())
#define ub(s,x) (upper_bound(all(s),x)-s.begin())
#define uniq(a) a.erase(unique(all(a)),a.end())
#define bit(k) (1LL<<(k))
#define INF int(1e9)

template<class T,class U> ostream& operator<<(ostream& ost, const pair<T,U> &p) { ost << "(" << p.first << ", " << p.second << ")"; return ost; }
#define ostream_container { ost << "{"; for(const auto &t : v) { if (&t != &*v.begin()) ost << ", "; ost << t; } ost << "}"; return ost; }
template<class T> ostream& operator<<(ostream &ost, const vector<T> &v) ostream_container
template<class T> ostream& operator<<(ostream &ost, const set<T> &v) ostream_container
template<class T,class U> ostream& operator<<(ostream &ost, const map<T,U> &v) ostream_container

template<class H> void recursive_debug(string s, H h) {
    cerr << "\033[33m" << s << " = " << h << endl << "\033[m";
}
template<class H,class... T> void recursive_debug(string s, H h, T... t) {
    int comma = s.find(',');
    cerr << "\033[33m" << s.substr(0,comma) << " = " << h << ", ";
    recursive_debug(s.substr(comma+1), t...);
}
#define debug(...) recursive_debug(#__VA_ARGS__, __VA_ARGS__)

struct Point
{
    ll x,y;
    char dir;
    bool operator<(const Point &rhs) const {
        return x != rhs.x ? x < rhs.x : y < rhs.y;
    }
};

int main(void) {
    int n;
    cin >> n;
    vector<Point> vp(n);
    map<ll, set<Point>> sub_DR, sub_UL, add_LD, add_UR, ver_U, ver_D, hor_L, hor_R;
    rep(i,n) {
        Point p;
        cin >> p.x >> p.y >> p.dir;
        vp[i] = p;
        sub_DR[p.y - p.x].insert(p);
        sub_UL[p.y - p.x].insert(p);
        add_LD[p.y + p.x].insert(p);
        add_UR[p.y + p.x].insert(p);
        if (p.dir == 'U') ver_U[p.x].insert(p);
        if (p.dir == 'D') ver_D[p.x].insert(p);
        if (p.dir == 'L') hor_L[p.y].insert(p);
        if (p.dir == 'R') hor_R[p.y].insert(p);
    }

    ll res = INF;
    for (auto itr : sub_DR) {
        set<Point> &line = itr.second;
        Point pre = {0,0,'?'};
        for (auto &p : line) {
            if (pre.dir == 'R' && p.dir == 'D') {
                // debug(__LINE__);
                res = min(res, abs(pre.x-p.x) * 10);
            }
            pre = p;
        }
    }

    for (auto itr : sub_UL) {
        set<Point> &line = itr.second;
        Point pre = {0,0,'?'};
        for (auto &p : line) {
            if (pre.dir == 'U' && p.dir == 'L') {
                // debug(__LINE__);
                res = min(res, abs(pre.x-p.x) * 10);
            }
            pre = p;
        }
    }

    for (auto itr : add_LD) {
        set<Point> &line = itr.second;
        Point pre = {0,0,'?'};
        for (auto &p : line) {
            if (pre.dir == 'D' && p.dir == 'L') {
                // debug(__LINE__);
                res = min(res, abs(pre.x-p.x) * 10);
            }
            pre = p;
        }
    }

    for (auto itr : add_UR) {
        set<Point> &line = itr.second;
        Point pre = {0,0,'?'};
        for (auto &p : line) {
            if (pre.dir == 'R' && p.dir == 'U') {
                // debug(__LINE__);
                res = min(res, abs(pre.x-p.x) * 10);
            }
            pre = p;
        }
    }

    for (auto itr : ver_U) {
        ll k = itr.first;
        if (ver_D.find(k) == ver_D.end()) continue;
        set<Point> &line = itr.second;
        for (auto &p : line) {
            auto lb = ver_D[k].lower_bound(p);
            if (lb == ver_D[k].end()) break;
            // debug(__LINE__);
            res = min(res, abs(p.y - lb->y) * 5);
        }
    }

    for (auto itr : hor_R) {
        ll k = itr.first;
        if (hor_L.find(k) == hor_L.end()) continue;
        set<Point> &line = itr.second;
        for (auto &p : line) {
            auto lb = hor_L[k].lower_bound(p);
            if (lb == hor_L[k].end()) break;
            // debug(__LINE__);
            res = min(res, abs(p.x - lb->x) * 5);
        }
    }
    if (res == INF) {
        cout << "SAFE" << endl;
    } else {
        cout << res << endl;
    }

    return 0;
}


Measured memory_bytes: 162632