// includes {{{
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<tuple>
#include<cmath>
#include<random>
#include<cassert>
#include<bitset>
#include<cstdlib>
// #include<deque>
// #include<multiset>
// #include<cstring>
// #include<bits/stdc++.h>
// }}}
using namespace std;
using ll = long long;
// #undef DEBUG
// #define DEBUG
// DEBUG {{{
#include <array>
#include <deque>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <tuple>
#include <valarray>
#include <vector>
template < int n, class... T >
typename std::enable_if< (n >= sizeof...(T)) >::type __output_tuple(
    std::ostream &, std::tuple< T... > const &) {}
template < int n, class... T >
typename std::enable_if< (n < sizeof...(T)) >::type __output_tuple(
    std::ostream &os, std::tuple< T... > const &t) {
  os << (n == 0 ? "" : ", ") << std::get< n >(t);
  __output_tuple< n + 1 >(os, t);
}
template < class... T >
std::ostream &operator<<(std::ostream &os, std::tuple< T... > const &t) {
  os << "(";
  __output_tuple< 0 >(os, t);
  os << ")";
  return os;
}
template < class T, class U >
std::ostream &operator<<(std::ostream &os, std::pair< T, U > const &p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}
template < class T >
std::ostream &operator<<(std::ostream &os, const std::stack< T > &a) {
  os << "{";
  for(auto tmp = a; tmp.size(); tmp.pop())
    os << (a.size() == tmp.size() ? "" : ", ") << tmp.top();
  os << "}";
  return os;
}
template < class T, class Container, class Compare >
std::ostream &operator<<(std::ostream &os,
    std::priority_queue< T, Container, Compare > a) {
  os << "{ (top) ";
  while(a.size()) os << a.top() << (a.size() == 1 ? "" : ", "), a.pop();
  os << " }";
  return os;
}
template < class T, class Container >
std::ostream &operator<<(std::ostream &os, std::queue< T, Container > a) {
  os << "{ ";
  while(a.size()) os << a.front() << (a.size() == 1 ? "" : ", "), a.pop();
  os << " }";
  return os;
}
#ifdef DEBUG
#if !defined(DEBUG_OUT)
#define DEBUG_OUT std::cerr
#endif
#define dump(...)                                                                \
  [&]() {                                                                        \
    auto __debug_tap = std::make_tuple(__VA_ARGS__);                             \
    DEBUG_OUT << "[" << __LINE__ << "] " << #__VA_ARGS__ << " = " << __debug_tap \
    << std::endl;                                                      \
  }()
template < class T >
inline void dump2D(T &d, size_t sizey, size_t sizex) {
  for(size_t i = 0; i < sizey; i++) {
    DEBUG_OUT << "\t";
    for(size_t j = 0; j < sizex; j++)
      DEBUG_OUT << d[i][j] << (j + 1 == sizex ? "" : "\t");
    DEBUG_OUT << std::endl;
  }
}
template < class T >
inline void dump1D(T &d, size_t sizey) {
  for(size_t i = 0; i < sizey; i++) {
    DEBUG_OUT << d[i] << (i + 1 == sizey ? "" : " ");
  }
  DEBUG_OUT << std::endl;
}
template <
class T, class = typename std::iterator_traits< decltype(begin(T())) >::value_type,
      class = typename std::enable_if< !std::is_same< T, std::string >::value >::type >
      std::ostream &operator<<(std::ostream &os, const T &a) {
        os << "{";
        for(auto ite = begin(a); ite != end(a); ++ite)
          os << (ite == begin(a) ? "" : ", ") << *ite;
        os << "}";
        return os;
      }
#else
#define dump(...) ((void) 42)
#define dump2D(...) ((void) 42)
#define dump1D(...) ((void) 42)
template <
class T, class = typename std::iterator_traits< decltype(begin(T())) >::value_type,
      class = typename std::enable_if< !std::is_same< T, std::string >::value >::type >
      std::ostream &operator<<(std::ostream &os, const T &a) {
        for(auto ite = begin(a); ite != end(a); ++ite)
          os << (ite == begin(a) ? "" : " ") << *ite;
        return os;
      }
#endif
// }}}


// 何秒後に衝突するか
using A = ll;
ll inf = 1e10;

