Language: C++

#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <numeric>
#include <map>
#include <queue>
#include <deque>
#include <set>
#define rep(i,n) for (ll i = 0; i < (n); ++i)
#define FOR(i,a,b) for (ll i = (a) ; i <= (b); ++i)
#define RFOR(i,a,b) for (ll i = (a) ; i >= (b); --i)
#define all(x) (x).begin(), (x).end()
//#define repitr(itr, st) for(auto itr = st.begin(); itr != st.end(); ++itr)
#define P_B push_back
#define PO_B pop_back
#define E_B emplace_back
#define M_P make_pair
#define fi first
#define se second
#define debug(x) cout << #x << ": " << x << endl
//#define print(x) printf("%d\n", x)

using namespace std;
using ll = long long;
using Pii = pair<int,int>;
using Pll = pair<ll,ll>;
using VI = vector<int>;
using VL = vector<ll>;
using VVI = vector<vector<int> >;
using VVL = vector<vector<ll> >;
//int dx[4] = {1, 0, -1, 0};
//int dy[4] = {0, 1, 0, -1};
const ll LINF = (ll)1e18 - 1;
//const int INF = 1e9 - 1;
//int:±2*10^9くらい　ll:±9*10^18くらい



void Main(){
	int n;
	ll ans = LINF;
	cin >> n;
	VLL U, R, D, L;
	rep(i, n){
		ll x, y;
		string u;
		cin >> x >> y >> u;
		if(u[0] == 'U') U.P_B(VL{x,y});
		if(u[0] == 'R') R.P_B(VL{x,y});
		if(u[0] == 'D') D.P_B(VL{x,y});
		if(u[0] == 'L') L.P_B(VL{x,y});
	}

	VLL dl;
	rep(i, D.size()){
		dl[D[0]+D[1]].P_B(D[0]);
	}
	rep(i, L.size()){
		if(dl[L[0]+L[1]].empty()) continue;
		rep(j, dl[L[0]+L[1]].size()){
			if(dl[L[0]+L[1]][j] < L[0]) ans = min(ans, (L[0] - dl[L[0]+L[1]][j])*10);
		}
	}

	VLL dr;
	rep(i, D.size()){
		dr[D[0]-D[1]].P_B(D[0]);
	}
	rep(i, R.size()){
		if(dr[R[0]-R[1]].empty()) continue;
		rep(j, dr[R[0]-R[1]].size()){
			if(dr[R[0]-R[1]][j] > R[0]) ans = min(ans, (dr[R[0]-R[1]][j] - R[0])*10);
		}
	}

	VLL ur;
	rep(i, U.size()){
		ur[U[0]+U[1]].P_B(U[0]);
	}
	rep(i, R.size()){
		if(ur[R[0]+R[1]].empty()) continue;
		rep(j, ur[R[0]+R[1]].size()){
			if(ur[R[0]+R[1]][j] > R[0]) ans = min(ans, (ur[R[0]+R[1]][j] - R[0])*10);
		}
	}

	VLL ul;
	rep(i, U.size()){
		ul[U[0]-U[1]].P_B(U[0]);
	}
	rep(i, L.size()){
		if(ul[L[0]-L[1]].empty()) continue;
		rep(j, ul[L[0]-L[1]].size()){
			if(ul[L[0]-L[1]][j] < L[0]) ans = min(ans, (L[0] - ul[L[0]-L[1]][j])*10);
		}
	}

	VLL ud;
	rep(i, U.size()){
		ud[U[0]].P_B(U[1]);
	}
	rep(i, D.size()){
		if(ud[D[0]].empty()) continue;
		rep(j, ud[D[0]].size()){
			if(ud[D[0]][j] < D[1]) ans = min(ans, (D[1] - ud[D[0]][j])*5);
		}
	}

	VLL lr;
	rep(i, R.size()){
		lr[R[1]].P_B(R[0]);
	}
	rep(i, L.size()){
		if(lr[L[1]].empty()) continue;
		rep(j, lr[L[1]].size()){
			if(lr[L[1]][j] < L[0]) ans = min(ans, (L[0] - lr[L[1]][j])*5);
		}
	}

	if(ans != LINF) cout << ans << endl;
	else cout << "SAFE" << endl;
}

int main()
{
	cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
	cout << fixed << setprecision(15);
	Main();

  return 0;
}


