#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <random>
#include <iomanip>
#include <functional>
#include <cassert>

using namespace std;

typedef long long ll;

const int Inf = 1e9;

int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif
  int n;
  cin >> n;
  vector <int> x(n), y(n);
  vector <char> go(n);
  for (int i = 0; i < n; ++i) {
    cin >> x[i] >> y[i] >> go[i];
    x[i] *= 10;
    y[i] *= 10;
  }
  int ans = Inf; 
  {
    map <int, vector <int>> up, down;
    for (int i = 0; i < n; ++i) {
      if (go[i] == 'U') {
        up[x[i]].push_back(y[i]);
      }
      if (go[i] == 'D') {
        down[x[i]].push_back(y[i]);
      }
    }
    for (auto &p : up) {
      sort(p.second.begin(), p.second.end());
    }
    for (auto &p : down) {
      sort(p.second.begin(), p.second.end());
    }
    for (auto p : up) {
      int x = p.first;
      auto &gs = down[x];
      for (auto y : p.second) {
        auto it = lower_bound(gs.begin(), gs.end(), y);
        if (it != gs.end()) {
          ans = min(ans, ((*it) - y) / 2);
        }
      }
    }
  }

  {
    map <int, vector <int>> right, left;
    for (int i = 0; i < n; ++i) {
      if (go[i] == 'R') {
        right[y[i]].push_back(x[i]);
      }
      if (go[i] == 'L') {
        left[y[i]].push_back(x[i]);
      }
    }
    for (auto &p : right) {
      sort(p.second.begin(), p.second.end());
    }
    for (auto &p : left) {
      sort(p.second.begin(), p.second.end());
    }
    for (auto p : right) {
      int y = p.first;
      auto &gs = left[y];
      for (auto x : p.second) {
        auto it = lower_bound(gs.begin(), gs.end(), x);
        if (it != gs.end()) {
          ans = min(ans, ((*it) - x) / 2);
        }
      }
    }
  }

  {
    map <int, vector <int>> f, s;
    for (int i = 0; i < n; ++i) {
      if (go[i] == 'U') {
        f[x[i] - y[i]].push_back(x[i]);
      }
      if (go[i] == 'L') {
        s[x[i] - y[i]].push_back(x[i]);
      }
    }
    for (auto &p : f) {
      sort(p.second.begin(), p.second.end());
    }
    for (auto &p : s) {
      sort(p.second.begin(), p.second.end());
    }
    for (auto p : f) {
      int gogo = p.first;
      auto &gs = s[gogo];
      for (auto a : p.second) {
        auto it = lower_bound(gs.begin(), gs.end(), a);
        if (it != gs.end()) {
          ans = min(ans, (*it) - a);
        }
      }
    }
  }

  {
    map <int, vector <int>> f, s;
    for (int i = 0; i < n; ++i) {
      if (go[i] == 'D') {
        f[x[i] + y[i]].push_back(x[i]);
      }
      if (go[i] == 'L') {
        s[x[i] + y[i]].push_back(x[i]);
      }
    }
    for (auto &p : f) {
      sort(p.second.begin(), p.second.end());
    }
    for (auto &p : s) {
      sort(p.second.begin(), p.second.end());
    }
    for (auto p : f) {
      int gogo = p.first;
      auto &gs = s[gogo];
      for (auto a : p.second) {
        auto it = lower_bound(gs.begin(), gs.end(), a);
        if (it != gs.end()) {
          ans = min(ans, (*it) - a);
        }
      }
    }
  }

  {
    map <int, vector <int>> f, s;
    for (int i = 0; i < n; ++i) {
      if (go[i] == 'R') {
        f[x[i] - y[i]].push_back(x[i]);
      }
      if (go[i] == 'D') {
        s[x[i] - y[i]].push_back(x[i]);
      }
    }
    for (auto &p : f) {
      sort(p.second.begin(), p.second.end());
    }
    for (auto &p : s) {
      sort(p.second.begin(), p.second.end());
    }
    for (auto p : f) {
      int gogo = p.first;
      auto &gs = s[gogo];
      for (auto a : p.second) {
        auto it = lower_bound(gs.begin(), gs.end(), a);
        if (it != gs.end()) {
          ans = min(ans, (*it) - a);
        }
      }
    }
  }

  {
    map <int, vector <int>> f, s;
    for (int i = 0; i < n; ++i) {
      if (go[i] == 'R') {
        f[x[i] + y[i]].push_back(x[i]);
      }
      if (go[i] == 'U') {
        s[x[i] + y[i]].push_back(x[i]);
      }
    }
    for (auto &p : f) {
      sort(p.second.begin(), p.second.end());
    }
    for (auto &p : s) {
      sort(p.second.begin(), p.second.end());
    }
    for (auto p : f) {
      int gogo = p.first;
      auto &gs = s[gogo];
      for (auto a : p.second) {
        auto it = lower_bound(gs.begin(), gs.end(), a);
        if (it != gs.end()) {
          ans = min(ans, (*it) - a);
        }
      }
    }
  }
  
  if (ans != Inf) cout << ans << '\n';
  else cout << "SAFE\n";
}



Measured memory_bytes: 41796