#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef vector<vector<vector<ll>>> vvvll;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef vector<vector<vector<bool>>> vvvb;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;
typedef vector<double> vd;
typedef vector<vd> vdd;

#define FOR(i,x,y) for(ll i=(ll)x; i<(ll)y; ++i)
#define REP(i,y) FOR(i, 0, y)
#define RFOR(i,x,y) for(ll i=(ll)x; i>=(ll)y; --i)
#define RREP(i,x) RFOR(i, x, 0)
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define debug_print(x...) cerr << "line " << __LINE__ << " : "; debug_print_in(x);

template <typename First>
void debug_print_in(First first){
  cerr << first << endl;
  return;
}

template <typename First, typename... Rest>
void debug_print_in(First first, Rest... rest){
  cerr << first << " ";
  debug_print_in(rest...);
  return;
}

void IN(void){
  return;
}

template <typename First, typename... Rest>
void IN(First& first, Rest&... rest){
  cin >> first;
  IN(rest...);
  return;
}

template <typename First>
void OUT(First first){
  cout << first << endl;
  return;
}

template <typename First, typename... Rest>
void OUT(First first, Rest... rest){
  cout << first << " ";
  OUT(rest...);
  return;
}

template<class t, class u> void chmax(t&a,u b){if(a<b)a=b;};
template<class t, class u> void chmin(t&a,u b){if(a>b)a=b;};
int popcount(int t){return __builtin_popcount(t);} //GCC
int popcount(ll t){return __builtin_popcountll(t);} //GCC

template <typename T>
void vec_print(vector<T> VEC){
  REP(i, VEC.size()){
    cerr << VEC[i] << " ";
  }
  cerr << endl;
};

template <typename T>
void mat_print(vector<vector<T> > MAT){
  REP(i,MAT.size()){
    REP(j,MAT[i].size()){
      cerr << MAT[i][j] << " ";
    }
    cerr << endl;
  }
};

constexpr int INF = (1<<30);
constexpr ll INFLL = 1LL<<62;
constexpr long double EPS = 1e-12;
constexpr ll MOD = (ll)((1E+9)+7);

int main(){
  cin.tie(0); // cut the cin and cout (default, std::flush is performed after std::cin)
  ios::sync_with_stdio(false); // cut the iostream and stdio (DON'T endl; BUT "\n";)

  ll N;
  IN(N);
  vll X(N), Y(N);
  vector<vector<pair<ll, char>>> X_same(200007);
  vector<vector<pair<ll, char>>> Y_same(200007);
  vector<vector<pair<ll, char>>> XpY_same(400007);
  vector<vector<pair<ll, char>>> XmY_same(400007);
  ll XmY_shift = 200004;
  vector<char> U(N);
  REP(i,N){
    IN(X[i], Y[i], U[i]);
    X_same[X[i]].push_back(make_pair(Y[i], U[i]));
    Y_same[Y[i]].push_back(make_pair(X[i], U[i]));
    XpY_same[X[i]+Y[i]].push_back(make_pair(X[i]-Y[i], U[i]));
    XmY_same[X[i]-Y[i]+XmY_shift].push_back(make_pair(X[i]+Y[i], U[i]));
  }

  ll T_ans = INFLL;

  REP(i,X_same.size()){
    sort(ALL(X_same[i]));
    ll Up_max = -1;
    REP(j,X_same[i].size()){
      if(X_same[i][j].second=='U'){
        Up_max = X_same[i][j].first;
      }else if(X_same[i][j].second=='D'){
        if(Up_max>=0){
          chmin(T_ans, (X_same[i][j].first-Up_max)*5);
        }
      }
    }
  }
  
  REP(i,Y_same.size()){
    sort(ALL(Y_same[i]));
    ll Right_max = -1;
    REP(j,Y_same[i].size()){
      if(Y_same[i][j].second=='R'){
        Right_max = Y_same[i][j].first;
      }else if(Y_same[i][j].second=='L'){
        if(Right_max>=0){
          chmin(T_ans, (Y_same[i][j].first-Right_max)*5);
        }
      }
    }
  }

  REP(i,XpY_same.size()){
    sort(ALL(XpY_same[i]));
    ll Right_max = INFLL;
    ll Down_min = INFLL;
    REP(j,XpY_same[i].size()){
      if(XpY_same[i][j].second=='R'){
        Right_max = XpY_same[i][j].first;
      }else if(XpY_same[i][j].second=='D'){
        Down_min = XpY_same[i][j].first;
      }else if(XpY_same[i][j].second=='U'){
        if(Right_max<INFLL){
          chmin(T_ans, abs(XpY_same[i][j].first-Right_max)*5);
        }
      }else{
        if(Down_min<INFLL){
          chmin(T_ans, abs(XpY_same[i][j].first-Down_min)*5);
        }
      }
    }
  }

  REP(i,XmY_same.size()){
    sort(ALL(XmY_same[i]));
    ll Right_max = INFLL;
    ll Up_max = INFLL;
    REP(j,XmY_same[i].size()){
      if(XmY_same[i][j].second=='R'){
        Right_max = XmY_same[i][j].first;
      }else if(XmY_same[i][j].second=='U'){
        Up_max = XmY_same[i][j].first;
      }else if(XmY_same[i][j].second=='D'){
        if(Right_max<INFLL){
          chmin(T_ans, abs(XmY_same[i][j].first-Right_max)*5);
        }
      }else{
        if(Up_max<INFLL){
          chmin(T_ans, abs(XmY_same[i][j].first-Up_max)*5);
        }
      }
    }
  }

  if(T_ans==INFLL){
    OUT("SAFE");
  }else{
    OUT(T_ans);
  }



  return 0;
}


Measured memory_bytes: 56460