#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
void *wmem;
char memarr[96000000];
template<class T> inline void walloc1d(T **arr, int x, void **mem = &wmem){
  static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
  (*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] );
  (*arr)=(T*)(*mem);
  (*mem)=((*arr)+x);
}
template<class T1, class T2> void sortA_L(int N, T1 a[], T2 b[], void *mem = wmem){
  int i;
  pair<T1, T2> *arr;
  walloc1d(&arr, N, &mem);
  for(i=(0);i<(N);i++){
    arr[i].first = a[i];
    arr[i].second = b[i];
  }
  sort(arr, arr+N);
  for(i=(0);i<(N);i++){
    a[i] = arr[i].first;
    b[i] = arr[i].second;
  }
}
template<class T1, class T2, class T3> void sortA_L(int N, T1 a[], T2 b[], T3 c[], void *mem = wmem){
  int i;
  pair<T1, pair<T2, T3> > *arr;
  walloc1d(&arr, N, &mem);
  for(i=(0);i<(N);i++){
    arr[i].first = a[i];
    arr[i].second.first = b[i];
    arr[i].second.second = c[i];
  }
  sort(arr, arr+N);
  for(i=(0);i<(N);i++){
    a[i] = arr[i].first;
    b[i] = arr[i].second.first;
    c[i] = arr[i].second.second;
  }
}
inline int my_getchar_unlocked(){
  static char buf[1048576];
  static int s = 1048576;
  static int e = 1048576;
  if(s == e && e == 1048576){
    e = fread_unlocked(buf, 1, 1048576, stdin);
    s = 0;
  }
  if(s == e){
    return EOF;
  }
  return buf[s++];
}
inline void rd(int &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = my_getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = my_getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
inline void rd(char &c){
  int i;
  for(;;){
    i = my_getchar_unlocked();
    if(i!=' '&&i!='\n'&&i!='\r'&&i!='\t'&&i!=EOF){
      break;
    }
  }
  c = i;
}
inline int rd(char c[]){
  int i;
  int sz = 0;
  for(;;){
    i = my_getchar_unlocked();
    if(i!=' '&&i!='\n'&&i!='\r'&&i!='\t'&&i!=EOF){
      break;
    }
  }
  c[sz++] = i;
  for(;;){
    i = my_getchar_unlocked();
    if(i==' '||i=='\n'||i=='\r'||i=='\t'||i==EOF){
      break;
    }
    c[sz++] = i;
  }
  c[sz]='\0';
  return sz;
}
inline int rd_int(void){
  int x;
  rd(x);
  return x;
}
struct MY_WRITER{
  char buf[1048576];
  int s;
  int e;
  MY_WRITER(){
    s = 0;
    e = 1048576;
  }
  ~MY_WRITER(){
    if(s){
      fwrite_unlocked(buf, 1, s, stdout);
    }
  }
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar_unlocked(int a){
  if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
    fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
    MY_WRITER_VAR.s = 0;
  }
  MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
  my_putchar_unlocked(a);
}
inline void wt_L(int x){
  int s=0;
  int m=0;
  char f[10];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    my_putchar_unlocked('-');
  }
  while(s--){
    my_putchar_unlocked(f[s]+'0');
  }
}
inline void wt_L(const char c[]){
  int i=0;
  for(i=0;c[i]!='\0';i++){
    my_putchar_unlocked(c[i]);
  }
}
template<class S> inline void arrInsert(const int k, int &sz, S a[], const S aval){
  int i;
  sz++;
  for(i=sz-1;i>k;i--){
    a[i] = a[i-1];
  }
  a[k] = aval;
}
template<class S, class T> inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[], const T bval){
  int i;
  sz++;
  for(i=sz-1;i>k;i--){
    a[i] = a[i-1];
  }
  for(i=sz-1;i>k;i--){
    b[i] = b[i-1];
  }
  a[k] = aval;
  b[k] = bval;
}
template<class S, class T, class U> inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[], const T bval, U c[], const U cval){
  int i;
  sz++;
  for(i=sz-1;i>k;i--){
    a[i] = a[i-1];
  }
  for(i=sz-1;i>k;i--){
    b[i] = b[i-1];
  }
  for(i=sz-1;i>k;i--){
    c[i] = c[i-1];
  }
  a[k] = aval;
  b[k] = bval;
  c[k] = cval;
}
template<class S, class T, class U, class V> inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[], const T bval, U c[], const U cval, V d[], const V dval){
  int i;
  sz++;
  for(i=sz-1;i>k;i--){
    a[i] = a[i-1];
  }
  for(i=sz-1;i>k;i--){
    b[i] = b[i-1];
  }
  for(i=sz-1;i>k;i--){
    c[i] = c[i-1];
  }
  for(i=sz-1;i>k;i--){
    d[i] = d[i-1];
  }
  a[k] = aval;
  b[k] = bval;
  c[k] = cval;
  d[k] = dval;
}
template<class S, class T> inline S chmin(S &a, T b){
  if(a>b){
    a=b;
  }
  return a;
}
int X;
int Y;
char D;
int sz[4];
int x[4][200000];
int y[4][200000];
int res = 1073709056;
int s;
int d[200000];
int v[200000];
int t[200000];
void solve(){
  int i;
  sortA_L(s, t, v, d);
  for(i=(1);i<(s);i++){
    if(d[i-1]==1 && d[i]==-1 && t[i-1]==t[i]){
      chmin(res, (v[i] - v[i-1]) * 5);
    }
  }
}
int main(){
  int Q5VJL1cS, i;
  wmem = memarr;
  int e98WHCEY = rd_int();
  for(Q5VJL1cS=(0);Q5VJL1cS<(e98WHCEY);Q5VJL1cS++){
    rd(X);
    rd(Y);
    rd(D);
    if(D=='U'){
      D = 0;
    }
    if(D=='R'){
      D = 1;
    }
    if(D=='D'){
      D = 2;
    }
    if(D=='L'){
      D = 3;
    }
    arrInsert(sz[D], sz[D], x[D], X, y[D], Y);
  }
  s = 0;
  for(i=(0);i<(sz[0]);i++){
    arrInsert(s, s, d, +1, v, y[0][i], t, x[0][i]);
  }
  for(i=(0);i<(sz[2]);i++){
    arrInsert(s, s, d, -1, v, y[2][i], t, x[2][i]);
  }
  solve();
  s = 0;
  for(i=(0);i<(sz[1]);i++){
    arrInsert(s, s, d, +1, v, x[1][i], t, y[1][i]);
  }
  for(i=(0);i<(sz[3]);i++){
    arrInsert(s, s, d, -1, v, x[3][i], t, y[3][i]);
  }
  solve();
  s = 0;
  for(i=(0);i<(sz[0]);i++){
    arrInsert(s, s, d, +1, v, y[0][i]-x[0][i], t, y[0][i]+x[0][i]);
  }
  for(i=(0);i<(sz[1]);i++){
    arrInsert(s, s, d, -1, v, y[1][i]-x[1][i], t, y[1][i]+x[1][i]);
  }
  solve();
  s = 0;
  for(i=(0);i<(sz[0]);i++){
    arrInsert(s, s, d, +1, v, y[0][i]+x[0][i], t, y[0][i]-x[0][i]);
  }
  for(i=(0);i<(sz[3]);i++){
    arrInsert(s, s, d, -1, v, y[3][i]+x[3][i], t, y[3][i]-x[3][i]);
  }
  solve();
  s = 0;
  for(i=(0);i<(sz[2]);i++){
    arrInsert(s, s, d, +1, v, -y[2][i]-x[2][i], t, y[2][i]-x[2][i]);
  }
  for(i=(0);i<(sz[1]);i++){
    arrInsert(s, s, d, -1, v, -y[1][i]-x[1][i], t, y[1][i]-x[1][i]);
  }
  solve();
  s = 0;
  for(i=(0);i<(sz[2]);i++){
    arrInsert(s, s, d, +1, v, -y[2][i]+x[2][i], t, y[2][i]+x[2][i]);
  }
  for(i=(0);i<(sz[3]);i++){
    arrInsert(s, s, d, -1, v, -y[3][i]+x[3][i], t, y[3][i]+x[3][i]);
  }
  solve();
  if(res==1073709056){
    wt_L("SAFE");
    wt_L('\n');
  }
  else{
    wt_L(res);
    wt_L('\n');
  }
  return 0;
}
// cLay varsion 20200509-1

