#include <bits/stdc++.h>

using namespace std;

#define SZ(x) (int)(x.size())
#define REP(i, n) for(int i=0;i<(n);++i)
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define RREP(i, n) for(int i=(int)(n);i>=0;--i)
#define RFOR(i, a, b) for(int i=(int)(a);i>=(int)(b);--i)
#define ALL(a) (a).begin(),(a).end()
#define DUMP(x) cerr<<#x<<" = "<<(x)<<endl
#define DEBUG(x) cerr<<#x<<" = "<<(x)<<" (L"<<__LINE__<<")"<< endl;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using P = pair<int, int>;

const double eps = 1e-8;
const ll MOD = 1000000007;
const int INF = INT_MAX / 2;
const ll LINF = LLONG_MAX / 2;

template<typename T1, typename T2>
bool chmax(T1 &a, const T2 &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template<typename T1, typename T2>
bool chmin(T1 &a, const T2 &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template<typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> p) {
  os << p.first << ":" << p.second;
  return os;
}

template<class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  REP(i, SZ(v)) {
    if (i) os << " ";
    os << v[i];
  }
  return os;
}

struct Elm {
  int x, y, dir;
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  int n;
  cin >> n;
  vector<Elm> v(n);

  vector<map<int, set<int>>> mp1(4), mp2(4), mp3(4), mp4(4);
  REP(i, n) {
    char c;
    cin >> v[i].x >> v[i].y >> c;
    if (c == 'U') {
      v[i].dir = 0;
    } else if (c == 'R') {
      v[i].dir = 1;
    } else if (c == 'D') {
      v[i].dir = 2;
    } else {
      v[i].dir = 3;
    }

    mp1[v[i].dir][v[i].x + v[i].y].insert(v[i].x);
    mp2[v[i].dir][v[i].y - v[i].x].insert(v[i].x);
    mp3[v[i].dir][v[i].x].insert(v[i].y);
    mp4[v[i].dir][v[i].y].insert(v[i].x);
  }

  ll mi = LINF;
  for (auto &e1: mp1[0]) {
    for (auto &e: e1.second) {
      auto it = mp1[1][e1.first].lower_bound(e);
      if (it != mp1[1][e1.first].begin()) {
        it--;
        chmin(mi, (e - (*it)) * 10);
      }
    }
  }

  for (auto &e1: mp1[3]) {
    for (auto &e: e1.second) {
      auto it = mp1[2][e1.first].lower_bound(e);
      if (it != mp1[2][e1.first].begin()) {
        it--;
        chmin(mi, (e - (*it)) * 10);
      }
    }
  }

  for (auto &e1: mp2[3]) {
    for (auto &e: e1.second) {
      auto it = mp2[0][e1.first].lower_bound(e);
      if (it != mp2[0][e1.first].begin()) {
        it--;
        chmin(mi, (e - (*it)) * 10);
      }
    }
  }

  for (auto &e1: mp2[2]) {
    for (auto &e: e1.second) {
      auto it = mp2[1][e1.first].lower_bound(e);
      if (it != mp2[1][e1.first].begin()) {
        it--;
        chmin(mi, (e - (*it)) * 10);
      }
    }
  }


  for (auto &e1: mp3[2]) {
    for (auto &e: e1.second) {
      auto it = mp3[0][e1.first].lower_bound(e);
      if (it != mp3[0][e1.first].begin()) {
        it--;
        chmin(mi, (e - (*it)) * 5);
      }
    }
  }


  for (auto &e1: mp4[3]) {
    for(auto &e: e1.second) {
      auto it = mp4[1][e1.first].lower_bound(e);
      if (it != mp4[1][e1.first].begin()) {
        it--;
        chmin(mi, (e - (*it)) * 5);
      }
    }
  }

  if (mi == LINF) {
    cout << "SAFE" << endl;
  } else {
    cout << mi << endl;
  }

}








Measured memory_bytes: 135432