#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <queue>
#include <tuple>
#include <set>
#include <map>

using namespace std;

int main(void) {
    int N;
    cin >> N;
    vector<int> X(N), Y(N), U(N);
    for(int i=0; i<N; i++) {
        char c;
        cin >> X[i] >> Y[i] >> c;
        X[i] *= 10;
        Y[i] *= 10;
        if(c == 'U') U[i] = 0; 
        if(c == 'R') U[i] = 1; 
        if(c == 'D') U[i] = 2; 
        if(c == 'L') U[i] = 3; 
    }

    int ans = numeric_limits<int>::max();

    map< pair<int,int>, vector<int> > mp;
    for(int i=0; i<N; i++) {
        if(U[i] % 2) {
            mp[make_pair(X[i] + Y[i], U[i])].emplace_back(Y[i]); // R, L
        } else {
            mp[make_pair(X[i] + Y[i], U[i])].emplace_back(X[i]); // U, D
        }
    }
    for(auto [key, val]: mp) {
        int z = key.first; // x+y
        int d = key.second; // direction
        if(d == 0) {
            auto v = mp[make_pair(z, 1)];
            sort(v.begin(), v.end());
            for(auto x: val) {
                int y = z - x;
                auto itr = lower_bound(v.begin(), v.end(), y);
                if(itr == v.end()) continue;
                ans = min(ans, *itr - y);
            }
        }
        if(d == 2) {
            auto v = mp[make_pair(z, 3)];
            sort(v.begin(), v.end());
            for(auto x: val) {
                int y = z - x;
                auto itr = lower_bound(v.begin(), v.end(), y);
                if(itr == v.begin()) continue;
                ans = min(ans, y - *prev(itr));
            }
        }
    }

    mp.clear();
    for(int i=0; i<N; i++) {
        if(U[i] % 2) {
            mp[make_pair(X[i] - Y[i], U[i])].emplace_back(Y[i]);
        } else {
            mp[make_pair(X[i] - Y[i], U[i])].emplace_back(X[i]);
        }
    }
    for(auto [key, val]: mp) {
        int z = key.first; // x-y
        int d = key.second; // direction
        if(d == 0) {
            auto v = mp[make_pair(z, 3)];
            sort(v.begin(), v.end());
            for(auto x: val) {
                int y = x - z;
                auto itr = lower_bound(v.begin(), v.end(), y);
                if(itr == v.end()) continue;
                ans = min(ans, *itr - y);
            }
        }
        if(d == 2) {
            auto v = mp[make_pair(z, 1)];
            sort(v.begin(), v.end());
            for(auto x: val) {
                int y = x - z;
                auto itr = lower_bound(v.begin(), v.end(), y);
                if(itr == v.begin()) continue;
                ans = min(ans, y - *prev(itr));
            }
        }
    }

    mp.clear();
    for(int i=0; i<N; i++) {
        if(U[i] % 2) {
            mp[make_pair(Y[i], U[i])].emplace_back(X[i]);
        } else {
            mp[make_pair(X[i], U[i])].emplace_back(Y[i]);
        }
    }
    for(auto [key, val]: mp) {
        int z = key.first; // 
        int d = key.second; // direction
        if(d == 0) {
            auto v = mp[make_pair(z, 2)];
            sort(v.begin(), v.end());
            for(auto y: val) {
                auto itr = lower_bound(v.begin(), v.end(), y);
                if(itr == v.end()) continue;
                ans = min(ans, (*itr - y) / 2);
            }
        }
        if(d == 1) {
            auto v = mp[make_pair(z, 3)];
            sort(v.begin(), v.end());
            for(auto x: val) {
                auto itr = lower_bound(v.begin(), v.end(), x);
                if(itr == v.end()) continue;
                ans = min(ans, (*itr - x) / 2);
            }
        }
    }

    if(ans == numeric_limits<int>::max()) {
        cout << "SAFE" << endl;
    } else {
        cout << ans << endl;
    }
    return 0;
}


Measured memory_bytes: 42472