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

int main() {
  const int inf = 1000000000;
  int ans = inf;
  int n;
  cin >> n;
  vector<int> x(n), y(n);
  vector<char> v(n);
  for (int i = 0; i < n; i++) {
    cin >> x.at(i) >> y.at(i) >> v.at(i);
    x.at(i)++;
    y.at(i)++;
  }
  map<int, vector<int>> u, r, d, l, u_ur, u_ul, r_ur, r_dr, d_dr, d_dl, l_ul, l_dl;
  for (int i = 0; i < n; i++) {
    switch (v.at(i)) {
      case 'U':
        u[x.at(i)].push_back(y.at(i));
        u_ur[x.at(i) + y.at(i)].push_back(x.at(i) - y.at(i));
        u_ul[x.at(i) - y.at(i)].push_back(x.at(i) + y.at(i));
        break;
      case 'R':
        r[y.at(i)].push_back(x.at(i));
        r_ur[x.at(i) + y.at(i)].push_back(x.at(i) - y.at(i));
        r_dr[x.at(i) - y.at(i)].push_back(x.at(i) + y.at(i));
        break;
      case 'D':
        d[x.at(i)].push_back(y.at(i));
        d_dl[x.at(i) + y.at(i)].push_back(x.at(i) - y.at(i));
        d_dr[x.at(i) - y.at(i)].push_back(x.at(i) + y.at(i));
        break;
      case 'L':
        l[y.at(i)].push_back(x.at(i));
        l_dl[x.at(i) + y.at(i)].push_back(x.at(i) - y.at(i));
        l_ul[x.at(i) - y.at(i)].push_back(x.at(i) + y.at(i));
        break;
    }
  }
  for (auto t : u) {
    int k = t.first;
    vector<int> a = t.second;
    if (d[k].empty()) continue;
    sort(d[k].begin(), d[k].end());
    for (int c : a) {
      int dx = lower_bound(d[k].begin(), d[k].end(), c) - d[k].begin();
      if (dx == d[k].size()) continue;
      ans = min(ans, 5 * (d[k][dx] - c));
    }
  }
  for (auto t : r) {
    int k = t.first;
    vector<int> a = t.second;
    if (l[k].empty()) continue;
    sort(l[k].begin(), l[k].end());
    for (int c : a) {
      int dx = lower_bound(l[k].begin(), l[k].end(), c) - l[k].begin();
      if (dx == l[k].size()) continue;
      ans = min(ans, 5 * (l[k][dx] - c));
    }
  }
  for (auto t : r_ur) {
    int k = t.first;
    vector<int> a = t.second;
    if (u_ur[k].empty()) continue;
    sort(u_ur[k].begin(), u_ur[k].end());
    for (int c : a) {
      int dx = lower_bound(u_ur[k].begin(), u_ur[k].end(), c) - u_ur[k].begin();
      if (dx == u_ur[k].size()) continue;
      ans = min(ans, 5 * (u_ur[k][dx] - c));
    }
  }
  for (auto t : u_ul) {
    int k = t.first;
    vector<int> a = t.second;
    if (l_ul[k].empty()) continue;
    sort(l_ul[k].begin(), l_ul[k].end());
    for (int c : a) {
      int dx = lower_bound(l_ul[k].begin(), l_ul[k].end(), c) - l_ul[k].begin();
      if (dx == l_ul[k].size()) continue;
      ans = min(ans, 5 * (l_ul[k][dx] - c));
    }
  }
  for (auto t : r_dr) {
    int k = t.first;
    vector<int> a = t.second;
    if (d_dr[k].empty()) continue;
    sort(d_dr[k].begin(), d_dr[k].end());
    for (int c : a) {
      int dx = lower_bound(d_dr[k].begin(), d_dr[k].end(), c) - d_dr[k].begin();
      if (dx == d_dr[k].size()) continue;
      ans = min(ans, 5 * (d_dr[k][dx] - c));
    }
  }
  for (auto t : d_dl) {
    int k = t.first;
    vector<int> a = t.second;
    if (l_dl[k].empty()) continue;
    sort(l_dl[k].begin(), l_dl[k].end());
    for (int c : a) {
      int dx = lower_bound(l_dl[k].begin(), l_dl[k].end(), c) - l_dl[k].begin();
      if (dx == l_dl[k].size()) continue;
      ans = min(ans, 5 * (l_dl[k][dx] - c));
    }
  }
  if (ans == inf) {
    cout << "SAFE" << endl;
  } else {
    cout << ans << endl;
  }
}


Measured memory_bytes: 85320