#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
using ll = long long;
typedef pair<int, int> P;
ll Mod = 1000000007;
ll INF = Mod * 100;

int C_plus(P p) {
  return 200000 - p.S + p.F;
}
int C_minus(P p) {
  return p.S + p.F;
} 

int Calc_plus(P p) {
  if (C_plus(p) < 200000) {
    return p.F;
  } else {
    return 200000 - C_plus(p) + p.S;
  }
} 
int Calc_minus(P p) {
  if (C_minus(p) < 200000) {
    return p.F;
  } else {
    return 200000 - C_minus(p) + p.F;
  }
} 

ll Serach(vector<int> A,vector<int> B) {
  ll res = INF;
  if ((int)A.size() == 0 || (int)B.size() == 0) {
    return INF;
  }
  for (int i = 0; i < (int)A.size(); i++) {
    auto itr = lower_bound(B.begin(),B.end(),A[i]);
    if (itr == B.end()) {
      continue;
    }
    res = min(res , (ll)*itr - A[i]);
  }
  return res;
}

int main() {
  int N;
  cin >> N;
  vector<P> U;
  vector<P> R;
  vector<P> D;
  vector<P> L;
  vector<int> U_Y[200001];
  vector<int> D_Y[200001];
  vector<int> L_X[200001];
  vector<int> R_X[200001];
  vector<int> U_plus[400001];
  vector<int> R_plus[400001];
  vector<int> D_plus[400001];
  vector<int> L_plus[400001];
  vector<int> U_minus[400001];
  vector<int> R_minus[400001];
  vector<int> D_minus[400001];
  vector<int> L_minus[400001];
  ll ans = INF;
  for (int i = 0; i < N; i++) {
    int X,Y;
    char u;
    cin >> X >> Y >> u;
    if (u == 'U') {
      U.push_back(make_pair(X,Y));
    }
    if (u == 'R') {
      R.push_back(make_pair(X,Y));
    }
    if (u == 'D') {
      D.push_back(make_pair(X,Y));
    }
    if (u == 'L') {
      L.push_back(make_pair(X,Y));
    }
  }
  for (int i = 0; i < (int)U.size(); i++) {
    U_Y[U[i].F].push_back(U[i].S);
    U_plus[C_plus(U[i])].push_back(Calc_plus(U[i]));
    U_minus[C_minus(U[i])].push_back(Calc_minus(U[i]));
  }
  for (int i = 0; i < (int)R.size(); i++) {
    R_X[R[i].S].push_back(R[i].F);
    R_plus[C_plus(R[i])].push_back(Calc_plus(R[i]));
    R_minus[C_minus(R[i])].push_back(Calc_minus(R[i]));
  }
  for (int i = 0; i < (int)D.size(); i++) {
    D_Y[D[i].F].push_back(D[i].S);
    D_plus[C_plus(D[i])].push_back(Calc_plus(D[i]));
    D_minus[C_minus(D[i])].push_back(Calc_minus(D[i]));
  }
  for (int i = 0; i < (int)L.size(); i++) {
    L_X[L[i].S].push_back(L[i].F);
    L_plus[C_plus(L[i])].push_back(Calc_plus(L[i]));
    L_minus[C_minus(L[i])].push_back(Calc_minus(L[i]));
  }
  for (int i = 0; i <= 200000; i++) {
    sort(U_Y[i].begin(),U_Y[i].end());
    sort(D_Y[i].begin(),D_Y[i].end());
    sort(L_X[i].begin(),L_X[i].end());
    sort(R_X[i].begin(),R_X[i].end());
  }
  for (int i = 0; i <= 400000; i++) {
    sort(U_minus[i].begin(),U_minus[i].end());
    sort(D_minus[i].begin(),D_minus[i].end());
    sort(L_minus[i].begin(),L_minus[i].end());
    sort(R_minus[i].begin(),R_minus[i].end());
    sort(U_plus[i].begin(),U_plus[i].end());
    sort(D_plus[i].begin(),D_plus[i].end());
    sort(L_plus[i].begin(),L_plus[i].end());
    sort(R_plus[i].begin(),R_plus[i].end());
  }
  for (int i = 0; i <= 200000; i++) {
    ans = min(ans,Serach(U_Y[i],D_Y[i]) * 5LL);
    ans = min(ans,Serach(R_X[i],L_X[i]) * 5LL);
  }
  for (int i = 0; i <= 400000; i++) {
    
    ans = min(ans,Serach(U_plus[i],L_plus[i]) * 10LL);
    ans = min(ans,Serach(R_plus[i],D_plus[i]) * 10LL);
    ans = min(ans,Serach(D_minus[i],L_minus[i]) * 10LL);
    ans = min(ans,Serach(R_minus[i],U_minus[i]) * 10LL);  
  }
  if (ans == INF) {
    cout << "SAFE" << endl;
  } else {
    cout << ans << endl;
  }
  // 正面衝突 U,D L,R
  // 横に衝突 U,L U,R D,L D,R
  return 0;
}

Measured memory_bytes: 117356