#include<bits/stdc++.h>
#define int long long
using namespace std;
void a(vector<vector<pair<int,char>>> &A,char c1,char c2,int b,int &ans){
  for(vector<pair<int,char>> &v:A){
    sort(v.begin(),v.end());
    for(int i=0;i+1<v.size();i++)
      if(v[i].second==c1 && v[i+1].second==c2)
        ans=min(ans,(v[i+1].first-v[i].first)*b);
  }
}
signed main(){
  int N;
  cin>>N;
  vector<vector<pair<int,char>>> A(200001),B(200001),C(400001),D(400001),E(400001),F(400001);
  for(int i=0;i<N;i++){
    int X,Y;
    char c;
    cin>>X>>Y>>c;
    if(c=='U'){
      A[X].push_back({Y,'U'});
      C[X-Y+200000].push_back({X,'U'});
      D[X+Y].push_back({X,'U'});
    }else if(c=='D'){
      A[X].push_back({Y,'D'});
      E[X+Y].push_back({X,'D'});
      F[X-Y+200000].push_back({X,'D'});
    }else if(c=='R'){
      B[Y].push_back({X,'R'});
      D[X+Y].push_back({X,'R'});
      F[X-Y+200000].push_back({X,'R'});
    }else{
      B[Y].push_back({X,'L'});
      C[X-Y+200000].push_back({X,'L'});
      E[X+Y].push_back({X,'L'});
    }
  }
  int ans=1000000000000000000;
  a(A,'U','D',5,ans);
  a(B,'R','L',5,ans);
  a(C,'U','L',10,ans);
  a(D,'R','U',10,ans);
  a(E,'D','L',10,ans);
  a(F,'R','D',10,ans);
  if(ans==1000000000000000000)puts("SAFE");
  else cout<<ans<<endl;
}