#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define MOD 1000000007
#define MOD2 998244353
#define PRIM 3
#define INF (1 << 29)
#define LINF (1LL << 60)
#define EPS (1e-10)
#define PI 3.1415926535897932384626433832795028
typedef long long Int;
typedef pair<Int, Int> P;
typedef long double Real;
typedef complex<Real> CP;

Int ans = LINF;

Int n, x, y;
char u;

vector<P> rl[440000];
vector<P> ud[440000];
vector<P> lu[440000];
vector<P> ru[440000];
vector<P> ld[440000];
vector<P> rd[440000];

Int calc(vector<P> &vec)
{
    Int ans = LINF;
    sort(vec.begin(), vec.end());
    for (int i = 0; i + 1 < vec.size(); i++) {
        //        cout << vec[i].first << " " << vec[i + 1].first << endl;
        if (vec[i].second == 0 && vec[i + 1].second == 1) {
            ans = min(ans, (vec[i + 1].first - vec[i].first) * 5);
        }
    }
    return ans;
}

int main()
{
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> x >> y >> u;
        if (u == 'R') {
            rl[y].push_back(P(x, 0));
            rd[x - y + 200000].push_back(P(x + y, 0));
            ru[x + y].push_back(P(x - y, 0));
        }
        if (u == 'L') {
            rl[y].push_back(P(x, 1));
            ld[x + y].push_back(P(x - y, 1));
            lu[x - y + 200000].push_back(P(x + y, 1));
        }
        if (u == 'U') {
            ud[x].push_back(P(y, 0));
            lu[x - y + 200000].push_back(P(x + y, 0));
            ru[x + y].push_back(P(x - y, 1));
        }
        if (u == 'D') {
            ud[x].push_back(P(y, 1));
            ld[x + y].push_back(P(x - y, 0));
            rd[x - y + 200000].push_back(P(x + y, 1));
        }
    }

    for (int i = 0; i <= 400000; i++) {
        ans = min(ans, calc(rl[i]));
        ans = min(ans, calc(ud[i]));
        ans = min(ans, calc(ld[i]));
        ans = min(ans, calc(rd[i]));
        ans = min(ans, calc(lu[i]));
        ans = min(ans, calc(ru[i]));
    }
    if (ans == LINF)
        cout << "SAFE" << endl;
    else
        cout << ans << endl;
    return 0;
}