#include <bits/stdc++.h>
 
const double pi = 3.141592653589793238462643383279;
using namespace std;
//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
 
 
//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SQ(a) ((a)*(a))
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
 
 
//repetition
//------------------------------------------
#define FOR(i,s,n) for(int i=s;i<(int)n;++i)
#define REP(i,n) FOR(i,0,n)
#define MOD 1000000007
 
 
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
 
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
 
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)

//ここから編集 

ll X[200010], Y[200010];
char U[200010];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  
  int N; cin >> N;
  REP(i,N){
    cin >> X[i] >> Y[i] >> U[i];
  }

  ll ans = (1LL<<60);
  {
    /* mp: 右, mp2: 左*/
    map<ll,vector<ll>> mp, mp2;
    REP(i,N){
      if(U[i] == 'R'){
        mp[Y[i]].push_back(X[i]);
      }else if(U[i] == 'L'){
        mp2[Y[i]].push_back(X[i]);
      }
    }

    for(auto e: mp){
      if(!mp2.count(e.first)) continue;

      vector<ll> v;
      for(auto e2: mp2[e.first]){
        v.push_back(e2);
      }
      sort(all(v));
      v.push_back(1LL<<60);
      for(auto ee: e.second){
        auto itr = lower_bound(all(v), ee);
        
        if(*(itr) == (1LL<<60)) continue;

        ans = min(ans, abs(*(itr)-ee)*5LL);
      }
    }
  }
  
  {
    /* mp: 上方向, mp2: 下方向*/
    map<ll,vector<ll>> mp, mp2;
    REP(i,N){
      if(U[i] == 'U'){
        mp[X[i]].push_back(Y[i]);
      }else if(U[i] == 'D'){
        mp2[X[i]].push_back(Y[i]);
      }
    }

    for(auto e: mp){
      if(!mp2.count(e.first)) continue;
      vector<ll> v;
      for(auto e2: mp2[e.first]){
        v.push_back(e2);
      }
      sort(all(v));
      v.push_back(1LL<<60);
      for(auto ee: e.second){
        auto itr = lower_bound(all(v), ee);
        if(*(itr) ==1LL<<60) continue;

        ans = min(ans, abs(*(itr)-ee)*5LL);
      }
    }
  }  
  {
    /* D, L (X, Y) -> (X+Y, X-Y)でX軸が一致する*/
    map<ll,vector<ll>> mp, mp2;
    REP(i,N){
      if(U[i] == 'D'){
        mp[X[i]+Y[i]].push_back(X[i]-Y[i]);
      }else if(U[i] == 'L'){
        mp2[X[i]+Y[i]].push_back(X[i]-Y[i]);
      }
    }

    for(auto e: mp){
      if(!mp2.count(e.first)) continue;
      vector<ll> v;
      for(auto e2: mp2[e.first]){
        v.push_back(e2);
      }
      sort(all(v));
      v.push_back(1LL<<60);
      
      for(auto ee: e.second){
        auto itr = lower_bound(all(v), ee);
        if(*(itr) ==1LL<<60) continue;

        ans = min(ans, abs(*(itr)-ee)*5LL);
      }
    }
  }

  {
    /* R, D */
    map<ll,vector<ll>> mp, mp2;
    REP(i,N){
      if(U[i] == 'R'){
        mp[X[i]-Y[i]].push_back(X[i]+Y[i]);
      }else if(U[i] == 'D'){
        mp2[X[i]-Y[i]].push_back(X[i]+Y[i]);
      }
    }

    for(auto e: mp){
      if(!mp2.count(e.first)) continue;
      vector<ll> v;
      for(auto e2: mp2[e.first]){
        v.push_back(e2);
      }
      sort(all(v));
      v.push_back(1LL<<60);
      
      for(auto ee: e.second){
        auto itr = lower_bound(all(v), ee);
        if(*(itr) ==1LL<<60) continue;

        ans = min(ans, abs(*(itr)-ee)*5LL);
      }
    }
  }
  {
    /* R, U */
    map<ll,vector<ll>> mp, mp2;
    REP(i,N){
      if(U[i] == 'R'){
        mp[X[i]+Y[i]].push_back(X[i]-Y[i]);
      }else if(U[i] == 'U'){
        mp2[X[i]+Y[i]].push_back(X[i]-Y[i]);
      }
    }

    for(auto e: mp){
      if(!mp2.count(e.first)) continue;
      vector<ll> v;
      for(auto e2: mp2[e.first]){
        v.push_back(e2);
      }
      sort(all(v));
      v.push_back(1LL<<60);
      for(auto ee: e.second){
        auto itr = lower_bound(all(v), ee);
        if(*(itr) ==1LL<<60) continue;

        ans = min(ans, abs(*(itr)-ee)*5LL);
      }
    }
  }
  {
    /* L, U */
    map<ll,vector<ll>> mp, mp2;
    REP(i,N){
      if(U[i] == 'U'){
        mp[X[i]-Y[i]].push_back(X[i]+Y[i]);
      }else if(U[i] == 'L'){
        mp2[X[i]-Y[i]].push_back(X[i]+Y[i]);
      }
    }

    for(auto e: mp){
      if(!mp2.count(e.first)) continue;
      vector<ll> v;
      for(auto e2: mp2[e.first]){
        v.push_back(e2);
      }
      sort(all(v));
      v.push_back(1LL<<60);
      for(auto ee: e.second){
        auto itr = lower_bound(all(v), ee);
        if(*(itr) ==1LL<<60) continue;

        ans = min(ans, abs(*(itr)-ee)*5LL);
      }
    }
  }
  if(ans == (1LL<<60)) cout << "SAFE" << endl;
  else cout << ans << endl;
  return 0;
}


Measured memory_bytes: 28588