#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int,int> P;
int INF = 1e16+7;
int mod = 1e9+7;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int MAX = 200005;
int MAX_2 = 400005;
signed main() {
    int N;
    cin >> N;
    vector<vector<P>>UD(MAX);
    vector<vector<P>>LR(MAX);
    vector<vector<P>>UR(MAX_2);
    vector<vector<P>>UL(MAX_2);
    vector<vector<P>>DR(MAX_2);
    vector<vector<P>>DL(MAX_2);
    for(int i = 0; i < N; i++) {
        int X,Y;
        string u;
        cin >> X >> Y >> u;
        if(u == "U") {
            UD[X].push_back({Y,0});
            UR[X+Y].push_back({X,0});
            UL[X-Y+MAX].push_back({X,0});
        }
        else if(u == "D"){
            UD[X].push_back({Y,1});
            DL[X+Y].push_back({X,0});
            DR[X-Y+MAX].push_back({X,0});
        }
        else if(u == "L") {
            LR[Y].push_back({X,0});
            DL[X+Y].push_back({X,1});
            UL[X-Y+MAX].push_back({X,1});
        }
        else {
            LR[Y].push_back({X,1});
            UR[X+Y].push_back({X,1});
            DR[X-Y+MAX].push_back({X,1});
        }
        
    }
    int ans = INF;
    for(int i = 0; i < MAX; i++) {
        sort(UD[i].begin(),UD[i].end());
        for(int j = 0; j < UD[i].size(); j++) {
            if(j) {
                if(UD[i][j-1].second < UD[i][j].second) {
                    ans = min(ans,(UD[i][j].first-UD[i][j-1].first)*5);
                }
            }
        }
    }
    for(int i = 0; i < MAX; i++) {
        sort(LR[i].begin(),LR[i].end());
        for(int j = 0; j < LR[i].size(); j++) {
            if(j) {
                if(LR[i][j-1].second > LR[i][j].second) {
                    ans = min(ans,(LR[i][j].first-LR[i][j-1].first)*5);
                }
            }
        }
    }
    for(int i = 0; i < MAX_2; i++) {
        sort(UR[i].begin(),UR[i].end());
        for(int j = 0; j < UR[i].size(); j++) {
            if(j) {
                if(UR[i][j-1].second > UR[i][j].second) {
                    ans = min(ans,(UR[i][j].first-UR[i][j-1].first)*10);
                }
            }
        }
    }
    for(int i = 0; i < MAX_2; i++) {
        sort(DL[i].begin(),DL[i].end());
        for(int j = 0; j < DL[i].size(); j++) {
            if(j) {
                if(DL[i][j-1].second < DL[i][j].second) {
                    ans = min(ans,(DL[i][j].first-DL[i][j-1].first)*10);
                }
            }
        }
    }
    for(int i = 0; i < MAX_2; i++) {
        sort(UL[i].begin(),UL[i].end());
        for(int j = 0; j < UL[i].size(); j++) {
            if(j) {
                if(UL[i][j-1].second < UL[i][j].second) {
                    ans = min(ans,(UL[i][j].first-UL[i][j-1].first)*10);
                }
            }
        }
    }
    for(int i = 0; i < MAX_2; i++) {
        sort(DR[i].begin(),DR[i].end());
        for(int j = 0; j < DR[i].size(); j++) {
            if(j) {
                if(DR[i][j-1].second > DR[i][j].second) {
                    ans = min(ans,(DR[i][j].first-DR[i][j-1].first)*10);
                }
            }
        }
    }
    if(ans == INF) {
        cout << "SAFE" << endl;
    }
    else {
        cout << ans << endl;
    }
}


Measured memory_bytes: 68608