#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <algorithm>
#include <map>
#include <fstream>
#include <bitset>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef priority_queue<int> pqi;
typedef priority_queue<ii> pqii;
typedef map<int, int> mii;
typedef map<int, string> mis;
typedef map<string, int> msi;
typedef set<int> si;
typedef set<ll> sl;
typedef map<ll, ll> mll;
typedef queue<int> qi;
typedef queue<ii> qii;
typedef vector<string> vs;
typedef pair<ll, ll> iil;
typedef vector<iil> viil;
typedef vector<vi> vvi;
typedef long double ld;
typedef vector<pair<pair<int,int>,int>> viii;
typedef vector<pair<pair<ll,ll>,ll>> viiil;

#define pb push_back
#define mp make_pair
#define rep(i, n) for (int i = 0 ; i < n ; i++)
#define INF 1000000000
#define MOD 1000000007;

void solve() {
    int n;
    cin >> n;
    vector<pair<int, pair<pair<int, int>, int>>> d;
    rep(i, n) {
        int x, y;
        char c;
        cin >> x >> y >> c;
        int dir;
        if (c == 'U') {
            dir = 0;
        }
        else if (c == 'R') {
            dir = 1;
        }
        else if (c == 'D') {
            dir = 2;
        }
        else {
            dir = 3;
        }
        d.pb({x + y, {{x, y}, dir}});
    }
    
    sort(d.begin(), d.end());
    
    ii ls[4];
    rep(i, 4) {
        ls[i] = {-INF, -INF};
    }
    
    int ord[4] = {1, 0, 3, 2};
    
    int ans = INF;
    
    rep(i, n) {
        ii cr = d[i].second.first;
        ii ocr = ls[ord[d[i].second.second]];
        if (ocr.first != -INF && ocr.first + ocr.second == cr.first + cr.second && (d[i].second.second == 0 || d[i].second.second == 3)) {
            ans = min(ans, abs(cr.second - ocr.second) * 10);
        }
        ls[d[i].second.second] = cr;
    }
    
    vector<pair<int, pair<pair<int, int>, int>>> e;
    
    rep(i, n) {
        e.pb({d[i].second.first.second - d[i].second.first.first, d[i].second});
    }
    d = e;
    sort(d.begin(), d.end());
    
    rep(i, 4) {
        ls[i] = {-INF, -INF};
    }
    
    int ord2[4] = {3, 2, 1, 0};
    
    rep(i, n) {
        ii cr = d[i].second.first;
        ii ocr = ls[ord2[d[i].second.second]];
        if (ocr.first != -INF && ocr.first - ocr.second == cr.first - cr.second && (d[i].second.second == 2 || d[i].second.second == 3)) {
            ans = min(ans, abs(cr.second - ocr.second) * 10);
        }
        ls[d[i].second.second] = cr;
    }
    
    rep(i, d.size()) {
        e[i] = {d[i].second.first.second, d[i].second};
    }
    d = e;
    sort(d.begin(), d.end());
    
    rep(i, 4) {
        ls[i] = {-INF, -INF};
    }
    
    int ord3[4] = {2, 3, 0, 1};
    
    rep(i, n) {
        if (d[i].second.second % 2 == 0) continue;
        ii cr = d[i].second.first;
        ii ocr = ls[ord3[d[i].second.second]];
        if (ocr.first != -INF && ocr.second == cr.second && d[i].second.second == 3) {
            ans = min(ans, abs(cr.first - ocr.first) * 5);
        }
        ls[d[i].second.second] = cr;
    }
    
    rep(i, d.size()) {
        e[i] = {d[i].second.first.first, d[i].second};
    }
    d = e;
    sort(d.begin(), d.end());
    
    rep(i, 4) {
        ls[i] = {-INF, -INF};
    }
    
    rep(i, n) {
        if (d[i].second.second % 2 == 1) continue;
        ii cr = d[i].second.first;
        ii ocr = ls[ord3[d[i].second.second]];
        if (ocr.first != -INF && ocr.first == cr.first && d[i].second.second == 2) {
            ans = min(ans, abs(cr.second - ocr.second) * 5);
        }
        ls[d[i].second.second] = cr;
    }
    
    
    if (ans == INF) {
        cout << "SAFE" << endl;
    }
    else {
        cout << ans << endl;
    }

}

void querySolve() {
    int n;
    cin >> n;
    while (n--) {
        solve();
    }
}

int main() {
    //querySolve();
    solve();

}


Measured memory_bytes: 12384