#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
using u64 = unsigned long long;
#define REP(i, n) for (int i = 0; (i64)(i) < (i64)(n); ++i)

#ifdef ENABLE_DEBUG
template <typename T>
void debug(T value) {
  cerr << value;
}
template <typename T, typename... Ts>
void debug(T value, Ts... args) {
  cerr << value << ", ";
  debug(args...);
}
#define DEBUG(...)                              \
  do {                                          \
    cerr << " \033[33m (L" << __LINE__ << ") "; \
    cerr << #__VA_ARGS__ << ":\033[0m ";        \
    debug(__VA_ARGS__);                         \
    cerr << endl;                               \
  } while (0)
#else
#define debug(...)
#define DEBUG(...)
#endif

const i64 INF = 1LL << 40;

using VP = vector<tuple<i64, i64>>;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  VP pu, pr, pd, pl;
  REP(i, N) {
    i64 x, y;
    char u;
    cin >> x >> y >> u;
    if (u == 'R') {
      pr.emplace_back(x, y);
    } else if (u == 'L') {
      pl.emplace_back(x, y);
    } else if (u == 'U') {
      pu.emplace_back(x, y);
    } else {
      assert(u == 'D');
      pd.emplace_back(x, y);
    }
  }
  i64 ans = INF;

  auto check_ud = [&](const VP& pu, const VP& pd) {
    map<i64, set<i64>> t;
    for (auto [xu, yu] : pu) {
      t[xu].insert(yu);
    }
    for (auto [xd, yd] : pd) {
      auto& s = t[xd];
      auto it = s.lower_bound(yd);
      if (it == s.begin()) continue;
      i64 yu = *--it;
      assert(yu < yd);
      ans = min<i64>(ans, (yd - yu) * 5);
    }
  };
  check_ud(pu, pd);

  auto rotate = [&](const VP& p, VP& px) {
    for (auto [x, y] : p) {
      px.emplace_back(-y, x);
    }
  };
  VP pr_r, pl_r;
  rotate(pr, pr_r);
  rotate(pl, pl_r);
  check_ud(pr_r, pl_r);

  //   {  // L-R
  //     map<i64, set<i64>> t;
  //     for (auto [xr, yr] : pr) {
  //       t[yr].insert(xr);
  //     }
  //     for (auto [xl, yl] : pl) {
  //       auto& s = t[yl];
  //       auto it = s.lower_bound(xl);
  //       if (it == s.begin()) continue;
  //       i64 xr = *--it;
  //       assert(xr < xl);
  //       ans = min<i64>(ans, (xl - xr) * 5);
  //     }
  //   }
  {  // D-R
    map<i64, set<i64>> t;
    for (auto [xd, yd] : pd) {
      t[yd - xd].insert(xd);
    }
    for (auto [xr, yr] : pr) {
      auto& s = t[yr - xr];
      auto it = s.lower_bound(xr);
      if (it == s.end()) continue;
      i64 xd = *it;
      assert(xd > xr);
      ans = min<i64>(ans, 10 * (xd - xr));
    }
  }
  {  // D-L
    map<i64, set<i64>> t;
    for (auto [xd, yd] : pd) {
      t[xd + yd].insert(xd);
    }
    for (auto [xl, yl] : pl) {
      auto& s = t[xl + yl];
      auto it = s.lower_bound(xl);
      if (it == s.begin()) continue;
      i64 xd = *--it;
      assert(xd < xl);
      DEBUG(xl - xd, xl, yl);
      ans = min<i64>(ans, 10 * (xl - xd));
    }
  }
  {  // U-R
    map<i64, set<i64>> t;
    for (auto [xu, yu] : pu) {
      t[yu + xu].insert(xu);
    }
    for (auto [xr, yr] : pr) {
      auto& s = t[yr + xr];
      auto it = s.lower_bound(xr);
      if (it == s.end()) continue;
      i64 xu = *it;
      assert(xu > xr);
      ans = min<i64>(ans, 10 * (xu - xr));
    }
  }
  {  // U-L
    map<i64, set<i64>> t;
    for (auto [x, y] : pu) {
      t[y - x].insert(x);
    }
    for (auto [xl, yl] : pl) {
      auto& s = t[yl - xl];
      auto it = s.lower_bound(xl);
      if (it == s.begin()) continue;
      i64 xu = *--it;
      assert(xu < xl);
      ans = min<i64>(ans, 10 * (xl - xu));
    }
  }
  if (ans == INF) {
    cout << "SAFE" << endl;
  } else {
    cout << ans << endl;
  }
}


Measured memory_bytes: 33936