#include<bits/stdc++.h>

int main(){
    using namespace std;
    unsigned long N;
    cin >> N;
    vector<pair<unsigned long, unsigned long>> u, d, l, r;
    for(unsigned long i{0}; i < N; ++i){
        unsigned long X, Y;
        cin >> X >> Y;
        X += 1000001;
        ++Y;
        char c;
        cin >> c;
        switch(c){
            case 'U':
                u.emplace_back(X, Y);
                break;
            case 'R':
                r.emplace_back(X, Y);
                break;
            case 'D':
                d.emplace_back(X, Y);
                break;
            case 'L':
                l.emplace_back(X, Y);
                break;
        }
    }
    unsigned long ans{numeric_limits<unsigned long>::max()};

    // U D
    {
        vector<tuple<unsigned long, unsigned long, bool>> ud;
        for(const auto& [x, y] : u)ud.emplace_back(x, y, false);
        for(const auto& [x, y] : d)ud.emplace_back(x, y, true);
        sort(begin(ud), end(ud));
        tuple<unsigned long, unsigned long, bool> last{0, 0, false};
        for(const auto& [x, y, f] : ud){
            if(f && get<0>(last) == x)ans = min(ans, y - get<1>(last));
            if(!f)last = {x, y, f};
        }
    }
    // L R
    {
        vector<tuple<unsigned long, unsigned long, bool>> lr;
        for(const auto& [x, y] : l)lr.emplace_back(y, x, true);
        for(const auto& [x, y] : r)lr.emplace_back(y, x, false);
        sort(begin(lr), end(lr));
        tuple<unsigned long, unsigned long, bool> last{0, 0, false};
        for(const auto& [x, y, f] : lr){
            if(f && get<0>(last) == x)ans = min(ans, y - get<1>(last));
            if(!f)last = {x, y, f};
        }
    }
    // U R
    {
        vector<tuple<unsigned long, unsigned long, bool>> ur;
        for(const auto& [x, y] : u)ur.emplace_back(x + y, x - y, true);
        for(const auto& [x, y] : r)ur.emplace_back(x + y, x - y, false);
        sort(begin(ur), end(ur));
        tuple<unsigned long, unsigned long, bool> last{0, 0, false};
        for(const auto& [x, y, f] : ur){
            if(f && get<0>(last) == x)ans = min(ans, y - get<1>(last));
            if(!f)last = {x, y, f};
        }
    }
    // D L
    {
        vector<tuple<unsigned long, unsigned long, bool>> dl;
        for(const auto& [x, y] : d)dl.emplace_back(x + y, x - y, false);
        for(const auto& [x, y] : l)dl.emplace_back(x + y, x - y, true);
        sort(begin(dl), end(dl));
        tuple<unsigned long, unsigned long, bool> last{0, 0, false};
        for(const auto& [x, y, f] : dl){
            if(f && get<0>(last) == x)ans = min(ans, y - get<1>(last));
            if(!f)last = {x, y, f};
        }
    }
    // U L
    {
        vector<tuple<unsigned long, unsigned long, bool>> ul;
        for(const auto& [x, y] : u)ul.emplace_back(x - y, x + y, false);
        for(const auto& [x, y] : l)ul.emplace_back(x - y, x + y, true);
        sort(begin(ul), end(ul));
        tuple<unsigned long, unsigned long, bool> last{0, 0, false};
        for(const auto& [x, y, f] : ul){
            if(f && get<0>(last) == x)ans = min(ans, y - get<1>(last));
            if(!f)last = {x, y, f};
        }
    }
    // D R
    {
        vector<tuple<unsigned long, unsigned long, bool>> dr;
        for(const auto& [x, y] : d)dr.emplace_back(x - y, x + y, true);
        for(const auto& [x, y] : r)dr.emplace_back(x - y, x + y, false);
        sort(begin(dr), end(dr));
        tuple<unsigned long, unsigned long, bool> last{0, 0, false};
        for(const auto& [x, y, f] : dr){
            if(f && get<0>(last) == x)ans = min(ans, y - get<1>(last));
            if(!f)last = {x, y, f};
        }
    }
    if(ans + 1)cout << ans * 5 << endl;
    else puts("SAFE");
    return 0;
}

Measured memory_bytes: 17156