#include <bits/stdc++.h>
using namespace std;
using ll = long long;

using grid = pair<int, int>;

int func(const vector<grid> &p, const vector<grid> &q) {
  auto ip = p.begin();
  auto iq = q.begin();
  int ans = INT_MAX / 10;
  while (ip != p.end() && iq != q.end()) {
    if ((*ip).first > (*iq).first)
      ++iq;
    else if ((*ip).first < (*iq).first)
      ++ip;
    else {
      if ((*ip).second < (*iq).second) {
        ans = min(ans, (*iq).second - (*ip).second);
        ++ip;
      } else {
        ++iq;
      }
    }
  }
  return ans;
}

int main() {
  int N;
  cin >> N;
  vector<grid> u;
  vector<grid> d;
  vector<grid> r;
  vector<grid> l;

  for (int i = 0; i < N; i++) {
    int x, y;
    cin >> x >> y;
    char dir;
    cin >> dir;
    if (dir == 'U') {
      u.push_back(make_pair(x, y));
    } else if (dir == 'D') {
      d.push_back(make_pair(x, y));
    } else if (dir == 'R') {
      r.push_back(make_pair(y, x));
    } else if (dir == 'L') {
      l.push_back(make_pair(y, x));
    }
  }

  sort(u.begin(), u.end());
  sort(d.begin(), d.end());
  sort(r.begin(), r.end());
  sort(l.begin(), l.end());
  int ans = INT_MAX;
  ans = min(ans, func(u, d) * 5);
  ans = min(ans, func(r, l) * 5);

  auto f1 = [](vector<grid> &p) {
    for (auto &[x, y] : p) {
      swap(x, y);
    }
  };
  auto f2 = [](vector<grid> &p) {
    for (auto &[y, x] : p) {
      y += x;
    }
  };
  f1(u);
  f1(d);
  f2(u);
  f2(d);
  f2(r);
  f2(l);
  sort(u.begin(), u.end());
  sort(d.begin(), d.end());
  sort(r.begin(), r.end());
  sort(l.begin(), l.end());
  ans = min(ans, func(r, u) * 10);
  ans = min(ans, func(d, l) * 10);
  auto f3 = [](vector<grid> &p) {
    for (auto &[xy, x] : p) {
      xy -= 2 * x;
    }
  };
  f3(u);
  f3(d);
  f3(r);
  f3(l);
  sort(u.begin(), u.end());
  sort(d.begin(), d.end());
  sort(r.begin(), r.end());
  sort(l.begin(), l.end());
  ans = min(ans, func(u, l) * 10);
  ans = min(ans, func(r, d) * 10);

  if (ans > INT_MAX / 10)
    cout << "SAFE" << endl;
  else
    cout << ans << endl;
}