ll x[212345];
ll y[212345];
int dir[212345];

map<char, int> cdir{
  {'U', 0},
  {'R', 1},
  {'D', 2},
  {'L', 3},
};

int dx[] = {
  0, 1, 0, -1,
};

int dy[] = {
  1, 0, -1, 0
};

int n;
template <class T, class U> inline void smax(T &a, U b) { a = a > b ? a : b; }
template <class T, class U> inline void smin(T &a, U b) { a = a < b ? a : b; }


using P = pair<ll, ll>;
using V = vector<P>;
using VI = vector<A>;
V swap_xy(V v) {
  for(auto&e:v) e = P(e.second, e.first);
  return v;
}
V inv_x(V v) {
  for(auto&e:v) e = P(-e.first, e.second);
  return v;
}
V inv_y(V v) {
  for(auto&e:v) e = P(e.first, -e.second);
  return v;
}
A chikai(VI v1, VI v2) {
  // v1 正へ～
  if(v1.size() + v2.size()) dump(v1, v2);
  v2.push_back(-inf * 6);
  v2.push_back(inf * 6);

  A ans = inf;
  sort(v1.begin(), v1.end());
  sort(v2.begin(), v2.end());
  for(auto e: v1) {
    auto it = lower_bound(v2.begin(), v2.end(), e);
    assert(it != v2.end());
    smin(ans, abs(e - *it));
  }
  return ans * 10 / 2;
}
A check_shoumen(V v1, V v2, int i1) {
  if(i1) {
    v1 = swap_xy(v1);
    v2 = swap_xy(v2);
  }
  // 上下衝突見る
  // v1 上
  // v2 下
  vector<VI> y1(212345);
  vector<VI> y2(212345);
  for(auto e:v1) y1[e.first].push_back(e.second);
  for(auto e:v2) y2[e.first].push_back(e.second);
  A ans = inf;
  for(int i = 0; i < 212345; i++) {
    smin(ans, chikai(y1[i], y2[i]));
  }
  return ans;
}

A check_yoko(V v1, V v2, int i1, int i2) {
  if (i1 == 2) {
    // 下→上
    v1 = inv_y(v1);
    v2 = inv_y(v2);
  }
  if (i2 == 3) {
    // 左→右
    v1 = inv_x(v1);
    v2 = inv_x(v2);
  }
  // v1 が上へ
  // v2 が右へ

  vector<vector<ll>> naname1(212345 * 4);
  vector<vector<ll>> naname2(212345 * 4);
  for(auto [x, y]: v1) {
    naname1[x + y + 212345 * 2].push_back(y);
  }
  for(auto [x, y]: v2) {
    naname2[x + y + 212345 * 2].push_back(y);
  }

  A ans = inf;

  for(int i = 0; i < 212345 * 4; i++) {
    smin(ans, chikai(naname1[i], naname2[i]) * 2);
  }
  return ans;
}

V dir2ps[4];

int main() {
  std::ios::sync_with_stdio(false), std::cin.tie(0);
  cin >> n;
  for(int i = 0; i < n; i++) {
    char s;
    cin >> x[i] >> y[i] >> s;
    dir[i] = cdir[s];
    dir2ps[dir[i]].push_back(P(x[i], y[i]));
  }
  dump(dir2ps[0]);
  dump(dir2ps[1]);
  dump(dir2ps[2]);
  dump(dir2ps[3]);
  A ans = inf;
  ans = min(ans, check_shoumen(dir2ps[0], dir2ps[2], 0));
  dump(ans);
  ans = min(ans, check_shoumen(dir2ps[1], dir2ps[3], 1));
  dump(ans);

  ans = min(ans, check_yoko(dir2ps[0], dir2ps[1], 0, 1));
  dump(ans);
  ans = min(ans, check_yoko(dir2ps[0], dir2ps[3], 0, 3));
  dump(ans);
  ans = min(ans, check_yoko(dir2ps[2], dir2ps[1], 2, 1));
  dump(ans);
  ans = min(ans, check_yoko(dir2ps[2], dir2ps[3], 2, 3));
  dump(ans);

  if (ans == inf) {
    cout << "SAFE" << endl;
  } else {
    cout << ans << endl;
  }

  return 0;
}


Measured memory_bytes: 74388