#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <memory>
#include <complex>
#include <numeric>
#include <cstdio>
#include <iomanip>
#include <random>

#define REP(i,m,n) for(int i=int(m);i<int(n);i++)
#define RREP(i,m,n) for(int i=int(n)-1;i>=int(m);--i)
#define EACH(i,c) for (auto &(i): c)
#define all(c) begin(c),end(c)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort(begin(c),end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
//#define int long long

#ifdef LOCAL
#define DEBUG(s) cout << (s) << endl
#define dump(x)  cerr << #x << " = " << (x) << endl
#define BR cout << endl;
#else
#define DEBUG(s) do{}while(0)
#define dump(x) do{}while(0)
#define BR 
#endif
using namespace std;

using UI = unsigned int;
using UL = unsigned long;
using LL = long long;
using ULL = unsigned long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<LL>;
using VVLL = vector<VLL>;
using VS = vector<string>;
using PII = pair<int,int>;
using VP = vector<PII>;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

template<class T> inline T sqr(T x) {return x*x;}

void solve() {
    int n;
    cin >> n;
    VI x(n), y(n), u(n);
    char c;
    REP(i,0,n) {
        cin >> x[i] >> y[i] >> c;
        if (c == 'U') u[i] = 0;
        if (c == 'R') u[i] = 1;
        if (c == 'D') u[i] = 2;
        if (c == 'L') u[i] = 3;
    }
    vector<map<int, set<int>>> xs(4);
    vector<map<int, set<int>>> ys(4);
    vector<map<int, set<int>>> rs(4);
    vector<map<int, set<int>>> ls(4);
    REP(i,0,n) {
        xs[u[i]][x[i]].insert(y[i]);
        ys[u[i]][y[i]].insert(x[i]);
        rs[u[i]][x[i]-y[i]+200000].insert(x[i]);
        ls[u[i]][x[i]+y[i]].insert(x[i]);
    }
    int ans = 1 << 30;
    REP(i,0,n) {
        if (u[i] == 0) {
            {
                auto it = xs[2][x[i]].lower_bound(y[i]);
                if (it != xs[2][x[i]].end()) chmin(ans, abs(*it - y[i]) * 5);
            }
            {
                auto it = rs[3][x[i]-y[i]+200000].lower_bound(x[i]);
                if (it != rs[3][x[i]-y[i]+200000].end()) chmin(ans, abs(*it - x[i]) * 10);
            }
            {
                auto it = ls[1][x[i]+y[i]].upper_bound(x[i]);
                if (it != ls[1][x[i]+y[i]].begin()) {
                    --it;
                    chmin(ans, abs(*it - x[i]) * 10);
                }
            }
        }
        if (u[i] == 2) {
            {
                auto it = xs[0][x[i]].upper_bound(y[i]);
                if (it != xs[0][x[i]].begin()) {
                    --it;
                    chmin(ans, abs(*it - y[i]) * 5);
                }
            }
            {
                auto it = rs[1][x[i]-y[i]+200000].upper_bound(x[i]);
                if (it != rs[1][x[i]-y[i]+200000].begin()) {
                    --it;
                    chmin(ans, abs(*it - x[i]) * 10);
                }
            }
            {
                auto it = ls[3][x[i]+y[i]].lower_bound(x[i]);
                if (it != ls[3][x[i]+y[i]].end()) {
                    chmin(ans, abs(*it - x[i]) * 10);
                }
            }
        }
        if (u[i] == 1) {
            {
                auto it = ys[3][y[i]].lower_bound(x[i]);
                if (it != ys[3][y[i]].end()) chmin(ans, abs(*it - x[i]) * 5);
            }
            {
                auto it = rs[2][x[i]-y[i]+200000].lower_bound(x[i]);
                if (it != rs[2][x[i]-y[i]+200000].end()) chmin(ans, abs(*it - x[i]) * 10);
            }
            {
                auto it = ls[0][x[i]+y[i]].lower_bound(x[i]);
                if (it != ls[0][x[i]+y[i]].end()) {
                    chmin(ans, abs(*it - x[i]) * 10);
                }
            }
        }
        if (u[i] == 3) {
            {
                auto it = ys[1][y[i]].upper_bound(x[i]);
                if (it != ys[1][y[i]].begin()) {
                    --it;
                    chmin(ans, abs(*it - x[i]) * 5);
                }
            }
            {
                auto it = rs[0][x[i]-y[i]+200000].upper_bound(x[i]);
                if (it != rs[0][x[i]-y[i]+200000].begin()) {
                    --it;
                    chmin(ans, abs(*it - x[i]) * 10);
                }
            }
            {
                auto it = ls[2][x[i]+y[i]].upper_bound(x[i]);
                if (it != ls[2][x[i]+y[i]].begin()) {
                    --it;
                    chmin(ans, abs(*it - x[i]) * 10);
                }
            }
        }
        // dump(ans);
    }

    if (ans == 1<<30) cout << "SAFE" << endl;
    else cout << ans << endl;
}

signed main() {
    solve();
    
    return 0;
}

Measured memory_bytes: 153796