#include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <numeric>
#include <list>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <tuple>
#include <deque>
#include <complex>

using namespace std;

/*
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
*/

typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<long long, long long> pll;
typedef vector<pll> vpll;

typedef long double ld;
typedef vector<ld> vld;

typedef vector<bool> vb;

#define rep(i, n) for (ll i = 0; i < (n); i++)
#define reps(i, n) for (ll i = 1; i <= (n); i++)
#define rrep(i, n) for (ll i = (n) - 1; i >= 0; i--)
#define rreps(i, n) for (ll i = (n); i >= 1; i--)
#define all(v) (v).begin(), (v).end()

template <class T> void chmin(T& a, T b) { a = min(a, b);}
template <class T> void chmax(T& a, T b) { a = max(a, b);}

constexpr int INF = 1 << 30;
constexpr ll INFL = 1LL << 60;
constexpr ll MOD = 1000000007;
constexpr ld EPS = 1e-12;
ld PI = acos(-1.0);

void solve() {
    ll n;
    cin >> n;
    vll x(n), y(n);
    vector<char> u(n);
    rep(i, n) cin >> x[i] >> y[i] >> u[i];

    const ll size = 400010;
    vvll pos1(size), pos2(size);
    ll ans = INFL;
    for(ll i = 0; i < n; i++) {
        if(u[i] == 'D') pos1[x[i]].push_back(y[i]);
        else if(u[i] == 'U') pos2[x[i]].push_back(y[i]);
    }
    for(ll i = 0; i <= 200000; i++) {
        if(pos1[i].size() == 0 || pos2[i].size() == 0) continue;
        sort(pos1[i].begin(), pos1[i].end());
        sort(pos2[i].begin(), pos2[i].end());
        ll id = 0;
        for(ll iu = 0; iu < pos2[i].size(); iu++) {
            while(id < pos1[i].size() && pos1[i][id] <= pos2[i][iu]) id++;
            if(id >= pos1[i].size()) break;
            chmin(ans, (pos1[i][id] - pos2[i][iu]) * 5);
        }
    }

    pos1.assign(size, vll(0));
    pos2.assign(size, vll(0));
    for(ll i = 0; i < n; i++) {
        if(u[i] == 'L') pos1[y[i]].push_back(x[i]);
        else if(u[i] == 'R') pos2[y[i]].push_back(x[i]);
    }
    for(ll i = 0; i <= 200000; i++) {
        if(pos1[i].size() == 0 || pos2[i].size() == 0) continue;
        sort(pos1[i].begin(), pos1[i].end());
        sort(pos2[i].begin(), pos2[i].end());
        ll id = 0;
        for(ll iu = 0; iu < pos2[i].size(); iu++) {
            while(id < pos1[i].size() && pos1[i][id] <= pos2[i][iu]) id++;
            if(id >= pos1[i].size()) break;
            chmin(ans, (pos1[i][id] - pos2[i][iu]) * 5);
        }
    }

    pos1.assign(size, vll(0));
    pos2.assign(size, vll(0));
    for(ll i = 0; i < n; i++) {
        if(u[i] == 'U') pos1[x[i] + y[i]].push_back(x[i]);
        else if(u[i] == 'R') pos2[x[i] + y[i]].push_back(x[i]);
    }
    for(ll i = 0; i <= 400000; i++) {
        if(pos1[i].size() == 0 || pos2[i].size() == 0) continue;
        sort(pos1[i].begin(), pos1[i].end());
        sort(pos2[i].begin(), pos2[i].end());
        ll id = 0;
        for(ll iu = 0; iu < pos2[i].size(); iu++) {
            while(id < pos1[i].size() && pos1[i][id] <= pos2[i][iu]) id++;
            if(id >= pos1[i].size()) break;
            chmin(ans, (pos1[i][id] - pos2[i][iu]) * 10);
        }
    }

    pos1.assign(size, vll(0));
    pos2.assign(size, vll(0));
    for(ll i = 0; i < n; i++) {
        if(u[i] == 'L') pos1[x[i] + y[i]].push_back(x[i]);
        else if(u[i] == 'D') pos2[x[i] + y[i]].push_back(x[i]);
    }
    for(ll i = 0; i <= 400000; i++) {
        if(pos1[i].size() == 0 || pos2[i].size() == 0) continue;
        sort(pos1[i].begin(), pos1[i].end());
        sort(pos2[i].begin(), pos2[i].end());
        ll id = 0;
        for(ll iu = 0; iu < pos2[i].size(); iu++) {
            while(id < pos1[i].size() && pos1[i][id] <= pos2[i][iu]) id++;
            if(id >= pos1[i].size()) break;
            chmin(ans, (pos1[i][id] - pos2[i][iu]) * 10);
        }
    }

    pos1.assign(size, vll(0));
    pos2.assign(size, vll(0));
    for(ll i = 0; i < n; i++) {
        if(u[i] == 'D') pos1[x[i] - y[i] + 200000].push_back(x[i]);
        else if(u[i] == 'R') pos2[x[i] - y[i] + 200000].push_back(x[i]);
    }
    for(ll i = 0; i <= 400000; i++) {
        if(pos1[i].size() == 0 || pos2[i].size() == 0) continue;
        sort(pos1[i].begin(), pos1[i].end());
        sort(pos2[i].begin(), pos2[i].end());
        ll id = 0;
        for(ll iu = 0; iu < pos2[i].size(); iu++) {
            while(id < pos1[i].size() && pos1[i][id] <= pos2[i][iu]) id++;
            if(id >= pos1[i].size()) break;
            chmin(ans, (pos1[i][id] - pos2[i][iu]) * 10);
        }
    }

    pos1.assign(size, vll(0));
    pos2.assign(size, vll(0));
    for(ll i = 0; i < n; i++) {
        if(u[i] == 'L') pos1[x[i] - y[i] + 200000].push_back(x[i]);
        else if(u[i] == 'U') pos2[x[i] - y[i] + 200000].push_back(x[i]);
    }
    for(ll i = 0; i <= 400000; i++) {
        if(pos1[i].size() == 0 || pos2[i].size() == 0) continue;
        sort(pos1[i].begin(), pos1[i].end());
        sort(pos2[i].begin(), pos2[i].end());
        ll id = 0;
        for(ll iu = 0; iu < pos2[i].size(); iu++) {
            while(id < pos1[i].size() && pos1[i][id] <= pos2[i][iu]) id++;
            if(id >= pos1[i].size()) break;
            chmin(ans, (pos1[i][id] - pos2[i][iu]) * 10);
        }
    }

    if(ans < 1e10) cout << ans << endl;
    else cout << "SAFE" << endl;
    return;
}

int main() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    solve();
}

Measured memory_bytes: 41104