#ifdef ONLINE_JUDGE
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2,bmi,bmi2,lzcnt")
#endif
#include <x86intrin.h>
#include <bits/stdc++.h>
using namespace std;

struct fenwick_set {
  int n, m, sz = 0;
  vector<uint64_t> dat;
  vector<int> tree;
  fenwick_set(int _n = 0) : n(_n), m(n / 64 + 1), dat(m), tree(m + 1) {}
  int operator[](int i) const {
    return dat[i / 64] >> i % 64 & 1;
  }
  void set(int i) {
    if ((*this)[i]) return;
    ++sz;
    dat[i / 64] |= 1uLL << i % 64;
    for (int j = i / 64 + 1; j <= m; j += _blsi_u64(j)) {
      ++tree[j];
    }
  }
  void reset(int i) {
    if ((*this)[i] == 0) return;
    --sz;
    dat[i / 64] &= ~(1uLL << i % 64);
    for (int j = i / 64 + 1; j <= m; j += _blsi_u64(j)) {
      --tree[j];
    }
  }
  int rank(int i) const {
    i = clamp(i, 0, n);
    int res = _popcnt64(_bzhi_u64(dat[i / 64], i % 64));
    for (int j = i / 64; j; j = _blsr_u64(j)) {
      res += tree[j];
    }
    return res;
  }
  int select(int k) const {
    if (k < 0) return -1;
    if (k >= sz) return n;
    int j = 0;
    for (int w = 1 << __lg(m); w; w >>= 1) {
      if (j + w <= m and tree[j + w] <= k) {
        j += w;
        k -= tree[j];
      }
    }
    return j * 64 + _tzcnt_u64(_pdep_u64(1uLL << k, dat[j]));
  }
  int pred(int i) const {
    return select(rank(i) - 1);
  }
  int succ(int i) const {
    return select(rank(i + 1));
  }
};

template <class T> constexpr T inf = numeric_limits<T>::max() / 2.1;

auto chmin = [](auto&& a, auto b) { return b < a ? a = b, 1 : 0; };

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> x(n), y(n);
  string s(n, '?');
  for (int i = 0; i < n; ++i) {
    cin >> x[i] >> y[i] >> s[i];
    x[i] *= 2;
    y[i] *= 2;
  }
  const string dir = "RULD";
  constexpr int lim = 4e5;
  int res = inf<int>;
  for (int _ = 4; _--; ) {
    {
      vector<vector<int>> vr(lim + 1), vl(lim + 1);
      for (int i = 0; i < n; ++i) {
        if (s[i] == 'R') {
          vr[y[i]].push_back(x[i]);
        }
        if (s[i] == 'L') {
          vl[y[i]].push_back(x[i]);
        }
      }
      fenwick_set fs(lim + 1);
      for (int z = 0; z <= lim; ++z) {
        for (int e : vr[z]) {
          fs.set(e);
        }
        for (int e : vl[z]) {
          int t = fs.pred(e);
          if (t != -1) {
            chmin(res, (e - t) / 2);
          }
        }
        for (int e : vr[z]) {
          fs.reset(e);
        }
      }
    }
    {
      vector<vector<int>> vr(lim + 1), vu(lim + 1);
      for (int i = 0; i < n; ++i) {
        if (s[i] == 'R') {
          vr[(x[i] + y[i]) / 2].push_back(x[i]);
        }
        if (s[i] == 'U') {
          vu[(x[i] + y[i]) / 2].push_back(x[i]);
        }
      }
      fenwick_set fs(lim + 1);
      for (int z = 0; z <= lim; ++z) {
        for (int e : vr[z]) {
          fs.set(e);
        }
        for (int e : vu[z]) {
          int t = fs.pred(e);
          if (t != -1) {
            chmin(res, e - t);
          }
        }
        for (int e : vr[z]) {
          fs.reset(e);
        }
      }
    }
    for (int i = 0; i < n; ++i) {
      tie(x[i], y[i]) = make_pair(lim - y[i], x[i]);
      s[i] = dir[(dir.find(s[i]) + 1) % 4];
    }
  }
  if (res == inf<int>) {
    cout << "SAFE\n";
  } else {
    res *= 5;
    cout << res << '\n';
  }
}


Measured memory_bytes: 29960