#include <bits/stdc++.h>

using namespace std;

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

  int n;
  cin >> n;
  vector<pair<int, int>> p[4];
  for (int i = 0; i < n; ++i) {
    int x, y;
    char c;
    cin >> x >> y >> c;
    if (c == 'U') c = 0;
    if (c == 'R') c = 1;
    if (c == 'D') c = 2;
    if (c == 'L') c = 3;
    p[c].emplace_back(x, y);
  }
  int ans = 1e9;
  {
    unordered_map<int, vector<pair<int, int>>> kek;
    for (auto [x, y] : p[3]) {
      kek[y - x].emplace_back(x, 0);
    }
    for (auto [x, y] : p[0]) {
      kek[y - x].emplace_back(x, 1);
    }
    for (auto& [d, v] : kek) {
      sort(v.begin(), v.end());
      for (int i = 1; i < v.size(); ++i) {
        if (v[i - 1].second == 1 and v[i].second == 0) {
          ans = min(ans, (v[i].first - v[i - 1].first) * 10);
        }
      }
    }
  }
  {
    unordered_map<int, vector<pair<int, int>>> kek;
    for (auto [x, y] : p[2]) {
      kek[y - x].emplace_back(x, 0);
    }
    for (auto [x, y] : p[1]) {
      kek[y - x].emplace_back(x, 1);
    }
    for (auto& [d, v] : kek) {
      sort(v.begin(), v.end());
      for (int i = 1; i < v.size(); ++i) {
        if (v[i - 1].second == 1 and v[i].second == 0) {
          ans = min(ans, (v[i].first - v[i - 1].first) * 10);
        }
      }
    }
  }
  {
    unordered_map<int, vector<pair<int, int>>> kek;
    for (auto [x, y] : p[0]) {
      kek[y + x].emplace_back(x, 0);
    }
    for (auto [x, y] : p[1]) {
      kek[y + x].emplace_back(x, 1);
    }
    for (auto& [d, v] : kek) {
      sort(v.begin(), v.end());
      for (int i = 1; i < v.size(); ++i) {
        if (v[i - 1].second == 1 and v[i].second == 0) {
          ans = min(ans, (v[i].first - v[i - 1].first) * 10);
        }
      }
    }
  }
  {
    unordered_map<int, vector<pair<int, int>>> kek;
    for (auto [x, y] : p[3]) {
      kek[y + x].emplace_back(x, 0);
    }
    for (auto [x, y] : p[2]) {
      kek[y + x].emplace_back(x, 1);
    }
    for (auto& [d, v] : kek) {
      sort(v.begin(), v.end());
      for (int i = 1; i < v.size(); ++i) {
        if (v[i - 1].second == 1 and v[i].second == 0) {
          ans = min(ans, (v[i].first - v[i - 1].first) * 10);
        }
      }
    }
  }
  {
    unordered_map<int, vector<pair<int, int>>> kek;
    for (auto [x, y] : p[2]) {
      kek[x].emplace_back(y, 0);
    }
    for (auto [x, y] : p[0]) {
      kek[x].emplace_back(y, 1);
    }
    for (auto& [d, v] : kek) {
      sort(v.begin(), v.end());
      for (int i = 1; i < v.size(); ++i) {
        if (v[i - 1].second == 1 and v[i].second == 0) {
          ans = min(ans, (v[i].first - v[i - 1].first) * 5);
        }
      }
    }
  }
  {
    unordered_map<int, vector<pair<int, int>>> kek;
    for (auto [x, y] : p[3]) {
      kek[y].emplace_back(x, 0);
    }
    for (auto [x, y] : p[1]) {
      kek[y].emplace_back(x, 1);
    }
    for (auto& [d, v] : kek) {
      sort(v.begin(), v.end());
      for (int i = 1; i < v.size(); ++i) {
        if (v[i - 1].second == 1 and v[i].second == 0) {
          ans = min(ans, (v[i].first - v[i - 1].first) * 5);
        }
      }
    }
  }
  if (ans == 1e9) {
    cout << "SAFE";
  } else {
    cout << ans;
  }

  return 0;
}


Measured memory_bytes: 22188