#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> p_ll;

template<class T>
void debug(T itr1, T itr2) { auto now = itr1; while(now<itr2) { cout << *now << " "; now++; } cout << endl; }
#define repr(i,from,to) for (ll i=(ll)from; i<(ll)to; i++)
#define all(vec) vec.begin(), vec.end()
#define rep(i,N) repr(i,0,N)
#define per(i,N) for (int i=(int)N-1; i>=0; i--)

const ll MOD = pow(10,9)+7;
const ll LLINF = pow(2,61)-1;
const int INF = pow(2,30)-1;

vector<ll> fac;
void c_fac(int x=pow(10,6)+10) { fac.resize(x,true); rep(i,x) fac[i] = i ? (fac[i-1]*i)%MOD : 1; }
ll inv(ll a, ll m=MOD) { ll b = m, x = 1, y = 0; while (b!=0) { int d = a/b; a -= b*d; swap(a,b); x -= y*d; swap(x,y); } return (x+m)%m; }
ll nck(ll n, ll k) { return fac[n]*inv(fac[k]*fac[n-k]%MOD)%MOD; }
ll gcd(ll a, ll b) { if (a<b) swap(a,b); return b==0 ? a : gcd(b, a%b); }
ll lcm(ll a, ll b) { return a/gcd(a,b)*b; }

struct air { ll X, Y; char U; };

int main() {
  ll N; cin >> N;
  air a[N]; rep(i,N) cin >> a[i].X >> a[i].Y >> a[i].U;
  map<ll, set<ll>> au, ad, al, ar;
  rep(i,N) {
    if (a[i].U=='U') au[a[i].X].insert(a[i].Y);
    if (a[i].U=='D') ad[a[i].X].insert(a[i].Y);
    if (a[i].U=='L') al[a[i].Y].insert(a[i].X);
    if (a[i].U=='R') ar[a[i].Y].insert(a[i].X);
  }

  ll result = LLINF;
  // U, D
  for (auto x: au) {
    if (ad.count(x.first)) {
      for (auto y: x.second) {
        auto pos = ad[x.first].upper_bound(y);
        if (pos!=ad[x.first].end()) result = min(result, ((*pos)-y)*5);
      }
    }
  }

  // L, R
  for (auto x: ar) {
    if (al.count(x.first)) {
      for (auto y: x.second) {
        auto pos = al[x.first].upper_bound(y);
        if (pos!=al[x.first].end()) result = min(result, ((*pos)-y)*5);
      }
    }
  }

  // 座標変換
  map<ll, set<ll>> aux, auy, adx, ady, alx, aly, arx, ary;
  rep(i,N) {
    if (a[i].U=='U') { aux[a[i].X-a[i].Y].insert(a[i].X+a[i].Y); auy[a[i].X+a[i].Y].insert(a[i].X-a[i].Y); }
    if (a[i].U=='D') { adx[a[i].X-a[i].Y].insert(a[i].X+a[i].Y); ady[a[i].X+a[i].Y].insert(a[i].X-a[i].Y); }
    if (a[i].U=='L') { alx[a[i].X-a[i].Y].insert(a[i].X+a[i].Y); aly[a[i].X+a[i].Y].insert(a[i].X-a[i].Y); }
    if (a[i].U=='R') { arx[a[i].X-a[i].Y].insert(a[i].X+a[i].Y); ary[a[i].X+a[i].Y].insert(a[i].X-a[i].Y); }
  }
  // for (auto x: adx) {
  //   cout << x.first << "->";
  //   for (auto y: x.second) cout << y << " "; cout << endl;
  // }
  // cout << "------------------" << endl;
  // for (auto x: alx) {
  //   cout << x.first << "->";
  //   for (auto y: x.second) cout << y << " "; cout << endl;
  // }


  // UとLはx方向が同じでyがU<Lならok
  for (auto x: aux) {
    if (alx.count(x.first)) {
      for (auto y: x.second) {
        auto pos = alx[x.first].upper_bound(y);
        if (pos!=alx[x.first].end()) result = min(result, ((*pos)-y)*5);
      }
    }
  }

  // UとRはy方向が同じでxがU>Rならok
  for (auto x: ary) {
    if (auy.count(x.first)) {
      for (auto y: x.second) {
        auto pos = auy[x.first].upper_bound(y);
        if (pos!=auy[x.first].end()) result = min(result, ((*pos)-y)*5);
      }
    }
  }

  // DとLはy方向が同じでxがD<Lならok
  for (auto x: ady) {
    if (aly.count(x.first)) {
      for (auto y: x.second) {
        auto pos = aly[x.first].upper_bound(y);
        if (pos!=aly[x.first].end()) result = min(result, ((*pos)-y)*5);
      }
    }
  }

  // DとRはx方向が同じでyがD>Rならok
  for (auto x: arx) {
    if (adx.count(x.first)) {
      for (auto y: x.second) {
        auto pos = adx[x.first].upper_bound(y);
        if (pos!=adx[x.first].end()) result = min(result, ((*pos)-y)*5);
      }
    }
  }

  if (result==LLINF) cout << "SAFE" << endl;
  else cout << result << endl;
  return 0;
}

Measured memory_bytes: 92484