#include<bits/stdc++.h>
using namespace std;
int main() {
  int N; cin >> N;
  vector<int> X(N), Y(N);
  vector<char> D(N);
  map<char, char> nd;
  nd['U'] = 'R';
  nd['R'] = 'D';
  nd['D'] = 'L';
  nd['L'] = 'U';
  constexpr int MAX = 200000;
  for (int i = 0; i < N; i++) {
    cin >> X[i] >> Y[i] >> D[i];
  }
  auto f = [&]() {
    for (int i = 0; i < N; i++) {
      int t = X[i];
      X[i] = Y[i];
      Y[i] = MAX-t;
      D[i] = nd[D[i]];
    }
  };
  int res = 1e9;
  for (int cnt = 0; cnt < 4; cnt++, f()) {
    vector<vector<pair<int, int>>> y(MAX+1);
    for (int i = 0; i < N; i++) {
      if (D[i] == 'U') y[X[i]].emplace_back(Y[i], -1);
      else if (D[i] == 'D') y[X[i]].emplace_back(Y[i], 1);
    }
    for (int i = 0; i <= MAX; i++) {
      sort(y[i].begin(), y[i].end());
      for (int j = 1; j < y[i].size(); j++) {
        auto [a, b] = y[i][j-1];
        auto [c, d] = y[i][j];
        if (b != -1 || d != 1) continue;
        res = min(res, (c - a) * 5);
      }
    }
    y.clear(); y.resize(MAX*2+1);
    for (int i = 0; i < N; i++) {
      if (D[i] == 'U') y[X[i] + Y[i]].emplace_back(Y[i], -1);
      else if (D[i] == 'R') y[X[i] + Y[i]].emplace_back(Y[i], 1);
    }
    for (int i = 0; i <= MAX*2; i++) {
      sort(y[i].begin(), y[i].end());
      for (int j = 1; j < y[i].size(); j++) {
        auto [a, b] = y[i][j-1];
        auto [c, d] = y[i][j];
        if (b != -1 || d != 1) continue;
        res = min(res, (c - a) * 10);
      }
    }
  }
  if (res == 1e9) cout << "SAFE" << endl;
  else cout << res << endl;
}
