#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<queue>
#include<cmath>
#include<cstdio>
#include<tuple>
#include<bitset>
#include<map>

using namespace std;
#define rep(i,n) for(int i=0;i<n;++i)
#define rep1(i,n) for(int i=1;i<=n;++i)
#define ALL(x) x.begin(),x.end()
using P=pair<int,int>;
using lint=long long;
using ll=long long;
const lint inf=1e18+7;
const int MOD=1000000007;
class Plane{
  public:
  int x,y;
  char u;
  void input(){
    cin>>x>>y;
    cin>>u;
  }
};
lint solve(int n,vector<Plane> plane){
  //uとl, ｒとd 
  map<int,vector<int>> l,r,u,d;
  lint res=inf;
  rep(i,n){
    if(plane[i].u=='L'){
      l[plane[i].y-plane[i].x].push_back(plane[i].x);
    }
    else if(plane[i].u=='R'){
      r[plane[i].y-plane[i].x].push_back(plane[i].x);
    }
    else if(plane[i].u=='U'){
      u[plane[i].y-plane[i].x].push_back(plane[i].x);
    }
    else if(plane[i].u=='D'){
      d[plane[i].y-plane[i].x].push_back(plane[i].x);
    }
  }
  for(auto& val:u){
    if(l.find(val.first)!=l.end()){
      sort(val.second.begin(),val.second.end());
      sort(l[val.first].begin(),l[val.first].end());
      for(auto& x:val.second){
        auto it=lower_bound(ALL(l[val.first]),x);
        if(it==l[val.first].end()){
          continue;
        }
        lint buf=*it-x;
        buf*=10;
        res=min(res,buf);
      }
    }
  }
  for(auto& val:r){
    if(d.find(val.first)!=d.end()){
      sort(val.second.begin(),val.second.end());
      sort(d[val.first].begin(),d[val.first].end());
      for(auto& x:val.second){
        auto it=lower_bound(ALL(d[val.first]),x);
        if(it==d[val.first].end())continue;
        lint buf=*it-x;
        buf*=10;
        res=min(res,buf);
      }
    }
  }
  return res;
}
lint solve2(int n,vector<Plane> plane){
  //dとl, rとu
  map<int,vector<int>> l,r,u,d;
  lint res=inf;
  rep(i,n){
    if(plane[i].u=='L'){
      l[plane[i].y+plane[i].x].push_back(plane[i].x);
    }
    else if(plane[i].u=='R'){
      r[plane[i].y+plane[i].x].push_back(plane[i].x);
    }
    else if(plane[i].u=='U'){
      u[plane[i].y+plane[i].x].push_back(plane[i].x);
    }
    else if(plane[i].u=='D'){
      d[plane[i].y+plane[i].x].push_back(plane[i].x);
    }
  }
  for(auto& val:d){
    if(l.find(val.first)!=l.end()){
      sort(val.second.begin(),val.second.end());
      sort(l[val.first].begin(),l[val.first].end());
      for(auto& x:val.second){
        auto it=lower_bound(ALL(l[val.first]),x);
        if(it==l[val.first].end())continue;
        lint buf=*it-x;
        buf*=10;
        res=min(res,buf);
      }
    }
  }
  for(auto& val:r){
    if(u.find(val.first)!=u.end()){
      sort(val.second.begin(),val.second.end());
      sort(u[val.first].begin(),u[val.first].end());
      for(auto& x:val.second){
        auto it=lower_bound(ALL(u[val.first]),x);
        if(it==u[val.first].end())continue;
        lint buf=*it-x;
        buf*=10;
        res=min(res,buf);
      }
    }
  }
  return res;
}
lint solve3(int n,vector<Plane> plane){
  map<int,vector<int>> l,r,u,d;
  lint res=inf;
  rep(i,n){
    if(plane[i].u=='L'){
      l[plane[i].y].push_back(plane[i].x);
    }
    else if(plane[i].u=='R'){
      r[plane[i].y].push_back(plane[i].x);
    }
    else if(plane[i].u=='U'){
      u[plane[i].x].push_back(plane[i].y);
    }
    else if(plane[i].u=='D'){
      d[plane[i].x].push_back(plane[i].y);
    }
  }
  for(auto& val:u){
    if(d.find(val.first)!=d.end()){
      sort(val.second.begin(),val.second.end());
      sort(d[val.first].begin(),d[val.first].end());
      for(auto& x:val.second){
        auto it=lower_bound(ALL(d[val.first]),x);
        if(it==d[val.first].end())continue;
        lint buf=*it-x;
        buf*=5;
        res=min(res,buf);
      }
    }
  }
  //cout<<res<<"\n";
  for(auto& val:r){
    if(l.find(val.first)!=l.end()){
      sort(val.second.begin(),val.second.end());
      sort(l[val.first].begin(),l[val.first].end());
      for(auto& x:val.second){
        auto it=lower_bound(ALL(l[val.first]),x);
        if(it==l[val.first].end())continue;
        lint buf=*it-x;
        buf*=5;
        res=min(res,buf);
      }
    }
  }
  //cout<<res<<"\n";
  return res;
}
signed main(){
  int n;cin>>n;
  vector<Plane> plane(n);
  rep(i,n)plane[i].input();
  lint res=min(inf,solve(n,plane));
  res=min(res,solve2(n,plane));
  res=min(res,solve3(n,plane));
  if(res==inf){
    cout<<"SAFE"<<"\n";
    return 0;
  }
  cout<<res<<"\n";
  return 0;
}


Measured memory_bytes: 29772