// Author: Tyler Wang

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdlib>
#include <deque>
#include <functional>
#include <initializer_list>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <memory>
#include <numeric>
#include <optional>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <string_view>
#include <tuple>
#include <type_traits>
#include <utility>
#include <vector>

using namespace std;

template <class C>
constexpr auto ssize(const C& c)
    -> std::common_type_t<std::ptrdiff_t,
                          std::make_signed_t<decltype(c.size())>> {
  using R = std::common_type_t<std::ptrdiff_t,
                               std::make_signed_t<decltype(c.size())>>;
  return (R)c.size();
}

template <class T, std::ptrdiff_t N>
constexpr std::ptrdiff_t ssize(const T (&)[N]) noexcept {
  return N;
}

template <typename T>
constexpr std::enable_if_t<std::is_unsigned_v<T>, bool> has_single_bit(
    T x) noexcept {
  return x != 0 && (x & (x - 1)) == 0;
}

template <typename T>
constexpr std::enable_if_t<std::is_unsigned_v<T>, int> countl_zero(
    T x) noexcept {
  if constexpr (sizeof(T) <= sizeof(unsigned)) {
    return __builtin_clz(x) - (std::numeric_limits<unsigned>::digits -
                               std::numeric_limits<T>::digits);
  } else if constexpr (sizeof(T) <= sizeof(unsigned long)) {
    return __builtin_clzl(x) - (std::numeric_limits<unsigned long>::digits -
                                std::numeric_limits<T>::digits);
  } else {
    static_assert(sizeof(T) <= sizeof(unsigned long long));
    return __builtin_clzll(x) -
           (std::numeric_limits<unsigned long long>::digits -
            std::numeric_limits<T>::digits);
  }
}

template <typename T>
constexpr std::enable_if_t<std::is_unsigned_v<T>, int> countr_zero(
    T x) noexcept {
  if constexpr (sizeof(T) <= sizeof(unsigned)) {
    return __builtin_ctz(x);
  } else if constexpr (sizeof(T) <= sizeof(unsigned long)) {
    return __builtin_ctzl(x);
  } else {
    static_assert(sizeof(T) <= sizeof(unsigned long long));
    return __builtin_ctzll(x);
  }
}

template <typename T>
constexpr std::enable_if_t<std::is_unsigned_v<T>, int> popcount(T x) noexcept {
  if constexpr (sizeof(T) <= sizeof(unsigned)) {
    return __builtin_popcount(x);
  } else if constexpr (sizeof(T) <= sizeof(unsigned long long)) {
    return __builtin_popcountl(x);
  } else {
    static_assert(sizeof(T) <= sizeof(unsigned long long));
    return __builtin_popcountll(x);
  }
}

template <typename T>
constexpr std::enable_if_t<std::is_unsigned_v<T>, T> bit_width(T x) noexcept {
  return (T)(std::numeric_limits<T>::digits - countl_zero(x));
}

template <typename T>
constexpr std::enable_if_t<std::is_unsigned_v<T>, T> bit_ceil(T x) noexcept {
  return (T)(x <= 1 ? 1 : (T)1 << bit_width((T)(x - 1)));
}

template <typename T>
constexpr std::enable_if_t<std::is_unsigned_v<T>, T> bit_floor(T x) noexcept {
  return (T)(x == 0 ? 0 : (T)1 << (bit_width(x) - 1));
}

#if defined(__GNUC__) && !defined(__clang__)

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

template <typename Key, typename Compare = std::less<Key>,
          typename Allocator = std::allocator<Key>>
using ordered_set =
    __gnu_pbds::tree<Key, __gnu_pbds::null_type, Compare,
                     __gnu_pbds::rb_tree_tag,
                     __gnu_pbds::tree_order_statistics_node_update, Allocator>;

template <typename Key, typename T, typename Compare = std::less<Key>,
          typename Allocator = std::allocator<std::pair<const Key, T>>>
using ordered_map =
    __gnu_pbds::tree<Key, T, Compare, __gnu_pbds::rb_tree_tag,
                     __gnu_pbds::tree_order_statistics_node_update, Allocator>;

