#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#ifdef __GXX_EXPERIMENTAL_CXX0X__
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#ifdef __GXX_EXPERIMENTAL_CXX0X__
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
size_t strlen(const char* s);
#define rep(i,n) for(long long int i=0;i<n;i++)
#define sz(x) ((int)(x).size())

using namespace std;
struct ToUpper {
    char operator()(char c) { return toupper(c); }
};
struct ToLower {
    char operator()(char c) { return tolower(c); }
};
const int N = 200010;
const int P = 1000000007;
using ll = long long;

//絶対値
int ze(int a, int b) {
    if (a > b) {
        return a - b;
    }
    else {
        return b - a;
    }
}
//小さい方
long long int min(long long int a, long long int b) {
    if (a > b) {
        return b;
    }
    else {
        return a;
    }
}
//大きい方
long long int max(long long int a, long long int b) {
    if (a > b) {
        return a;
    }
    else {
        return b;
    }
}
// a^n mod 
long long modpow(long long a, long long n, long long mod) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

// a^{-1} mod 
long long modinv(long long a, long long mod) {
    return modpow(a, mod - 2, mod);
}

//mod掛け算
long long mt(long long a, long long b, long long mod) {
    return (a * b) % mod;
}

//メモ

/*
    memset(b, '\0', sizeof(b));
初期化*/

/*
    char s[N];
    strlen(s)
*/

/*
    if(  <  )puts("Yes");
    else puts("No");
*/

/*文字列比較
if(strcmp(name[i],s)==0)
*/

/*
    graph[a].push_back(b);//graph[a][i番目に入れた]==b
    graph[j].size()	//forで回すときとか

*/

/*添字1がa個、2がb個
  vector<vector<ll>> map(a,vector<ll>(b));
*/

int main() {
    ll n = 200010;
    ll m;
    cin >> m;
    vector<tuple<ll, ll, char>> map0;

    vector<tuple<ll, ll, char>> map1;
    vector<tuple<ll, ll, char>> map2;
    vector<tuple<ll, ll, char>> map3;

    rep(i, m) {
        ll a, b;
        char c;
        cin >> a >> b >> c;
        map0.push_back(make_tuple(a + b, a, c));
        map1.push_back(make_tuple(a - b, a, c));
        map2.push_back(make_tuple(a, b, c));

        map3.push_back(make_tuple(b, a, c));

    }
    sort(map0.begin(), map0.end());
    sort(map1.begin(), map1.end());

    sort(map2.begin(), map2.end());

    sort(map3.begin(), map3.end());
    ll min = 4000000;
    ll now = 0;
    bool finish = false;
    for (ll k = 0; k < 2 * n; k++) {
        ll up = -4000000;
        ll down = -4000000;
        while (!finish) {
            if (get<0>(map0.at(now)) == k) {
                if (get<2>(map0.at(now)) == 'R') {
                    up = get<1>(map0.at(now));
                }
                if (get<2>(map0.at(now)) == 'D') {
                    down = get<1>(map0.at(now));
                }
                if (get<2>(map0.at(now)) == 'U') {
                    if (min > (get<1>(map0.at(now)) - up) * 10) {
                        min = (get<1>(map0.at(now)) - up) * 10;
                    }
                }
                if (get<2>(map0.at(now)) == 'L') {
                    if (min > (get<1>(map0.at(now)) - down) * 10) {
                        min = (get<1>(map0.at(now)) - down) * 10;
                    }
                }
                now++;
                if (now >= m) {
                    finish = true;
                    break;
                }
            }
            else {
                break;
            }
        }
    }
    now = 0;
    finish = false;
    for (ll k = 0 - n; k < n; k++) {

        ll up = -4000000;
        ll down = -4000000;
        while (!finish) {

            if (get<0>(map1.at(now)) == k) {
                if (get<2>(map1.at(now)) == 'U') {
                    up = get<1>(map1.at(now));
                }
                if (get<2>(map1.at(now)) == 'R') {
                    down = get<1>(map1.at(now));
                }
                if (get<2>(map1.at(now)) == 'L') {
                    if (min > (get<1>(map1.at(now)) - up) * 10) {
                        min = (get<1>(map1.at(now)) - up) * 10;
                    }
                }
                if (get<2>(map1.at(now)) == 'D') {
                    if (min > (get<1>(map1.at(now)) - down) * 10) {
                        min = (get<1>(map1.at(now)) - down) * 10;
                    }
                }
                now++;

                if (now >= m) {
                    finish = true;
                    break;
                }
            }
            else {
                break;
            }
        }
    }
    now = 0;
    finish = false;
    for (ll k = 0; k <= n; k++) {

        ll up = -4000000;
        ll down = -4000000;
        while (!finish) {

            if (get<0>(map2.at(now)) == k) {
                if (get<2>(map2.at(now)) == 'U') {
                    up = get<1>(map2.at(now));
                }
                if (get<2>(map2.at(now)) == 'D') {
                    if (min > (get<1>(map2.at(now)) - up) * 5) {
                        min = (get<1>(map2.at(now)) - up) * 5;
                    }
                }
                now++;

                if (now >= m) {
                    finish = true;
                    break;
                }
            }
            else {
                break;
            }
        }
    }
    now = 0;
    finish = false;
    for (ll k = 0; k <= n; k++) {
        ll up = -4000000;
        ll down = -4000000;
        while (!finish) {
            if (get<0>(map3.at(now)) == k) {
                if (get<2>(map3.at(now)) == 'R') {
                    up = get<1>(map3.at(now));
                }
                if (get<2>(map3.at(now)) == 'L') {
                    if (min > (get<1>(map3.at(now)) - up) * 5) {
                        min = (get<1>(map3.at(now)) - up) * 5;
                    }
                }
                now++;

                if (now >= m) {
                    finish = true;
                    break;
                }
            }
            else {
                break;
            }
        }
    }

    if (min == 4000000) {
        cout << "SAFE" << endl;
    }
    else {
        cout << min << endl;
    }

}


Measured memory_bytes: 22176