#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <iomanip>
#include <functional>
#include <bitset>
#include <limits>
#include <cstdio>
#include <cmath>
#include <cassert>
#include <random>

#ifdef DEBUG
#include "library/Utility/debug.cpp"
#else
#define debug(...)
#endif

#define rep(i,n) for(int i=0;i<(n);++i)
#define EL '\n'
#define print(i) std::cout << (i) << '\n'
#define all(v) (v).begin(), (v).end()
using lnt = long long;
struct FIO{FIO(){std::cin.tie(0);std::cout.tie(0);std::ios_base::sync_with_stdio(0);std::cout<<std::fixed<<std::setprecision(15);}}fIO;
/*-*/

struct P
{
	lnt x,y; char u;
	P(lnt x, lnt y, char u) : x(x),y(y),u(u) {}
	bool operator<(const P& rhs) const {
		if(x!=rhs.x) return x<rhs.x;
		return y<rhs.y;
	}
};

int main() {
	// check for -> <-
	// check for same x+y, y-x (sort by y-x, x+y respectively)
	int n;
	std::cin >> n;
	std::vector<P> p;
	std::map<int,std::vector<P> > X,Y;
	std::map<int,std::vector<P> > m1,m2;
	rep(i,n) {
		int a,b; char c;
		std::cin >> a >> b >> c;
		p.emplace_back(a,b,c);
		X[a].emplace_back(a,b,c);
		Y[b].emplace_back(a,b,c);
		m1[a+b].emplace_back(a,b,c);
		m2[b-a].emplace_back(a,b,c);
	}
	lnt INF=1e17;
	lnt mintime=INF;
	// check 1
	{
		for(auto& e:X) {
			std::sort(all(e.second));
			rep(i,e.second.size()-1) {
				if(e.second[i].u=='U'&&e.second[i+1].u=='D') mintime=std::min(mintime,(e.second[i+1].y-e.second[i].y)*5);
			}
		}
		for(auto& e:Y) {
			std::sort(all(e.second));
			rep(i,e.second.size()-1) {
				if(e.second[i].u=='R'&&e.second[i+1].u=='L') mintime=std::min(mintime,(e.second[i+1].x-e.second[i].x)*5);
			}
		}
	}
	debug("check1");
	// check 2
	{
		for(auto& e:m1) {
			std::vector<P> up,low;
			for(auto& o:e.second) {
				if(o.u=='U'||o.u=='R') up.emplace_back(o);
				else low.emplace_back(o);
			}
			debug("?");
			if(!up.empty()) std::sort(all(up),[](P a, P b) { return a.y-a.x<b.y-b.x; });
			if(!low.empty()) std::sort(all(low),[](P a, P b) { return a.y-a.x<b.y-b.x; });
			debug("?");
			if(!up.empty()) rep(i,up.size()-1) {
				debug(i);
				if(up[i].u=='U'&&up[i+1].u=='R') mintime=std::min(mintime,(up[i+1].y-up[i+1].x-(up[i].y-up[i].x))*5);
			}
			if(!low.empty()) rep(i,low.size()-1) {
				debug(i,low.size());
				if(low[i].u=='L'&&low[i+1].u=='D') mintime=std::min(mintime,(low[i+1].y-low[i+1].x-(low[i].y-low[i].x))*5);
			}
			debug("?");
		}
	}
	debug("check21");
	{
		for(auto& e:m2) {
			std::vector<P> up,low;
			for(auto& o:e.second) {
				if(o.u=='U'||o.u=='L') up.emplace_back(o);
				else low.emplace_back(o);
			}
			if(!up.empty()) std::sort(all(up),[](P a, P b) { return a.y+a.x<b.y+b.x; });
			if(!low.empty()) std::sort(all(low),[](P a, P b) { return a.y+a.x<b.y+b.x; });
			if(!up.empty()) rep(i,up.size()-1) if(up[i].u=='U'&&up[i+1].u=='L') mintime=std::min(mintime,(up[i+1].y+up[i+1].x-(up[i].y+up[i].x))*5);
			if(!low.empty()) rep(i,low.size()-1) if(low[i].u=='R'&&low[i+1].u=='D') mintime=std::min(mintime,(low[i+1].y+low[i+1].x-(low[i].y+low[i].x))*5);
		}
	}
	debug("check22");
	if(mintime==INF) print("SAFE");
	else print(mintime);
}


Measured memory_bytes: 86424