#include <bits/stdc++.h>
using namespace std;
int main(){
  int N;
  cin >> N;
  vector<int> X(N), Y(N);
  vector<char> U(N);
  for (int i = 0; i < N; i++){
    cin >> X[i] >> Y[i] >> U[i];
  }
  int ans = 1000000;
  //UD
  vector<set<int>> A1(200001);
  vector<set<int>> A2(200001);
  for (int i = 0; i < N; i++){
    if (U[i] == 'U'){
      A1[X[i]].insert(Y[i]);
    }
    if (U[i] == 'D'){
      A2[X[i]].insert(Y[i]);
    }
  }
  for (int i = 0; i <= 200000; i++){
    for (int j : A1[i]){
      auto itr = A2[i].lower_bound(j);
      if (itr != A2[i].end()){
        ans = min(ans, *itr - j);
      }
    }
  }
  //LR
  vector<set<int>> B1(200001);
  vector<set<int>> B2(200001);
  for (int i = 0; i < N; i++){
    if (U[i] == 'R'){
      B1[Y[i]].insert(X[i]);
    }
    if (U[i] == 'L'){
      B2[Y[i]].insert(X[i]);
    }
  }
  for (int i = 0; i <= 200000; i++){
    for (int j : B1[i]){
      auto itr = B2[i].lower_bound(j);
      if (itr != B2[i].end()){
        ans = min(ans, *itr - j);
      }
    }
  }
  //RD
  map<int, vector<pair<int, char>>> mp1;
  for (int i = 0; i < N; i++){
    if (U[i] == 'R'){
      mp1[Y[i] - X[i]].push_back(make_pair(X[i], 'R'));
    }
    if (U[i] == 'D'){
      mp1[Y[i] - X[i]].push_back(make_pair(X[i], 'D'));
    }
  }
  for (auto P : mp1){
    vector<pair<int, char>> A = P.second;
    int sz = A.size();
    sort(A.begin(), A.end());
    for (int i = 0; i < sz - 1; i++){
      if (A[i].second == 'R' && A[i + 1].second == 'D'){
        ans = min(ans, (A[i + 1].first - A[i].first) * 2);
      }
    }
  }
  //LU
  map<int, vector<pair<int, char>>> mp2;
  for (int i = 0; i < N; i++){
    if (U[i] == 'U'){
      mp2[Y[i] - X[i]].push_back(make_pair(X[i], 'U'));
    }
    if (U[i] == 'L'){
      mp2[Y[i] - X[i]].push_back(make_pair(X[i], 'L'));
    }
  }
  for (auto P : mp2){
    vector<pair<int, char>> A = P.second;
    int sz = A.size();
    sort(A.begin(), A.end());
    for (int i = 0; i < sz - 1; i++){
      if (A[i].second == 'U' && A[i + 1].second == 'L'){
        ans = min(ans, (A[i + 1].first - A[i].first) * 2);
      }
    }
  }
  //RU
  map<int, vector<pair<int, char>>> mp3;
  for (int i = 0; i < N; i++){
    if (U[i] == 'R'){
      mp3[X[i] + Y[i]].push_back(make_pair(X[i], 'R'));
    }
    if (U[i] == 'U'){
      mp3[X[i] + Y[i]].push_back(make_pair(X[i], 'U'));
    }
  }
  for (auto P : mp3){
    vector<pair<int, char>> A = P.second;
    int sz = A.size();
    sort(A.begin(), A.end());
    for (int i = 0; i < sz - 1; i++){
      if (A[i].second == 'R' && A[i + 1].second == 'U'){
        ans = min(ans, (A[i + 1].first - A[i].first) * 2);
      }
    }
  }
  //LD
  map<int, vector<pair<int, char>>> mp4;
  for (int i = 0; i < N; i++){
    if (U[i] == 'L'){
      mp4[X[i] + Y[i]].push_back(make_pair(X[i], 'L'));
    }
    if (U[i] == 'D'){
      mp4[X[i] + Y[i]].push_back(make_pair(X[i], 'D'));
    }
  }
  for (auto P : mp4){
    vector<pair<int, char>> A = P.second;
    int sz = A.size();
    sort(A.begin(), A.end());
    for (int i = 0; i < sz - 1; i++){
      if (A[i].second == 'D' && A[i + 1].second == 'L'){
        ans = min(ans, (A[i + 1].first - A[i].first) * 2);
      }
    }
  }
  if (ans == 1000000){
    cout << "SAFE" << endl;
  } else {
    cout << ans * 5 << endl;
  }
}

Measured memory_bytes: 95104