#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <cmath>
#include <iomanip>
#include <stack>
#include <queue>
#include <numeric>
#include <map>
#include <unordered_map>
#include <set>
#include <fstream>
#include <chrono>
#include <random>
#include <bitset>
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) ((int)(x).size())
#define pb push_back
using ll = long long;
using namespace std;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
ll gcd(ll a, ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) {return a/gcd(a,b)*b;}

int main(){
  int N; cin >> N;
  vector<vector<int>> U(200010);
  vector<vector<int>> R(200010);
  vector<vector<int>> D(200010);
  vector<vector<int>> L(200010);

  vector<vector<int>> U_(400010);
  vector<vector<int>> R_(400010);
  vector<vector<int>> D_(400010);
  vector<vector<int>> L_(400010);

  vector<vector<int>> UU(400010);
  vector<vector<int>> RR(400010);
  vector<vector<int>> DD(400010);
  vector<vector<int>> LL(400010);

  ll ad = 200001;
  rep(i,N){
    int x, y;
    char v;
    cin >> x >> y >> v;
    if(v=='U') {
      U[x].pb(y);
      U_[x+y].pb(x);
      UU[x-y+ad].pb(y);
    }
    if(v=='R') {
      R[y].pb(x);
      R_[x+y].pb(x);
      RR[x-y+ad].pb(y);
    }
    if(v=='D') {
      D[x].pb(y);
      D_[x+y].pb(x);
      DD[x-y+ad].pb(y);
    }
    if(v=='L') {
      L[y].pb(x);
      L_[x+y].pb(x);
      LL[x-y+ad].pb(y);
    }
  }
  ll ans = 1e18;
  bool flag = false;
  rep(i,200005){
    sort(all(U[i]));
    sort(all(R[i]));
    sort(all(D[i]));
    sort(all(L[i]));
  }
  rep(i,400005){
    sort(all(U_[i]));
    sort(all(R_[i]));
    sort(all(L_[i]));
    sort(all(D_[i]));
    sort(all(UU[i]));
    sort(all(RR[i]));
    sort(all(LL[i]));
    sort(all(DD[i]));
  }
  // UD
  rep(i,200005){
    if(U[i].empty() || D[i].empty()) continue;
    for(int j=0;j<sz(U[i]);j++){
      ll now = U[i][j];
      ll p=-1;
      if(upper_bound(all(D[i]), now) != D[i].end()) p = *upper_bound(all(D[i]), now);
      if(p!=-1){
        chmin(ans, abs(now-p));
        flag = true;
      }
    }
  }

  // LR
  rep(i,200005){
    if(R[i].empty() || L[i].empty()) continue;
    for(int j=0;j<sz(R[i]);j++){
      ll now = R[i][j];
      ll p=-1;
      if(upper_bound(all(L[i]), now) != L[i].end()) p = *upper_bound(all(L[i]), now);
      if(p!=-1){
        chmin(ans, abs(now-p));
        flag = true;
      }
    }
  }

  //UR
  rep(i,400005){
    if(R_[i].empty() || U_[i].empty()) continue;
    for(int j=0;j<sz(R_[i]);j++){
      ll now = R_[i][j];
      ll p=-1;
      if(upper_bound(all(U_[i]), now) != U_[i].end()) p = *upper_bound(all(U_[i]), now);
      if(p!=-1){
        chmin(ans, abs(now-p)*2);
        flag = true;
      }
    }
  }

  //LD
  rep(i,400005){
    if(D_[i].empty() || L_[i].empty()) continue;
    for(int j=0;j<sz(D_[i]);j++){
      ll now = D_[i][j];
      ll p=-1;
      if(upper_bound(all(L_[i]), now) != L_[i].end()) p = *upper_bound(all(L_[i]), now);
      if(p!=-1){
        chmin(ans, abs(now-p)*2);
        flag = true;
      }
    }
  }

  //UL
  rep(i,400005){
    if(UU[i].empty() || LL[i].empty()) continue;
    for(int j=0;j<sz(UU[i]);j++){
      ll now = UU[i][j];
      ll p=-1;
      if(upper_bound(all(LL[i]), now) != LL[i].end()) p = *upper_bound(all(LL[i]), now);
      if(p!=-1){
        chmin(ans, abs(now-p)*2);
        flag = true;
      }
    }
  }

  //DR
  rep(i,400005){
    if(RR[i].empty() || DD[i].empty()) continue;
    for(int j=0;j<sz(RR[i]);j++){
      ll now = RR[i][j];
      ll p=-1;
      if(upper_bound(all(DD[i]), now) != DD[i].end()) p = *upper_bound(all(DD[i]), now);
      if(p!=-1){
        chmin(ans, abs(now-p)*2);
        flag = true;
      }
    }
  }

  if(!flag) cout << "SAFE" << endl;
  else cout << ans*5 << endl;
  return 0;
}


Measured memory_bytes: 115632