#include "bits/stdc++.h"
using namespace std;

// Inserted snippets: io, root
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
template<typename H> bool chmin(H& v1, const H v2) { if (v1 > v2) { v1 = v2; return true; } return false; }
template<typename H> bool chmax(H& v1, const H v2) { if (v1 < v2) { v1 = v2; return true; } return false; }
template<typename H> void read(H& head) { cin >> head; }
template<typename H, typename ...T> void read(H& head, T& ...tail) { cin >> head; read(tail...); }
template<typename H> void write(H head) { cout << head << '\n'; }
template<typename H, typename ...T> void write(H head, T ...tail) { cout << head << " "; write(tail...); }
template<typename ...T> void die(T ...tok) { write(tok...); exit(0); }
// End snippets

int main() {
	cin.tie(0)->sync_with_stdio(0);
	cin.exceptions(cin.failbit);

  cin >> skipws;

  int n; read(n);
  
  map<ll, vector<pair<ll, char>>> horz, vert, sposul, sposdr, snegur, snegdl;

  rep(i, 0, n) {
    ll x, y; char u;
    read(x, y, u);
    if (u == 'L') horz[y].push_back({x, 'L'}), sposul[x - y].push_back({x + y, 'L'}), snegdl[x + y].push_back({x - y, 'L'});
    else if (u == 'R') horz[y].push_back({x, 'R'}), sposdr[x - y].push_back({x + y, 'R'}), snegur[x + y].push_back({x - y, 'R'});
    else if (u == 'U') vert[x].push_back({y, 'U'}), sposul[x - y].push_back({x + y, 'U'}), snegur[x + y].push_back({x - y, 'U'});
    else if (u == 'D') vert[x].push_back({y, 'D'}), sposdr[x - y].push_back({x + y, 'D'}), snegdl[x + y].push_back({x - y, 'D'});
  }

  for (auto&& p : horz) sort(all(p.second));
  for (auto&& p : vert) sort(all(p.second));
  for (auto&& p : sposul) sort(all(p.second));
  for (auto&& p : sposdr) sort(all(p.second));
  for (auto&& p : snegur) sort(all(p.second));
  for (auto&& p : snegdl) sort(all(p.second));

  const ll inf = 0x3f3f3f3f3f3f3f3fll;
  ll ans = inf;

  for (auto&& p : horz)
    rep(i, 0, sz(p.second) - 1)
      if (p.second[i].second == 'R' && p.second[i + 1].second == 'L')
        chmin(ans, (p.second[i + 1].first - p.second[i].first) * 5);
  for (auto&& p : vert)
    rep(i, 0, sz(p.second) - 1)
      if (p.second[i].second == 'U' && p.second[i + 1].second == 'D')
        chmin(ans, (p.second[i + 1].first - p.second[i].first) * 5);
  for (auto&& p : sposdr)
    rep(i, 0, sz(p.second) - 1)
      if (p.second[i].second == 'R' && p.second[i + 1].second == 'D')
        chmin(ans, (p.second[i + 1].first - p.second[i].first) * 5);
  for (auto&& p : sposul)
    rep(i, 0, sz(p.second) - 1)
      if (p.second[i].second == 'U' && p.second[i + 1].second == 'L')
        chmin(ans, (p.second[i + 1].first - p.second[i].first) * 5);
  for (auto&& p : snegdl)
    rep(i, 0, sz(p.second) - 1)
      if (p.second[i].second == 'D' && p.second[i + 1].second == 'L')
        chmin(ans, (p.second[i + 1].first - p.second[i].first) * 5);
  for (auto&& p : snegur)
    rep(i, 0, sz(p.second) - 1)
      if (p.second[i].second == 'R' && p.second[i + 1].second == 'U')
        chmin(ans, (p.second[i + 1].first - p.second[i].first) * 5);

  if (ans == inf)
    write("SAFE");
  else
    write(ans);
}


Measured memory_bytes: 68556