#define INF 10000000000
#define MOD 1000000007

#if 1   // use_templates

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define rep(i, up) for (ll i = 0; i < up; ++i)
#define repp(i, up) for (ll i = 0; i <= up; ++i)
#define rrep(i, lo, up) for (ll i = lo; i < up; ++i)
#define rrepp(i, lo, up) for (ll i = lo; i <= up; ++i)
#define eep(i, lo, up) for ((i) = (lo); (i) < (up); ++(i))
#define eepp(i, lo, up) for ((i) = (lo); (i) <= (up); ++(i))


ll gcd(ll a, ll b);

ll gcd(ll a, ll b)
{
    if (a < b) {
        return gcd(b, a);
    }
    ll r;
    while (r = a % b) {
        a = b;
        b = r;
    }
    return b;
}

#endif  // end of use_templates


struct plane
{
    ll x, y;
    char u;
};

int main()
{
    ll n;
    scanf("%lld", &n);
    vector<plane> p(n);
    rep(i, n) {
        scanf("%lld%lld %c", &p[i].x, &p[i].y, &p[i].u);
    }
    map<ll, vector<plane>> sums, diffs, xs, ys;
    rep(i, n) {
        sums[p[i].x + p[i].y].push_back(p[i]);
        diffs[p[i].x - p[i].y].push_back(p[i]);
        switch (p[i].u) {
        case 'L':
        case 'R':
            xs[p[i].y].push_back(p[i]);
            break;
        default:
            ys[p[i].x].push_back(p[i]);
            break;
        }
    }
    ll result = INF;
    for (auto it = sums.begin(); it != sums.end(); ++it) {
        if (it->second.size() <= 0) {
            continue;
        }
        vector<plane> hs, vs;
        for (auto it2 = it->second.begin(); it2 != it->second.end(); ++it2) {
            switch (it2->u) {
            case 'D':
                hs.push_back(*it2);
                break;
            case 'L':
                vs.push_back(*it2);
                break;
            }
        }
        if (hs.size() <= 0 || vs.size() <= 0) {
            continue;
        }
        sort(hs.begin(), hs.end(), [](plane left, plane right) { return left.x < right.x; });
        sort(vs.begin(), vs.end(), [](plane left, plane right) { return left.x < right.x; });
        for (auto ith = hs.begin(), itv = vs.begin(); ith != hs.end(); ++ith) {
            while (itv != vs.end() && itv->x < ith->x) {
                ++itv;
            }
            if (itv != vs.end()) {
                result = min(result, (itv->x - ith->x) * 2);
            }
        }
    }
    for (auto it = sums.begin(); it != sums.end(); ++it) {
        if (it->second.size() <= 0) {
            continue;
        }
        vector<plane> hs, vs;
        for (auto it2 = it->second.begin(); it2 != it->second.end(); ++it2) {
            switch (it2->u) {
            case 'R':
                hs.push_back(*it2);
                break;
            case 'U':
                vs.push_back(*it2);
                break;
            }
        }
        if (hs.size() <= 0 || vs.size() <= 0) {
            continue;
        }
        sort(hs.begin(), hs.end(), [](plane left, plane right) { return left.x < right.x; });
        sort(vs.begin(), vs.end(), [](plane left, plane right) { return left.x < right.x; });
        for (auto ith = hs.begin(), itv = vs.begin(); ith != hs.end(); ++ith) {
            while (itv != vs.end() && itv->x < ith->x) {
                ++itv;
            }
            if (itv != vs.end()) {
                result = min(result, (itv->x - ith->x) * 2);
            }
        }
    }
    for (auto it = diffs.begin(); it != diffs.end(); ++it) {
        if (it->second.size() <= 0) {
            continue;
        }
        vector<plane> hs, vs;
        for (auto it2 = it->second.begin(); it2 != it->second.end(); ++it2) {
            switch (it2->u) {
            case 'R':
                hs.push_back(*it2);
                break;
            case 'D':
                vs.push_back(*it2);
                break;
            }
        }
        if (hs.size() <= 0 || vs.size() <= 0) {
            continue;
        }
        sort(hs.begin(), hs.end(), [](plane left, plane right) { return left.x < right.x; });
        sort(vs.begin(), vs.end(), [](plane left, plane right) { return left.x < right.x; });
        for (auto ith = hs.begin(), itv = vs.begin(); ith != hs.end(); ++ith) {
            while (itv != vs.end() && itv->x < ith->x) {
                ++itv;
            }
            if (itv != vs.end()) {
                result = min(result, (itv->x - ith->x) * 2);
            }
        }
    }
    for (auto it = diffs.begin(); it != diffs.end(); ++it) {
        if (it->second.size() <= 0) {
            continue;
        }
        vector<plane> hs, vs;
        for (auto it2 = it->second.begin(); it2 != it->second.end(); ++it2) {
            switch (it2->u) {
            case 'U':
                hs.push_back(*it2);
                break;
            case 'L':
                vs.push_back(*it2);
                break;
            }
        }
        if (hs.size() <= 0 || vs.size() <= 0) {
            continue;
        }
        sort(hs.begin(), hs.end(), [](plane left, plane right) { return left.x < right.x; });
        sort(vs.begin(), vs.end(), [](plane left, plane right) { return left.x < right.x; });
        for (auto ith = hs.begin(), itv = vs.begin(); ith != hs.end(); ++ith) {
            while (itv != vs.end() && itv->x < ith->x) {
                ++itv;
            }
            if (itv != vs.end()) {
                result = min(result, (itv->x - ith->x) * 2);
            }
        }
    }
    for (auto it = xs.begin(); it != xs.end(); ++it) {
        if (it->second.size() <= 0) {
            continue;
        }
        vector<plane> rs, ls;
        for (auto it2 = it->second.begin(); it2 != it->second.end(); ++it2) {
            if (it2->u == 'R') {
                rs.push_back(*it2);
            } else {
                ls.push_back(*it2);
            }
        }
        if (rs.size() <= 0 || ls.size() <= 0) {
            continue;
        }
        sort(rs.begin(), rs.end(), [](plane left, plane right) { return left.x < right.x; });
        sort(ls.begin(), ls.end(), [](plane left, plane right) { return left.x < right.x; });
        for (auto itr = rs.begin(), itl = ls.begin(); itr != rs.end(); ++itr) {
            while (itl != ls.end() && itl->x < itr->x) {
                ++itl;
            }
            if (itl != ls.end()) {
                result = min(result, itl->x - itr->x);
            }
        }
    }
    for (auto it = ys.begin(); it != ys.end(); ++it) {
        if (it->second.size() <= 0) {
            continue;
        }
        vector<plane> us, ds;
        for (auto it2 = it->second.begin(); it2 != it->second.end(); ++it2) {
            if (it2->u == 'U') {
                us.push_back(*it2);
            } else {
                ds.push_back(*it2);
            }
        }
        if (us.size() <= 0 || ds.size() <= 0) {
            continue;
        }
        sort(us.begin(), us.end(), [](plane left, plane right) { return left.y < right.y; });
        sort(ds.begin(), ds.end(), [](plane left, plane right) { return left.y < right.y; });
        for (auto itu = us.begin(), itd = ds.begin(); itu != us.end(); ++itu) {
            while (itd != ds.end() && itd->y < itu->y) {
                ++itd;
            }
            if (itd != ds.end()) {
                result = min(result, itd->y - itu->y);
            }
        }
    }
    if (result >= INF) {
        cout << "SAFE" << endl;
    } else {
        cout << result * 5 << endl;
    }
    return 0;
}


Measured memory_bytes: 72488