#include<bits/stdc++.h>

const int INF = 1e9 + 9;

using namespace std;

int main(){
  int N;
  cin >> N;
  
  vector<int> X(N), Y(N);
  vector<char> d(N);
  map<pair<char, int>, vector<int>> mp, mm, mv, mh;
  for(int i = 0; i < N; i++){
    cin >> X[i] >> Y[i] >> d[i];
    
    mp[{d[i], X[i] + Y[i]}].push_back(X[i]);
    mm[{d[i], X[i] - Y[i]}].push_back(X[i]);
    mv[{d[i], X[i]}].push_back(Y[i]);
    mh[{d[i], Y[i]}].push_back(X[i]);
  }
  
  for(auto& [k, v] : mp){
    sort(v.begin(), v.end());
  }
  
  for(auto& [k, v] : mm){
    sort(v.begin(), v.end());
  }
  
  for(auto& [k, v] : mv){
    sort(v.begin(), v.end());
  }
  
  for(auto& [k, v] : mh){
    sort(v.begin(), v.end());
  }
  
  int time = INF;
  for(int i = 0; i < N; i++){
    if(d[i] == 'L'){
      pair<char, int> key = {'D', X[i] + Y[i]};
      if(mp[key].size()){
        int ok = -1, ng = mp[key].size();
        while(abs(ng - ok) > 1){
          int mid = (ok + ng) >> 1;
          (mp[key][mid] <= X[i] ? ok : ng) = mid;
        }
        
        if(0 <= ok && ok < mp[key].size()) time = min(2 * abs(mp[key][ok] - X[i]), time);
      }
      
      key = {'U', X[i] - Y[i]};
      if(mm[key].size()){
        int ok = -1, ng = mm[key].size();
        while(abs(ng - ok) > 1){
          int mid = (ok + ng) >> 1;
          (mm[key][mid] <= X[i] ? ok : ng) = mid;
        }
        
        if(0 <= ok && ok < mm[key].size()) time = min(2 * abs(mm[key][ok] - X[i]), time);
      }
      
      key = {'R', Y[i]};
      if(mh[key].size()){
        int ok = -1, ng = mh[key].size();
        while(abs(ng - ok) > 1){
          int mid = (ok + ng) >> 1;
          (mh[key][mid] <= X[i] ? ok : ng) = mid;
        }
        
        if(0 <= ok && ok < mh[key].size()) time = min(abs(mh[key][ok] - X[i]), time);
      }
    }else if(d[i] == 'R'){
      pair<char, int> key = {'U', X[i] + Y[i]};
      if(mp[key].size()){
        int ng = -1, ok = mp[key].size();
        while(abs(ng - ok) > 1){
          int mid = (ok + ng) >> 1;
          (mp[key][mid] >= X[i] ? ok : ng) = mid;
        }
        
        if(0 <= ok && ok < mp[key].size()) time = min(2 * abs(mp[key][ok] - X[i]), time);
      }
      
      key = {'D', X[i] - Y[i]};
      if(mm[key].size()){
        int ng = -1, ok = mm[key].size();
        while(abs(ng - ok) > 1){
          int mid = (ok + ng) >> 1;
          (mm[key][mid] >= X[i] ? ok : ng) = mid;
        }
        
        if(0 <= ok && ok < mm[key].size()) time = min(2 * abs(mm[key][ok] - X[i]), time);
      }
      
      key = {'L', Y[i]};
      if(mh[key].size()){
        int ng = -1, ok = mh[key].size();
        while(abs(ng - ok) > 1){
          int mid = (ok + ng) >> 1;
          (mh[key][mid] >= X[i] ? ok : ng) = mid;
        }
        
        if(0 <= ok && ok < mh[key].size()) time = min(abs(mh[key][ok] - X[i]), time);
      }
    }else if(d[i] == 'U'){
      pair<char, int> key = {'R', X[i] + Y[i]};
      if(mp[key].size()){
        int ok = -1, ng = mp[key].size();
        while(abs(ng - ok) > 1){
          int mid = (ok + ng) >> 1;
          (mp[key][mid] <= X[i] ? ok : ng) = mid;
        }
        
        if(0 <= ok && ok < mp[key].size()) time = min(2 * abs(mp[key][ok] - X[i]), time);
      }
      
      key = {'L', X[i] - Y[i]};
      if(mm[key].size()){
        int ng = -1, ok = mm[key].size();
        while(abs(ng - ok) > 1){
          int mid = (ok + ng) >> 1;
          (mm[key][mid] >= X[i] ? ok : ng) = mid;
        }
        
        if(0 <= ok && ok < mm[key].size()) time = min(2 * abs(mm[key][ok] - X[i]), time);
      }
      
      key = {'D', X[i]};
      if(mv[key].size()){
        int ng = -1, ok = mv[key].size();
        while(abs(ng - ok) > 1){
          int mid = (ok + ng) >> 1;
          (mv[key][mid] >= Y[i] ? ok : ng) = mid;
        }
        
        if(0 <= ok && ok < mv[key].size()) time = min(abs(mv[key][ok] - Y[i]), time);
      }
    }else{
      pair<char, int> key = {'L', X[i] + Y[i]};
      if(mp[key].size()){
        int ng = -1, ok = mp[key].size();
        while(abs(ng - ok) > 1){
          int mid = (ok + ng) >> 1;
          (mp[key][mid] >= X[i] ? ok : ng) = mid;
        }
        
        if(0 <= ok && ok < mp[key].size()) time = min(2 * abs(mp[key][ok] - X[i]), time);
      }
      
      key = {'R', X[i] - Y[i]};
      if(mm[key].size()){
        int ok = -1, ng = mm[key].size();
        while(abs(ng - ok) > 1){
          int mid = (ok + ng) >> 1;
          (mm[key][mid] <= X[i] ? ok : ng) = mid;
        }
        
        if(0 <= ok && ok < mm[key].size()) time = min(2 * abs(mm[key][ok] - X[i]), time);
      }
      
      key = {'U', X[i]};
      if(mv[key].size()){
        int ok = -1, ng = mv[key].size();
        while(abs(ng - ok) > 1){
          int mid = (ok + ng) >> 1;
          (mv[key][mid] <= Y[i] ? ok : ng) = mid;
        }
        
        if(0 <= ok && ok < mv[key].size()) time = min(abs(mv[key][ok] - Y[i]), time);
      }
    }
  }
  
  if(time < INF) cout << time * 5 << endl;
  else cout << "SAFE" << endl;
  
  return 0;
}

Measured memory_bytes: 119060