#include <bits/stdc++.h>
#define rep(i, n) for (lli i = 0; i < (n); i++)
#define rrep(i, n) for (lli i = (n)-1; i >= 0; i--)
#define vall(x) (x).begin(), (x).end()
using namespace std;
using lli = long long int;
void YESNO(bool), YesNo(bool);
template <class T1, class T2> bool chmin(T1 &l, const T2 &r);
template <class T1, class T2> bool chmax(T1 &l, const T2 &r);
template <class T1, class T2> void vadd(vector<T1> &v, T2 x);

#define int long long int

void solve(long long N, std::vector<long long> X, std::vector<long long> Y,
           std::vector<std::string> U) {
  lli ans = 1e18;
  map<int, vector<int>> P;
  rep(i, N) P[U[i][0]].push_back(i);
  // P['U'] <-> P['D']
  {
    map<int, set<int>> up;
    map<int, set<int>> down;
    for (auto s : P['U']) {
      up[X[s]].insert(Y[s]);
    }
    for (auto s : P['D']) {
      down[X[s]].insert(Y[s]);
    }
    for (auto [s, t1] : up) {
      auto &t2 = down[s];
      for (auto tt : t1) {
        auto hoge = t2.lower_bound(tt);
        if (hoge != t2.end()) {
          chmin(ans, (*hoge - tt)*5);
        }
      }
    }
  }
  {
    map<int, set<int>> up;
    map<int, set<int>> down;
    for (auto s : P['R']) {
      up[Y[s]].insert(X[s]);
    }
    for (auto s : P['L']) {
      down[Y[s]].insert(X[s]);
    }
    for (auto [s, t1] : up) {
      auto &t2 = down[s];
      for (auto tt : t1) {
        auto hoge = t2.lower_bound(tt);
        if (hoge != t2.end()) {
          chmin(ans, (*hoge - tt)*5);
        }
      }
    }
  }

  {
    // U, L
    map<int, set<int>> m;
    for (auto s : P['L']) {
      m[-Y[s] + X[s]].insert(X[s]);
    }
    for (auto s : P['U']) {
      auto &t = m[-Y[s] + X[s]];
      auto tt = t.lower_bound(X[s]);
      if (tt != t.end()) {
        chmin(ans, abs(*tt - X[s]) * 10);
      }
    }
  }

  {
    // U, L
    map<int, set<int>> m;
    for (auto s : P['D']) {
      m[-Y[s] + X[s]].insert(X[s]);
    }
    for (auto s : P['R']) {
      auto &t = m[-Y[s] + X[s]];
      auto tt = t.lower_bound(X[s]);
      if (tt != t.end()) {
        chmin(ans, abs(*tt - X[s]) * 10);
      }
    }
  }

  {
    // U, L
    map<int, set<int>> m;
    for (auto s : P['L']) {
      m[Y[s] + X[s]].insert(X[s]);
    }
    for (auto s : P['D']) {
      auto &t = m[Y[s] + X[s]];
      auto tt = t.lower_bound(X[s]);
      if (tt != t.end()) {
        chmin(ans, abs(*tt - X[s]) * 10);
      }
    }
  }

  {
    // U, L
    map<int, set<int>> m;
    for (auto s : P['U']) {
      m[Y[s] + X[s]].insert(X[s]);
    }
    for (auto s : P['R']) {
      auto &t = m[Y[s] + X[s]];
      auto tt = t.lower_bound(X[s]);
      if (tt != t.end()) {
        chmin(ans, abs(*tt - X[s]) * 10);
      }
    }
  }
  if(ans==1e18){
    cout << "SAFE"<<endl;
  }
  else{
    cout << ans <<endl;
  }

}

signed main() {
  long long N;
  scanf("%lld", &N);
  std::vector<long long> X(N);
  std::vector<long long> Y(N);
  std::vector<std::string> U(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &X[i]);
    scanf("%lld", &Y[i]);
    std::cin >> U[i];
  }
  solve(N, std::move(X), std::move(Y), std::move(U));
  return 0;
}

// -- lib
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }

template <class T1, class T2> bool chmin(T1 &l, const T2 &r) {
  return (l > r) ? (l = r, true) : false;
}

template <class T1, class T2> bool chmax(T1 &l, const T2 &r) {
  return (l < r) ? (l = r, true) : false;
}

template <class T1, class T2> void vadd(vector<T1> &v, T2 x) {
  for (auto &s : v)
    s += T2(x);
}


Measured memory_bytes: 47872