#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <algorithm>
#include <set>
#include <map>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#define vll vector<ll>
#define vvvl vector<vvl>
#define vvl vector<vector<ll>>
#define VV(a, b, c, d) vector<vector<d>>(a, vector<d>(b, c))
#define VVV(a, b, c, d) vector<vvl>(a, vvl(b, vll (c, d)));
#define re(c, b) for(ll c=0;c<b;c++)
#define all(obj) (obj).begin(), (obj).end()
typedef long long int ll;
typedef long double ld;
using namespace std;

struct p{
  vvl dat = VV(0, 2, 0, ll);
  vvl x = VV(200001, 0, 0, ll);
  vvl y = VV(200001, 0, 0, ll);
};

ll M = 200001;
ll INF = 1000000000000000000;

int main(int argc, char const *argv[]) {
  // 文字が逆のもの同士で判定
  // 交差するもの同士で判定
  ll n;std::cin >> n;

  p U, D, L, R;

  for(int i=0;i<n;i++){
    ll a, b;
    char c;
    std::cin >> a >> b >> c;
    if(c=='U') {
      U.dat.push_back(vll{a, b});
      U.x[a].push_back(b);
      U.y[b].push_back(a);
    }else if(c=='D') {
      D.dat.push_back(vll{a, b});
      D.x[a].push_back(b);
      D.y[b].push_back(a);
    }else if(c=='L') {
      L.dat.push_back(vll{a, b});
      L.x[a].push_back(b);
      L.y[b].push_back(a);
    }else if(c=='R') {
      R.dat.push_back(vll{a, b});
      R.x[a].push_back(b);
      R.y[b].push_back(a);
    }
  }
  for(int i=0;i<M;i++){
    sort(all(U.x[i]));
    sort(all(U.y[i]));
    sort(all(D.x[i]));
    sort(all(D.y[i]));
    sort(all(L.x[i]));
    sort(all(L.y[i]));
    sort(all(R.x[i]));
    sort(all(R.y[i]));
  }

  // u d r l
  ll ans = INF;
  for(int i=0;i<M;i++){
    for(int j=0;j<U.x[i].size();j++){
      auto itr = upper_bound(all(D.x[i]), U.x[i][j]);
      if(itr!=D.x[i].end()) ans = min(ans, 5*((*itr)-U.x[i][j]));
    }
    for(int j=0;j<R.y[i].size();j++){
      auto itr = upper_bound(all(L.y[i]), R.y[i][j]);
      if(itr!=L.y[i].end()) ans = min(ans, 5*((*itr)-R.y[i][j]));
    }
  }

  //cross
  // u l
  vector<set<ll>> tmp(400001);// x - y + 200000のx
  for(int i=M-1;i>=0;i--){
    for(auto v:L.x[i]){
      ll num = i - v + 200000;
      tmp[num].insert(i);
    }
    for(auto v:U.x[i]){
      ll num = i - v + 200000;
      auto itr = tmp[num].lower_bound(i);
      if(itr!= tmp[num].end()) ans = min(ans, 10*((*itr) - i));
    }
  }

  // d l
  tmp = vector<set<ll>>(400001);
  for(int i=M-1;i>=0;i--){
    for(auto v:L.x[i]){
      ll num = i + v;
      tmp[num].insert(i);
    }
    for(auto v:D.x[i]){
      ll num = i + v;
      auto itr = tmp[num].lower_bound(i);
      if(itr!= tmp[num].end()) ans = min(ans, 10*((*itr) - i));
    }
  }

  // d r
  tmp = vector<set<ll>>(400001);
  for(int i=0;i<M;i++){
    for(auto v:R.x[i]){
      ll num = i - v + 200000;
      tmp[num].insert(i);
    }
    for(auto v:D.x[i]){
      ll num = i - v + 200000;
      if(tmp[num].size()==0) continue;
      auto itr = tmp[num].upper_bound(i);
      if(itr==tmp[num].begin()) continue;
      itr--;
      ans = min(ans, 10*(i - (*itr)));
    }
  }

  //u r
  tmp = vector<set<ll>>(400001);
  for(int i=0;i<M;i++){
    for(auto v:R.x[i]){
      ll num = i + v;
      tmp[num].insert(i);
    }

    for(auto v:U.x[i]){
      ll num = i + v;
      if(tmp[num].size()==0) continue;
      auto itr = tmp[num].upper_bound(i);
      if(itr==tmp[num].begin()) continue;
      itr--;
      ans = min(ans, 10*(i - (*itr)));
    }
  }

  if(ans==INF) std::cout << "SAFE" << '\n';
  else std::cout << ans << '\n';
  return 0;
}


Measured memory_bytes: 87132