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

#define pr16 

#ifdef pr16
  #define pr(x)                 cerr << #x << ": " << x << endl;
  #define pr2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
  #define pr3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
  #define pr4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
  #define pr5(a, b, c, d, e)    cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
  #define pr6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;
  #define prdd(a,r,c) for(int i=0;i<(r);i++) { for(int j = 0;j<(c);j++) cerr<<a[i][j]<<" "; cerr<<endl; } cerr<<endl;
  #define prc(a) tr(a, it) cerr<<*(it)<<" "; cerr<<endl
  #define pra(a,n) for(int i=0; i<(n); i++) cerr<<((a)[i])<<" "; cerr<<"\n"
  #define prdd(a,r,c) for(int i=0;i<(r);i++) { for(int j = 0;j<(c);j++) cerr<<a[i][j]<<" "; cerr<<endl; } cerr<<endl; 
  #define prddd(a,x,y,z) for(int i=0;i<x;i++) {cerr<<"layer "<<i<<":\n";prdd(a[i],y,z)}
 
#else
  #define pr(x)
  #define pr2(x, y)
  #define pr3(x, y, z)
  #define pr4(a, b, c, d)
  #define pr5(a, b, c, d, e)
  #define pr6(a, b, c, d, e, f)
  #define prc(a)
  #define pra(a,n)
  #define prdd(a, r, c)
  #define prddd(a,x,y,z)
#endif

int check(vector<int> v1, vector<int> v2) {

	int n1 = v1.size(), n2 = v2.size();
	int ans = 1e9;
	if (n1 == 0 or n2 == 0)
		return ans;
	if (v1[0] > v2[n2-1])
		return ans;
	int p1 = 0, p2 = 0;

	while(p1 < n1 and p2 < n2) {
		if (v1[p1] < v2[p2])
			ans = min(ans, (0 - v1[p1] + v2[p2]) * 5);

		if (v1[p1] < v2[p2])
			p1 ++;
		else
			p2 ++;
	}
	return ans;

}

int find(vector<pair<int, int>> v) {
	sort(v.begin(), v.end());
	std::vector<int> v1,v2;
	for(auto el : v) {
		if (el.second == 1) 
			v1.push_back(el.first);
		else 
			v2.push_back(el.first);
	}
	return check(v1,v2);
}

int eq(char c1, char c2) {
	if (c1 == c2)
		return 1;
	return 0;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin>>n;
    int x[n], y[n];
    string u[n];
    for(int i = 0; i< n; i ++) {
    	cin >> x[i] >> y[i] >> u[i];
    }

    int ans = 1e9;

    map<int, std::vector<pair<int, int>>> dic;

    for(int i = 0; i< n; i ++) {
    	if (u[i][0] == 'U' or u[i][0] == 'D')
    		dic[x[i]].push_back(make_pair(y[i], eq(u[i][0], 'U')));
    }

    for (auto el : dic)
    	ans = min(ans, find(el.second));

    dic.clear();

    for(int i = 0; i< n; i ++) {
    	if (u[i][0] == 'R' or u[i][0] == 'L')
    		dic[y[i]].push_back(make_pair(x[i], eq(u[i][0], 'R')));
    }

    for (auto el : dic)
    	ans = min(ans, find(el.second));

    dic.clear();


    for(int i = 0; i< n; i ++) {
    	if (u[i][0] == 'U' or u[i][0] == 'R')
    		dic[x[i]+y[i]].push_back(make_pair(2*y[i], eq(u[i][0], 'U')));
    }

    for (auto el : dic)
    	ans = min(ans, find(el.second));

    dic.clear();

    for(int i = 0; i< n; i ++) {
    	if (u[i][0] == 'D' or u[i][0] == 'L')
    		dic[x[i]+y[i]].push_back(make_pair(2*y[i], eq(u[i][0], 'L')));
    }

    for (auto el : dic)
    	ans = min(ans, find(el.second));

    dic.clear();

    for(int i = 0; i< n; i ++) {
    	if (u[i][0] == 'U' or u[i][0] == 'L')
    		dic[x[i]-y[i]].push_back(make_pair(2*y[i], eq(u[i][0], 'U')));
    }

    for (auto el : dic)
    	ans = min(ans, find(el.second));

    dic.clear();

    for(int i = 0; i< n; i ++) {
    	if (u[i][0] == 'D' or u[i][0] == 'R')
    		dic[x[i]-y[i]].push_back(make_pair(2*x[i], eq(u[i][0], 'R')));
    }

    for (auto el : dic)
    	ans = min(ans, find(el.second));

    dic.clear();
    if (ans == 1e9)
    	cout << "SAFE\n";
    else
    cout << ans << "\n";
    return 0;
}

Measured memory_bytes: 32984