#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <array>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <stack>
#include <queue>
#include <bitset>
#include <tuple>
#include <cmath>
#include <complex>
#include <algorithm>
#include <utility>
#include <regex>
#include <cstdint>
#include <numeric>
#include <functional>
#include <cassert>

using namespace std;

namespace utils{
#define ALL(x) begin(x), end(x)
#define RALL(x) rbegin(x), rend(x)
    ///----- aliases
    using i32 = int32_t;
    using u32 = uint32_t;
    using i64 = int64_t;
    using u64 = uint64_t;
    using ll = i64;
    using ull = u64;

    using f32 = float;
    using f64 = double;

    template<class T, class Compare> using p_queue = priority_queue<T, vector<T>, Compare>;
    template<class T> using min_queue = p_queue<T, greater<T>>;
    template<class T> using max_queue = p_queue<T, less<T>>;

    template<class T> inline bool CHMIN(T& X, const T& A){ if(X > A) {X = A; return true;} return false; }
    template<class T> inline bool CHMAX(T& X, const T& A){ if(X < A) {X = A; return true;} return false; }

    ///----- vector I/O
    template<class T>
    vector<T> VEC(size_t n, T t){
        return vector<T>(n, t);
    }

    template<class ...Ts>
    auto VEC(size_t n, Ts ... ts){
        return vector<decltype(VEC(ts...))>(n, VEC(ts...));
    }

    template<class T>
    vector<T> VIN(istream& is, size_t n){
        vector<T> v(n);
        for (int i = 0; i < n; ++i) {
            is >> v[i];
        } // end i
        return v;
    }

    template<class T>
    auto VIN(istream& is, size_t n, size_t ts...){
        auto v = vector<decltype(VIN<T>(is, ts))>(n);
        for (auto &&x : v) {
            x = VIN<T>(is, ts);
        } // end x
        return v;
    }

    template<class T>
    bool VOUT(ostream& os, vector<T>& v){
        for (auto &&x : v) {
            os << x  << ' ';
        } // end x
        return !!os;
    }

    template<class T>
    bool VOUT(ostream& os, vector<vector<T>>& v, char endc = '\n'){
        for (auto &&vs : v) {
            VOUT(os, vs);
            os << endc;
        } // end vs
        return !!os;
    }

    ///----- tuple I/O
    template <class S, class T>
    istream& operator>>(istream& is, tuple<S, T>& t){
        return is >> get<0>(t) >> get<1>(t);
    }

    template <class S, class T, class U>
    istream& operator>>(istream& is, tuple<S, T, U>& t){
        return is >> get<0>(t) >> get<1>(t) >> get<2>(t);
    }

    template <class S, class T>
    ostream& operator<<(ostream& os, const tuple<S, T>& t){
        return os << get<0>(t) << ' ' <<  get<1>(t);
    }

    template <class S, class T, class U>
    ostream& operator<<(ostream& os, const tuple<S, T, U>& t){
        return os << get<0>(t) << ' ' <<  get<1>(t) << ' ' <<  get<2>(t);
    }

    ///----- constants
    constexpr i64 INFLL   = 1'000'000'000'000'000'020ll;
    constexpr i32 INF     = 1'000'000'009;
    constexpr f64 PI      = 3.14'159'265'358'979'323'846;
    constexpr f64 EPS     = 1e-12;
}
using namespace utils;


class solver{
    istream& is;
    ostream& os;

public:
    solver(istream& I, ostream& O) :is(I), os(O) {}

//    map<tuple<int, int>, int> vec_xy;
//    map<tuple<int, int>, int> vec_uv;
    vector<vector<set<int>>> vec_xy;
    vector<vector<vector<set<int>>>> vec_uv;
    bool input() {
        map<char, int> urdl;
        urdl['L'] = 0;
        urdl['R'] = 1;
        urdl['D'] = 2;
        urdl['U'] = 3;
        int N = 0;
        is >> N;
        vec_xy = VEC(4, 200002, set<int>());
        vec_uv = VEC(4, 400004, 2, set<int>());
        for (int i = 0; i < N; ++i) {
            int x, y;
            char u;
            is >> x >> y >> u;
            int s = y-x + 200002;
            int t = x+y;
            if(u == 'L'){
                vec_xy[0][y].insert(x);
                vec_uv[0][t][0].insert(s);
                vec_uv[2][s][1].insert(t);
            }
            if(u == 'R'){
                vec_xy[1][y].insert(x);
                vec_uv[1][s][0].insert(t);
                vec_uv[3][t][1].insert(s);
            }
            if(u == 'D'){
                vec_xy[2][x].insert(y);
                vec_uv[0][t][1].insert(s);
                vec_uv[1][s][1].insert(t);
            }
            if(u == 'U'){
                vec_xy[3][x].insert(y);
                vec_uv[2][s][0].insert(t);
                vec_uv[3][t][0].insert(s);
            }
        } // end i
        return !!is;
    }

    void run();
};

void solver::run(){
    if(!input()) return;
    ll miss = INF;
    for (int i = 0; i < vec_xy[0].size(); ++i) {
        for (auto &&y : vec_xy[1][i]) {
            auto ptr = vec_xy[0][i].lower_bound(y);
            if(ptr == vec_xy[0][i].end()) continue;
            ll t = *vec_xy[0][i].lower_bound(y) - y;
            if(t >= 0) CHMIN(miss, t);
        } // end y
        for (auto &&y : vec_xy[3][i]) {
            auto ptr = vec_xy[2][i].lower_bound(y);
            if(ptr == vec_xy[2][i].end()) continue;
            ll t = *vec_xy[2][i].lower_bound(y) - y;
            if(t >= 0) CHMIN(miss, t);
        } // end y
    } // end i
    for (int i = 0; i < vec_uv[0].size(); ++i) {
        for (int k = 0; k < 4; ++k) {
            for (auto &&y : vec_uv[k][i][0]) {
                auto ptr = vec_uv[k][i][1].lower_bound(y);
                if(ptr == vec_uv[k][i][1].end()) continue;
                ll t = *ptr - y;
                if(t >= 0) CHMIN(miss, t);
            } // end y
        } // end k
    } // end i

    if(miss == INF){
        cout << "SAFE" << endl;
        return;
    }
    cout << miss * 5 << endl;
}

int main(int argc, char *argv[]) {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << setprecision(16) << scientific;
#ifdef XELMH
    string test_cases = "test_F.txt";
    cerr << test_cases << " -->" << endl;
    auto fs = fstream(test_cases, fstream::in);
    int loop = 0;
    while(fs) {
        loop++;
        cout << '#' << loop << "#------\n";
        solver(fs, cout).run();
    }
    if(loop <= 1) {
        cout << "===" << endl;
        while(cin) solver(cin, cout).run();
    }
#else
    solver(cin, cout).run();
#endif
    return 0;
}


Measured memory_bytes: 306460