#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <string>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <tuple>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cassert>
#include <cstdint>
#include <cctype>
#include <numeric>
#include <bitset>
#include <functional>

using namespace std;

using ll =  long long;
using Pll = pair<ll, ll>;
using Pii = pair<int, int>;

constexpr int INF = 1 << 30;
constexpr ll LINF = 1LL << 60;
constexpr ll MOD = 1000000007;
constexpr long double EPS = 1e-10;
constexpr int dyx[4][2] = {
    { 0, 1}, {-1, 0}, {0,-1}, {1, 0}
};

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int n, x, y; char direction;
    cin >> n;

    map<int, set<int>> up, down, right, left;
    // up, down: x 座標 -> y 座標
    // right, left: y 座標 -> x 座標
    map<int, set<int>> right2, right3, left2, left3;
    // right2, left2: (y-x) -> x 座標
    // right3, left3: (y+x) -> x 座標
    int ans = INF;
    for(int i=0;i<n;++i) {
        cin >> x >> y >> direction;
        if(direction == 'U') {
            auto itr = down[x].lower_bound(y);
            if(itr != down[x].end()) {
                ans = min(ans, 5 * (*itr - y));
            }
            up[x].insert(y);
        } else if(direction == 'D') {
            auto itr = up[x].upper_bound(y);
            if(itr != up[x].begin()) {
                --itr;
                ans = min(ans, 5 * (y - *itr));
            }
            down[x].insert(y);
        } else if(direction == 'R') {
            auto itr = left[y].lower_bound(x);
            if(itr != left[y].end()) {
                ans = min(ans, 5 * (*itr - x));
            }
            right[y].insert(x);
            right2[y-x].insert(x);
            right3[y+x].insert(x);
        } else if(direction == 'L') {
            auto itr = right[y].upper_bound(x);
            if(itr != right[y].begin()) {
                --itr;
                ans = min(ans, 5 * (x - *itr));
            }
            left[y].insert(x);
            left2[y-x].insert(x);
            left3[y+x].insert(x);
        }
    }

    // 90 度でぶつかるやつ
    for(auto itr=up.begin();itr!=up.end();++itr) {
        int x = itr->first;
        for(int y: up[x]) {
            auto itr = left2[y-x].lower_bound(x);
            if(itr != left2[y-x].end()) {
                ans = min(ans, 10 * (*itr - x));
            }
            itr = right3[y+x].lower_bound(x);
            if(itr != right3[y+x].begin()) {
                --itr;
                ans = min(ans, 10 * (x - *itr));
            }
        }
    }
    for(auto itr=down.begin();itr!=down.end();++itr) {
        int x = itr->first;
        for(int y: down[x]) {
            auto itr = right2[y-x].lower_bound(x);
            if(itr != right2[y-x].begin()) {
                --itr;
                ans = min(ans, 10 * (x - *itr));
            }
            itr = left3[y+x].lower_bound(x);
            if(itr != left3[y+x].end()) {
                ans = min(ans, 10 * (*itr - x));
            }
        }
    }

    if(ans == INF) {
        cout << "SAFE" << endl;
        return 0;
    }
    cout << ans << endl;
}

Measured memory_bytes: 87068