#include <bits/stdc++.h>
#define rep(i,n) for (ll i = 0; i < n; ++i)
using namespace std;
typedef long long ll;
typedef pair<ll,int> P;
const ll INF=1e18;

ll base=200005;
int main() {
  ll n;
  cin>>n;
  vector<vector<ll>> U(400010);
  vector<vector<ll>>  D(400010);
  vector<vector<ll>>  L(400010);
  vector<vector<ll>>  R(400010);

  vector<vector<ll>> U1(400010);
  vector<vector<ll>>  D1(400010);
  vector<vector<ll>>  L1(400010);
  vector<vector<ll>>  R1(400010);

  vector<vector<ll>> U2(400010);
  vector<vector<ll>>  D2(400010);
  vector<vector<ll>>  L2(400010);
  vector<vector<ll>>  R2(400010);




  rep(i,n){
    ll x1,y1;
    string s;
    cin>>x1>>y1>>s;
    ll k=y1-x1;
    ll k1=y1+x1;
    k+=base;
    if (s=="U"){
      U[k].push_back(x1);
      U1[k1].push_back(x1);
      U2[x1].push_back(y1);

    }
    if (s=="R"){
      R[k].push_back(x1);
      R1[k1].push_back(x1);
      R2[y1].push_back(x1);

    }
    if (s=="D"){
      D[k].push_back(x1);
      D1[k1].push_back(x1);
      D2[x1].push_back(y1);


    }
    if (s=="L"){
      L[k].push_back(x1);
      L1[k1].push_back(x1);
      L2[y1].push_back(x1);

    }
  }
ll ans=INF;
  rep(i,400010){
    ll u,d,l,r,u1,d1,l1,r1,u2,d2,l2,r2;
    u=U[i].size();
    d=D[i].size();
    l=L[i].size();
    r=R[i].size();
    u1=U1[i].size();
    d1=D1[i].size();
    l1=L1[i].size();
    r1=R1[i].size();
    u2=U2[i].size();
    d2=D2[i].size();
    l2=L2[i].size();
    r2=R2[i].size();
  //  cout<<U[200005][0]<<endl;


    if (u!=0 and l!=0){
      vector<P> v;
      rep(j,u){
        v.push_back(make_pair(U[i][j],0));
      }
      rep(j,l){
        v.push_back(make_pair(L[i][j],1));
      }
      sort(v.begin(),v.end());
      rep(j,u+l-1){
        if (v[j].second==0 and v[j+1].second==1){
          ans=min(ans,(v[j+1].first-v[j].first)*10);
    //      cout<<"ul"<<endl;
        }
      }
    }

    if (r!=0 and d!=0){
      vector<P> v;
      rep(j,r){
        v.push_back(make_pair(R[i][j], 0));
      }
      rep(j,d){
        v.push_back(make_pair(D[i][j], 1));
      }
      sort(v.begin(),v.end());
      rep(j,r+d-1){
        if (v[j].second==0 and v[j+1].second==1){
          ans=min(ans,(v[j+1].first-v[j].first)*10);
      //    cout<<"rd"<<endl;

        }
      }
    }


    if (r1!=0 and u1!=0){
      vector<P> v;
      rep(j,r1){
        v.push_back(make_pair(R1[i][j], 0));
      }
      rep(j,u1){
        v.push_back(make_pair(U1[i][j], 1));
      }
      sort(v.begin(),v.end());
      rep(j,r1+u1-1){
        if (v[j].second==0 and v[j+1].second==1){
          ans=min(ans,(v[j+1].first-v[j].first)*10);
      //    cout<<"RU"<<endl;

        }
      }
    }

    if (d1!=0 and l1!=0){
      vector<P> v;
      rep(j,d1){
        v.push_back(make_pair(D1[i][j], 0));
      }
      rep(j,l1){
        v.push_back(make_pair(L1[i][j], 1));
      }
      sort(v.begin(),v.end());
      rep(j,d1+l1-1){
        if (v[j].second==0 and v[j+1].second==1){
          ans=min(ans,(v[j+1].first-v[j].first)*10);
      //    cout<<"DL"<<endl;

        }
      }
    }

    if (u2!=0 and d2!=0){
      vector<P> v;
      rep(j,u2){
        v.push_back(make_pair(U2[i][j], 0));
      }
      rep(j,d2){
        v.push_back(make_pair(D2[i][j], 1));
      }
      sort(v.begin(),v.end());
      rep(j,u2+d2-1){
        if (v[j].second==0 and v[j+1].second==1){
          ans=min(ans,(v[j+1].first-v[j].first)*5);
      //    cout<<"ud"<<endl;

        }
      }
    }

    if (l2!=0 and r2!=0){
      vector<P> v;
      rep(j,r2){
        v.push_back(make_pair(R2[i][j], 0));
      }
      rep(j,l2){
        v.push_back(make_pair(L2[i][j], 1));
      }
      sort(v.begin(),v.end());
      rep(j,l2+r2-1){
        if (v[j].second==0 and v[j+1].second==1){
          ans=min(ans,(v[j+1].first-v[j].first)*5);
      //    cout<<"lr"<<endl;

        }
      }
    }




  }
if (ans==INF)cout<<"SAFE";
else cout<<ans;
//else cout<<ll(ans*10);


}


Measured memory_bytes: 134396