Language: C++

#include<bits/stdc++.h>
using namespace std;
#define FO(x) {freopen(#x".in","r",stdin);freopen(#x".out","w",stdout);}
#define pa pair<int,int>
#define mod 998244353
#define ll long long
#define mk make_pair
#define pb push_back
#define fi first
#define se second
#define cl(x) memset(x,0,sizeof x)
#ifdef Devil_Gary
#define bug(x) cout<<(#x)<<" "<<(x)<<endl
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define bug(x)
#define debug(...)
#endif

const int INF = INT_MAX/4;
const int N=2e5+5;
/*
char *TT,*mo,but[(1<<15)+2];
#define getchar() ((TT==mo&&(mo=(TT=but)+fread(but,1,1<<15,stdin),TT==mo))?-1:*TT++)//*/
inline int read(){
    int x=0,rev=0,ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')rev=1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
    return rev?-x:x;
}
struct data{
	int x,y,z;
}a[N];
int t[N],flg,lst,lstu,lstr,lstd;
int ans=INF,n;
bool cmp1(data i,data j){
	
	if(i.x==j.x) return i.y<j.y; 
	return i.x<j.x;
}
bool cmp2(data i,data j){
//	if()
	if(i.y==j.y) return i.x<j.x; 
	return i.y<j.y;
}
bool cmp3(data i,data j){
	
	if(i.x+i.y==j.x+j.y) return i.x<j.x; 
	return i.x+i.y<j.x+j.y;
}
bool cmp4(data i,data j){
	
	if(i.x-i.y==j.x-j.y) return i.x<j.x; 
	return i.x-i.y<j.x-j.y;
}

int main(){
#ifdef Devil_Gary
    freopen("in.txt","r",stdin);
//  freopen("out.txt","w",stdout);
#endif	
	n=read();
	for(int i=1,x,y,z;i<=n;i++){
		char s[10];
		x=read(),y=read();cin>>s;
		if(s[0]=='U') z=1;
		if(s[0]=='D') z=2;
		if(s[0]=='L') z=3;
		if(s[0]=='R') z=4;
		a[i]=data{x,y,z};
//		a[x].pb(data{x,y,z}),d[y].pb(data{x,y,z});
//		c[x+y].pb(data{x,y,z}),d[x-y+M].pb(data{x,y,z})
//		a[i]=data{x,y,z};
		
	}
	sort(a+1,a+n+1,cmp1);
//	sort(a+1,a+n+1,cmp1);
	
	for(int i=1;i<=n;i++){
		if(i==1||a[i].x!=a[i-1].x){
			flg++;
		}
		if(a[i].z==3||a[i].z==4) continue;
		if(a[i].z==1){
			lst=i,t[lst]=flg;
		}
		if(a[i].z==2){
			if(t[lst]!=flg) continue; 
			ans=min(ans,(a[i].y-a[lst].y)*5);
		}
	}
	
	sort(a+1,a+n+1,cmp2);
	for(int i=1;i<=n;i++){
		if(i==1||a[i].y!=a[i-1].y){
			flg++;
		}
		if(a[i].z==1||a[i].z==2) continue;
		if(a[i].z==4){
			lst=i,t[lst]=flg;
		}
		if(a[i].z==3){
			if(t[lst]!=flg) continue; 
			ans=min(ans,(a[i].x-a[lst].x)*5);
		}
	}
	
	sort(a+1,a+n+1,cmp3);
	for(int i=1;i<=n;i++){
		if(i==1||a[i].y+a[i].x!=a[i-1].y+a[i-1].x){
			flg++;
		}
//		if(a[i].z==3||a[i].z==4) continue;
		if(a[i].z==2){
			lstd=i,t[lstd]=flg;
		}
		if(a[i].z==4){
			lstr=i,t[lstr]=flg;
		}
		if(a[i].z==1){
			if(t[lstr]!=flg) continue; 
			ans=min(ans,(a[i].x-a[lstr].x)*10);
		}
		if(a[i].z==3){
			if(t[lstd]!=flg) continue; 
			ans=min(ans,(a[i].x-a[lstd].x)*10);
		}
	}
	
	
	sort(a+1,a+n+1,cmp4);
	for(int i=1;i<=n;i++){
		if(i==1||a[i].y-a[i].x!=a[i-1].y-a[i-1].x){
			flg++;
		}
//		if(a[i].z==3||a[i].z==4) continue;
		if(a[i].z==1){
			lstu=i,t[lstu]=flg;
		}
		if(a[i].z==4){
			lstr=i,t[lstr]=flg;
		}
		if(a[i].z==3){
			if(t[lstu]!=flg) continue; 
			ans=min(ans,(a[i].x-a[lstu].x)*10);
		}
		if(a[i].z==2){
			if(t[lstr]!=flg) continue; 
			ans=min((a[i].x-a[lstr].x)*10,ans);
		}
	}
	if(ans!=INF) cout<<ans<<endl;
	else puts("SAFE");
	return 0;
}


Compiler output:
a.cc:36:11: error: reference to 'data' is ambiguous
   36 | bool cmp1(data i,data j){
      |           ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 from a.cc:1:
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:31:8: note:                 'struct data'
   31 | struct data{
      |        ^~~~
a.cc:36:18: error: reference to 'data' is ambiguous
   36 | bool cmp1(data i,data j){
      |                  ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:31:8: note:                 'struct data'
   31 | struct data{
      |        ^~~~
a.cc:36:24: error: expression list treated as compound expression in initializer [-fpermissive]
   36 | bool cmp1(data i,data j){
      |                        ^
a.cc:41:11: error: reference to 'data' is ambiguous
   41 | bool cmp2(data i,data j){
      |           ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:31:8: note:                 'struct data'
   31 | struct data{
      |        ^~~~
a.cc:41:18: error: reference to 'data' is ambiguous
   41 | bool cmp2(data i,data j){
      |                  ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:31:8: note:                 'struct data'
   31 | struct data{
      |        ^~~~
a.cc:41:24: error: expression list treated as compound expression in initializer [-fpermissive]
   41 | bool cmp2(data i,data j){
      |                        ^
a.cc:46:11: error: reference to 'data' is ambiguous
   46 | bool cmp3(data i,data j){
      |           ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:31:8: note:                 'struct data'
   31 | struct data{
      |        ^~~~
a.cc:46:18: error: reference to 'data' is ambiguous
   46 | bool cmp3(data i,data j){
      |                  ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:31:8: note:                 'struct data'
   31 | struct data{
      |        ^~~~
a.cc:46:24: error: expression list treated as compound expression in initializer [-fpermissive]
   46 | bool cmp3(data i,data j){
      |                        ^
a.cc:51:11: error: reference to 'data' is ambiguous
   51 | bool cmp4(data i,data j){
      |           ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:31:8: note:                 'struct data'
   31 | struct data{
      |        ^~~~
a.cc:51:18: error: reference to 'data' is ambiguous
   51 | bool cmp4(data i,data j){
      |                  ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:31:8: note:                 'struct data'
   31 | struct data{
      |        ^~~~
a.cc:51:24: error: expression list treated as compound expression in initializer [-fpermissive]
   51 | bool cmp4(data i,data j){
      |                        ^
a.cc: In function 'int main()':
a.cc:70:22: error: reference to 'data' is ambiguous
   70 |                 a[i]=data{x,y,z};
      |                      ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  3