#include <bits/stdc++.h>
#define rep(i,n) for (long long i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
using T = tuple<ll,ll,ll>;
const ll MOD = 1000000007;
const ll INF = 10000000000000;
#define all(v) v.begin(), v.end()

int main(){
  ll N,m=INF;
  cin >> N;
  vector<ll> X(N),Y(N);
  vector<char> U(N);
  vector<vector<vector<ll>>> A(200001,vector<vector<ll>>(2)),B(200001,vector<vector<ll>>(2)),C(400001,vector<vector<ll>>(4)),D(400001,vector<vector<ll>>(4));
  rep(i,N){
    cin >> X.at(i) >> Y.at(i) >> U.at(i);
    if(U.at(i)=='U'){
      A.at(X.at(i)).at(0).push_back(Y.at(i));
      C.at(X.at(i)+Y.at(i)).at(0).push_back(Y.at(i));
      D.at(X.at(i)-Y.at(i)+200000).at(0).push_back(Y.at(i));
    }
    if(U.at(i)=='R'){
      B.at(Y.at(i)).at(0).push_back(X.at(i));
      C.at(X.at(i)+Y.at(i)).at(1).push_back(Y.at(i));
      D.at(X.at(i)-Y.at(i)+200000).at(1).push_back(Y.at(i));
    }
    if(U.at(i)=='D'){
      A.at(X.at(i)).at(1).push_back(Y.at(i));
      C.at(X.at(i)+Y.at(i)).at(2).push_back(Y.at(i));
      D.at(X.at(i)-Y.at(i)+200000).at(2).push_back(Y.at(i));
    }
    if(U.at(i)=='L'){
      B.at(Y.at(i)).at(1).push_back(X.at(i));
      C.at(X.at(i)+Y.at(i)).at(3).push_back(Y.at(i));
      D.at(X.at(i)-Y.at(i)+200000).at(3).push_back(Y.at(i));
    }
  }
  rep(i, 200001){
    sort(all(A.at(i).at(0)));
    sort(all(A.at(i).at(1)));
    if(A.at(i).at(0).size()>0&&A.at(i).at(1).size()>0){
      ll t=0;
      rep(j,A.at(i).at(0).size()){
        while(t<A.at(i).at(1).size()&&A.at(i).at(0).at(j)>A.at(i).at(1).at(t)){
          t++;
        }
        if(t==A.at(i).at(1).size()){
          break;
        }
        m=min(m,(A.at(i).at(1).at(t)-A.at(i).at(0).at(j))*5);
      }
    }
    sort(all(B.at(i).at(0)));
    sort(all(B.at(i).at(1)));
    if(B.at(i).at(0).size()>0&&B.at(i).at(1).size()>0){
      ll t=0;
      rep(j,B.at(i).at(0).size()){
        while(t<B.at(i).at(1).size()&&B.at(i).at(0).at(j)>B.at(i).at(1).at(t)){
          t++;
        }
        if(t==B.at(i).at(1).size()){
          break;
        }
        m=min(m,(B.at(i).at(1).at(t)-B.at(i).at(0).at(j))*5);
      }
    }
  }
  rep(i, 400001){
    sort(all(C.at(i).at(0)));
    sort(all(C.at(i).at(1)));
    sort(all(C.at(i).at(2)));
    sort(all(C.at(i).at(3)));
    if(C.at(i).at(0).size()>0&&C.at(i).at(1).size()>0){
      ll t=0;
      rep(j,C.at(i).at(0).size()){
        while(t<C.at(i).at(1).size()&&C.at(i).at(0).at(j)>C.at(i).at(1).at(t)){
          t++;
        }
        if(t==C.at(i).at(1).size()){
          break;
        }
        m=min(m,(C.at(i).at(1).at(t)-C.at(i).at(0).at(j))*10);
      }
    }
    if(C.at(i).at(3).size()>0&&C.at(i).at(2).size()>0){
      ll t=0;
      rep(j,C.at(i).at(3).size()){
        while(t<C.at(i).at(2).size()&&C.at(i).at(3).at(j)>C.at(i).at(2).at(t)){
          t++;
        }
        if(t==C.at(i).at(2).size()){
          break;
        }
        m=min(m,(C.at(i).at(2).at(t)-C.at(i).at(3).at(j))*10);
      }
    }
    sort(all(D.at(i).at(0)));
    sort(all(D.at(i).at(1)));
    sort(all(D.at(i).at(2)));
    sort(all(D.at(i).at(3)));
    if(D.at(i).at(0).size()>0&&D.at(i).at(3).size()>0){
      ll t=0;
      rep(j,D.at(i).at(0).size()){
        while(t<D.at(i).at(3).size()&&D.at(i).at(0).at(j)>D.at(i).at(3).at(t)){
          t++;
        }
        if(t==D.at(i).at(3).size()){
          break;
        }
        m=min(m,(D.at(i).at(3).at(t)-D.at(i).at(0).at(j))*10);
      }
    }
    if(D.at(i).at(1).size()>0&&D.at(i).at(2).size()>0){
      ll t=0;
      rep(j,D.at(i).at(1).size()){
        while(t<D.at(i).at(2).size()&&D.at(i).at(1).at(j)>D.at(i).at(2).at(t)){
          t++;
        }
        if(t==D.at(i).at(2).size()){
          break;
        }
        m=min(m,(D.at(i).at(2).at(t)-D.at(i).at(1).at(j))*10);
      }
    }
  }
  if(m==INF){
    cout << "SAFE" << endl;
  }else{
    cout << m << endl;
  }
}

Measured memory_bytes: 165820