#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <cstring>
#include <unordered_set>
#include <chrono>
#include <cassert>

//#pragma GCC optimize("Ofast,unroll-loops,fast-math,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt")
//#pragma GCC target("avx,avx2")

using namespace std;

#define int long long
#define pii pair<int, int>
#define mp make_pair
const int SZ = 5e5 + 4;
const int inf = 2e9;
int hashpow = 41;
int mod = 1e9 + 7;

int n;
pii a[SZ];
char c[SZ];

inline void read() {
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> a[i].first >> a[i].second >> c[i];
    }
}

inline void solve() {
    map<char, vector<int>> v;
    for (int i = 0; i < n; ++i) {
        v[c[i]].push_back(i);
    }

    vector<pair<char, char>> p;
    p.emplace_back('D', 'U');
    p.emplace_back('L', 'R');

    int ans = inf;
    for (auto [dir1, dir2] : p) {
        if (dir1 == 'D' && dir2 == 'U') {
            map<int, pair<vector<int>, vector<int>>> x;
            for (auto y : v[dir1]) {
                x[a[y].first].first.push_back(a[y].second);
            }
            for (auto y : v[dir2]) {
                x[a[y].first].second.push_back(a[y].second);
            }

            for (pair<const int, pair<vector<int>, vector<int>>> &q : x) {
                sort(q.second.first.begin(), q.second.first.end(), [&](int i, int j) {
                    return i < j;
                });

                if (!q.second.first.empty()) {
                    for (int i : q.second.second) {
                        auto it = lower_bound(q.second.first.begin(), q.second.first.end(), i);
                        if (it != q.second.first.end()) {
                            ans = min(ans, (*it - i) * 5);
                        }
                    }
                }
            }
        } else {
            map<int, pair<vector<int>, vector<int>>> x;
            for (auto y : v[dir1]) {
                x[a[y].second].first.push_back(a[y].first);
            }
            for (auto y : v[dir2]) {
                x[a[y].second].second.push_back(a[y].first);
            }

            for (pair<const int, pair<vector<int>, vector<int>>> &q : x) {
                sort(q.second.first.begin(), q.second.first.end(), [&](int i, int j) {
                    return i < j;
                });

                if (!q.second.first.empty()) {
                    for (int i : q.second.second) {
                        auto it = lower_bound(q.second.first.begin(), q.second.first.end(), i);
                        if (it != q.second.first.end()) {
                            ans = min(ans, (*it - i) * 5);
                        }
                    }
                }
            }
        }
    }


    p.clear();

    p.emplace_back('U', 'R');
    p.emplace_back('L', 'U');
    p.emplace_back('D', 'R');
    p.emplace_back('L', 'D');

    for (auto [dir1, dir2] : p) {
        if (dir1 == 'U' && dir2 == 'R' || dir1 == 'L' && dir2 == 'D') {
            map<int, pair<vector<int>, vector<int>>> x;
            for (auto y : v[dir1]) {
                x[a[y].first + a[y].second].first.push_back(a[y].first);
            }
            for (auto y : v[dir2]) {
                x[a[y].first + a[y].second].second.push_back(a[y].first);
            }

            for (pair<const int, pair<vector<int>, vector<int>>> &q : x) {
                sort(q.second.first.begin(), q.second.first.end(), [&](int i, int j) {
                    return i < j;
                });

                if (!q.second.first.empty()) {
                    for (int i : q.second.second) {
                        auto it = lower_bound(q.second.first.begin(), q.second.first.end(), i);
                        if (it != q.second.first.end()) {
                            ans = min(ans, (*it - i) * 10);
                        }
                    }
                }
            }
        } else {
            map<int, pair<vector<int>, vector<int>>> x;
            for (auto y : v[dir1]) {
                x[a[y].first - a[y].second].first.push_back(a[y].first);
            }
            for (auto y : v[dir2]) {
                x[a[y].first - a[y].second].second.push_back(a[y].first);
            }

            for (pair<const int, pair<vector<int>, vector<int>>> &q : x) {
                sort(q.second.first.begin(), q.second.first.end(), [&](int i, int j) {
                    return i < j;
                });

                if (!q.second.first.empty()) {
                    for (int i : q.second.second) {
                        auto it = lower_bound(q.second.first.begin(), q.second.first.end(), i);
                        if (it != q.second.first.end()) {
                            ans = min(ans, (*it - i) * 10);
                        }
                    }
                }
            }
        }
    }

    if (ans == inf) {
        cout << "SAFE\n";
        return;
    }
    cout << ans << "\n";
}

int32_t main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

//    int t;
//    cin >> t;
//    for (int i = 0; i < t; ++i) {
        read();
        solve();
//    }

    return 0;
}

Measured memory_bytes: 32800