#include <algorithm>
#include <iostream>
#include <vector>
#include <math.h>
#include <set>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <bitset>
#include <complex>
#include <unistd.h>
#include <cassert>
#include <cctype>
#include <random>
#include <time.h>
#include <functional>
#define _USE_MATH_DEFINES
#define _GLIBCXX_DEBUG
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plglg;
typedef pair<double, ll> pdlg;
typedef tuple<int, int, int> tiii;
typedef tuple<ll, ll, ll> tlglglg;
typedef tuple<ll, ll, ll, ll> tlglglglg;
typedef tuple<double, double, double> tddd;
typedef complex<double> xy_t;
typedef vector<ll> vll;
typedef vector< vector<ll> > matrix;
typedef unsigned int uint;
typedef unsigned long long ull;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
template <class T> using VVV = V<VV<T>>;
#define REP(i, x, y) for(ll i = (ll)x; i < (ll)y; i++)
#define DREP(i, x, y, d) for(ll i = (ll)x; i < (ll)y; i += (ll)d)
#define PER(i, x, y) for(ll i = (ll)x; i > (ll)y; i--)
#define DPER(i, x, y, d) for(ll i = (ll)x; i > (ll)y; i -= (ll)d)
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
double pi = 3.141592653589793;
ll mod = 1000000007;
int intmax = 2147483647;
int intmin = -2147483648;
ll llmax = 9223372036854775807;
ll llmin = -9223372036854775807;
int iinf = intmax / 8;
ll inf = llmax / 8;
double eps = 1e-12;
void yesno(bool ok, string yes = "Yes", string no = "No") {
    if (ok) {
        cout << yes << endl;
    } else {
        cout << no << endl;
    }
}

V<plglg> updown[200010];
V<plglg> skew[400010];

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll N;
    cin >> N;
    ll X[N], Y[N];
    char U[N];
    map<char, ll> mp;
    mp['U'] = 0;
    mp['R'] = 1;
    mp['D'] = 2;
    mp['L'] = 3;
    tlglglg data[N];
    ll colli = inf;
    REP(i, 0, N) {
        cin >> X[i] >> Y[i] >> U[i];
        data[i] = tlglglg(X[i], Y[i], mp[U[i]]);
    }
    REP(loop, 0, 4) {
        sort(data, data + N);
        REP(i, 0, 200010) {
            updown[i].clear();
        }
        REP(i, 0, N) {
            ll u = get<2>(data[i]);
            if (u == 0 || u == 2) {
                ll x = get<0>(data[i]);
                ll y = get<1>(data[i]);
                updown[x].push_back(plglg(y, u));
            }
        }
        REP(i, 0, 200010) {
            sort(updown[i].begin(), updown[i].end());
            REP(j, 0, updown[i].size() - 1) {
                if (updown[i][j].second == 0 && updown[i][j + 1].second == 2) {
                    colli = min(colli, (updown[i][j + 1].first - updown[i][j].first) * 5);
                }
            }
        }
        REP(i, 0, 400010) {
            skew[i].clear();
        }
        REP(i, 0, N) {
            ll u = get<2>(data[i]);
            if (u == 0 || u == 1) {
                ll x = get<0>(data[i]);
                ll y = get<1>(data[i]);
                skew[x + y].push_back(plglg(y, u));
            }
        }
        REP(i, 0, 400010) {
            sort(skew[i].begin(), skew[i].end());
            REP(j, 0, skew[i].size() - 1) {
                if (skew[i][j].second == 0 && skew[i][j + 1].second == 1) {
                    colli = min(colli, (skew[i][j + 1].first - skew[i][j].first) * 10);
                }
            }
        }
        REP(i, 0, N) {
            ll x = get<0>(data[i]);
            ll y = get<1>(data[i]);
            ll u = get<2>(data[i]);
            data[i] = tlglglg(y, 200000 - x, (u + 1) % 4);
        }
    }
    if (colli == inf) {
        cout << "SAFE" << endl;
    } else {
        cout << colli << endl;
    }
}


Measured memory_bytes: 42100