#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;
typedef long long ll;

template <typename T>
class OffsetVec{
    public:
	int n;
    vector<T> v;
	T& operator[](int x) {
        return v[x+n];
	}
    void print(){
        for(int i = -n; i <= n; i++) cout << v[i+n] << ' ';
        cout << endl;
    }
    OffsetVec(int _n){
        n = _n;
        v = vector<T>(2*n+1);
    }
};

int N;

OffsetVec<vector<int>> vl_sum(400000), vr_sum(400000), vu_sum(400000), vd_sum(400000);
OffsetVec<vector<int>> vl_sub(400000), vr_sub(400000), vu_sub(400000), vd_sub(400000);

// vl[y]
vector<int> vl[200001], vr[200001];
// vd[x]
vector<int> vu[200001], vd[200001];

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    cin >> N;
    for(int i = 0; i < N; i++){
        int x, y; char u;
        cin >> x >> y >> u;
        if(u == 'L'){
            vl_sum[x+y].push_back(x);
            vl_sub[x-y].push_back(x);
            vl[y].push_back(x);
        }else if(u == 'R'){
            vr_sum[x+y].push_back(x);
            vr_sub[x-y].push_back(x);
            vr[y].push_back(x);
        }else if(u == 'U'){
            vu_sum[x+y].push_back(x);
            vu_sub[x-y].push_back(x);
            vu[x].push_back(y);
        }else{
            vd_sum[x+y].push_back(x);
            vd_sub[x-y].push_back(x);
            vd[x].push_back(y);
        }
    }
    int m = 1e7;

    for(int i = -400000; i <= 400000; i++){
        sort(vl_sum[i].begin(), vl_sum[i].end());
        sort(vl_sub[i].begin(), vl_sub[i].end());
        sort(vr_sum[i].begin(), vr_sum[i].end());
        sort(vr_sub[i].begin(), vr_sub[i].end());
        sort(vu_sum[i].begin(), vu_sum[i].end());
        sort(vu_sub[i].begin(), vu_sub[i].end());
        sort(vd_sum[i].begin(), vd_sum[i].end());
        sort(vd_sub[i].begin(), vd_sub[i].end());
    }

    for(int i = 0; i <= 200000; i++){
        sort(vl[i].begin(), vl[i].end());
        sort(vr[i].begin(), vr[i].end());
        sort(vu[i].begin(), vu[i].end());
        sort(vd[i].begin(), vd[i].end());
    }
    
    for(int i = -400000; i <= 400000; i++){
        for(int j : vr_sum[i]){
            // u
            auto p = upper_bound(vu_sum[i].begin(), vu_sum[i].end(), j);
            if(p != vu_sum[i].end()){
                m = min(m, *p-j);
            } 
        }
    }

    for(int i = -400000; i <= 400000; i++){
        for(int j : vd_sum[i]){
            // r
            auto p = upper_bound(vl_sum[i].begin(), vl_sum[i].end(), j);
            if(p != vl_sum[i].end()){
                m = min(m, *p-j);
            } 
        }
    }

    // sub
    for(int i = -400000; i <= 400000; i++){
        for(int j : vu_sub[i]){
            auto p = upper_bound(vl_sub[i].begin(), vl_sub[i].end(), j);
            if(p != vl_sub[i].end()){
                m = min(m, *p-j);
            } 
        }
    }

    for(int i = -400000; i <= 400000; i++){
        for(int j : vr_sub[i]){
            // d
            auto p = upper_bound(vd_sub[i].begin(), vd_sub[i].end(), j);
            if(p != vd_sub[i].end()){
                m = min(m, *p-j);
            } 
        }
    }

    m *= 10;

    for(int i = 0; i <= 200000; i++){
        for(int j : vr[i]){
            auto p = upper_bound(vl[i].begin(), vl[i].end(), j);
            if(p != vl[i].end()){
                m = min(m, (*p-j)*5);
            } 
        }
    }

    for(int i = 0; i <= 200000; i++){
        for(int j : vu[i]){
            auto p = upper_bound(vd[i].begin(), vd[i].end(), j);
            if(p != vd[i].end()){
                m = min(m, (*p-j)*5);
            } 
        }
    }


    if(m >= 1e7){
        cout << "SAFE" << endl;
    }else{
        cout << m << endl;
    }
}

Measured memory_bytes: 190596