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

template<typename T>
void chmin(T &a,T b){
  if(a>b)a=b;
}
typedef pair<int,int> P;
const int INF=1e8;

int main(){
  int n;cin>>n;
  int ans=INF;
  set<P> ry,ly,ux,dx,rp,rm,lp,lm,up,um,dp,dm;
  ry.insert(P(-INF,-INF));ry.insert(P(INF,INF));
  ly.insert(P(-INF,-INF));ly.insert(P(INF,INF));
  ux.insert(P(-INF,-INF));ux.insert(P(INF,INF));
  dx.insert(P(-INF,-INF));dx.insert(P(INF,INF));
  rp.insert(P(-INF,-INF));rp.insert(P(INF,INF));
  rm.insert(P(-INF,-INF));rm.insert(P(INF,INF));
  lp.insert(P(-INF,-INF));lp.insert(P(INF,INF));
  lm.insert(P(-INF,-INF));lm.insert(P(INF,INF));
  up.insert(P(-INF,-INF));up.insert(P(INF,INF));
  um.insert(P(-INF,-INF));um.insert(P(INF,INF));
  dp.insert(P(-INF,-INF));dp.insert(P(INF,INF));
  dm.insert(P(-INF,-INF));dm.insert(P(INF,INF));
  for(int i=0;i<n;i++){
    int x,y;cin>>x>>y;
    char c;cin>>c;
    if(c=='R'){
      ry.insert(P(y,x));
      rp.insert(P(x+y,x));
      rm.insert(P(x-y,x));
    }
    if(c=='L'){
      ly.insert(P(y,x));
      lp.insert(P(x+y,x));
      lm.insert(P(x-y,x));
    }
    if(c=='U'){
      ux.insert(P(x,y));
      up.insert(P(x+y,x));
      um.insert(P(x-y,x));
    }
    if(c=='D'){
      dx.insert(P(x,y));
      dp.insert(P(x+y,x));
      dm.insert(P(x-y,x));
    }
  }
  for(P R:ry){
    P L=*ly.lower_bound(R);
    if(L.first==R.first&&L.second>R.second)chmin(ans,(L.second-R.second)*5);
  }
  for(P U:ux){
    P D=*dx.lower_bound(U);
    if(D.first==U.first&&D.second>U.second)chmin(ans,(D.second-U.second)*5);
  }
  for(P R:rp){
    P U=*up.lower_bound(R);
    if(R.first==U.first&&R.second<U.second)chmin(ans,(U.second-R.second)*10);
  }
  for(P R:rm){
    P D=*dm.lower_bound(R);
    if(R.first==D.first&&R.second<D.second)chmin(ans,(D.second-R.second)*10);
  }
  for(P L:lp){
    P D=*--dp.lower_bound(L);
    if(L.first==D.first&&L.second>D.second)chmin(ans,(L.second-D.second)*10);
  }
  for(P L:lm){
    P U=*--um.lower_bound(L);
    if(L.first==U.first&&L.second>U.second)chmin(ans,(L.second-U.second)*10);
  }
  if(ans==INF)cout<<"SAFE"<<endl;
  else cout<<ans<<endl;
}
