#pragma GCC optimize("Ofast")
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <iomanip> // setprecision
#include <complex> // complex
#include <math.h> 
#include <functional>
using namespace std;
using ll = long long;
using P = pair<int,int>;
constexpr ll INF = 1e18;
constexpr int inf = 1e9;
constexpr ll mod = 1000000007;
constexpr ll mod2 = 998244353;
const int dx[8] = {1, 0, -1, 0,1,1,-1,-1};
const int dy[8] = {0, 1, 0, -1,1,-1,1,-1};
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
// --------------------------------------------------------------------------

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  const int MAX = 200100;
  int N;
  cin >> N;
  vector<int> X(N),Y(N);
  vector<char> D(N);
  for(int i=0; i<N; i++){
    cin >> X[i] >> Y[i] >> D[i];
  }
  int ans = inf;
  // 逆向き
  { // U-D
    vector<vector<pair<int,char>>> x(MAX);
    for(int i=0; i<N; i++){
      if(D[i] != 'U' && D[i] != 'D') continue;
      x[X[i]].emplace_back(Y[i],D[i]);
    }
    for(int i=0; i<MAX; i++){
      sort(x[i].begin(),x[i].end());
      for(int j=1; j<x[i].size(); j++){
        if(x[i][j-1].second == 'U' && x[i][j].second == 'D'){
          chmin(ans,(x[i][j].first-x[i][j-1].first)*5);
        }
      }
    }
  }
  { // L-R
    vector<vector<pair<int,char>>> y(MAX);
    for(int i=0; i<N; i++){
      if(D[i] != 'L' && D[i] != 'R') continue;
      y[Y[i]].emplace_back(X[i],D[i]);
    }
    for(int i=0; i<MAX; i++){
      sort(y[i].begin(),y[i].end());
      for(int j=1; j<y[i].size(); j++){
        if(y[i][j-1].second == 'R' && y[i][j].second == 'L'){
          chmin(ans,(y[i][j].first-y[i][j-1].first)*5);
        }
      }
    }
  }
  using pcc = pair<char,char>;
  vector<pcc> p = {pcc('U','R'),pcc('L','U'),pcc('D','L'),pcc('R','D')};
  for(int _=0; _<4; _++){
    vector<vector<pair<int,char>>> B(MAX+MAX);
    for(int i=0; i<N; i++){
      if(D[i] != p[_].first && D[i] != p[_].second) continue;
      B[X[i]+Y[i]].emplace_back(Y[i],D[i]);
    }
    for(int i=0; i<MAX+MAX; i++){
      sort(B[i].begin(),B[i].end());
      for(int j=1; j<B[i].size(); j++){
        if(B[i][j-1].second == p[_].first && B[i][j].second == p[_].second){
          chmin(ans,(B[i][j].first - B[i][j-1].first)*10);
        }
      }
    }
    // 時計周りに90度回転
    for(int i=0; i<N; i++){
      swap(X[i],Y[i]);
      Y[i] = MAX-Y[i];
    }
  }
  if(ans == inf){
    cout << "SAFE\n";
  }else{
    cout << ans << "\n";
  }
  return 0;
}


Measured memory_bytes: 22916