#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

int main() {
    int n;
    cin >> n;
    vector<int> xs(n), ys(n);
    vector<char> us(n);
    for (int i = 0; i < n; ++i) {
        cin >> xs[i] >> ys[i] >> us[i];
    }
    int ans = INF<int>;
    { // lとr
        vector<vector<int>> A(200001), B(200001);
        for (int i = 0; i < n; ++i) {
            if(us[i] == 'R') A[ys[i]].emplace_back(xs[i]);
            if(us[i] == 'L') B[ys[i]].emplace_back(xs[i]);
        }
        for (int i = 0; i <= 200000; ++i) {
            if(!A[i].empty() && !B[i].empty()){
                sort(A[i].begin(),A[i].end());
                sort(B[i].begin(),B[i].end());
                for (int j = 0; j < A[i].size(); ++j) {
                    auto x = lower_bound(B[i].begin(),B[i].end(), A[i][j]);
                    if(x != B[i].end()){
                        ans = min(ans, *x-A[i][j]);
                    }
                }
            }
        }
    }
    { // uとd
        vector<vector<int>> A(200001), B(200001);
        for (int i = 0; i < n; ++i) {
            if(us[i] == 'U') A[xs[i]].emplace_back(ys[i]);
            if(us[i] == 'D') B[xs[i]].emplace_back(ys[i]);
        }
        for (int i = 0; i <= 200000; ++i) {
            if(!A[i].empty() && !B[i].empty()){
                sort(A[i].begin(),A[i].end());
                sort(B[i].begin(),B[i].end());
                for (int j = 0; j < A[i].size(); ++j) {
                    auto x = lower_bound(B[i].begin(),B[i].end(), A[i][j]);
                    if(x != B[i].end()){
                        ans = min(ans, *x-A[i][j]);
                    }
                }
            }
        }
    }
    { // rとu
        vector<vector<int>> A(400002), B(400002);
        for (int i = 0; i < n; ++i) {
            if(us[i] == 'R') A[xs[i]-ys[i]+200000].emplace_back(xs[i]);
            if(us[i] == 'D') B[xs[i]-ys[i]+200000].emplace_back(xs[i]);
        }
        for (int i = 0; i <= 400000; ++i) {
            if(!A[i].empty() && !B[i].empty()){
                sort(A[i].begin(),A[i].end());
                sort(B[i].begin(),B[i].end());
                for (int j = 0; j < A[i].size(); ++j) {
                    auto x = lower_bound(B[i].begin(),B[i].end(), A[i][j]);
                    if(x != B[i].end()){
                        ans = min(ans, (*x-A[i][j])*2);
                    }
                }
            }
        }
    }
    { // dとl
        vector<vector<int>> A(400002), B(400002);
        for (int i = 0; i < n; ++i) {
            if(us[i] == 'U') A[xs[i]-ys[i]+200000].emplace_back(xs[i]);
            if(us[i] == 'L') B[xs[i]-ys[i]+200000].emplace_back(xs[i]);
        }
        for (int i = 0; i <= 400000; ++i) {
            if(!A[i].empty() && !B[i].empty()){
                sort(A[i].begin(),A[i].end());
                sort(B[i].begin(),B[i].end());
                for (int j = 0; j < A[i].size(); ++j) {
                    auto x = lower_bound(B[i].begin(),B[i].end(), A[i][j]);
                    if(x != B[i].end()){
                        ans = min(ans, (*x-A[i][j])*2);
                    }
                }
            }
        }
    }
    { // dとl
        vector<vector<int>> A(400002), B(400002);
        for (int i = 0; i < n; ++i) {
            if(us[i] == 'D') A[xs[i]+ys[i]].emplace_back(xs[i]);
            if(us[i] == 'L') B[xs[i]+ys[i]].emplace_back(xs[i]);
        }
        for (int i = 0; i <= 400000; ++i) {
            if(!A[i].empty() && !B[i].empty()){
                sort(A[i].begin(),A[i].end());
                sort(B[i].begin(),B[i].end());
                for (int j = 0; j < A[i].size(); ++j) {
                    auto x = lower_bound(B[i].begin(),B[i].end(), A[i][j]);
                    if(x != B[i].end()){
                        ans = min(ans, (*x-A[i][j])*2);
                    }
                }
            }
        }
    }
    { // dとl
        vector<vector<int>> A(400002), B(400002);
        for (int i = 0; i < n; ++i) {
            if(us[i] == 'R') A[xs[i]+ys[i]].emplace_back(xs[i]);
            if(us[i] == 'U') B[xs[i]+ys[i]].emplace_back(xs[i]);
        }
        for (int i = 0; i <= 400000; ++i) {
            if(!A[i].empty() && !B[i].empty()){
                sort(A[i].begin(),A[i].end());
                sort(B[i].begin(),B[i].end());
                for (int j = 0; j < A[i].size(); ++j) {
                    auto x = lower_bound(B[i].begin(),B[i].end(), A[i][j]);
                    if(x != B[i].end()){
                        ans = min(ans, (*x-A[i][j])*2);
                    }
                }
            }
        }
    }
    if(ans == INF<int>) puts("SAFE");
    else cout << ans*5 << "\n";
    return 0;
}

Measured memory_bytes: 29820