/// kazuki08
/*
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int long long
typedef long long ll;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int,int> pi;
typedef vector<pi> vpi;
#define ar array
//#define inf 1000000000
#define mod 1000000007
#define dmp(x) cerr<<"line "<<__LINE__<<" "<<#x<<":"<<x<<endl
#define fs first
#define sc second
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define mt make_tuple
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){
    return o<<"("<<p.fs<<","<<p.sc<<")";
}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){
    o<<"{";
    for(const T& v:vc) o<<v<<",";
    o<<"}";
    return o;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int MAXN = 202000;
int n,x[MAXN],y[MAXN];
char type[MAXN];
map<int,vi> leftplus,leftminus,rightplus,rightminus,upplus,upminus,downplus,downminus;
vi up[MAXN],down[MAXN],lef[MAXN],rig[MAXN];

signed main(){
IOS;
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
  
  cin >> n;
  for(int i=1;i<=n;i++){
    cin >> x[i] >> y[i] >> type[i];
    if(type[i]=='L'){
      leftplus[x[i]+y[i]].pb(x[i]);
      leftminus[x[i]-y[i]].pb(x[i]);
      lef[y[i]].pb(x[i]);
    }
    else if(type[i]=='R'){
      rightplus[x[i]+y[i]].pb(x[i]);
      rightminus[x[i]-y[i]].pb(x[i]);
      rig[y[i]].pb(x[i]);
    }
    else if(type[i]=='U'){
      upplus[x[i]+y[i]].pb(x[i]);
      upminus[x[i]-y[i]].pb(x[i]);
      up[x[i]].pb(y[i]);
    }
    else {
      downplus[x[i]+y[i]].pb(x[i]);
      downminus[x[i]-y[i]].pb(x[i]);
      down[x[i]].pb(y[i]);
    }
  }
  for(auto &t:leftplus){
    sort(all(t.second));
  }
  for(auto &t:leftminus){
    sort(all(t.second));
  }
  for(auto &t:rightplus){
    sort(all(t.second));
  }
  for(auto &t:rightminus){
    sort(all(t.second));
  }
  for(auto &t:upplus){
    sort(all(t.second));
  }
  for(auto &t:upminus){
    sort(all(t.second));
  }
  for(auto &t:downplus){
    sort(all(t.second));
  }
  for(auto &t:downminus){
    sort(all(t.second));
  }
  int ans = 1e9;
  for(auto t:leftplus){
    int slope = t.first;
    for(auto u:t.second){
      if(downplus.find(slope)!=downplus.end()){
        auto it = lower_bound(downplus[slope].begin(),downplus[slope].end(),u);
        if(it==downplus[slope].begin())continue;
        it--;
        ans = min(ans, (u-(*it))*10);
        // cerr << "found leftplus" << " " << ans << endl;
      }
    }
  }
  for(auto t:leftminus){
    int slope = t.first;
    for(auto u:t.second){
      if(upminus.find(slope)!=upminus.end()){
        auto it = lower_bound(upminus[slope].begin(),upminus[slope].end(),u);
        if(it==upminus[slope].begin())continue;
        it--;
        ans = min(ans, (u-(*it))*10);
        // cerr << "found leftminus" << " " << ans << endl;
      }
    }
  }
  for(auto t:rightplus){
    int slope = t.first;
    for(auto u:t.second){
      if(upplus.find(slope)!=upplus.end()){
        auto it = lower_bound(upplus[slope].begin(),upplus[slope].end(),u);
        if(it==upplus[slope].end())continue;
        ans = min(ans, ( (*it)-u )*10);
        // cerr << "found rightplus" << " " << ans << endl;
      }
    }
  }
  for(auto t:rightminus){
    int slope = t.first;
    for(auto u:t.second){
      if(downminus.find(slope)!=downminus.end()){
        auto it = lower_bound(downminus[slope].begin(),downminus[slope].end(),u);
        if(it==downminus[slope].end())continue;
        ans = min(ans, ( (*it)-u )*10);
        // cerr << "found rightminus" << " " << ans << endl;
      }
    }
  }
  cerr << ans << endl;
  for(int i=0;i<=200000;i++){
    sort(all(lef[i]));
    sort(all(rig[i]));
    sort(all(up[i]));
    sort(all(down[i]));
  }
  for(int i=0;i<=200000;i++){
    for(auto t:rig[i]){
      auto it = lower_bound(all(lef[i]),t);
      if(it==lef[i].end())continue;
      ans = min(ans, ((*it)-t)*5 );
    }
    for(auto t:up[i]){
      auto it = lower_bound(all(down[i]),t);
      if(it==down[i].end())continue;
      ans = min(ans, ((*it)-t)*5 );
    }
  }
  if(ans<1e8)
    cout << ans << endl;
  else{
    cout << "SAFE" << endl;
  }
return 0;
}
///....


Measured memory_bytes: 75684