// F.

#include <iostream>
#include <sstream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>

using namespace std;
typedef long long LL;

const int INF = 1 << 30;

int solve(vector<int> x, vector<int> y, vector<string> dir) {
	int ans = INF;
	int n = (int)x.size();
	map<int, set<int>> a, b, v, h;
	for (int i = 0; i < n; ++i) {
		if (dir[i] == "L") {
			a[x[i] - y[i]].insert(x[i]);
			h[y[i]].insert(x[i]);
		}
		if (dir[i] == "R") {
			b[x[i] + y[i]].insert(x[i]);
		}
		if (dir[i] == "D") {
			v[x[i]].insert(y[i]);
		}
	}
	for (int i = 0; i < n; ++i) {
		if (dir[i] == "R") {
			if (h.find(y[i]) != h.end()) {
				set<int>& s = h[y[i]];
				auto it = s.lower_bound(x[i]);
				if (it != s.end()) {
					ans = min(ans, (*it - x[i]) * 5);
				}
			}
		}
		if (dir[i] != "U") {
			continue;
		}
		int key = x[i] - y[i];
		if (a.find(key) != a.end()) {
			set<int>& s = a[key];
			auto it = s.lower_bound(x[i]);
			if (it != s.end()) {
				ans = min(ans, (*it - x[i]) * 10);
			}
		}
		key = x[i] + y[i];
		if (b.find(key) != b.end()) {
			set<int>& s = b[key];
			auto it = s.lower_bound(x[i]);
			if (it != s.begin()) {
				--it;
				ans = min(ans, (x[i] - *it) * 10);
			}
		}
		if (v.find(x[i]) != v.end()) {
			set<int>& s = v[x[i]];
			auto it = s.lower_bound(y[i]);
			if (it != s.end()) {
				ans = min(ans, (*it - y[i]) * 5);
			}
		}
	}
	return ans;
}

int main(int argc, char* argv[]) {
	cout.precision(20);
#ifdef _MSC_VER
	while (true)
#endif
	{
		int ans = INF;
		LL n = -1;
		cin >> n;
		if (n < 0) return 0;
		vector<int> x(n), y(n);
		vector<string> dir(n);
		for (int i = 0; i < n; ++i) {
			cin >> x[i] >> y[i] >> dir[i];
		}
		ans = solve(x, y, dir);
		map<string, string> t;
		t["D"] = "U";
		t["U"] = "D";
		t["L"] = "R";
		t["R"] = "L";
		for (int i = 0; i < n; ++i) {
			x[i] = -x[i];
			y[i] = -y[i];
			dir[i] = t[dir[i]];
		}
		ans = min(ans, solve(x, y, dir));
		if (ans == INF) {
			cout << "SAFE" << endl;
		} else {
			cout << ans << endl;
		}
	}
	return 0;
}

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int,int>;

const int INF = 1e8;

// URDL = 0123

int f(vector<P> a) {
  sort(a.begin(), a.end());
  int res = INF;
  for (int i = 1; i < a.size(); ++i) {
    if (a[i-1].second == 0 && a[i].second != 0) {
      res = min(res, a[i].first-a[i-1].first);
    }
  }
  return res;
}
int f(map<int,vector<P>>& mp) {
  int res = INF;
  for (auto p : mp) {
    res = min(res, f(p.second));
  }
  return res;
}

int main() {
  int n;
  cin >> n;
  vector<int> x(n), y(n), dir(n);
  rep(i,n) {
    char c;
    cin >> x[i] >> y[i] >> c;
    if (c == 'U') dir[i] = 0;
    if (c == 'R') dir[i] = 1;
    if (c == 'D') dir[i] = 2;
    if (c == 'L') dir[i] = 3;
  }

  int ans = INF;
  rep(ri,4) {
    { // UD
      map<int,vector<P>> mp;
      rep(i,n) {
        if (dir[i] != 0 && dir[i] != 2) continue;
        mp[x[i]].emplace_back(y[i],dir[i]);
      }
      ans = min(ans, f(mp)*5);
    }
    { // UR
      map<int,vector<P>> mp;
      rep(i,n) {
        if (dir[i] != 0 && dir[i] != 1) continue;
        mp[x[i]+y[i]].emplace_back(y[i],dir[i]);
      }
      ans = min(ans, f(mp)*10);
    }
    // rotate
    rep(i,n) {
      int px = x[i], py = y[i];
      x[i] = py;
      y[i] = -px;
      dir[i] = (dir[i]+1)%4;
    }
  }
  if (ans == INF) cout << "SAFE" << endl;
  else cout << ans << endl;
  return 0;
}