#include <iostream>
#include <array>
#include <algorithm>
#include <vector>
#include <bitset>
#include <set>
#include <unordered_set>
#include <cmath>
#include <complex>
#include <deque>
#include <iterator>
#include <numeric>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <limits>
#include <iomanip>
#include <functional>
#include <cassert>
using namespace std;

using ll=long long;
template<class T> using V = vector<T>;
template<class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
#define rep(i, k, n) for (ll i=k; i<(ll)n; ++i)
#define REP(i, n) rep(i, 0, n)
#define ALL(v) v.begin(),v.end()
template < class T > inline bool chmax(T& a, T b) {if (a < b) { a=b; return true; } return false; }
template < class T > inline bool chmin(T& a, T b) {if (a > b) { a=b; return true; } return false; }
#define DEBUG_VLL(vec) REP(sz, vec.size()) std::cerr<<vec[sz]<<(sz==vec.size()-1?'\n':' ');

const long long MOD = 1000000007;
const long long HIGHINF = (long long)1e18;
const int INF = (int)1e9;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    V< map<int, set<int> > > aip(4), aip45minus(4), aip45plus(4);
    for (int i = 0; i < n; i++) {
        int x, y; char u;
        cin >> x >> y >> u;
        x *= 10, y *= 10;
        if (u == 'U') {
            aip[0][x].insert(y);
            aip45plus[0][x + y].insert(y - x);
            aip45minus[0][y - x].insert(x + y);
        } else if (u == 'D') {
            aip[1][x].insert(y);
            aip45plus[1][x + y].insert(y - x);
            aip45minus[1][y - x].insert(x + y);
        } else if (u == 'L') {
            aip[2][y].insert(x);
            aip45plus[2][x + y].insert(y - x);
            aip45minus[2][y - x].insert(x + y);
        } else { // R
            aip[3][y].insert(x);
            aip45plus[3][x + y].insert(y - x);
            aip45minus[3][y - x].insert(x + y);
        }
    }

    int ans = INF;
    // 上下左右判定
    {
        // UD
        for (P<int, set<int> > aaa: aip[0]) {
            if (aip[1].find(aaa.first) == aip[1].end()) continue;
            for (int uy: aaa.second) {
                auto it = aip[1][aaa.first].lower_bound(uy);
                if (it != aip[1][aaa.first].end()) chmin(ans, *it - uy);
            }
        }
        // LR
        for (P<int, set<int> > aaa: aip[3]) {
            if (aip[2].find(aaa.first) == aip[2].end()) continue;
            for (int uy: aaa.second) {
                auto it = aip[2][aaa.first].lower_bound(uy);
                if (it != aip[2][aaa.first].end()) chmin(ans, *it - uy);
            }
        }
    }
    // 右斜め上判定
    {
        // UL
        for (P<int, set<int> > aaa: aip45minus[0]) {
            if (aip45minus[2].find(aaa.first) == aip45minus[2].end()) continue;
            for (int uy: aaa.second) {
                auto it = aip45minus[2][aaa.first].lower_bound(uy);
                if (it != aip45minus[2][aaa.first].end()) chmin(ans, *it - uy);
            }
        }
        // RD
        for (P<int, set<int> > aaa: aip45minus[3]) {
            if (aip45minus[1].find(aaa.first) == aip45minus[1].end()) continue;
            for (int uy: aaa.second) {
                auto it = aip45minus[1][aaa.first].lower_bound(uy);
                if (it != aip45minus[1][aaa.first].end()) chmin(ans, *it - uy);
            }
        }
    }
    // 右斜め下判定
    {
        // RU
        for (P<int, set<int> > aaa: aip45plus[0]) {
            if (aip45plus[3].find(aaa.first) == aip45plus[3].end()) continue;
            for (int uy: aaa.second) {
                auto it = aip45plus[3][aaa.first].lower_bound(uy);
                if (it != aip45plus[3][aaa.first].end()) chmin(ans, *it - uy);
            }
        }
        // LD
        for (P<int, set<int> > aaa: aip45plus[2]) {
            if (aip45plus[1].find(aaa.first) == aip45plus[1].end()) continue;
            for (int uy: aaa.second) {
                auto it = aip45plus[1][aaa.first].lower_bound(uy);
                if (it != aip45plus[1][aaa.first].end()) chmin(ans, *it - uy);
            }
        }
    }
    
    if (ans == INF) cout << "SAFE\n";
    else cout << ans / 2 << '\n';
    return 0;
}


Measured memory_bytes: 88020