#endif

template <typename Fun>
class y_combinator_result {
 public:
  template <typename T>
  explicit y_combinator_result(T&& fun) : fun_(std::forward<T>(fun)) {}

  template <typename... Args>
  decltype(auto) operator()(Args&&... args) {
    return fun_(std::ref(*this), std::forward<Args>(args)...);
  }

 private:
  Fun fun_;
};

template <typename Fun>
decltype(auto) y_combinator(Fun&& fun) {
  return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const std::optional<T>& opt);

template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, const std::pair<T1, T2>& p);

template <typename... Ts>
std::ostream& operator<<(std::ostream& os, const std::tuple<Ts...>& t);

template <typename Container,
          std::enable_if_t<!std::is_convertible_v<Container, std::string_view>,
                           typename Container::const_iterator>* = nullptr>
std::ostream& operator<<(std::ostream& os, const Container& c);

template <typename ContainerAdaptor,
          typename ContainerAdaptor::container_type* = nullptr>
std::ostream& operator<<(std::ostream& os, const ContainerAdaptor& a);

template <typename T>
std::ostream& operator<<(std::ostream& os, const std::optional<T>& opt) {
  return opt ? os << *opt : os << "nullopt";
}

template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, const std::pair<T1, T2>& p) {
  return os << '{' << p.first << ", " << p.second << '}';
}

template <typename... Ts>
std::ostream& operator<<(std::ostream& os, const std::tuple<Ts...>& t) {
  return std::apply(
      [&os](const Ts&... args) -> std::ostream& {
        os << '{';
        int i = 0;
        ((os << (i++ == 0 ? "" : ", ") << args), ...);
        return os << '}';
      },
      t);
}

template <typename Container,
          std::enable_if_t<!std::is_convertible_v<Container, std::string_view>,
                           typename Container::const_iterator>*>
std::ostream& operator<<(std::ostream& os, const Container& c) {
  os << '{';
  for (auto it = c.begin(); it != c.end(); ++it) {
    os << (it == c.begin() ? "" : ", ") << *it;
  }
  return os << '}';
}

template <typename ContainerAdaptor, typename ContainerAdaptor::container_type*>
std::ostream& operator<<(std::ostream& os, const ContainerAdaptor& a) {
  class subadaptor : public ContainerAdaptor {
   public:
    const auto& c() const { return ContainerAdaptor::c; }
  };
  return os << static_cast<const subadaptor&>(a).c();
}

namespace detail {

template <typename... Ts>
void print(std::ostream& os, int line, std::string_view keys,
           const Ts&... vals) {
  if constexpr (sizeof...(Ts) == 0) {
    os << "  " << line << " |" << std::endl;
  } else {
    std::vector<std::string_view> key_list;
    key_list.reserve(sizeof...(Ts));
    int start = 0, paren = 0;
    for (int i = 0; i < (int)keys.size(); ++i) {
      switch (keys[i]) {
        case '(':
        case '[':
        case '{':
          ++paren;
          break;
        case ')':
        case ']':
        case '}':
          --paren;
          break;
        case ',':
          if (paren == 0) {
            key_list.push_back(keys.substr(start, i - start));
            start = i;
          }
          break;
      }
    }
    key_list.push_back(keys.substr(start));
    assert(key_list.size() == sizeof...(Ts));
    os << "  " << line << " | ";
    int i = 0;
    ((os << key_list[i++] << " = " << vals), ...);
    os << std::endl;
  }
}

}  // namespace detail

