//45度回転するだけ. u = x - y, v = x + yという座標系を考える.
#include <iostream>
#include <algorithm>
#include <vector>
#define int long long
#define rep(i, n) for(i = 0; i < n; i++)
using namespace std;

int INF = 1e+15;
int n;
int x[200000], y[200000]; char dir[200000];
int u[200000], v[200000];

int calcUD() {
	static vector<int> ysU[200001];
	static vector<int> ysD[200001];
	int i, j;
	
	rep(i, n) {
		if (dir[i] == 'U') {
			ysU[x[i]].push_back(y[i]);
		}
		if (dir[i] == 'D') {
			ysD[x[i]].push_back(y[i]);
		}
	}
	rep(i, 200001) {
		sort(ysU[i].begin(), ysU[i].end());
		sort(ysD[i].begin(), ysD[i].end());
	}
	
	int ret = INF;
	rep(i, 200001) { //x = i
		rep(j, ysU[i].size()) {
			int id = lower_bound(ysD[i].begin(), ysD[i].end(), ysU[i][j]) - ysD[i].begin();
			if (id == ysD[i].size()) continue;
			ret = min(ret, ysD[i][id] - ysU[i][j]);
		}
	}
	return 5 * ret;
}

int calcLR() {
	static vector<int> xsL[200001];
	static vector<int> xsR[200001];
	int i, j;
	
	rep(i, n) {
		if (dir[i] == 'L') {
			xsL[y[i]].push_back(x[i]);
		}
		if (dir[i] == 'R') {
			xsR[y[i]].push_back(x[i]);
		}
	}
	rep(i, 200001) {
		sort(xsL[i].begin(), xsL[i].end());
		sort(xsR[i].begin(), xsR[i].end());
	}
	
	int ret = INF;
	rep(i, 200001) { //y = i
		rep(j, xsR[i].size()) {
			int id = lower_bound(xsL[i].begin(), xsL[i].end(), xsR[i][j]) - xsL[i].begin();
			if (id == xsL[i].size()) continue;
			ret = min(ret, xsL[i][id] - xsR[i][j]);
		}
	}
	return 5 * ret;
}

int calcUR() {
	static vector<int> usU[400001];
	static vector<int> usR[400001];
	int i, j;
	
	rep(i, n) {
		if (dir[i] == 'U') {
			usU[v[i]].push_back(u[i]);
		}
		if (dir[i] == 'R') {
			usR[v[i]].push_back(u[i]);
		}
	}
	
	rep(i, 400001) {
		sort(usU[i].begin(), usU[i].end());
		sort(usR[i].begin(), usR[i].end());
	}
	
	int ret = INF;
	rep(i, 400001) { //v = i
		rep(j, usR[i].size()) {
			int id = lower_bound(usU[i].begin(), usU[i].end(), usR[i][j]) - usU[i].begin();
			if (id == usU[i].size()) continue;
			ret = min(ret, usU[i][id] - usR[i][j]);
		}
	}
	return 5 * ret;
}

int calcDL() {
	static vector<int> usD[400001];
	static vector<int> usL[400001];
	int i, j;
	
	rep(i, n) {
		if (dir[i] == 'D') {
			usD[v[i]].push_back(u[i]);
		}
		if (dir[i] == 'L') {
			usL[v[i]].push_back(u[i]);
		}
	}
	
	rep(i, 400001) {
		sort(usD[i].begin(), usD[i].end());
		sort(usL[i].begin(), usL[i].end());
	}
	
	int ret = INF;
	rep(i, 400001) { //v = i
		rep(j, usD[i].size()) {
			int id = lower_bound(usL[i].begin(), usL[i].end(), usD[i][j]) - usL[i].begin();
			if (id == usL[i].size()) continue;
			ret = min(ret, usL[i][id] - usD[i][j]);
		}
	}
	return 5 * ret;
}

int calcUL() {
	static vector<int> vsU[400001];
	static vector<int> vsL[400001];
	int i, j;
	
	rep(i, n) {
		if (dir[i] == 'U') {
			vsU[u[i]].push_back(v[i]);
		}
		if (dir[i] == 'L') {
			vsL[u[i]].push_back(v[i]);
		}
	}
	
	rep(i, 400001) {
		sort(vsU[i].begin(), vsU[i].end());
		sort(vsL[i].begin(), vsL[i].end());
	}
	
	int ret = INF;
	rep(i, 400001) { //u = i
		rep(j, vsU[i].size()) {
			int id = lower_bound(vsL[i].begin(), vsL[i].end(), vsU[i][j]) - vsL[i].begin();
			if (id == vsL[i].size()) continue;
			ret = min(ret, vsL[i][id] - vsU[i][j]);
		}
	}
	return 5 * ret;
}

int calcDR() {
	static vector<int> vsD[400001];
	static vector<int> vsR[400001];
	int i, j;
	
	rep(i, n) {
		if (dir[i] == 'D') {
			vsD[u[i]].push_back(v[i]);
		}
		if (dir[i] == 'R') {
			vsR[u[i]].push_back(v[i]);
		}
	}
	
	rep(i, 400001) {
		sort(vsD[i].begin(), vsD[i].end());
		sort(vsR[i].begin(), vsR[i].end());
	}
	
	int ret = INF;
	rep(i, 400001) { //u = i
		rep(j, vsR[i].size()) {
			int id = lower_bound(vsD[i].begin(), vsD[i].end(), vsR[i][j]) - vsD[i].begin();
			if (id == vsD[i].size()) continue;
			ret = min(ret, vsD[i][id] - vsR[i][j]);
		}
	}
	return 5 * ret;
}

signed main() {
	int i;
	
	cin >> n;
	rep(i, n) {
		cin >> x[i] >> y[i] >> dir[i];
		u[i] = x[i] - y[i] + 200000;
		v[i] = x[i] + y[i];
	}
	
	int ret = INF;
	int res = calcUD();
	ret = min(ret, res);
	cerr << "UD = " << res << endl;
	
	res = calcLR();
	ret = min(ret, res);
	cerr << "LR = " << res << endl;
	
	res = calcUR();
	ret = min(ret, res);
	cerr << "UR = " << res << endl;
	
	res = calcDL();
	ret = min(ret, res);
	cerr << "DL = " << res << endl;
	
	res = calcUL();
	ret = min(ret, res);
	cerr << "UL = " << res << endl;
	
	res = calcDR();
	ret = min(ret, res);
	cerr << "DR = " << res << endl;
	
	if (ret >= INF) cout << "SAFE" << endl;
	else cout << ret << endl;
	return 0;
}

Measured memory_bytes: 122460