#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcountll

#define INF 1e18
#define mod 1000000007

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll N;
  cin>>N;
  vector<ll> X(N),Y(N),K(N);
  rep(i,N){
    char c;
    cin>>X[i]>>Y[i]>>c;
    if(c=='R')K[i]=0;
    if(c=='U')K[i]=1;
    if(c=='L')K[i]=2;
    if(c=='D')K[i]=3;
  }

  ll res=INF;

  {
    vector<ll> keys;
    rep(i,N){
      keys.push_back(Y[i]-X[i]);
    }
    sort(all(keys)); uni(keys);

    map<ll,vector<ll>> xs[4];
    rep(i,N){
      xs[K[i]][Y[i]-X[i]].push_back(X[i]);
    }

    for(ll k : keys){
      vector<ll> nids[4];
      rep(dir,4){
        vector<ll> tmp=xs[dir][k];
        sort(all(tmp));
        nids[dir]=tmp;
      }

      rep(i,nids[1].size()){
        ll x=nids[1][i];
        auto it=lower_bound(all(nids[2]),x);
        if(it==nids[2].end())continue;
        minch(res,(*it-x)*10);
      }
      rep(i,nids[0].size()){
        ll x=nids[0][i];
        auto it=lower_bound(all(nids[3]),x);
        if(it==nids[3].end())continue;
        minch(res,(*it-x)*10);
      }
    }
  }

  {
    vector<ll> keys;
    rep(i,N){
      keys.push_back(Y[i]+X[i]);
    }
    sort(all(keys)); uni(keys);

    map<ll,vector<ll>> xs[4];
    rep(i,N){
      xs[K[i]][Y[i]+X[i]].push_back(X[i]);
    }

    for(ll k : keys){
      vector<ll> nids[4];
      rep(dir,4){
        vector<ll> tmp=xs[dir][k];
        sort(all(tmp));
        nids[dir]=tmp;
      }

      rep(i,nids[0].size()){
        ll x=nids[0][i];
        auto it=lower_bound(all(nids[1]),x);
        if(it==nids[1].end())continue;
        minch(res,(*it-x)*10);
      }
      rep(i,nids[3].size()){
        ll x=nids[3][i];
        auto it=lower_bound(all(nids[2]),x);
        if(it==nids[2].end())continue;
        minch(res,(*it-x)*10);
      }
    }
  }

  {
    vector<ll> keys;
    rep(i,N){
      keys.push_back(X[i]);
    }
    sort(all(keys)); uni(keys);

    map<ll,vector<ll>> xs[4];
    rep(i,N){
      xs[K[i]][X[i]].push_back(Y[i]);
    }

    for(ll k : keys){
      vector<ll> nids[4];
      rep(dir,4){
        vector<ll> tmp=xs[dir][k];
        sort(all(tmp));
        nids[dir]=tmp;
      }

      rep(i,nids[1].size()){
        ll x=nids[1][i];
        auto it=lower_bound(all(nids[3]),x);
        if(it==nids[3].end())continue;
        minch(res,(*it-x)*5);
      }
    }
  }

  {
    vector<ll> keys;
    rep(i,N){
      keys.push_back(Y[i]);
    }
    sort(all(keys)); uni(keys);

    map<ll,vector<ll>> xs[4];
    rep(i,N){
      xs[K[i]][Y[i]].push_back(X[i]);
    }

    for(ll k : keys){
      vector<ll> nids[4];
      rep(dir,4){
        vector<ll> tmp=xs[dir][k];
        sort(all(tmp));
        nids[dir]=tmp;
      }

      rep(i,nids[0].size()){
        ll x=nids[0][i];
        auto it=lower_bound(all(nids[2]),x);
        if(it==nids[2].end())continue;
        minch(res,(*it-x)*5);
      }
    }
  }

  if(res==INF)cout<<"SAFE"<<endl;
  else cout<<res<<endl;
  return 0;
}

#include<bits/stdc++.h>
using namespace std;
const int MAX=100000+5;

int n;
pair<int,int> coord[MAX];
vector<int> par(MAX,0);
vector<pair<int,pair<int,int> > > edges;
int root(int x)
{
	while(par[x]!=x)
	{	
		par[x]=par[par[x]];
		x=par[x];
	}
	return x;
}

void union1(int x,int y)
{
	int u=root(x);
	int v=root(y);
	par[u]=v;
}
int main()
{
	cin>>n;
	vector<pair<int,int> > xc;
	vector<pair<int,int> > yc;

	for(int i=1;i<=n;i++)
	{
		int x,y;
		cin>>x>>y;
		coord[i]={x,y};
		xc.push_back({x,i});
		yc.push_back({y,i});
	}
	for(int i=1;i<=n;i++)
	{
		par[i]=i;
	}
	sort(xc.begin(),xc.end());
	sort(yc.begin(),yc.end());
	for(int i=0;i<n-1;i++)
	{
		edges.push_back({abs(xc[i].first-xc[i+1].first),{xc[i].second,xc[i+1].second}});
	}
	for(int i=0;i<n-1;i++)
	{
		edges.push_back({abs(yc[i].first-yc[i+1].first),{yc[i].second,yc[i+1].second}});
	}
	long long int sum=0;
	//kruskal
	for(int i=1;i<=n;i++)
		par[i]=i;
	sort(edges.begin(),edges.end());
	for(int i=0;i<edges.size();i++)
	{
		int w=edges[i].first;
		int u=edges[i].second.first;
		int v=edges[i].second.second;
		if(root(u)!=root(v))
		{
			union1(u,v);
			sum+=w;
		}
	}
	cout<<sum<<endl;
}