#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define llu unsigned long long int
#define sa(a,n,t) for(int ii=0;ii<n;ii++) scanf("%"#t"",&(a)[ii])
#define rep(i,n) for(int i=0;i<n;i++) 
#define reps(i,a,n) for(int i=a;i<n;i++) 
#define rrep(i,n) for(int i=n-1;i>=0;i--)
#define rreps(i,a,n) for(int i=n-1;i>=a;i--)
#define validl(i,a,b) ((i>=a)&&(i<=b))?1:0
#define validmat(x,y,a,b,c,d) (validl(x,a,b) && validl(y,a,b))?1:0
#define validmn(x,y,m,n) validmat(x,y,0,m,0,n)
#define valid(i,n) validl(i,0,n)
#define v(t) vector<t>
#define vv(t) vector<vector<t> >
#define mk make_pair
#define pb push_back
#define pa(a,n,t)  for(int i=0;i<n;i++) printf("%"#t"",a[i])
#define p2 pair<int,int>
#define p3 pair<p2,int>
#define fi first
#define se second
#define sd(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define pl(a) printf("%lld",a)
#define pd(a) printf("%d",a)
#define sf(a) scanf("%lf",&a)
#define pf(a) printf("%lf",a)
#define nline printf("\n")
#define ss(a) scanf("%s",a)
#define ps(a) printf("%s",a)
#define sc(a) scanf("%c",&a)
#define pc(a) printf("%c",&a)
#define mf(m,f) m.find(f)!=m.end()
#define pp3(m) cout<<m.fi.fi<<" "<<m.fi.se<<" "<<m.se<<" "
#define pp2(m) cout<<m.fi<<" "<<m.se<<" "
#define debug 0
/*v(int) adj[200001];
void dfs(int i,int p){
    rep(j,adj[i].size()){
        if(p!=adj[i][j])
            dfs(adj[i][j],i);
    }
}*/
//lli ans[16];
v(int) arr[200001][4];
int main()
{
 
  
  int n,k;
  cin>>n;
  rep(i,n){
      int a,b;
      char c;
      cin>>a>>b>>c;
      if(c=='U')
        arr[a][0].pb(b);
      else if(c=='D')  
        arr[a][1].pb(b);
    else if(c=='R')  
        arr[a][2].pb(b);
        else if(c=='L')  
        arr[a][3].pb(b);
  }
  int l[200001]={0};
  rep(i,200001) l[i]=INT_MAX;
  map<int,int> ss,st;
  int ans=INT_MAX;
  rrep(i,200001){
      for(auto j:arr[i][2]){
          auto it=ss.find(j+i);
          if(it!=ss.end()){
              ans=min(ans,(it->se-i)*10);
             // cout<<"here a"<<i<<","<<j<<(i+(it->se-i)) <<","<<(j-(it->se-i))<<"a ";
          }
          it=st.find(j-i);
          if(it!=st.end()){
              ans=min(ans,(it->se-i)*10);
            //  cout<<"here a"<<i<<","<<j<<(i+(it->se-i)) <<","<<(j+(it->se-i))<<"a ";
          }
          if(l[j]!=INT_MAX){
              ans=min(ans,(l[j]-i)*5);
            //  cout<<"here a"<<i<<","<<j<<l[j] <<","<<j<<"a ";
          }
      }
     for(auto j:arr[i][0]){
         ss[i+j]=i;
     }
     for(auto j:arr[i][1]){
         st[j-i]=i;
     }
     for(auto j:arr[i][3]) l[j]=min(l[j],i);
  }
  //cout<<ans<<endl;
  ss.clear();st.clear();
  rep(i,200001) l[i]=-1;
  rep(i,200001){
      for(auto j:arr[i][3]){
          auto it=ss.find(j-i);
          if(it!=ss.end()){
              ans=min(ans,(i-it->se)*10);
             // cout<<"here a"<<i<<","<<j<<(i-(i-it->se)) <<","<<(j-(i-it->se))<<"a ";
          }
          it=st.find(j+i);
          if(it!=st.end()){
              ans=min(ans,(i-it->se)*10);
             // cout<<"here a"<<i<<","<<j<<(i-(i-it->se)) <<","<<(j+(i-it->se))<<"a ";
          }
          if(l[j]!=-1){
              ans=min(ans,(i-l[j])*5);
             // cout<<"here a"<<i<<","<<j<<l[j] <<","<<j<<"a ";
          }
      }
     for(auto j:arr[i][0]){
         ss[j-i]=i;
     }
     for(auto j:arr[i][1]){
         st[j+i]=i;
     }
     for(auto j:arr[i][2]) l[j]=max(l[j],i);
  }
  //cout<<ans<<endl;
  rep(i,200001) l[i]=-1;
  rep(i,200001){
      int k=0;
      vector<int> su;
      for(auto j:arr[i][0]) su.pb(j);
      sort(su.begin(),su.end());
      for(auto j:arr[i][1]) {
          auto xx=upper_bound(su.begin(),su.end(),j);
          if(xx!=su.begin()) {
              --xx;
              ans=min(ans,(j-*xx)*5);
          }
      }
  }

  if(ans!=INT_MAX)
  cout<<ans<<endl;
  else cout<<"SAFE"<<endl;
  return 0;
}


Measured memory_bytes: 38524