#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cmath>
#include <sstream>
#include <numeric>
#include <stack>
#include <sstream>
#include <set>
using namespace std;

/*
    vector<vector<int> > ux(200001,vector<int>(0)) ;
    vector<vector<int> > dx(200001,vector<int>(0)) ;
    vector<vector<int> > ry(200001,vector<int>(0)) ;
    vector<vector<int> > ly(200001,vector<int>(0)) ;
*/
//larger 第二要素が大きくなる方

int64_t min(int64_t a,int64_t b) {
    if (a > b)
    {
        return b ;
    }else
    {
        return a ;
    }
}

int64_t check(vector<pair<int,int> > larger, vector<pair<int,int> > smaller){
    int64_t dist = INT64_MAX ;
    int i = 0 ;
    int j = 0 ;
    while (i < larger.size() && j < smaller.size())
    {
        if (larger.at(i) > smaller.at(j))
        {
            if (larger.at(i).first == smaller.at(j).first)
            {
                dist = min(dist, larger.at(i).second - smaller.at(j).second) ;
            }
            j ++ ;
        }else
        {
            i ++ ;
        }
        
        
    }
    return dist ;
}

int main(){
    int n ;
    cin >> n ;
    vector<pair<int,int> > ux(0) ;
    vector<pair<int,int> > dx(0) ;
    vector<pair<int,int> > ry(0) ;
    vector<pair<int,int> > ly(0) ;
    vector<pair<int,int> > uplus(0) ;
    vector<pair<int,int> > uminus(0) ;
    vector<pair<int,int> > dplus(0) ;
    vector<pair<int,int> > dminus(0) ;
    vector<pair<int,int> > rplus(0) ;
    vector<pair<int,int> > rminus(0) ;
    vector<pair<int,int> > lplus(0) ;
    vector<pair<int,int> > lminus(0) ;
    for (int i = 0; i < n; i++)
    {
        int x,y ;
        char u ;
        cin >> x >> y >> u ;
        if (u == 'U')
        {
            ux.push_back(make_pair(x,y)) ;
            uplus.push_back(make_pair(x+y,x)) ;
            uminus.push_back(make_pair(x-y,x)) ;
        }if (u == 'D')
        {
            dx.push_back(make_pair(x,y)) ;
            dplus.push_back(make_pair(x+y,x)) ;
            dminus.push_back(make_pair(x-y,x)) ;
        }if (u == 'R')
        {
            ry.push_back(make_pair(y,x)) ;
            rplus.push_back(make_pair(x+y,x)) ;
            rminus.push_back(make_pair(x-y,x)) ;
        }if (u == 'L')
        {
            ly.push_back(make_pair(y,x)) ;
            lplus.push_back(make_pair(x+y,x)) ;
            lminus.push_back(make_pair(x-y,x)) ;
        }
        
    }
    sort(ux.begin(),ux.end()) ;
    sort(dx.begin(),dx.end()) ;
    sort(ry.begin(),ry.end()) ;
    sort(ly.begin(),ly.end()) ;
    sort(uplus.begin(),uplus.end()) ;
    sort(uminus.begin(),uminus.end()) ;
    sort(dplus.begin(),dplus.end()) ;
    sort(dminus.begin(),dminus.end()) ;
    sort(rplus.begin(),rplus.end()) ;
    sort(rminus.begin(),rminus.end()) ;
    sort(lplus.begin(),lplus.end()) ;
    sort(lminus.begin(),lminus.end()) ;
    int64_t ans = INT64_MAX ;
    int64_t temp ;
    temp = check(dx,ux) ;
    if (temp != INT64_MAX)
    {
        ans = min(ans, temp * 5) ;
    }
    temp = check(ly,ry) ;
    if (temp != INT64_MAX)
    {
        ans = min(ans, temp * 5) ;
    }
    temp = check(uplus,rplus) ;
    if (temp != INT64_MAX)
    {
        ans = min(ans, temp * 10) ;
    }
    temp = check(lplus,dplus) ;
    if (temp != INT64_MAX)
    {
        ans = min(ans, temp * 10) ;
    }
    temp = check(lminus,uminus) ;
    if (temp != INT64_MAX)
    {
        ans = min(ans, temp * 10) ;
    }
    temp = check(dminus,rminus) ;
    if (temp != INT64_MAX)
    {
        ans = min(ans, temp * 10) ;
    }
    if (ans < INT64_MAX)
    {
        cout << ans << endl ;
    }else
    {
        cout << "SAFE" << endl ;
    }
    
    
    
}


Measured memory_bytes: 9760