#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;
using ll = long long ;
using P = pair<int,int> ;
using pll = pair<long long,long long>;
using pp = pair<int,char>;
constexpr int INF = 1e9;
constexpr long long LINF = 1e17;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979323846;
const int MX = 200005;

int main(){
    int n;
    cin >> n;
    vector<vector<int>> xd(MX);
    vector<vector<int>> xu(MX);
    vector<vector<int>> yl(MX);
    vector<vector<int>> yr(MX);
    vector<vector<int>> xyl(2*MX); // y-x+MX;
    vector<vector<int>> xyr(2*MX); // y-x+MX;
    vector<vector<int>> xyd(2*MX); // y-x+MX;
    vector<vector<int>> xyu(2*MX); // y-x+MX;
    vector<vector<int>> yxl(2*MX); // y+x;
    vector<vector<int>> yxr(2*MX); // y+x;
    vector<vector<int>> yxd(2*MX); // y+x;
    vector<vector<int>> yxu(2*MX); // y+x;

    rep(i,n){
        int xx,yy;
        char u;
        cin >> xx >> yy >> u;
        if(u=='U') xd[xx].push_back(yy);
        if(u=='D') xu[xx].push_back(yy);
        if(u=='R') yl[yy].push_back(xx);
        if(u=='L') yr[yy].push_back(xx);
        if(u=='R') xyl[yy-xx+MX].push_back(xx);
        if(u=='D') xyr[yy-xx+MX].push_back(xx);
        if(u=='L') xyu[yy-xx+MX].push_back(xx);
        if(u=='U') xyd[yy-xx+MX].push_back(xx);
        if(u=='R') yxl[yy+xx].push_back(xx);
        if(u=='U') yxr[yy+xx].push_back(xx);
        if(u=='L') yxu[yy+xx].push_back(xx);
        if(u=='D') yxd[yy+xx].push_back(xx);
    }

    int mn = INF;

    rep(i,xd.size()){
        sort(xu[i].begin(),xu[i].end());
        rep(j,xd[i].size()){
            int l = -1 ,r = xu[i].size();
            while(r - l > 1){
                int m = (l+r)/2;
                if(xd[i][j]<=xu[i][m]) r = m;
                else l = m;
            }
            if(r==xu[i].size()) continue;
            mn = min(mn,abs(xd[i][j]-xu[i][r])*5);
        }
    }

    rep(i,yl.size()){
        sort(yr[i].begin(),yr[i].end());
        rep(j,yl[i].size()){
            int l = -1 ,r = yr[i].size();
            while(r - l > 1){
                int m = (l+r)/2;
                if(yl[i][j]<=yr[i][m]) r = m;
                else l = m;
            }
            if(r==yr[i].size()) continue;
            mn = min(mn,abs(yl[i][j]-yr[i][r])*5);
        }
    }

    rep(i,xyl.size()){
        sort(xyr[i].begin(),xyr[i].end());
        rep(j,xyl[i].size()){
            int l = -1 ,r = xyr[i].size();
            while(r - l > 1){
                int m = (l+r)/2;
                if(xyl[i][j]<=xyr[i][m]) r = m;
                else l = m;
            }
            if(r==xyr[i].size()) continue;
            mn = min(mn,abs(xyl[i][j]-xyr[i][r])*10);
        }
    }

    rep(i,xyd.size()){
        sort(xyu[i].begin(),xyu[i].end());
        rep(j,xyd[i].size()){
            int l = -1 ,r = xyu[i].size();
            while(r - l > 1){
                int m = (l+r)/2;
                if(xyd[i][j]<=xyu[i][m]) r = m;
                else l = m;
            }
            if(r==xyu[i].size()) continue;
            mn = min(mn,abs(xyd[i][j]-xyu[i][r])*10);
        }
    }

    rep(i,yxl.size()){
        sort(yxr[i].begin(),yxr[i].end());
        rep(j,yxl[i].size()){
            int l = -1 ,r = yxr[i].size();
            while(r - l > 1){
                int m = (l+r)/2;
                if(yxl[i][j]<=yxr[i][m]) r = m;
                else l = m;
            }
            if(r==yxr[i].size()) continue;
            mn = min(mn,abs(yxl[i][j]-yxr[i][r])*10);
        }
    }

    rep(i,yxd.size()){
        sort(yxu[i].begin(),yxu[i].end());
        rep(j,yxd[i].size()){
            int l = -1 ,r = yxu[i].size();
            while(r - l > 1){
                int m = (l+r)/2;
                if(yxd[i][j]<=yxu[i][m]) r = m;
                else l = m;
            }
            if(r==yxu[i].size()) continue;
            mn = min(mn,abs(yxd[i][j]-yxu[i][r])*10);
        }
    }

    if(mn==INF) cout << "SAFE" << endl;
    else cout << mn << endl;
    return 0;
}

Measured memory_bytes: 115732