#ifdef TYLER
#define debug(...) \
  detail::print(std::cerr, __LINE__, #__VA_ARGS__ __VA_OPT__(, ) __VA_ARGS__)
#else
#define debug(...) ((void)0)
#endif

int main() {
  auto start_time = std::chrono::steady_clock::now();
  std::mt19937 thanos(
      (std::uint_fast32_t)start_time.time_since_epoch().count());
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::cout.precision(24);
  int n;
  cin >> n;
  map<int, vector<int>> up, down, left, right;
  for (int i = 0; i < n; ++i) {
    int x, y;
    char u;
    cin >> x >> y >> u;
    if (u == 'U') {
      up[x].push_back(y);
    } else if (u == 'D') {
      down[x].push_back(y);
    } else if (u == 'L') {
      left[y].push_back(x);
    } else {
      assert(u == 'R');
      right[y].push_back(x);
    }
  }
  for (auto& [k, v] : up) {
    sort(v.begin(), v.end());
  }
  for (auto& [k, v] : down) {
    sort(v.begin(), v.end());
  }
  for (auto& [k, v] : left) {
    sort(v.begin(), v.end());
  }
  for (auto& [k, v] : right) {
    sort(v.begin(), v.end());
  }
  constexpr int INF = 1e9;
  int ans = INF;
  for (auto& [k, v] : up) {
    auto& d = down[k];
    for (int y : v) {
      auto it = upper_bound(d.begin(), d.end(), y);
      if (it != d.end()) {
        ans = min(ans, 5 * (*it - y));
      }
    }
  }
  for (auto& [k, v] : right) {
    auto& l = left[k];
    for (int x : v) {
      auto it = upper_bound(l.begin(), l.end(), x);
      if (it != l.end()) {
        ans = min(ans, 5 * (*it - x));
      }
    }
  }
  // abs(x1 - x2) = abs(y1 - y2)
  // x1 - y1 = x2 - y2 or x1 + y1 = x2 + y2
  {
    map<int, vector<int>> sum, sub;
    for (auto& [x, v] : up) {
      for (int y : v) {
        sum[x + y].push_back(x);
        sub[x - y].push_back(x);
      }
    }
    for (auto& [k, v] : sum) {
      sort(v.begin(), v.end());
    }
    for (auto& [k, v] : sub) {
      sort(v.begin(), v.end());
    }
    // x1 = x2 - t
    // y1 + t = y2
    // x1 - y1 = x2 - y2
    for (auto& [y2, v2] : left) {
      for (int x2 : v2) {
        auto& v1 = sub[x2 - y2];
        auto it = lower_bound(v1.begin(), v1.end(), x2);
        if (it != v1.begin()) {
          ans = min(ans, 10 * abs(*prev(it) - x2));
        }
      }
    }
    // x1 = x2 + t
    // y1 + t = y2
    // x1 + y1 = x2 + y2
    for (auto& [y2, v2] : right) {
      for (int x2 : v2) {
        auto& v1 = sum[x2 + y2];
        auto it = upper_bound(v1.begin(), v1.end(), x2);
        if (it != v1.end()) {
          ans = min(ans, 10 * abs(*it - x2));
        }
      }
    }
  }
  {
    map<int, vector<int>> sum, sub;
    for (auto& [x, v] : down) {
      for (int y : v) {
        sum[x + y].push_back(x);
        sub[x - y].push_back(x);
      }
    }
    for (auto& [k, v] : sum) {
      sort(v.begin(), v.end());
    }
    for (auto& [k, v] : sub) {
      sort(v.begin(), v.end());
    }
    // x1 = x2 - t
    // y1 - t = y2
    // x1 + y1 = x2 + y2
    for (auto& [y2, v2] : left) {
      for (int x2 : v2) {
        auto& v1 = sum[x2 + y2];
        auto it = lower_bound(v1.begin(), v1.end(), x2);
        if (it != v1.begin()) {
          ans = min(ans, 10 * abs(*prev(it) - x2));
        }
      }
    }
    // x1 = x2 + t
    // y1 - t = y2
    // x1 - y1 = x2 - y2
    for (auto& [y2, v2] : right) {
      for (int x2 : v2) {
        auto& v1 = sub[x2 - y2];
        auto it = upper_bound(v1.begin(), v1.end(), x2);
        if (it != v1.end()) {
          ans = min(ans, 10 * abs(*it - x2));
        }
      }
    }
  }
  if (ans == INF) {
    cout << "SAFE\n";
  } else {
    cout << ans << '\n';
  }
  return 0;
}

Measured memory_bytes: 68548