// #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;
using ll = long long int;
using int64 = long long int;
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
// int dx[] = {0, 0, 1, -1};
// int dy[] = {1, -1, 0, 0};
const int INF = 1LL << 29;
const ll LONGINF = 1LL << 60;
const ll MOD = 1000000007LL;
 
int main() {
    int N; scanf("%d", &N);
    vector< vector< vector<int> > > xpy(4, vector< vector<int> >(400010));
    vector< vector< vector<int> > > xmy(4, vector< vector<int> >(400010));

    // L, R ... y ごとに分類
    // R, U ... x ごとに分類
    vector< vector<int> > LS(200010), RS(200010), US(200010), DS(200010);
    
    const string pat = "LDUR";
    for(int i=0; i<N; i++) {
        int x, y; char c;
        cin >> x >> y >> c;
        int id = pat.find(c);
        xpy[id][x+y].emplace_back(x);
        xmy[id][x-y+200000].emplace_back(x);
        if(c == 'L') LS[y].emplace_back(x);
        if(c == 'R') RS[y].emplace_back(x);
        if(c == 'U') US[x].emplace_back(y);
        if(c == 'D') DS[x].emplace_back(y);
    }

    for(int i=0; i<4; i++) {
        for(int j=0; j<400010; j++) {
            sort(xpy[i][j].begin(), xpy[i][j].end());
            sort(xmy[i][j].begin(), xmy[i][j].end());
        }
    }
    for(int i=0; i<200010; i++) {
        sort(LS[i].begin(), LS[i].end());
        sort(RS[i].begin(), RS[i].end());
        sort(DS[i].begin(), DS[i].end());
        sort(US[i].begin(), US[i].end());
    }

    ll ans = LONGINF;

    // L <-> R
    for(int i=0; i<200010; i++) {
        if(LS[i].empty() or RS[i].empty()) continue;
        int ptr = 0;
        for(auto r : RS[i]) {
            while(ptr < LS[i].size() and LS[i][ptr] < r) ptr++;
            if(ptr >= LS[i].size()) continue;
            int lx = LS[i][ptr], rx = r;
            chmin(ans, 10LL * (lx - rx) / 2);
        }
    }

    // U <-> D
    for(int i=0; i<200010; i++) {
        if(US[i].empty() or DS[i].empty()) continue;
        int ptr = 0;
        for(auto u : US[i]) {
            while(ptr < DS[i].size() and DS[i][ptr] < u) ptr++;
            if(ptr >= DS[i].size()) continue;
            int dy = DS[i][ptr], uy = u;
            chmin(ans, 10LL * (dy - uy) / 2);
        }
    }

    // R <-> U (x+y)
    for(int i=0; i<400010; i++) {
        int c1 = pat.find('R'), c2 = pat.find('U');
        if(xpy[c1][i].empty() or xpy[c2][i].empty()) continue;

        int ptr = 0;
        for(auto lx : xpy[c1][i]) {
            while(ptr < xpy[c2][i].size() and xpy[c2][i][ptr] < lx) ptr++;
            if(ptr >= xpy[c2][i].size()) continue;
            int rx = xpy[c2][i][ptr];
            chmin(ans, 10LL * (rx - lx));
        }
    }

    // L <-> D (x+y)
    for(int i=0; i<400010; i++) {
        int c1 = pat.find('D'), c2 = pat.find('L');
        if(xpy[c1][i].empty() or xpy[c2][i].empty()) continue;

        int ptr = 0;
        for(auto lx : xpy[c1][i]) {
            while(ptr < xpy[c2][i].size() and xpy[c2][i][ptr] < lx) ptr++;
            if(ptr >= xpy[c2][i].size()) continue;
            int rx = xpy[c2][i][ptr];
            chmin(ans, 10LL * (rx - lx));
        }
    }

    // L <-> U (x-y)
    for(int i=0; i<400010; i++) {
        int c1 = pat.find('U'), c2 = pat.find('L');
        if(xmy[c1][i].empty() or xmy[c2][i].empty()) continue;

        int ptr = 0;
        for(auto lx : xmy[c1][i]) {
            while(ptr < xmy[c2][i].size() and xmy[c2][i][ptr] < lx) ptr++;
            if(ptr >= xmy[c2][i].size()) continue;
            int rx = xmy[c2][i][ptr];
            chmin(ans, 10LL * (rx - lx));
        }
    }
    
    // R <-> D (x-y)
    for(int i=0; i<400010; i++) {
        int c1 = pat.find('R'), c2 = pat.find('D');
        if(xmy[c1][i].empty() or xmy[c2][i].empty()) continue;

        int ptr = 0;
        for(auto lx : xmy[c1][i]) {
            while(ptr < xmy[c2][i].size() and xmy[c2][i][ptr] < lx) ptr++;
            if(ptr >= xmy[c2][i].size()) continue;
            int rx = xmy[c2][i][ptr];
            chmin(ans, 10LL * (rx - lx));
        }
    }

    if(ans == LONGINF) puts("SAFE");
    else cout << ans << endl;
    return 0;
}


Measured memory_bytes: 115852