// --- original code ---
// int X, Y; char D;
// int sz[4], x[4][2d5], y[4][2d5];
// 
// int res = int_inf;
// int s, d[2d5], v[2d5], t[2d5];
// 
// void solve(){
//   sortA(s, t, v, d);
//   rep(i,1,s) if(d[i-1]==1 && d[i]==-1 && t[i-1]==t[i]) res <?= (v[i] - v[i-1]) * 5;
// }
// 
// {
//   REP(rd_int()){
//     rd(X,Y,D);
//     if(D=='U') D = 0;
//     if(D=='R') D = 1;
//     if(D=='D') D = 2;
//     if(D=='L') D = 3;
//     arrInsert(sz[D], sz[D], x[D], X, y[D], Y);
//   }
// 
//   s = 0;
//   rep(i,sz[0]) arrInsert(s, s, d, +1, v, y[0][i], t, x[0][i]);
//   rep(i,sz[2]) arrInsert(s, s, d, -1, v, y[2][i], t, x[2][i]);
//   solve();
// 
//   s = 0;
//   rep(i,sz[1]) arrInsert(s, s, d, +1, v, x[1][i], t, y[1][i]);
//   rep(i,sz[3]) arrInsert(s, s, d, -1, v, x[3][i], t, y[3][i]);
//   solve();
// 
//   s = 0;
//   rep(i,sz[0]) arrInsert(s, s, d, +1, v, y[0][i]-x[0][i], t, y[0][i]+x[0][i]);
//   rep(i,sz[1]) arrInsert(s, s, d, -1, v, y[1][i]-x[1][i], t, y[1][i]+x[1][i]);
//   solve();
// 
//   s = 0;
//   rep(i,sz[0]) arrInsert(s, s, d, +1, v, y[0][i]+x[0][i], t, y[0][i]-x[0][i]);
//   rep(i,sz[3]) arrInsert(s, s, d, -1, v, y[3][i]+x[3][i], t, y[3][i]-x[3][i]);
//   solve();
// 
//   s = 0;
//   rep(i,sz[2]) arrInsert(s, s, d, +1, v, -y[2][i]-x[2][i], t, y[2][i]-x[2][i]);
//   rep(i,sz[1]) arrInsert(s, s, d, -1, v, -y[1][i]-x[1][i], t, y[1][i]-x[1][i]);
//   solve();
// 
//   s = 0;
//   rep(i,sz[2]) arrInsert(s, s, d, +1, v, -y[2][i]+x[2][i], t, y[2][i]+x[2][i]);
//   rep(i,sz[3]) arrInsert(s, s, d, -1, v, -y[3][i]+x[3][i], t, y[3][i]+x[3][i]);
//   solve();
// 
//   wt(if[res==int_inf, "SAFE", res]);
// }


Measured memory_bytes: 10752