#define LOCAL
#define _USE_MATH_DEFINES
#include <array>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <complex>
#include <cmath>
#include <numeric>
#include <bitset>
#include <functional>
#include <random>
#include <ctime>

using namespace std;

template <typename A, typename B>
ostream& operator <<(ostream& out, const pair<A, B>& a) {
  out << "(" << a.first << "," << a.second << ")";
  return out;
}
template <typename T, size_t N>
ostream& operator <<(ostream& out, const array<T, N>& a) {
  out << "["; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
  return out;
}
template <typename T>
ostream& operator <<(ostream& out, const vector<T>& a) {
  out << "["; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
  return out;
}
template <typename T, class Cmp>
ostream& operator <<(ostream& out, const set<T, Cmp>& a) {
  out << "{"; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream& operator <<(ostream& out, const map<U, T, Cmp>& a) {
  out << "{"; bool first = true;
  for (auto& p : a) { out << (first ? "" : ", "); out << p.first << ":" << p.second; first = 0;} out << "}";
  return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

typedef long long int64;
typedef pair<int, int> ii;
const int INF = 1 << 29;
const int MOD = 1e9 + 7;
mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x; }
#define SZ(x) (int)((x).size())

struct fast_ios {
  fast_ios() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  };
} fast_ios_;

int main() {
  int n;
  scanf("%d", &n);
  vector<ii> L, R, U, D;
  for (int i = 0; i < n; ++i) {
    static char s[10];
    int x, y;
    scanf("%d%d%s", &x, &y, s);
    if (s[0] == 'L') {
      L.push_back({x, y});
    } else if (s[0] == 'R') {
      R.push_back({x, y});
    } else if (s[0] == 'U') {
      U.push_back({x, y});
    } else {
      D.push_back({x, y});
    }
  }
  int ret = INF;
  // U collide with D
  sort(U.begin(), U.end());
  sort(D.begin(), D.end());
  for (int i = 0, j = 0, ni, nj; i < SZ(U) && j < SZ(D); ) {
    if (U[i].first < D[j].first) {
      ++i;
    } else if (U[i].first > D[j].first) {
      ++j;
    } else {
      for (ni = i + 1; ni < SZ(U) && U[ni].first == U[i].first; ++ni);
      for (nj = j + 1; nj < SZ(D) && D[nj].first == D[j].first; ++nj);
      for (int k = i; k < ni; ++k) {
        int pos = upper_bound(D.begin() + j, D.begin() + nj, U[k]) - D.begin();
        if (pos != nj) ret = min(ret, D[pos].second - U[k].second);
      }
      i = ni;
      j = nj;
    }
  }
  // L collide with R
  sort(L.begin(), L.end(), [](const ii& u, const ii& v) {
                             return u.second < v.second ||
                                               (u.second == v.second && u.first < v.first);
                           });
  sort(R.begin(), R.end(), [](const ii& u, const ii& v) {
                             return u.second < v.second ||
                                               (u.second == v.second && u.first < v.first);
                           });
  for (int i = 0, j = 0, ni, nj; i < SZ(L) && j < SZ(R); ) {
    if (L[i].second < R[j].second) {
      ++i;
    } else if (L[i].second > R[j].second) {
      ++j;
    } else {
      for (ni = i + 1; ni < SZ(L) && L[ni].second == L[i].second; ++ni);
      for (nj = j + 1; nj < SZ(R) && R[nj].second == R[j].second; ++nj);
      for (int k = j; k < nj; ++k) {
        int pos = upper_bound(L.begin() + i, L.begin() + ni, R[k]) - L.begin();
        if (pos != ni) ret = min(ret, L[pos].first - R[k].first);
      }
      i = ni;
      j = nj;
    }
  }
  // D collide with L
  {
    map<int, set<int>> mL;
    for (auto& [x, y] : L) mL[x + y].insert(x);
    for (auto& [x, y] : D) {
      if (!mL.count(x + y)) continue;
      auto& v = mL[x + y];
      auto it = v.upper_bound(x);
      if (it != v.end()) ret = min(ret, 2 * (*it - x));
    }
  }
  // D collide with R
  {
    map<int, set<int>> mD;
    for (auto& [x, y] : D) mD[x - y].insert(x);
    for (auto& [x, y] : R) {
      if (!mD.count(x - y)) continue;
      auto& v = mD[x - y];
      auto it = v.upper_bound(x);
      if (it != v.end()) ret = min(ret, 2 * (*it - x));
    }
  }
  // U collide with L
  {
    map<int, set<int>> mL;
    for (auto& [x, y] : L) mL[x - y].insert(x);
    for (auto& [x, y] : U) {
      if (!mL.count(x - y)) continue;
      auto& v = mL[x - y];
      auto it = v.upper_bound(x);
      if (it != v.end()) ret = min(ret, 2 * (*it - x));
    }
  }
  // U collide with R
  {
    map<int, set<int>> mU;
    for (auto& [x, y] : U) mU[x + y].insert(x);
    for (auto& [x, y] : R) {
      if (!mU.count(x + y)) continue;
      auto& v = mU[x + y];
      auto it = v.upper_bound(x);
      if (it != v.end()) ret = min(ret, 2 * (*it - x));
    }
  }
  if (ret == INF) {
    puts("SAFE");
  } else {
    printf("%d\n", ret * 5);
  }

  return 0;
}


Measured memory_bytes: 29096