#include <iostream>
#include <iomanip>
#include <algorithm>
#include <array>
#include <cassert>
#include <utility>
#include <vector>
#include <set>

template <class S, class T> void chmin(S& x, const T& y){
    if(x > y) x = y;
}

bool sortByYX(const std::pair<int, int>& x, const std::pair<int, int>& y){
    if(x.second != y.second) return x.second < y.second;
    return x.first < y.first;
}

bool sortByPP(const std::pair<int, int>& x, const std::pair<int, int>& y){
    const int vx = x.first + x.second;
    const int vy = y.first + y.second;
    if(vx != vy) return vx < vy;
    return x.first < y.first;
}

bool sortByNP(const std::pair<int, int>& x, const std::pair<int, int>& y){
    const int vx = -x.first + x.second;
    const int vy = -y.first + y.second;
    if(vx != vy) return vx < vy;
    return x.first < y.first;
}

bool sortByPN(const std::pair<int, int>& x, const std::pair<int, int>& y){
    const int vx = x.first - x.second;
    const int vy = y.first - y.second;
    if(vx != vy) return vx < vy;
    return x.first < y.first;
}


int main(void){
    std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); 
    std::cout << std::fixed << std::setprecision(16);
    
    int n; std::cin >> n;
    std::vector<std::pair<int, int>> Px, Nx, Py, Ny;
    for(int i = 0; i < n; ++i){
        int x, y; char c; std::cin >> x >> y >> c;
        if(c == 'R') Px.emplace_back(x, y);
        else if(c == 'L') Nx.emplace_back(x, y);
        else if(c == 'U') Py.emplace_back(x, y);
        else Ny.emplace_back(x, y);
    }
    long long int res = std::numeric_limits<long long int>::max();

    std::sort(Px.begin(), Px.end(), sortByYX); 
    std::sort(Nx.begin(), Nx.end(), sortByYX);
    for(const auto& itr1 : Px){
        const auto itr2 = std::lower_bound(Nx.cbegin(), Nx.cend(), itr1, sortByYX);
        if(itr2 == Nx.cend()) continue;
        if(itr1.second != itr2 -> second) continue;
        long long int dist = itr2 -> first - itr1.first;
        chmin(res, 5 * dist);
    }
    std::sort(Py.begin(), Py.end());
    std::sort(Ny.begin(), Ny.end());
    for(const auto& itr1 : Py){
        const auto itr2 = std::lower_bound(Ny.cbegin(), Ny.cend(), itr1);
        if(itr2 == Ny.cend()) continue;
        if(itr1.first != itr2 -> first) continue;
        long long int dist = itr2 -> second - itr1.second;
        chmin(res, 5 * dist);
    }

    // Py, Px
    // (x0, y0 + t) and (x1 + t, y1) -> t = x0 - x1 = y1 - y0 <=> x0 + y0 = x1 + y1
    std::sort(Px.begin(), Px.end(), sortByPP);
    std::sort(Py.begin(), Py.end(), sortByPP);
    int value = std::numeric_limits<int>::min(); 
    int idx = 0; std::set<int> S;
    for(const auto& itr : Px){
        const int val = itr.first + itr.second;
        if(val != value){
            while(idx < Py.size()){
                const int val2 = Py[idx].first + Py[idx].second;
                if(val2 < val) ++idx; else break;
            }
            S.clear(); while(idx < Py.size()){
                const int val2 = Py[idx].first + Py[idx].second;
                if(val2 == val){ S.insert(Py[idx].first); ++idx; }
                else break;
            }
            value = val;
        }
        if(S.empty()) continue;
        const auto itr3 = S.upper_bound(itr.first); if(itr3 == S.end()) continue;
        chmin(res, 10LL * (*itr3 - itr.first));
    }
    // Ny, Px
    // (x0, y0 - t) and (x1 + t, y1) -> t = x0 - x1 = y0 - y1 <=> -x0 + y0 = -x1 + y1
    S.clear(); value = std::numeric_limits<int>::min(); idx = 0;
    std::sort(Px.begin(), Px.end(), sortByNP);
    std::sort(Ny.begin(), Ny.end(), sortByNP);
    for(const auto& itr : Px){
        const int val = -itr.first + itr.second;
        if(val != value){
            while(idx < Ny.size()){
                const int val2 = -Ny[idx].first + Ny[idx].second;
                if(val2 < val) ++idx; else break;
            }
            S.clear(); while(idx < Ny.size()){
                const int val2 = -Ny[idx].first + Ny[idx].second;
                if(val2 == val){ S.insert(Ny[idx].first); ++idx; }
                else break;
            }
            value = val;
        }
        if(S.empty()) continue;
        const auto itr3 = S.upper_bound(itr.first); if(itr3 == S.end()) continue;
        chmin(res, 10LL * (*itr3 - itr.first));
    }


    // Py, Nx
    // (x0, y0 + t) and (x1 - t, y1) -> t = x1 - x0 = y1 - y0 <=> x0 - y0 = x1 - y1
    S.clear(); value = std::numeric_limits<int>::min(); idx = 0;
    std::sort(Nx.begin(), Nx.end(), sortByPN);
    std::sort(Py.begin(), Py.end(), sortByPN);
    for(const auto& itr : Nx){
        const int val = itr.first - itr.second;
        if(val != value){
            while(idx < Py.size()){
                const int val2 = Py[idx].first - Py[idx].second;
                if(val2 < val) ++idx; else break;
            }
            S.clear(); while(idx < Py.size()){
                const int val2 = Py[idx].first - Py[idx].second;
                if(val2 == val){ S.insert(Py[idx].first); ++idx; }
                else break;
            }
            value = val;
        }
        if(S.empty()) continue;
        auto itr3 = S.lower_bound(itr.first); if(itr3 == S.begin()) continue;
        --itr3; chmin(res, 10LL * (itr.first - *itr3));
    }

    // Ny, Nx
    // (x0, y0 - t) and (x1 - t, y1) -> t = x0 - x1 = y1 - y0 <=> x0 + y0 = x1 + y1
    S.clear(); value = std::numeric_limits<int>::min(); idx = 0;
    std::sort(Nx.begin(), Nx.end(), sortByPP);
    std::sort(Ny.begin(), Ny.end(), sortByPP);
    for(const auto& itr : Nx){
        const int val = itr.first + itr.second;
        if(val != value){
            while(idx < Ny.size()){
                const int val2 = Ny[idx].first + Ny[idx].second;
                if(val2 < val) ++idx; else break;
            }
            S.clear(); while(idx < Ny.size()){
                const int val2 = Ny[idx].first + Ny[idx].second;
                if(val2 == val){ S.insert(Ny[idx].first); ++idx; }
                else break;
            }
            value = val;
        }
        if(S.empty()) continue;
        auto itr3 = S.lower_bound(itr.first); if(itr3 == S.begin()) continue;
        --itr3; chmin(res, 10LL * (itr.first - *itr3));
    }


    if(res == std::numeric_limits<long long int>::max()){
        std::cout << "SAFE\n";
    }
    else std::cout << res << '\n';

    
    return 0;
}


Measured memory_bytes: 9532