#define _CRT_SECURE_NO_WARNINGS
#pragma comment(linker, "/STACK:256000000")
#define _USE_MATH_DEFINES
#include<iostream>
#include<vector>
#include<string>
#include<stack>
#include<algorithm>
#include<cmath>
#include<set>
#include<queue>
#include<sstream>
#include<utility>
#include<map>
#include<ctime>
#include<cstdio>
#include<cassert>
#include<functional>
#include<unordered_map>
#include<deque>
#include<cmath>





using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef pair<char, char> pcc;
typedef pair<double, double> pdd;

#define show(x) cerr << x
#define debug(x) show(#x << ": " << (x) << endl)

const long double PI = 3.14159265358979323846;
const long double eps = 1e-5;
const ll INF = numeric_limits<int>::max();
const ll LINF = numeric_limits<ll>::max();
const ll mod = 998244353;

int get(vector<vector<int> >& a) {
    for (int i = 0; i < 4; ++i) {
        sort(a[i].begin(), a[i].end());
    }
    int res = INF;
    for (int i = 0; i < a[0].size(); ++i) {
        int id = lower_bound(a[1].begin(), a[1].end(), a[0][i]) - a[1].begin();
        if (id < a[1].size())
            res = min(res, 10 * (a[1][id] - a[0][i]));
    }

    for (int i = 0; i < a[2].size(); ++i) {
        int id = lower_bound(a[3].begin(), a[3].end(), a[2][i]) - a[3].begin() - 1;
        if (id >= 0)
            res = min(res, 10 * (a[2][i] - a[3][id]));
    }
    return res;

}


int get2(vector<vector<int> >& a) {
    for (int i = 0; i < 4; ++i) {
        sort(a[i].begin(), a[i].end());
    }
    int res = INF;
    for (int i = 0; i < a[0].size(); ++i) {
        int id = lower_bound(a[3].begin(), a[3].end(), a[0][i]) - a[3].begin();
        if (id < a[3].size())
            res = min(res, 10 * (a[3][id] - a[0][i]));
    }

    for (int i = 0; i < a[2].size(); ++i) {
        int id = lower_bound(a[1].begin(), a[1].end(), a[2][i]) - a[1].begin() - 1;
        if (id >= 0)
            res = min(res, 10 * (a[2][i] - a[1][id]));
    }
    return res;

}

int get3(vector<vector<int> >& a) {
    for (int i = 0; i < 4; ++i) {
        sort(a[i].begin(), a[i].end());
    }
    int res = INF;
    for (int i = 0; i < a[0].size(); ++i) {
        int id = lower_bound(a[2].begin(), a[2].end(), a[0][i]) - a[2].begin();
        if (id < a[2].size())
            res = min(res, 5 * (a[2][id] - a[0][i]));
    }
    return res;

}

int get4(vector<vector<int> >& a) {
    for (int i = 0; i < 4; ++i) {
        sort(a[i].begin(), a[i].end());
    }
    int res = INF;
    for (int i = 0; i < a[1].size(); ++i) {
        int id = lower_bound(a[3].begin(), a[3].end(), a[1][i]) - a[3].begin();
        if (id < a[3].size())
            res = min(res, 5 * (a[3][id] - a[1][i]));
    }
    return res;

}

void solve() {
    int n;
    cin >> n;
    vector<int> x(n), y(n), dr(n);
    vector<string> s(n);
    vector<vector<int> > pl(600000+10), mn(400000 + 10);
    vector<vector<int> > xx(200000 + 10), yy(200000 + 10);
    for (int i = 0; i < n; ++i) {
        cin >> x[i] >> y[i] >> s[i];
        xx[x[i]].push_back(i);
        yy[y[i]].push_back(i);
        y[i] += 200000;
        if (s[i] == "U")
            dr[i] = 0;
        if (s[i] == "R")
            dr[i] = 1;
        if (s[i] == "D")
            dr[i] = 2;
        if (s[i] == "L")
            dr[i] = 3;
        pl[x[i] + y[i]].push_back(i);
        mn[y[i] - x[i]].push_back(i);
    }


    int res = INF;


    vector<vector<int> > a(4);
    for (int i = 0; i < xx.size(); ++i) {
        if (xx[i].size() >= 2) {
            a.assign(4, vector<int>());
            for (int j = 0; j < xx[i].size(); ++j) {
                int id = xx[i][j];
                a[dr[id]].push_back(y[id]);
            }
            res = min(res, get3(a));
        }
    }
    for (int i = 0; i < yy.size(); ++i) {
        if (yy[i].size() >= 2) {
            a.assign(4, vector<int>());
            for (int j = 0; j < yy[i].size(); ++j) {
                int id = yy[i][j];
                a[dr[id]].push_back(x[id]);
            }
            res = min(res, get4(a));
        }
    }



    for (int i = 0; i < pl.size(); ++i) {
        if (pl[i].size() >= 2) {
            a.assign(4, vector<int>());
            for (int j = 0; j < pl[i].size(); ++j) {
                int id = pl[i][j];
                a[dr[id]].push_back(y[id]);
            }
            res = min(res, get(a));
        }
    }
    for (int i = 0; i < mn.size(); ++i) {
        if (mn[i].size() >= 2) {
            a.assign(4, vector<int>());
            for (int j = 0; j < mn[i].size(); ++j) {
                int id = mn[i][j];
                a[dr[id]].push_back(y[id]);
            }
            res = min(res, get2(a));
        }
    }
    if (res == INF) {
        cout << "SAFE\n";
    }
    else {
        cout << res << endl;
    }
}


//#define LOCAL

int main() {
    ios_base::sync_with_stdio(0);cin.tie(nullptr);cout.tie(nullptr);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
#endif
    solve();



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


Measured memory_bytes: 63296