#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <vector>

using namespace std;

#define rep(i, n) for (int64_t i = 0; i < (int64_t)(n); i++)
#define irep(i, n) for (int64_t i = 0; i <= (int64_t)(n); i++)
#define rrep(i, n) for (int64_t i = (n)-1; i >= 0; i--)
#define rirep(i, n) for (int64_t i = n; i >= 0; i--)

#define chmax(a, b) (a) = max(a, b)
#define chmin(a, b) (a) = min(a, b)

int main() {
  int N;
  cin >> N;

  using D = tuple<int, int, int>;
  vector<D> data;
  rep(i, N) {
    int x, y;
    char d;
    cin >> x >> y >> d;

    int dir = 0;
    if (d == 'D' || d == 'U') {
      dir |= 2;
    }
    if (d == 'D' || d == 'L') {
      dir |= 1;
    }
    data.push_back(D(x, y, dir));
  }

  constexpr int INF = numeric_limits<int>::max();
  int result = INF;

  constexpr int MAX = 200'000;
  vector<vector<vector<int>>> tbl(4, vector<vector<int>>(MAX + 1));
  vector<vector<vector<int>>> cross(4, vector<vector<int>>(2 * MAX + 1)),
      cross2(4, vector<vector<int>>(2 * MAX + 1));
  rep(i, N) {
    int x, y, d;
    tie(x, y, d) = data[i];

    tbl[d][((d & 2) ? x : y)].push_back((d & 2) ? y : x);
    cross[d][x + y].push_back(x - y + MAX);
    cross2[d][x - y + MAX].push_back(x + y);
  }
  rep(i, 4) irep(j, MAX) { sort(tbl[i][j].begin(), tbl[i][j].end()); }

  rep(i, 4) irep(j, 2 * MAX) {
    sort(cross[i][j].begin(), cross[i][j].end());
    sort(cross2[i][j].begin(), cross2[i][j].end());
  }

  rep(i, 2) {
    irep(j, MAX) {
      for (int v : tbl[i << 1][j]) {
        const auto &tmp = tbl[(i << 1) ^ 1][j];
        auto iter = lower_bound(tmp.begin(), tmp.end(), v);
        if (iter != tmp.end()) {
          // cerr << i << " " << j << " " << v << " " << *iter << "\n";
          chmin(result, 5 * (*iter - v));
        }
      }
    }
  }

  rep(i, 2) {
    irep(j, 2 * MAX) {
      for (int v : cross[i][j]) {
        const auto &tmp = cross[i ^ 2][j];
        auto iter = lower_bound(tmp.begin(), tmp.end(), v);
        if (i == 0) {
          if (iter != tmp.end()) {
            cerr << i << " " << j << " " << v << " " << *iter << "\n";
            chmin(result, 5 * (*iter - v));
          }
        } else {
          if (iter != tmp.begin()) {
            chmin(result, 5 * (v - *prev(iter)));
          }
        }
      }
    }
  }

  rep(i, 2) {
    irep(j, 2 * MAX) {
      for (int v : cross2[i][j]) {
        const auto &tmp = cross2[i ^ 3][j];
        auto iter = lower_bound(tmp.begin(), tmp.end(), v);
        if (i == 0) {
          if (iter != tmp.end()) {
            chmin(result, 5 * (*iter - v));
          }
        } else {
          if (iter != tmp.begin()) {
            chmin(result, 5 * (v - *prev(iter)));
          }
        }
      }
    }
  }

  if (result == INF) {
    cout << "SAFE" << endl;
  } else {
    cout << result << endl;
  }
  return 0;
}
#include<bits/stdc++.h>
#define tmp(x) std::cout<<"& "<<(x)<<" &\n"
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define sz(x) ((int)x.size())
#define all(x) x.begin(),x.end()
typedef long long ll;
#define  pii pair<int,int>
#define  pll pair<ll,ll>
#define  vi vector<int>


using namespace std;
const int maxn=3e5+100;
const int inf=(int)1e9+7;
int n;

vector<pair<int,char> > x[maxn],y[maxn],le[2*maxn],ri[2*maxn];
int ans=inf;
void work(vi v1,vi v2,int aa){

    if(!v2.empty()&&!v1.empty()){
        vector<int>::iterator it1,it2;
        sort(v2.begin(),v2.end());
        sort(v1.begin(),v1.end());
        it2=v2.begin(),it1=v1.begin();
        for(auto p1:v1){
            for(auto p2:v2){
                if(p2<p1)continue;
                else {
                    ans=min(ans,abs(p1-p2)*10/aa);
                    break;
                }
            }
        }
        
        /*while(*it1!=*v1.end()&&*it2!=*v2.end()){
            while(*it2!=*v2.end()&&*it2<*it1)++it2;
            if(*it2==*v2.end())break;
            ans=min(ans,abs(*it1-*it2)*10/aa);
            ++it1;
        }*/
    }
}
int main(){
//	freopen("in.txt","r",stdin);
    scanf("%d",&n);
    int xx,yy;char c;
    rep(i,1,n){
        scanf("%d%d %c",&xx,&yy,&c);

        x[xx].push_back(make_pair(yy,c));
        y[yy].push_back(make_pair(xx,c));
        le[xx-yy+200000].push_back(make_pair(xx,c));
        ri[xx+yy].push_back(make_pair(xx,c));
    }
    rep(i,0,200000){
        if(!x[i].empty()){
            vector<int>up,down;
            for(auto p:x[i]){
                if(p.second=='U')up.push_back(p.first);
                if(p.second=='D')down.push_back(p.first);
            }
            work(up,down,2);
        }
        if(!y[i].empty()){
            vector<int>right,left;
            for(auto p:y[i]){
                if(p.second=='R')right.push_back(p.first);
                if(p.second=='L')left.push_back(p.first);
            }
            work(right,left,2);
        }
    }

    rep(i,0,400000){
        if(!le[i].empty()){
            vector<int> right,down,left,up;
            for(auto p:le[i]){
                if(p.second=='D')down.push_back(p.first);
                else if(p.second=='R')right.push_back(p.first);
                else if(p.second=='U')up.push_back(p.first);
                else if(p.second=='L')left.push_back(p.first);
            }
            work(right,down,1);
            work(up,left,1);
        }
        if(!ri[i].empty()) {
            vector<int> right, up,left, down;
            for (auto p:ri[i]) {
                if (p.second == 'U')up.push_back(p.first);
                else if (p.second == 'R')right.push_back(p.first);
                else if (p.second == 'D')down.push_back(p.first);
                else if (p.second == 'L')left.push_back(p.first);
            }
            work(right, up,1);
            work(down, left,1);
        }
    }
    if(ans==inf)printf("SAFE");
    else  printf("%d",ans);




    return 0;
}