#include <bits/stdc++.h>

using namespace std;
using int64 = long long;

constexpr int DEBUG = 0;

constexpr int64 INF = 1E12;

template<class T> inline bool UpdateMin(T& a, T b) {
  if (a > b) { a = b; return 1; } return 0;
}

template<class T> inline bool UpdateMax(T& a, T b) {
  if (a < b) { a = b; return 1; } return 0;
}

int64 Solve(const vector<int64>& raw_zs, const vector<int>& raw_signs) {
  int n = raw_zs.size();
  vector<pair<int64, int>> pairs;
  for (int i = 0; i < n; i++) {
    pairs.push_back({raw_zs[i], raw_signs[i]});
  }
  sort(pairs.begin(), pairs.end());
  vector<int64> zs(n);
  vector<int> signs(n);
  for (int i = 0; i < n; i++) {
    zs[i] = pairs[i].first;
    signs[i] = pairs[i].second;
  }

  int start_index = -1;
  int64 min_distance = INF;
  for (int i = 0; i < n; i++) {
    if (signs[i] == 1) {
      start_index = i;
    } else {
      if (start_index >= 0) {
        UpdateMin(min_distance, static_cast<int64>(zs[i] - zs[start_index]));
      }
    }
  }
  // cout << "min_distance: " << min_distance << endl;
  return min_distance;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n;
  cin >> n;
  vector<int64> xs(n), ys(n);
  vector<char> ds(n);
  for (int i = 0; i < n; i++) {
    cin >> xs[i] >> ys[i] >> ds[i];
  }

  int64 ans = INF;

  // TYPE I
  {
    map<int64, vector<tuple<int64, char>>> g_to_tuples;
    for (int i = 0; i < n; i++) {
      int64 g = ys[i];
      int64 z = xs[i];
      g_to_tuples[g].push_back({z, ds[i]});
    }

    for (const auto [g, tuples] : g_to_tuples) {
      vector<int64> zs;
      vector<int> signs;
      for (const auto [z, d] : tuples) {
        if (d == 'R' || d == 'L') {
          zs.push_back(z);
          if (d == 'R') {
            signs.push_back(1);
          } else {
            signs.push_back(-1);
          }
        }
      }
      UpdateMin(ans, Solve(zs, signs) * 5);
    }
  }

  // TYPE II
  {
    map<int64, vector<tuple<int64, char>>> g_to_tuples;
    for (int i = 0; i < n; i++) {
      int64 g = xs[i];
      int64 z = ys[i];
      g_to_tuples[g].push_back({z, ds[i]});
    }

    for (const auto [g, tuples] : g_to_tuples) {
      vector<int64> zs;
      vector<int> signs;
      for (const auto [z, d] : tuples) {
        if (d == 'U' || d == 'D') {
          zs.push_back(z);
          if (d == 'U') {
            signs.push_back(1);
          } else {
            signs.push_back(-1);
          }
        }
      }
      UpdateMin(ans, Solve(zs, signs) * 5);
    }
  }

  // TYPE III
  {
    map<int64, vector<tuple<int64, char>>> g_to_tuples;
    for (int i = 0; i < n; i++) {
      int64 g = xs[i] + ys[i];
      int64 z = xs[i];
      g_to_tuples[g].push_back({z, ds[i]});
    }

    for (const auto [g, tuples] : g_to_tuples) {
      {
        vector<int64> zs;
        vector<int> signs;
        for (const auto [z, d] : tuples) {
          if (d == 'R' || d == 'U') {
            zs.push_back(z);
            if (d == 'R') {
              signs.push_back(1);
            } else {
              signs.push_back(-1);
            }
          }
        }
        UpdateMin(ans, Solve(zs, signs) * 10);
      }
      {
        vector<int64> zs;
        vector<int> signs;
        for (const auto [z, d] : tuples) {
          if (d == 'D' || d == 'L') {
            zs.push_back(z);
            if (d == 'D') {
              signs.push_back(1);
            } else {
              signs.push_back(-1);
            }
          }
        }
        UpdateMin(ans, Solve(zs, signs) * 10);
      }
    }
  }

  // TYPE IV
  {
    map<int64, vector<tuple<int64, char>>> g_to_tuples;
    for (int i = 0; i < n; i++) {
      int64 g = xs[i] - ys[i];
      int64 z = xs[i];
      g_to_tuples[g].push_back({z, ds[i]});
    }

    for (const auto [g, tuples] : g_to_tuples) {
      {
        vector<int64> zs;
        vector<int> signs;
        for (const auto [z, d] : tuples) {
          if (d == 'U' || d == 'L') {
            zs.push_back(z);
            if (d == 'U') {
              signs.push_back(1);
            } else {
              signs.push_back(-1);
            }
          }
        }
        UpdateMin(ans, Solve(zs, signs) * 10);
      }
      {
        vector<int64> zs;
        vector<int> signs;
        for (const auto [z, d] : tuples) {
          if (d == 'R' || d == 'D') {
            zs.push_back(z);
            if (d == 'R') {
              signs.push_back(1);
            } else {
              signs.push_back(-1);
            }
          }
        }
        UpdateMin(ans, Solve(zs, signs) * 10);
      }
    }
  }

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

Measured memory_bytes: 28256