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

#define double long double
#define ALL(x) x.begin(),x.end()
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
#define pb(x) push_back(x)
#define eb emplace_back
#define SORT(ls) sort(ls.begin(), ls.end())
#define DESC(ls) sort(ls.rbegin(), ls.rend()) 
#define mp(x, y) make_pair(x, y)
#define INF 1000000000
#define MOD 998244353 
#define F(i, a, n) for(i=a;i<n;i++)
#define SI(x) cin>>x
#define DI(x, y) cin>>x>>y
#define TI(x, y, z) cin>>x>>y>>z
#define FI(w, x, y, z) cin>>w>>x>>y>>z
#define SO(x) cout<<x<<"\n"
#define DO(x, y) cout<<x<<" "<<y<<"\n"
#define TO(x, y, z) cout<<x<<" "<<y<<" "<<z<< "\n"
#define FO(w, x, y, z) cout<<w<<" "<<x<<" "<<y<<" "<<z<<"\n"
#define FASTER ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)

const int N = 2e5 + 5;
vii rightTravel, leftTravel, upTravel;
vi leftTravelX[N], downTravelY[N];
map<int, vi> rightDownIntersect, rightUpIntersect, leftDownIntersect, leftUpIntersect;

int32_t main(void)
{
    FASTER;
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    int i, j;
	int n;
	SI(n);
	F(i, 0, n)
	{
		int x, y;
		char u;
		TI(x, y, u);
		if(u == 'U')
		{
			upTravel.pb(mp(x, y));
			leftUpIntersect[x - y].pb(y);
			rightUpIntersect[x + y].pb(y);
		}
		else if(u == 'D')
		{
			downTravelY[x].pb(y);
			rightDownIntersect[x - y].pb(y);
			leftDownIntersect[x + y].pb(y);
		}
		else if(u == 'L')
		{
			leftTravel.pb(mp(x, y));
			leftTravelX[y].pb(x);
		}
		else if(u == 'R')
		{
			rightTravel.pb(mp(x, y));
		}
	}    
    
    int collisionTime = INF;

    F(i, 0, N)
    {
    	sort(ALL(leftTravelX[i]));
    	sort(ALL(downTravelY[i]));
    }

    for(auto &i: rightDownIntersect)
    	sort(ALL(i.second));
    
    for(auto &i: rightUpIntersect)
    	sort(ALL(i.second), greater<int>());

    for(auto &i: leftDownIntersect)
    	sort(ALL(i.second));

    for(auto &i: leftUpIntersect)
    	sort(ALL(i.second), greater<int>());
    

    //RD, RL, RU
    for(auto a: rightTravel)
    {
    	int x = a.first;
    	int y = a.second;
    	int z = lower_bound(leftTravelX[y].begin(), leftTravelX[y].end(), x) - leftTravelX[y].begin();
    	if(z >= 0 && z < leftTravelX[y].size())
    		collisionTime = min(collisionTime, 5*abs(x - leftTravelX[y][z]));

    	int at = x - y;
    	z = lower_bound(rightDownIntersect[at].begin(), rightDownIntersect[at].end(), y) - rightDownIntersect[at].begin();
    	if(z >= 0 && z < rightDownIntersect[at].size())
    		collisionTime = min(collisionTime, 10 * abs(rightDownIntersect[at][z]-y));

    	at = x + y;
    	z = lower_bound(rightUpIntersect[at].begin(), rightUpIntersect[at].end(), y, greater<int>()) - rightUpIntersect[at].begin();
    	if(z >= 0 && z < rightUpIntersect[at].size())
    		collisionTime = min(collisionTime, 10 * abs(rightUpIntersect[at][z]-y));
    }

    //LD, LU
    for(auto a: leftTravel)
    {
    	int x = a.first;
    	int y = a.second;
    	int z = lower_bound(leftDownIntersect[x + y].begin(), leftDownIntersect[x + y].end(), y) - leftDownIntersect[x + y].begin();	
    	if(z >= 0 && z < leftDownIntersect[x + y].size())
    		collisionTime = min(collisionTime, 10 * abs(leftDownIntersect[x + y][z] - y));

    	int at = x - y;
    	z = lower_bound(leftUpIntersect[at].begin(), leftUpIntersect[at].end(), y, greater<int>()) - leftUpIntersect[at].begin();
    	if(z >= 0 && z < leftUpIntersect[at].size())
    		collisionTime = min(collisionTime, 10 * abs(leftUpIntersect[at][z] - y));
    }

    //UD
    for(auto a: upTravel)
    {
    	int x = a.first;
    	int y = a.second;
    	int z = lower_bound(downTravelY[x].begin(), downTravelY[x].end(), y) - downTravelY[x].begin();
    	if(z >= 0 && z < downTravelY[x].size())
    		collisionTime = min(collisionTime, 5 * abs(y - downTravelY[x][z]));
    }


    if(collisionTime == INF)
    	SO("SAFE");
    else
    	SO(collisionTime);

    #ifdef LOCAL
    	cout<<endl<<endl<<"Time: "<<(float)clock()/CLOCKS_PER_SEC<<" seconds"<<endl;
    #endif
    return 0;
}

Measured memory_bytes: 62372