#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <map>
#include <iostream>
#include <vector>
#include <queue>
#include <unordered_map>
#include <random>
#include <stack>
#include <set>
#include <unordered_set>

#define bug(x) cout<<"zdongdebug1: "<<x<<endl;
#define bug2(x, y) cout<<"zdongdebug2: "<<x<<" "<<y<<endl;
#define bug3(x, y, z) cout<<"zdongdebug3: "<<x<<" "<<y<<" "<<z<<endl;
using namespace std;
typedef long long ll;
const int maxn = 200005;
const int mod = 1000000007;

struct P {
  int x,d;
  P(int x, int d):x(x),d(d){}
  bool operator<(const P&a)const {
    return x<a.x;
  }
};

struct T {
  int x,y,d;
}t[maxn];

int getD(string c){
  if(c[0]=='R')return 0;
  if(c[0]=='U')return 1;
  if(c[0]=='L')return 2;
  return 3;
}

int main() {
#ifdef suiyuan2009
  freopen("/Users/suiyuan2009/CLionProjects/icpc/input.txt", "r", stdin);
  //freopen("/Users/suiyuan2009/CLionProjects/icpc/output.txt", "w", stdout);
#endif
  int n;
  cin>>n;
  unordered_map<int, int>mx,my,mz,mf;
  int cx = 0, cy = 0, cz = 0, cf = 0;
  vector<vector<P>> vx, vy, vz, vf;
  for(int i=0;i<n;i++){
    string s;
    cin>>t[i].x>>t[i].y>>s;
    t[i].d = getD(s);
    int x = t[i].x;
    int y = t[i].y;
    int z = t[i].x - t[i].y;
    int f = t[i].x + t[i].y;
    if(mx.find(x)==mx.end()){
      mx[x] = cx++;
      vx.push_back({});
    }
    vx[mx[x]].push_back(P(y,t[i].d));
    if(my.find(y)==my.end()){
      my[y] = cy++;
      vy.push_back({});
    }
    vy[my[y]].push_back(P(x,t[i].d));
    if(mz.find(z)==mz.end()){
      mz[z] = cz++;
      vz.push_back({});
    }
    vz[mz[z]].push_back(P(x,t[i].d));
    if(mf.find(f)==mf.end()){
      mf[f] = cf++;
      vf.push_back({});
    }
    vf[mf[f]].push_back(P(x,t[i].d));
  }
  int ans = 1000000007;
  for(int i=0;i<vx.size();i++) {
    sort(vx[i].begin(),vx[i].end());
    for(int j=0;j<vx[i].size();j++){
      if(j&&vx[i][j].d==3&&vx[i][j-1].d==1){
        ans = min(ans, (vx[i][j].x-vx[i][j-1].x) * 5);
      }
    }
  }
  for(int i=0;i<vy.size();i++) {
    sort(vy[i].begin(),vy[i].end());
    for(int j=0;j<vy[i].size();j++){
      if(j&&vy[i][j].d==2&&vy[i][j-1].d==0){
        ans = min(ans, (vy[i][j].x-vy[i][j-1].x) * 5);
      }
    }
  }
  for(int i=0;i<vz.size();i++) {
    sort(vz[i].begin(),vz[i].end());
    int rx = -1, ux = -1;
    for(int j=0;j<vz[i].size();j++){
      if(vz[i][j].d==0){
        rx = vz[i][j].x;
      } else if(vz[i][j].d==1){
        ux = vz[i][j].x;
      } else if(vz[i][j].d==3){
        if(rx!=-1){
          ans = min(ans, (vz[i][j].x-rx) * 10);
        }
      } else {
        if(ux!=-1){
          ans = min(ans, (vz[i][j].x-ux)* 10);
        }
      }
    }
  }
  for(int i=0;i<vf.size();i++) {
    sort(vf[i].begin(),vf[i].end());
    int rx = -1, dx = -1;
    for(int j=0;j<vf[i].size();j++){
      if(vf[i][j].d==0){
        rx = vf[i][j].x;
      } else if(vf[i][j].d==3){
        dx = vf[i][j].x;
      } else if(vf[i][j].d==1){
        if(rx!=-1){
          ans = min(ans, (vf[i][j].x-rx)*10);
        }
      } else {
        if(dx!=-1){
          ans = min(ans, (vf[i][j].x-dx)*10);
        }
      }
    }
  }
  if(ans>=1000000007){
    cout<<"SAFE"<<endl;
    return 0;
  }
  cout<<ans<<endl;
  return 0;
}

Measured memory_bytes: 64220