#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double

int main() {
    int N;
    cin >> N;
    ll mintime = 1e18;
    vector<ll> X(N), Y(N);
    vector<char> U(N);
    for (int i = 0; i < N; i++) cin >> X[i] >> Y[i] >> U[i];
    
    vector<vector<ll>> xequal(N, vector<ll>(3, 0)), yequal(N, vector<ll>(3, 0));
    for (int i = 0; i < N; i++) {
        xequal[i][0] = X[i];
        xequal[i][1] = Y[i];
        xequal[i][2] = i;
    }
    sort(xequal.begin(), xequal.end());
    ll umax = -1, dmax = -1, rmax = -1, lmax = -1;
    for (int i = 0; i < N; i++) {
        if (i > 0 && xequal[i-1][0] != xequal[i][0]) {
            umax = -1; dmax = -1; rmax = -1; lmax = -1;
        }
        int idx = xequal[i][2];
        if (U[idx] == 'U') umax = Y[idx];
        if (U[idx] == 'D') dmax = Y[idx];
        if (U[idx] == 'R') rmax = Y[idx];
        if (U[idx] == 'L') lmax = Y[idx];
        if (umax >= 0 && U[idx] == 'D') mintime = min(mintime, (Y[idx]-umax)*5);
    }
    
    for (int i = 0; i < N; i++) {
        yequal[i][0] = Y[i];
        yequal[i][1] = X[i];
        yequal[i][2] = i;
    }
    sort(yequal.begin(), yequal.end());
    umax = -1; dmax = -1; rmax = -1; lmax = -1;
    for (int i = 0; i < N; i++) {
        if (i > 0 && yequal[i-1][0] != yequal[i][0]) {
            umax = -1; dmax = -1; rmax = -1; lmax = -1;
        }
        int idx = yequal[i][2];
        if (U[idx] == 'U') umax = X[idx];
        if (U[idx] == 'D') dmax = X[idx];
        if (U[idx] == 'R') rmax = X[idx];
        if (U[idx] == 'L') lmax = X[idx];
        if (rmax >= 0 && U[idx] == 'L') mintime = min(mintime, (X[idx]-rmax)*5);
    }
    
    
    vector<vector<ll>> plus(N, vector<ll>(3, 0)), minus(N, vector<ll>(3, 0));
    for (int i = 0; i < N; i++) {
        plus[i][0] = X[i]+Y[i];
        plus[i][1] = X[i];
        plus[i][2] = i;
    }
    sort(plus.begin(), plus.end());
    umax = -1; dmax = -1; rmax = -1; lmax = -1;
    for (int i = 0; i < N; i++) {
        if (i > 0 && plus[i-1][0] != plus[i][0]) {
            umax = -1; dmax = -1; rmax = -1; lmax = -1;
        }
        int idx = plus[i][2];
        if (U[idx] == 'U') umax = X[idx];
        if (U[idx] == 'D') dmax = X[idx];
        if (U[idx] == 'R') rmax = X[idx];
        if (U[idx] == 'L') lmax = X[idx];
        if (rmax >= 0 && U[idx] == 'U') mintime = min(mintime, (X[idx]-rmax)*10);
        if (dmax >= 0 && U[idx] == 'L') mintime = min(mintime, (X[idx]-dmax)*10);
    }
    
    for (int i = 0; i < N; i++) {
        minus[i][0] = X[i]-Y[i];
        minus[i][1] = X[i];
        minus[i][2] = i;
    }
    sort(minus.begin(), minus.end());
    umax = -1; dmax = -1; rmax = -1; lmax = -1;
    for (int i = 0; i < N; i++) {
        if (i > 0 && minus[i-1][0] != minus[i][0]) {
            umax = -1; dmax = -1; rmax = -1; lmax = -1;
        }
        int idx = minus[i][2];
        if (U[idx] == 'U') umax = X[idx];
        if (U[idx] == 'D') dmax = X[idx];
        if (U[idx] == 'R') rmax = X[idx];
        if (U[idx] == 'L') lmax = X[idx];
        if (rmax >= 0 && U[idx] == 'D') mintime = min(mintime, (X[idx]-rmax)*10);
        if (umax >= 0 && U[idx] == 'L') mintime = min(mintime, (X[idx]-umax)*10);
    }
    
    if (mintime > 1e17) cout << "SAFE" << endl;
    else cout << mintime << endl;
    return 0;
}


Measured memory_bytes: 50172