#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define fast_io() ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0)
using namespace std;
int n;
pair <int, int> get(string& s)
{
	stack <char> st;
	pair <int, int> ans;
	ans.first = 0;
	for (int i = 0; i < (int)s.size(); i++)
	{
		if (s[i] == '(')
			st.push('(');
		else
		{
			if (!st.empty())
				st.pop();
			else
				ans.first++;
		}
	}
	ans.second = (int)st.size();
	return ans;
}
bool balanced(string &s)
{
	int x = 0, y = 0;
	for (int i = 0; i < (int)s.size(); i++)
	{
		if (s[i] == '(')
			x++;
		else
			y++;
		if (x < y)
			return false;
	}
	if (x == y)
		return true;
	else
		return false;
}
int32_t main() {
	fast_io();
	cin >> n;
	vector < pair <int, int>> v1;
	vector < pair <int, int>> v2;
	for (int i = 0; i < n; i++)
	{
		string s;
		cin >> s;
		pair <int, int> p = get(s);
		if (p.first == 0 && p.second == 0)
			continue;
		else
		{
			if (p.second - p.first >= 0)
				v1.pb(p);
			else
				v2.pb({p.second, p.first});
		}
	}
	sort(v1.begin(), v1.end());
	sort(v2.rbegin(), v2.rend());
	string res;
	for (auto x : v1)
	{
		for (int i = 0; i < x.first; i++)
			res += ')';
		for (int i = 0; i < x.second; i++)
			res += '(';
	}
	for (auto x : v2)
	{
		for (int i = 0; i < x.second; i++)
			res += ')';
		for (int i = 0; i < x.first; i++)
			res += '(';
	}
	if (balanced(res))
		cout << "Yes\n";
	else
		cout << "No\n";
	return 0;
}
#include <bits/stdc++.h>
using namespace std;

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

using ll = long long;
using P = pair<int,int>;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int d[4] = {0,1,6,5};
const ll INF = 1LL<<60;
const ll mod = 1000000007LL;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
    ll N;
    cin>>N;
    bool s = true;
    vector<vector<ll>> xu(200005),xd(200005),yr(200005),yl(200005);
    vector<vector<vector<ll>>> A(8,vector<vector<ll>>(400005));
    rep(i,N){
        ll X,Y;
        char c;
        cin>>X>>Y>>c;
        if(c=='U') {
            xu[X].push_back(Y);
            A[0][X+Y].push_back(Y-X);
            A[1][X-Y+200000].push_back(X+Y);
        }
        if(c=='D') {
            xd[X].push_back(Y);
            A[2][X+Y].push_back(Y-X);
            A[3][X-Y+200000].push_back(X+Y);
        }
        if(c=='R') {
            yr[Y].push_back(X);
            A[4][X+Y].push_back(Y-X);
            A[5][X-Y+200000].push_back(X+Y);
        }
        if(c=='L') {
            yl[Y].push_back(X);
            A[6][X+Y].push_back(Y-X);
            A[7][X-Y+200000].push_back(X+Y);
        }
    }
    rep(i,200005){
        sort(xu[i].begin(),xu[i].end());
        sort(xd[i].begin(),xd[i].end());
        sort(yr[i].begin(),yr[i].end());
        sort(yl[i].begin(),yl[i].end());
    }
    rep(i,400005){
        rep(j,8){
            sort(A[j][i].begin(),A[j][i].end());
        }
    }
    ll ans = INF;
    rep(x,200005){
        if((!xu[x].empty()&&!xd[x].empty())&&(*xu[x].begin())<(*xd[x].rbegin())){
            for(auto t:xu[x]){
                auto ite = upper_bound(xd[x].begin(),xd[x].end(),t);
                if(ite == xd[x].end()) continue;
                s = false;
                chmin(ans,(*ite-t)*5);
            }
        }
    }
    rep(y,200005){
        if((!yr[y].empty()&&!yl[y].empty())&&(*yr[y].begin())<(*yl[y].rbegin())){
            for(auto t:yr[y]){
                auto ite = upper_bound(yl[y].begin(),yl[y].end(),t);
                if(ite == yl[y].end()) continue;
                s = false;
                chmin(ans,(*ite-t)*5);
            }
        }
    }
    rep(i,400005){
        rep(k,4){
            int j = d[k];
            if(j==0){
                if((!A[0][i].empty()&&!A[4][i].empty())){
                    for(auto p:A[0][i]){
                        auto ite = upper_bound(A[4][i].begin(),A[4][i].end(),p);
                        if(ite == A[4][i].end()) continue;
                        s = false;
                        chmin(ans,(*ite-p)*5);
                    }
                }
            }
            else{
                if((!A[j][i].empty()&&!A[8-j][i].empty())){
                    for(auto p:A[j][i]){
                        auto ite = upper_bound(A[8-j][i].begin(),A[8-j][i].end(),p);
                        if(ite == A[8-j][i].end()) continue;
                        s = false;
                        chmin(ans,(*ite-p)*5);
                    }
                }
            }
        }
    }
    if(s) cout<<"SAFE"<<endl;
    else cout<<ans<<endl;
}