#include <algorithm>
#include <iostream>
#include <cstdio>
#include <map>
#include <numeric>
#include <cmath>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <complex>
#include <string.h>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <iomanip>
#include <sys/time.h>
#include <tuple>
#include <random>
using namespace std;

#define endl '\n'
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define UNIQ(v) (v).erase(unique((v).begin(), (v).end()), (v).end())

typedef long long ll;
typedef long double ld;
typedef pair<int, char> P;
typedef complex<double> comp;
typedef vector< vector<ld> > matrix;
struct pairhash {
public:
    template<typename T, typename U>
    size_t operator()(const pair<T, U> &x) const {
	size_t seed = hash<T>()(x.first);
	return hash<U>()(x.second) + 0x9e3779b9 + (seed<<6) + (seed>>2);
    }
};
const int inf = 1e9 + 9;
const ll mod = 998244353;
const double eps = 1e-8;
const double pi = acos(-1);

int n;
int x[200100], y[200100];
char u[200100];

string solve() {
    map<P, vector<int>> m0;
    for (int i = 0; i < n; i++) {
        if (u[i] == 'U' || u[i] == 'D') {
            P p = make_pair(x[i], u[i]);
            if (m0.count(p) == 0) m0[p] = vector<int>();
            m0[p].push_back(y[i]);
        } else {
            P p = make_pair(y[i], u[i]);
            if (m0.count(p) == 0) m0[p] = vector<int>();
            m0[p].push_back(x[i]);
        }
    }

    int res = inf;
    for (auto p : m0) {
        if (p.first.second == 'U') {
            P key = make_pair(p.first.first, 'D');
            if (m0.count(key) == 0) continue;
            vector<int>& v = m0[key];
            sort(ALL(v));
            const int sz = (int)v.size();
            for (int pos : p.second) {
                int idx = (int)(lower_bound(ALL(v), pos) - v.begin());
                if (idx < sz) {
                    res = min(res, (v[idx]-pos)*5);
                }
            }
        } else if (p.first.second == 'R') {
            P key = make_pair(p.first.first, 'L');
            if (m0.count(key) == 0) continue;
            vector<int>& v = m0[key];
            sort(ALL(v));
            const int sz = (int)v.size();
            for (int pos : p.second) {
                int idx = (int)(lower_bound(ALL(v), pos) - v.begin());
                if (idx < sz) {
                    res = min(res, (v[idx]-pos)*5);
                }
            }
        }
    }

    map<P, vector<int>> m1;
    for (int i = 0; i < n; i++) {
        P p = make_pair(y[i]-x[i], u[i]);
        if (m1.count(p) == 0) m1[p] = vector<int>();
        m1[p].push_back(x[i]);
    }

    for (auto p : m1) {
        if (p.first.second == 'U') {
            P key = make_pair(p.first.first, 'L');
            if (m1.count(key) == 0) continue;
            vector<int>& v = m1[key];
            sort(ALL(v));
            const int sz = (int)v.size();
            for (int pos : p.second) {
                int idx = (int)(lower_bound(ALL(v), pos) - v.begin());
                if (idx < sz) {
                    res = min(res, (v[idx]-pos)*10);
                }
            }
        } else if (p.first.second == 'R') {
            P key = make_pair(p.first.first, 'D');
            if (m1.count(key) == 0) continue;
            vector<int>& v = m1[key];
            sort(ALL(v));
            const int sz = (int)v.size();
            for (int pos : p.second) {
                int idx = (int)(lower_bound(ALL(v), pos) - v.begin());
                if (idx < sz) {
                    res = min(res, (v[idx]-pos)*10);
                }
            }
        }
    }

    map<P, vector<int>> m2;
    for (int i = 0; i < n; i++) {
        P p = make_pair(y[i]+x[i], u[i]);
        if (m2.count(p) == 0) m2[p] = vector<int>();
        m2[p].push_back(x[i]);
    }

    for (auto p : m2) {
        if (p.first.second == 'R') {
            P key = make_pair(p.first.first, 'U');
            if (m2.count(key) == 0) continue;
            vector<int>& v = m2[key];
            sort(ALL(v));
            const int sz = (int)v.size();
            for (int pos : p.second) {
                int idx = (int)(lower_bound(ALL(v), pos) - v.begin());
                if (idx < sz) {
                    res = min(res, (v[idx]-pos)*10);
                }
            }
        } else if (p.first.second == 'D') {
            P key = make_pair(p.first.first, 'L');
            if (m2.count(key) == 0) continue;
            vector<int>& v = m2[key];
            sort(ALL(v));
            const int sz = (int)v.size();
            for (int pos : p.second) {
                int idx = (int)(lower_bound(ALL(v), pos) - v.begin());
                if (idx < sz) {
                    res = min(res, (v[idx]-pos)*10);
                }
            }
        }
    }
    
    return res >= inf ? "SAFE" : to_string(res);
}

void input() {
    cin >> n;
    for (int i = 0; i < n; i++) cin >> x[i] >> y[i] >> u[i];
}

int main() {
    input();
    cout << solve() << endl;
}


Measured memory_bytes: 70384