#include "bits/stdc++.h"
using namespace std;
#define int long long
#define FOR(i, a, b) for(int i=(a);i<(b);i++)
#define RFOR(i, a, b) for(int i=(b-1);i>=(a);i--)
#define REP(i, n) for(int i=0; i<(n); i++)
#define RREP(i, n) for(int i=(n-1); i>=0; i--)
#define REP1(i, n) for(int i=1; i<=(n); i++)
#define RREP1(i, n) for(int i=(n); i>=1; i--)
#define ALL(a) (a).begin(),(a).end()
#define UNIQUE_SORT(l) sort(ALL(l)); l.erase(unique(ALL(l)), l.end());
#define CONTAIN(a, b) find(ALL(a), (b)) != (a).end()
#define out(...) printf(__VA_ARGS__)
#if DEBUG
#define debug(...) printf(__VA_ARGS__)
#else
#define debug(...) /* ... */
#endif
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}

void solve();
signed main()
{
#if DEBUG
    std::ifstream in("input.txt");
    std::cin.rdbuf(in.rdbuf());
#endif
    cin.tie(0);
    ios::sync_with_stdio(false);
    solve();
    return 0;
}
/*================================*/
#if DEBUG
#define SIZE 1000
#else
#define SIZE 423450
#endif

int N,Q,K;

set<int> um[2][SIZE],dm[2][SIZE];
int BASE = SIZE/2;

struct Item2 {
    int x,y;
    char u;
};
vector<Item2> V;

set<int> ds[SIZE],ls[SIZE];

void solve() {
    cin>>N;
    int x,y;
    char u;
    REP(i,N) {
        cin>>x>>y>>u;
        if (u=='D') {
            dm[0][y-x+BASE].insert(x);
            dm[1][y+x].insert(x);
        }
        if (u=='U') {
            um[0][y-x+BASE].insert(x);
            um[1][y+x].insert(x);
        }
        V.push_back({x,y,u});
        if (u=='D') ds[x].insert(y);
        if (u=='L') ls[y].insert(x);
    }
    int ans = INT_MAX;
    REP(i,N) {
        auto p = V[i];
        if (p.u=='R') {
            {
                auto s = &dm[0][p.y-p.x+BASE];
                auto it = s->upper_bound(p.x);
                if (it != s->end()) chmin(ans, (*it - p.x) * 10);
            }
            {
                auto s = &um[1][p.y+p.x];
                auto it = s->upper_bound(p.x);
                if (it != s->end()) chmin(ans, (*it - p.x) * 10);
            }
            {
                auto s = &ls[p.y];
                auto it = s->upper_bound(p.x);
                if (it != s->end()) {
                    chmin(ans, (*it - p.x) * 5);
                }
            }
        }
        if (p.u=='L') {
            {
                auto s = &um[0][p.y-p.x+BASE];
                auto it = s->lower_bound(p.x);
                if (it != s->begin()) chmin(ans, (p.x - *(--it))*10);
            }
            {
                auto s = &dm[1][p.y+p.x];
                auto it = s->lower_bound(p.x);
                if (it != s->begin()) chmin(ans, (p.x - *(--it))*10);
            }
        }
        if (p.u=='U') {
            auto s = &ds[p.x];
            auto it = s->upper_bound(p.y);
            if (it != s->end()) {
                chmin(ans, (*it - p.y)*5);
            }
        }
    }
    
    if (ans==INT_MAX) {
        cout << "SAFE" << endl;
    } else {
        cout << ans << endl;
    }
}


#include <bits/stdc++.h>

#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0, a1, a2, a3, a4, x, ...) x
#define dump_1(x1) cerr << #x1 << ": " << x1 << endl
#define dump_2(x1, x2) \
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << endl
#define dump_3(x1, x2, x3)                                                \
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": " \
       << x3 << endl
#define dump_4(x1, x2, x3, x4)                                            \
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": " \
       << x3 << ", " #x4 << ": " << x4 << endl
#define dump_5(x1, x2, x3, x4, x5)                                        \
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": " \
       << x3 << ", " #x4 << ": " << x4 << ", " #x5 << ": " << x5 << endl
#define dump(...) \
  CHOOSE((__VA_ARGS__, dump_5, dump_4, dump_3, dump_2, dump_1, ~))(__VA_ARGS__)
#define check(s) cerr << s << endl

#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define unique(v) v.erase(unique(v.begin(), v.end()), v.end());

using namespace std;

using ll = long long;

vector<int> dx = {0, 1, 0, -1};
vector<int> dy = {1, 0, -1, 0};

const ll LINF = 2e18;
const int INF = 1e8;

int g(vector<pair<int, int>> a) {
  sort(all(a));
  int res = INF;
  int py = -INF * 2;
  for (int i = 1; i < sz(a); i++) {
    if (a.at(i - 1).second == 0 && a.at(i).second != 0) {
      res = min(res, a.at(i).first - a.at(i - 1).first);
    }
  }
  return res;
}

int f(map<int, vector<pair<int, int>>>& mp) {
  int res = INF;
  for (auto p : mp) {
    res = min(res, g(p.second));
  }
  return res;
}

void solve(ll N, std::vector<ll> X, std::vector<ll> Y, std::vector<char> U) {
  vector<ll> dir(N);  // URDL = 0123

  rep(i, N) {
    if (U.at(i) == 'U') dir.at(i) = 0;
    if (U.at(i) == 'R') dir.at(i) = 1;
    if (U.at(i) == 'D') dir.at(i) = 2;
    if (U.at(i) == 'L') dir.at(i) = 3;
  }

  int ans = INF;

  rep(ri, 4) {
    {  // UD
      map<int, vector<pair<int, int>>> mp;
      rep(i, N) {
        if (dir.at(i) != 0 && dir.at(i) != 2) continue;
        mp[X.at(i)].emplace_back(Y.at(i), dir.at(i));
      }
      ans = min(ans, f(mp) * 5);
    }
    {  // UR
      map<int, vector<pair<int, int>>> mp;
      rep(i, N) {
        if (dir.at(i) != 0 && dir.at(i) != 1) continue;
        mp[X.at(i) + Y.at(i)].emplace_back(Y.at(i), dir.at(i));
      }
      ans = min(ans, f(mp) * 10);
    }
    // rotate
    rep(i, N) {
      int px = X.at(i), py = Y.at(i);
      // 反時計に90度回転
      X.at(i) = py;
      Y.at(i) = -px;
      dir.at(i) = (dir.at(i) + 1) % 4;
    }
  }

  if (ans != INF) {
    cout << ans << endl;
  } else {
    cout << "SAFE" << endl;
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  ll N;
  cin >> N;
  std::vector<ll> X(N);
  std::vector<ll> Y(N);
  std::vector<char> U(N);
  for (int i = 0; i < N; i++) {
    cin >> X.at(i);
    cin >> Y.at(i);
    cin >> U.at(i);
  }
  solve(N, std::move(X), std::move(Y), std::move(U));
  return 0;
}
