#include <bits/stdc++.h>
using namespace std;

int main() {
    int n;
    cin >> n;
    vector< pair<int, int> > a[4];

    for(int i=0; i<n; i++){
        int x,y;
        char u;
        cin >> x >> y >> u;
        if(u=='U') a[0].push_back(make_pair(x,y));
        if(u=='R') a[1].push_back(make_pair(x,y));
        if(u=='D') a[2].push_back(make_pair(x,y));
        if(u=='L') a[3].push_back(make_pair(x,y));
    }

    int ans=200000*10;

    vector< tuple<int, int, int> > b;

    if(a[0].size()+a[2].size()>0){
    for(auto v: a[0]){
        b.emplace_back(get<0>(v),get<1>(v),0);
    }
    for(auto v: a[2]){
        b.emplace_back(get<0>(v),get<1>(v),2);
    }
    sort(b.begin(), b.end());
    for(int i=0; i<b.size()-1; i++){
        if((get<0>(b[i]) == get<0>(b[i+1]) && (get<2>(b[i])==0) && get<2>(b[i+1])==2)){
            ans = min(get<1>(b[i+1])-get<1>(b[i]), ans);
        }
    }
    }
    b.clear();
    
    if(a[1].size()+a[3].size()>0){
        for(auto v: a[1]){
            b.emplace_back(get<1>(v),get<0>(v),1);
        }
        for(auto v: a[3]){
            b.emplace_back(get<1>(v),get<0>(v),3);
            }
        sort(b.begin(), b.end());
        for(int i=0; i<b.size()-1; i++){
            if((get<0>(b[i]) == get<0>(b[i+1]) && (get<2>(b[i])==1) && get<2>(b[i+1])==3)){
                ans = min(get<1>(b[i+1])-get<1>(b[i]), ans);
            }
        }
    }
    b.clear();

    if(a[0].size()+a[1].size()>0){
        for(auto v: a[0]){
            b.emplace_back(get<0>(v)+get<1>(v),get<0>(v),0);
        }
        for(auto v: a[1]){
            b.emplace_back(get<0>(v)+get<1>(v),get<0>(v),1);
            }
        sort(b.begin(), b.end());
        for(int i=0; i<b.size()-1; i++){
            if((get<0>(b[i]) == get<0>(b[i+1]) && (get<2>(b[i])==1) && get<2>(b[i+1])==0)){
                ans = min((get<1>(b[i+1])-get<1>(b[i]))*2, ans);
            }
        }
    }
    b.clear();

    if(a[2].size()+a[3].size()>0){
        for(auto v: a[2]){
            b.emplace_back(get<0>(v)+get<1>(v),get<0>(v),2);
        }
        for(auto v: a[3]){
            b.emplace_back(get<0>(v)+get<1>(v),get<0>(v),3);
            }
        sort(b.begin(), b.end());
        for(int i=0; i<b.size()-1; i++){
            if((get<0>(b[i]) == get<0>(b[i+1]) && (get<2>(b[i])==2) && get<2>(b[i+1])==3)){
                 ans = min((get<1>(b[i+1])-get<1>(b[i]))*2, ans);
            }
        }
    }
    b.clear();

    if(a[0].size()+a[3].size()>0){
        for(auto v: a[0]){
            b.emplace_back(get<0>(v)-get<1>(v),get<0>(v),0);
        }
        for(auto v: a[3]){
            b.emplace_back(get<0>(v)-get<1>(v),get<0>(v),3);
            }
        sort(b.begin(), b.end());
        for(int i=0; i<b.size()-1; i++){
            if((get<0>(b[i]) == get<0>(b[i+1]) && (get<2>(b[i])==0) && get<2>(b[i+1])==3)){
                 ans = min((get<1>(b[i+1])-get<1>(b[i]))*2, ans);
            }
        }
    }
    b.clear();
    
    if(a[1].size()+a[2].size()>0){
        for(auto v: a[1]){
            b.emplace_back(get<0>(v)-get<1>(v),get<0>(v),1);
        }
        for(auto v: a[2]){
            b.emplace_back(get<0>(v)-get<1>(v),get<0>(v),2);
            }
        sort(b.begin(), b.end());
        for(int i=0; i<b.size()-1; i++){
            if((get<0>(b[i]) == get<0>(b[i+1]) && (get<2>(b[i])==1) && get<2>(b[i+1])==2)){
                ans = min((get<1>(b[i+1])-get<1>(b[i]))*2, ans);
            }
        }
    }
    b.clear();
    
    if(ans==200000*10) cout << "SAFE" << endl;
    else cout << ans*5 << endl;

}

Measured memory_bytes: 9312