#include <stdio.h>
#include <stdlib.h>

#include <iostream>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <stdint.h>
#include <string.h>

#define _USE_MATH_DEFINES
#include <math.h>

#include <vector>
#include <list>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <stack>
#include <deque>
#include <string>

#include <algorithm>
#include <functional>
#include <bitset>
#include <functional>
#include <chrono>
#include <random>

#define sqr(x) (x) * (x)

typedef unsigned int u32;
typedef int i32;
typedef unsigned long long int u64;
typedef long long int i64;
typedef uint16_t u16;
typedef int16_t i16;
typedef uint8_t u8;
typedef int8_t i8;

using namespace std;
using namespace std::chrono;

const i64 mod = 1'000'000'000ll + 7;
//const i64 smod = 998244353ll;
//const i64 inf = 1'000'000'000'000'000'000ll + 7;
const i64 inf = 1'000'000'007;

const long double eps = 1e-8;

const i64 MAXN = 200'000 + 5;

i64 n;

vector<i64> x;
vector<i64> y;
vector<char> u;

vector<map<i64, map<char, vector<i64>>>> z(2);
map<i64, map<char, vector<i64>>> v;
map<i64, map<char, vector<i64>>> h;

i64 R = -1;

void colide(map<char, vector<i64>>& dmap, char le, char gt, i64 div = 1) {
    for (auto cx : dmap[le]) {
        i64 i = upper_bound(dmap[gt].begin(), dmap[gt].end(), cx) - dmap[gt].begin();
        if (i < dmap[gt].size()) {
            i64 nx = dmap[gt][i];
            i64 v = (nx - cx) * 10 / div;
            if (R < 0 || R > v) {
                R = v;
            }
        }
    }
}

int main(int argc, char* argv[]) {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout.precision(15); cout.setf(ios::fixed); cerr.precision(15); cerr.setf(ios::fixed);

    cin >> n;
    x = vector<i64>(n);
    y = vector<i64>(n);
    u = vector<char>(n);

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

    /* diag */
    for (i64 t = 0; t < 2; t++) {
        for (i64 i = 0; i < n; i++) {
            i64 cx = x[i];
            i64 cy = y[i];
            char cu = u[i];

            i64 c = !t ? cx + cy : cx - cy;
            z[t][c][cu].push_back(cx);
        }
    }

    for (i64 t = 0; t < 2; t++) {
        for (auto& e : z[t]) {
            auto& dmap = e.second;
            for (auto& t : dmap) {
                auto& xvec = t.second;
                sort(xvec.begin(), xvec.end());
            }
        }
    }

    for (i64 t = 0; t < 2; t++) {
        for (auto& e : z[t]) {
            auto& dmap = e.second;
            if (t == 0) {
                colide(dmap, 'R', 'U');
                colide(dmap, 'D', 'L');
            }
            else {
                colide(dmap, 'R', 'D');
                colide(dmap, 'U', 'L');
            }
        }
    }

    /* vert */
    for (i64 i = 0; i < n; i++) {
        i64 cx = x[i];
        i64 cy = y[i];
        char cu = u[i];

        v[cx][cu].push_back(cy);
    }

    for (auto& e : v) {
        auto& dmap = e.second;
        for (auto& t : dmap) {
            auto& xvec = t.second;
            sort(xvec.begin(), xvec.end());
        }
    }

    for (auto& e : v) {
        auto& dmap = e.second;
        colide(dmap, 'U', 'D', 2);
    }


    /* horiz */
    for (i64 i = 0; i < n; i++) {
        i64 cx = x[i];
        i64 cy = y[i];
        char cu = u[i];

        h[cy][cu].push_back(cx);
    }


    for (auto& e : h) {
        auto& dmap = e.second;
        for (auto& t : dmap) {
            auto& xvec = t.second;
            sort(xvec.begin(), xvec.end());
        }
    }

    for (auto& e : h) {
        auto& dmap = e.second;
        colide(dmap, 'R', 'L', 2);
    }

    if (R == -1) {
        cout << "SAFE" << endl;
    }
    else {
        cout << R << endl;
    }



    return 0;
}

Measured memory_bytes: 206468