#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i=0; i<(int)(n); i++)
#define all(x) x.begin(), x.end()
#define mod 1000000007
typedef long long ll;

typedef struct point{
  ll x;
  ll y;
  char dis;
} point;

int main(){
  ll n;
  cin >> n;
  vector<point> vec;
  rep(i, n){
    point p;
    cin >> p.x >> p.y >> p.dis;
    vec.push_back(p);
  }
  ll smallest = 10000000;
  
  {
    map<ll, vector<pair<ll, char>>> mp;
    rep(i, n) mp[vec[i].y].push_back(make_pair(vec[i].x, vec[i].dis));
    for(auto v : mp){
      sort(all(v.second));
      ll left = -10000000000;
      for(auto p : v.second){
        if(p.second == 'R') left = p.first;
        else if(p.second == 'L') smallest = min(smallest, (p.first-left)*10/2);
      }
    }
  }
  
  {
    map<ll, vector<pair<ll, char>>> mp;
    rep(i, n) mp[vec[i].x].push_back(make_pair(vec[i].y, vec[i].dis));
    for(auto v : mp){
      sort(all(v.second));
      ll left = -10000000000;
      for(auto p : v.second){
        if(p.second == 'U') left = p.first;
        else if(p.second == 'D') smallest = min(smallest, (p.first-left)*10/2);
      }
    }
  }
  
  {
    map<ll, vector<point>> mp;
    rep(i, n) mp[vec[i].x+vec[i].y].push_back(vec[i]);
    for(auto v : mp){
      sort(all(v.second), [](point l, point r){
        if(l.x <= r.x) return true;
        else if(l.x > r.x) return false;
      });
      ll up = 10000000000;
      ll left = -10000000000;
      for(auto p : v.second){
        if(p.dis == 'R') left = p.x;
        else if(p.dis == 'D') up = p.y;
        else if(p.dis == 'L') smallest = min(smallest, (up - p.y)*10);
        else if(p.dis == 'U') smallest = min(smallest, (p.x - left)*10);
      }
    }
  }
  
  {
    map<ll, vector<point>> mp;
    rep(i, n) mp[-vec[i].x+vec[i].y].push_back(vec[i]);
    for(auto v : mp){
      sort(all(v.second), [](point l, point r){
        if(l.x <= r.x) return true;
        else if(l.x > r.x) return false;
      });
      ll down = -10000000000;
      ll left = -10000000000;
      for(auto p : v.second){
        if(p.dis == 'R') left = p.x;
        else if(p.dis == 'U') down = p.y;
        else if(p.dis == 'L') smallest = min(smallest, (p.y - down)*10);
        else if(p.dis == 'D') smallest = min(smallest, (p.x - left)*10);
      }
    }
  }
  
  if(smallest == 10000000) cout << "SAFE" << endl;
  else cout << smallest << endl;
  return 0;
}
  
  
  
  