#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
const int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} iosetup;

int main() {
  const int L = 200000;
  int n; cin >> n;
  vector<int> x(n), y(n);
  vector<char> u(n);
  REP(i, n) cin >> x[i] >> y[i] >> u[i];
  int ans = L * 10 + 1;
  vector<vector<int>> row(L + 1);
  REP(i, n) {
    if (u[i] == 'L' || u[i] == 'R') row[y[i]].emplace_back(i);
  }
  REP(i, L + 1) {
    vector<int> l, r;
    for (int idx : row[i]) (u[idx] == 'L' ? l : r).emplace_back(x[idx]);
    sort(ALL(l));
    sort(ALL(r));
    int pos = 0;
    REP(j, r.size()) {
      while (pos < l.size() && l[pos] < r[j]) ++pos;
      if (pos < l.size()) chmin(ans, (l[pos] - r[j]) * 5);
    }
  }
  vector<vector<int>> col(L + 1);
  REP(i, n) {
    if (u[i] == 'U' || u[i] == 'D') col[x[i]].emplace_back(i);
  }
  REP(i, L + 1) {
    vector<int> us, d;
    for (int idx : col[i]) (u[idx] == 'U' ? us : d).emplace_back(y[idx]);
    sort(ALL(us));
    sort(ALL(d));
    int pos = 0;
    REP(j, us.size()) {
      while (pos < d.size() && d[pos] < us[j]) ++pos;
      if (pos < d.size()) chmin(ans, (d[pos] - us[j]) * 5);
    }
  }
  map<int, set<int>> add_l, sub_l, add_r, sub_r;
  REP(i, n) {
    if (u[i] == 'L') {
      add_l[y[i] + x[i]].emplace(x[i]);
      sub_l[y[i] - x[i]].emplace(x[i]);
    } else if (u[i] == 'R') {
      add_r[y[i] + x[i]].emplace(x[i]);
      sub_r[y[i] - x[i]].emplace(x[i]);
    }
  }
  REP(i, n) {
    if (u[i] == 'U') {
      if (add_r.count(y[i] + x[i]) == 1) {
        auto it = add_r[y[i] + x[i]].lower_bound(x[i]);
        if (it != add_r[y[i] + x[i]].begin()) {
          it = prev(it);
          chmin(ans, (x[i] - *it) * 10);
        }
      }
      if (sub_l.count(y[i] - x[i]) == 1) {
        auto it = sub_l[y[i] - x[i]].lower_bound(x[i]);
        if (it != sub_l[y[i] - x[i]].end()) {
          chmin(ans, (*it - x[i]) * 10);
        }
      }
    } else if (u[i] == 'D') {
      if (add_l.count(y[i] + x[i]) == 1) {
        auto it = add_l[y[i] + x[i]].lower_bound(x[i]);
        if (it != add_l[y[i] + x[i]].end()) {
          chmin(ans, (*it - x[i]) * 10);
        }
      }
      if (sub_r.count(y[i] - x[i]) == 1) {
        auto it = sub_r[y[i] - x[i]].lower_bound(x[i]);
        if (it != sub_r[y[i] - x[i]].begin()) {
          it = prev(it);
          chmin(ans, (x[i] - *it) * 10);
        }
      }
    }
  }
  if (ans == L * 10 + 1) {
    cout << "SAFE\n";
  } else {
    cout << ans << '\n';
  }
  return 0;
}


Measured memory_bytes: 70728