Compiler output:
a.cc: In function 'void Main()':
a.cc:47:9: error: 'VLL' was not declared in this scope; did you mean 'VVL'?
   47 |         VLL U, R, D, L;
      |         ^~~
      |         VVL
a.cc:52:33: error: 'U' was not declared in this scope
   52 |                 if(u[0] == 'U') U.P_B(VL{x,y});
      |                                 ^
a.cc:53:33: error: 'R' was not declared in this scope
   53 |                 if(u[0] == 'R') R.P_B(VL{x,y});
      |                                 ^
a.cc:54:33: error: 'D' was not declared in this scope
   54 |                 if(u[0] == 'D') D.P_B(VL{x,y});
      |                                 ^
a.cc:55:33: error: 'L' was not declared in this scope
   55 |                 if(u[0] == 'L') L.P_B(VL{x,y});
      |                                 ^
a.cc:58:12: error: expected ';' before 'dl'
   58 |         VLL dl;
      |            ^~~
      |            ;
a.cc:59:16: error: 'D' was not declared in this scope
   59 |         rep(i, D.size()){
      |                ^
a.cc:13:38: note: in definition of macro 'rep'
   13 | #define rep(i,n) for (ll i = 0; i < (n); ++i)
      |                                      ^
a.cc:60:17: error: 'dl' was not declared in this scope; did you mean 'll'?
   60 |                 dl[D[0]+D[1]].P_B(D[0]);
      |                 ^~
      |                 ll
a.cc:62:16: error: 'L' was not declared in this scope
   62 |         rep(i, L.size()){
      |                ^
a.cc:13:38: note: in definition of macro 'rep'
   13 | #define rep(i,n) for (ll i = 0; i < (n); ++i)
      |                                      ^
a.cc:63:20: error: 'dl' was not declared in this scope; did you mean 'll'?
   63 |                 if(dl[L[0]+L[1]].empty()) continue;
      |                    ^~
      |                    ll
a.cc:64:24: error: 'dl' was not declared in this scope; did you mean 'll'?
   64 |                 rep(j, dl[L[0]+L[1]].size()){
      |                        ^~
a.cc:13:38: note: in definition of macro 'rep'
   13 | #define rep(i,n) for (ll i = 0; i < (n); ++i)
      |                                      ^
a.cc:69:12: error: expected ';' before 'dr'
   69 |         VLL dr;
      |            ^~~
      |            ;
a.cc:70:16: error: 'D' was not declared in this scope
   70 |         rep(i, D.size()){
      |                ^
a.cc:13:38: note: in definition of macro 'rep'
   13 | #define rep(i,n) for (ll i = 0; i < (n); ++i)
      |                                      ^
a.cc:71:17: error: 'dr' was not declared in this scope
   71 |                 dr[D[0]-D[1]].P_B(D[0]);
      |                 ^~
a.cc:73:16: error: 'R' was not declared in this scope
   73 |         rep(i, R.size()){
      |                ^
a.cc:13:38: note: in definition of macro 'rep'
   13 | #define rep(i,n) for (ll i = 0; i < (n); ++i)
      |                                      ^
a.cc:74:20: error: 'dr' was not declared in this scope
   74 |                 if(dr[R[0]-R[1]].empty()) continue;
      |                    ^~
a.cc:75:24: error: 'dr' was not declared in this scope
   75 |                 rep(j, dr[R[0]-R[1]].size()){
      |                        ^~
a.cc:13:38: note: in definition of macro 'rep'
   13 | #define rep(i,n) for (ll i = 0; i < (n); ++i)
      |                                      ^
a.cc:80:12: error: expected ';' before 'ur'
   80 |         VLL ur;
      |            ^~~
      |            ;
a.cc:81:16: error: 'U' was not declared in this scope
   81 |         rep(i, U.size()){
      |                ^
a.cc:13:38: note: in definition of macro 'rep'
   13 | #define rep(i,n) for (ll i = 0; i < (n); ++i)
      |                                      ^
a.cc:82:17: error: 'ur' was not declared in this scope
   82 |                 ur[U[0]+U[1]].P_B(U[0]);
      |                 ^~
a.cc:84:16: error: 'R' was not declared in this scope
   84 |         rep(i, R.size()){
      |                ^
a.cc:13:38: note: in definition of macro 'rep'
   13 | #define rep(i,n) for (ll i = 0; i < (n); ++i)
      |                                      ^
a.cc:85:20: error: 'ur' was not declared in this scope
   85 |                 if(ur[R[0]+R[1]].empty()) continue;
      |                    ^~
a.cc:86:24: error: 'ur' was not declared in this scope
   86 |                 rep(j, ur[R[0]+R[1]].size()){
      |                        ^~
a.cc:13:38: note: in definition of macro 'rep'
   13 | #define rep(i,n) for (ll i = 0; i < (n); ++i)
      |                                      ^
a.cc:91:12: error: expected ';' before 'ul'
   91 |         VLL ul;
      |            ^~~
      |            ;
a.cc:92:16: error: 'U' was not declared in this scope
   92 |         rep(i, U.size()){
      |                ^
a.cc:13:38: note: in definition of macro 'rep'
   13 | #define rep(i,n) for (ll i = 0; i < (n); ++i)
      |                                      ^
a.cc:93:17: error: 'ul' was not declared in this scope; did you mean 'll'?
   93 |                 ul[U[0]-U[1]].P_B(U[0]);
      |                 ^~
      |                 ll
a.cc:95:16: error: 'L' was not declared in this scope
   95 |         rep(i, L.size()){
      |                ^
a.cc:13:38: note: in definition of macro 'rep'
   13 | #define rep(i,n) for (ll i = 0; i < (n); ++i)
      |                                      ^
a.cc:96:20: error: 'ul' was not declared in this scope; did you mean 'll'?
   96 |                 if(ul[L[0]-L[1]].empty()) continue;
      |                    ^~
      |                    ll
a.cc:97:24: error: 'ul' was not declared in this scope; did you mean 'll'?
   97 |                 rep(j, ul[L[0]-L[1]].size()){
      |                        ^~
a.cc:13:38: note: in definition of macro 'rep'
   13 | #define rep(i,n) for (ll i = 0; i < (n); ++i)
      |                                      ^
a.cc:102:12: error: expected ';' before 'ud'
  102 |         VLL ud;
      |            ^~~
      |            ;
a.cc:103:16: error: 'U' was not declared in this scope
  103 |         rep(i, U.size()){
      |                ^
a.cc:13:38: note: in definition of macro 'rep'
   13 | #define rep(i,n) for (ll i = 0; i < (n); ++i)
      |                                      ^
a.cc:104:17: error: 'ud' was not declared in this scope
  104 |                 ud[U[0]].P_B(U[1]);
      |                 ^~
a.cc:106:16: error: 'D' was not declared in this scope
  106 |         rep(i, D.size()){
      |                ^
a.cc:13:38: note: in definition of macro 'rep'
   13 | #define rep(i,n) for (ll i = 0; i < (n); ++i)
      |                                      ^
a.cc:107:20: error: 'ud' was not declared in this scope
  107 |                 if(ud[D[0]].empty()) continue;
      |                    ^~
a.cc:108:24: error: 'ud' was not declared in this scope
  108 |                 rep(j, ud[D[0]].size()){
      |                        ^~
a.cc:13:38: note: in definition of macro 'rep'
   13 | #define rep(i,n) for (ll i = 0; i < (n); ++i)
      |                                      ^
a.cc:113:12: error: expected ';' before 'lr'
  113 |         VLL lr;
      |            ^~~
      |            ;
a.cc:114:16: error: 'R' was not declared in this scope
  114 |         rep(i, R.size()){
      |                ^
a.cc:13:38: note: in definition of macro 'rep'
   13 | #define rep(i,n) for (ll i = 0; i < (n); ++i)
      |                                      ^
a.cc:115:17: error: 'lr' was not declared in this scope; did you mean 'll'?
  115 |                 lr[R[1]].P_B(R[0]);
      |                 ^~
      |                 ll
a.cc:117:16: error: 'L' was not declared in this scope
  117 |         rep(i, L.size()){
      |                ^
a.cc:13:38: note: in definition of macro 'rep'
   13 | #define rep(i,n) for (ll i = 0; i < (n); ++i)
      |                                      ^
a.cc:118:20: error: 'lr' was not declared in this scope; did you mean 'll'?
  118 |                 if(lr[L[1]].empty()) continue;
      |                    ^~
      |                    ll
a.cc:119:24: error: 'lr' was not declared in this scope; did you mean 'll'?
  119 |                 rep(j, lr[L[1]].size()){
      |                        ^~
a.cc:13:38: note: in definition of macro 'rep'
   13 | #define rep(i,n) for (ll i = 0; i < (n); ++i)
      |                                      ^