//
#include<bits/stdc++.h>

using namespace std;

#define PB push_back
#define f first
#define s second
#define what_is(x) cerr << #x << " is " << x << endl;

typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;

const int N = 400005;
vector<pair<int, char>> vert[N], horiz[N], upright[N], downright[N];
int n;
int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> n;
    for(int i=0; i<n; i++)
    {
        int x, y;
        char c;
        cin >> x >> y >> c;
        vert[x].PB({y, c});
        horiz[y].PB({x, c});
        upright[y-x+N/2].PB({x, c});
        downright[x+y].PB({x, c});
    }

    int mn_time = 1000000007;
    for(int i=0; i<N/2; i++)
    {
        if(vert[i].size() == 0)
            continue;
        sort(vert[i].begin(), vert[i].end());
        int last_u = -1;
        for(pair<int, char> p : vert[i])
        {
            int ycoord = p.f;
            char c = p.s;
            if(c == 'D')
            {
                if(last_u != -1)
                {
                    mn_time = min(mn_time, 5*(ycoord - last_u));
                }
            }
            else if(c == 'U')
            {
                last_u = ycoord;
            }
        }
    }

    for(int i=0; i<N/2; i++)
    {
        if(horiz[i].size() == 0)
            continue;
        sort(horiz[i].begin(), horiz[i].end());
        int last_r = -1;
        for(pair<int, char> p : horiz[i])
        {
            int xcoord = p.f;
            char c = p.s;
            if(c == 'L')
            {
                if(last_r != -1)
                {
                    mn_time = min(mn_time, 5*(xcoord - last_r));
                }
            }
            else if(c == 'R')
            {
                last_r = xcoord;
            }
        }
    }

    for(int i=0; i<N; i++)
    {
        if(upright[i].size() == 0)
            continue;
        sort(upright[i].begin(), upright[i].end());
        int last_u = -1;
        int last_r = -1;
        for(pair<int, char> p : upright[i])
        {
            int xcoord = p.f;
            char c = p.s;
            if(c == 'L')
            {
                if(last_u != -1)
                {
                    mn_time = min(mn_time, 10*(xcoord-last_u));
                }
            }
            else if(c == 'D')
            {
                if(last_r != -1)
                {
                    mn_time = min(mn_time, 10*(xcoord-last_r));
                }
            }
            else if(c == 'U')
            {
                last_u = xcoord;
            }
            else
            {
                last_r = xcoord;
            }

        }
    }

    for(int i=0; i<N; i++)
    {
        if(downright[i].size() == 0)
            continue;
        sort(downright[i].begin(), downright[i].end());
        int last_d = -1;
        int last_r = -1;
        for(pair<int, char> p : downright[i])
        {
            int xcoord = p.f;
            char c = p.s;
            if(c == 'L')
            {
                if(last_d != -1)
                {
                    mn_time = min(mn_time, 10*(xcoord-last_d));
                }
            }
            else if(c == 'U')
            {
                if(last_r != -1)
                {
                    mn_time = min(mn_time, 10*(xcoord-last_r));
                }
            }
            else if(c == 'D')
            {
                last_d = xcoord;
            }
            else
            {
                last_r = xcoord;
            }

        }
    }

    if(mn_time == 1000000007)
    {
        cout << "SAFE";
    }
    else
    {
        cout << mn_time;
    }

    return 0;
}


Measured memory_bytes: 60708