#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

static const ll INF = (ll)(1e18);

vector<pair<int,int>> U, R, D, L;

vector<int> RY[200005];
vector<int> LY[200005];
vector<int> UX[200005];
vector<int> DX[200005];

int main(){
  int N;
  cin >> N;
  rep(i,N){
    int x, y;
    string s;
    cin >> x >> y >> s;
    if(s == "U"){
      U.emplace_back(x,y);
      UX[x].push_back(y);
    }
    if(s == "R"){
      R.emplace_back(x,y);
      RY[y].push_back(x);
    }
    if(s == "D"){
      D.emplace_back(x,y);
      DX[x].push_back(y);
    }
    if(s == "L"){
      L.emplace_back(x,y);
      LY[y].push_back(x);
    }
  }

  ll ret = INF;
  
  rep(i,200005){
    if(RY[i].size() > 0 && LY[i].size() > 0){
      sort(ALLOF(RY[i]));
      sort(ALLOF(LY[i]));

      int j = 0;
      int k = 0;
      while(j<RY[i].size()){
        while(k < LY[i].size() && RY[i][j] > LY[i][k]) k++;
        if(k == LY[i].size()) break;
        if(RY[i][j] > LY[i][k]) break;

        ret = min(ret, (LY[i][k]*10 - RY[i][j]*10)/2LL);
        j++;
      }
    }
    if(UX[i].size() > 0 && DX[i].size() > 0){
      sort(ALLOF(UX[i]));
      sort(ALLOF(DX[i]));

      int j = 0;
      int k = 0;
      while(j<UX[i].size()){
        while(k < DX[i].size() && UX[i][j] > DX[i][k]) k++;
        if(k == DX[i].size()) break;
        if(UX[i][j] > DX[i][k]) break;

        ret = min(ret, (DX[i][k]*10 - UX[i][j]*10)/2LL);
        j++;
      }
    }
  }
  
  {//R & D
    map<int,vector<pair<int,int>>> MR;
    map<int,vector<pair<int,int>>> MD;
    rep(i,R.size()){
      MR[R[i].second - R[i].first].push_back(R[i]);
    }
    rep(i,D.size()){
      MD[D[i].second - D[i].first].push_back(D[i]);
    }

    FOR(it,MR){
      int i = it->first;
      if(MR[i].size() == 0 || MD[i].size() == 0) continue;
      vector<pair<int,int>>& a = MR[i];
      vector<pair<int,int>>& b = MD[i];
      sort(ALLOF(a));
      sort(ALLOF(b));

      int j = 0;
      int k = 0;
      while(j<a.size()){
        while(k < b.size() && a[j].first > b[k].first) k++;
        if(k == b.size()) break;
        if(a[j].first > b[k].first) break;

        ret = min(ret, (ll)(b[k].first - a[j].first)*10LL);
        j++;
      }
    }
  }
  {//R & U
    map<int,vector<pair<int,int>>> MR;
    map<int,vector<pair<int,int>>> MU;
    rep(i,R.size()){
      MR[R[i].second + R[i].first].push_back(R[i]);
    }
    rep(i,U.size()){
      MU[U[i].second + U[i].first].push_back(U[i]);
    }

    FOR(it,MR){
      int i = it->first;
      if(MR[i].size() == 0 || MU[i].size() == 0) continue;
      vector<pair<int,int>>& a = MR[i];
      vector<pair<int,int>>& b = MU[i];
      sort(ALLOF(a));
      sort(ALLOF(b));

      int j = 0;
      int k = 0;
      while(j<a.size()){
        while(k < b.size() && a[j].first > b[k].first) k++;
        if(k == b.size()) break;
        if(a[j].first > b[k].first) break;

        ret = min(ret, (ll)(b[k].first - a[j].first)*10LL);
        j++;
      }
    }
  }
  {//L & D
    map<int,vector<pair<int,int>>> ML;
    map<int,vector<pair<int,int>>> MD;
    rep(i,L.size()){
      ML[L[i].second + L[i].first].push_back(L[i]);
    }
    rep(i,D.size()){
      MD[D[i].second + D[i].first].push_back(D[i]);
    }

    FOR(it,ML){
      int i = it->first;
      if(ML[i].size() == 0 || MD[i].size() == 0) continue;
      vector<pair<int,int>>& a = MD[i];
      vector<pair<int,int>>& b = ML[i];
      sort(ALLOF(a));
      sort(ALLOF(b));

      int j = 0;
      int k = 0;
      while(j<a.size()){
        while(k < b.size() && a[j].first > b[k].first) k++;
        if(k == b.size()) break;
        if(a[j].first > b[k].first) break;

        ret = min(ret, (ll)(b[k].first - a[j].first)*10LL);
        j++;
      }
    }
  }
  {//L & U
    map<int,vector<pair<int,int>>> ML;
    map<int,vector<pair<int,int>>> MU;
    rep(i,L.size()){
      ML[L[i].second - L[i].first].push_back(L[i]);
    }
    rep(i,U.size()){
      MU[U[i].second - U[i].first].push_back(U[i]);
    }

    FOR(it,ML){
      int i = it->first;
      if(ML[i].size() == 0 || MU[i].size() == 0) continue;
      vector<pair<int,int>>& a = MU[i];
      vector<pair<int,int>>& b = ML[i];
      sort(ALLOF(a));
      sort(ALLOF(b));

      int j = 0;
      int k = 0;
      while(j<a.size()){
        while(k < b.size() && a[j].first > b[k].first) k++;
        if(k == b.size()) break;
        if(a[j].first > b[k].first) break;

        ret = min(ret, (ll)(b[k].first - a[j].first)*10LL);
        j++;
      }
    }
  }


  if(ret == INF) cout << "SAFE" << endl;
  else cout << ret << endl;
  
  return 0;
}



Measured memory_bytes: 53376