#include<bits/stdc++.h>
using namespace std;

const int inf = 0x3f3f3f3f;
const int N = 2e5 + 10;
int x[N], y[N];
char op[N][2];
int n;

vector<pair<int, char> > v1[N * 2];
vector<pair<int, char> > v2[N * 2];

int cacl_1() {
    for(int i = 0; i < N * 2; i++) v1[i].clear();
    int res = inf;
    for(int i = 1; i <= n; i++) {
        if(!(op[i][0] == 'R' || op[i][0] == 'U')) continue;
        v1[x[i] + y[i]].push_back({x[i], op[i][0]});
    }
    for(int i = 0; i < N * 2; i++) {
        sort(v1[i].begin(), v1[i].end());
        for(int j = 0; j < (int)v1[i].size() - 1; j++) {
            if(v1[i][j].second == 'R' && v1[i][j + 1].second == 'U') {
                res = min(res, v1[i][j + 1].first - v1[i][j].first);
            }
        }
    }
    return res * 10;
}

int cacl_2() {
    for(int i = 0; i < N * 2; i++) v2[i].clear();
    int res = inf;
    for(int i = 1; i <= n; i++) {
        if(!(op[i][0] == 'R' || op[i][0] == 'L')) continue;
        v2[y[i]].push_back({x[i], op[i][0]});
    }
    for(int i = 0; i < N * 2; i++) {
        sort(v2[i].begin(), v2[i].end());
        for(int j = 0; j < (int)v2[i].size() - 1; j++) {
            if(v2[i][j].second == 'R' && v2[i][j + 1].second == 'L') {
                res = min(res, v2[i][j + 1].first - v2[i][j].first);
            }
        }
    }
    return res * 5;
}

void Rotate() {
	for(int i = 1; i <= n; i++) {
		int xx = y[i], yy = 200000 - x[i];
		char ch = op[i][0];
		if(ch == 'U') ch = 'R';
		else if(ch == 'R') ch = 'D';
		else if(ch == 'D') ch = 'L';
		else ch = 'U';
		x[i] = xx, y[i] = yy, op[i][0] = ch;
	}
}

void solve() {
    scanf("%d", &n);
    for(int i = 1; i <= n; i++) {
        scanf("%d%d%s", &x[i], &y[i], op[i]);
    }
    int res = 1e9;
    for(int i = 1; i <= 4; i++) {
        res = min(res, cacl_1());
        res = min(res, cacl_2());
        Rotate();
    }
    if(res == 1e9) puts("SAFE");
    else printf("%d\n", res);
}

int main() {
//    freopen("in.txt", "r", stdin);
//    int t; cin >> t; while(t--)
    solve();
    return 0;
}
