#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
#define endl '\n'
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define rf(i,a,b) for(int i=a;i>=b;i--) 
#define fast_io ios_base::sync_with_stdio(0);
#define show(x) cout<<x<<endl;
#define pret(x) cout<<x<<endl; return 0;
#define disp(x) cout<<x<<" ";
#define let(x,y) cout<<x<<" "<<y<<endl;
ll MOD=1e9+7;
const int maxn=2e5+5;

const int lefT = -2;
const int righT = 2;
const int down = -1;
const int up = 1;

vector<pair<int,int> >  vertical[maxn], horizontal[maxn] , leftSlant[maxn + maxn], rightSlant[maxn + maxn];

int straight(int x){
    return x * 5;
}

int cross(int x){
    return x * 10;
}

int32_t main(){
    fast_io;
    int n;
    cin>>n;
    int x[n],y[n],d[n];
    for (int i = 0; i < n; ++i)
    {
        cin>>x[i]>>y[i];
        char c;
        cin>>c;
        if(c == 'U') d[i] = up;
        if(c == 'D') d[i] = down;
        if(c == 'R') d[i] = righT;
        if(c == 'L') d[i] = lefT;
        vertical[x[i]].emplace_back(y[i],d[i]);
        horizontal[y[i]].emplace_back(x[i],d[i]);
        leftSlant[x[i] + y[i]].emplace_back(x[i], d[i]);
        rightSlant[maxn + x[i] - y[i]].emplace_back(x[i], d[i]);
    }
    int ans = MOD;
    for (int i = 0; i < maxn; ++i)
    {
        if(vertical[i].empty()) continue;
        sort(vertical[i].begin(), vertical[i].end());
        for (int j = 0; j < vertical[i].size() - 1; ++j)
        {
            if(vertical[i][j+1].second == down and vertical[i][j].second == up) {
                ans = min(ans, straight(vertical[i][j+1].first - vertical[i][j].first));
            }
        }
    }

    for (int i = 0; i < maxn; ++i)
    {
        if(horizontal[i].empty()) continue;
        sort(horizontal[i].begin(), horizontal[i].end());
        for (int j = 0; j < horizontal[i].size() - 1; ++j)
        {
            if(horizontal[i][j+1].second == lefT and horizontal[i][j].second == righT) {
                ans = min(ans, straight(horizontal[i][j+1].first - horizontal[i][j].first));
            }
        }
    }

    for (int i = 0; i < maxn + maxn; ++i)
    {
        if(leftSlant[i].empty()) continue;
        sort(leftSlant[i].begin(), leftSlant[i].end());
        for (int j = 0; j < leftSlant[i].size() - 1; ++j)
        {
            bool collision = 0;
            if(leftSlant[i][j+1].second == up and leftSlant[i][j].second == righT) collision = 1;
            if(leftSlant[i][j+1].second == lefT and leftSlant[i][j].second == down) collision = 1;

            if(collision){
                ans = min(ans, cross(leftSlant[i][j+1].first - leftSlant[i][j].first));
            }
        }
    }

    for (int i = 0; i < maxn + maxn; ++i)
    {
        if(rightSlant[i].empty()) continue;
        sort(rightSlant[i].begin(), rightSlant[i].end());
        for (int j = 0; j < rightSlant[i].size() - 1; ++j)
        {
            bool collision = 0;
            if(rightSlant[i][j+1].second == lefT and rightSlant[i][j].second == up) collision = 1;
            if(rightSlant[i][j+1].second == down and rightSlant[i][j].second == righT) collision = 1;

            if(collision){
                ans = min(ans, cross(rightSlant[i][j+1].first - rightSlant[i][j].first));
            }
        }
    }

    if(ans == MOD){
        show("SAFE");
    }
    else{
        show(ans);
    }
    return 0;
}

Measured memory_bytes: 53636