Language: C++

#include <bits/stdc++.h>
using namespace std;
using Point = pair<int, int>;

struct RightCollision
{
    bool operator()(const Point a, const Point b)
    {
        return make_pair(a.first + a.second, a.first) < make_pair(b.first + b.second, b.first);
    }
};

struct LeftCollision
{
    bool operator()(const Point a, const Point b)
    {
        return make_pair(a.first - a.second, a.first) < make_pair(b.first - b.second, b.first);
    }
};

struct DownCollision
{
    bool operator()(const Point a, const Point b)
    {
        return a < b;
    }
};

set<Point, RightCollision> SR;
set<Point, LeftCollision> SL;
set<Point, DownCollision> SD;
vector<Point> GU, GD, GL, GR;

const int UnReachable = numeric_limits<int>::max();

int Solve()
{
    int result = UnReachable;
    SR.clear();
    SL.clear();
    SD.clear();
    for (auto p : GR)
        SR.insert(p);
    for (auto p : GL)
        SL.insert(p);
    for (auto p : GD)
        SD.insert(p);
    for (auto p : GU)
    {
        {
            auto it = SR.lower_bound(p);
            if (it != SR.begin())
            {
                --it;
                if (it->first + it->second == p.first + p.second)
                    result = min(result, (p.first - it->first) * 10);
            }
        }
        {
            auto it = SL.upper_bound(p);
            if (it != SR.end())
            {
                if (it->first - it->second == p.first - p.second)
                    result = min(result, (it->first - p.first) * 10);
            }
        }
        {
            auto it = SD.upper_bound(p);
            if (it != SD.end())
            {
                if (it->first == p.first)
                    result = min(result, (it->second - p.second) * 5);
            }
        }
    }
    return result;
}

void Rotate()
{
    vector<Point> NU, ND, NL, NR;
    for (auto p : GU)
        NL.push_back(make_pair(-p.second, p.first));
    for (auto p : GL)
        ND.push_back(make_pair(-p.second, p.first));
    for (auto p : GD)
        NR.push_back(make_pair(-p.second, p.first));
    for (auto p : GR)
        NU.push_back(make_pair(-p.second, p.first));
    GU = move(NU);
    GL = move(NL);
    GD = move(ND);
    GR = move(NR);
}

int n, i, x, y, ans;
char c;
int main()
{
    ios::sync_with_stdio(false);
    cin >> n;
    for (i = 1; i <= n; ++i)
    {
        cin >> x >> y >> c;
        (c == 'U' ? GU : c == 'D' ? GD : c == 'L' ? GL : GR).push_back(make_pair(x, y));
    }
    ans = UnReachable;
    ans = min(ans, Solve());
    for (i = 0; i < 3; ++i)
    {
        Rotate();
        ans = min(ans, Solve());
    }
    if (ans == UnReachable)
        cout << "SAFE";
    else
        cout << ans;
}

Compiler output:
In file included from /usr/include/c++/14/map:62,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:152,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_tree.h: In instantiation of 'static const _Key& std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_S_key(_Const_Link_type) [with _Key = std::pair<int, int>; _Val = std::pair<int, int>; _KeyOfValue = std::_Identity<std::pair<int, int> >; _Compare = RightCollision; _Alloc = std::allocator<std::pair<int, int> >; _Const_Link_type = const std::_Rb_tree_node<std::pair<int, int> >*]':
/usr/include/c++/14/bits/stl_tree.h:2116:47:   required from 'std::pair<std::_Rb_tree_node_base*, std::_Rb_tree_node_base*> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_get_insert_unique_pos(const key_type&) [with _Key = std::pair<int, int>; _Val = std::pair<int, int>; _KeyOfValue = std::_Identity<std::pair<int, int> >; _Compare = RightCollision; _Alloc = std::allocator<std::pair<int, int> >; key_type = std::pair<int, int>]'
 2116 |           __comp = _M_impl._M_key_compare(__k, _S_key(__x));
      |                                                ~~~~~~^~~~~
/usr/include/c++/14/bits/stl_tree.h:2169:4:   required from 'std::pair<std::_Rb_tree_iterator<_Val>, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(_Arg&&) [with _Arg = const std::pair<int, int>&; _Key = std::pair<int, int>; _Val = std::pair<int, int>; _KeyOfValue = std::_Identity<std::pair<int, int> >; _Compare = RightCollision; _Alloc = std::allocator<std::pair<int, int> >]'
 2169 |         = _M_get_insert_unique_pos(_KeyOfValue()(__v));
      |           ^~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_set.h:514:25:   required from 'std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(const value_type&) [with _Key = std::pair<int, int>; _Compare = RightCollision; _Alloc = std::allocator<std::pair<int, int> >; typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator = std::_Rb_tree<std::pair<int, int>, std::pair<int, int>, std::_Identity<std::pair<int, int> >, RightCollision, std::allocator<std::pair<int, int> > >::const_iterator; typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other = std::allocator<std::pair<int, int> >; typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key> = __gnu_cxx::__alloc_traits<std::allocator<std::pair<int, int> >, std::pair<int, int> >::rebind<std::pair<int, int> >; typename _Alloc::value_type = std::pair<int, int>; value_type = std::pair<int, int>]'
  514 |           _M_t._M_insert_unique(__x);
      |           ~~~~~~~~~~~~~~~~~~~~~^~~~~
a.cc:43:18:   required from here
   43 |         SR.insert(p);
      |         ~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_tree.h:768:15: error: static assertion failed: comparison object must be invocable as const
  768 |               is_invocable_v<const _Compare&, const _Key&, const _Key&>,
      |               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h:768:15: note: 'std::is_invocable_v<const RightCollision&, const std::pair<int, int>&, const std::pair<int, int>&>' evaluates to false
/usr/include/c++/14/bits/stl_tree.h: In instantiation of 'static const _Key& std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_S_key(_Const_Link_type) [with _Key = std::pair<int, int>; _Val = std::pair<int, int>; _KeyOfValue = std::_Identity<std::pair<int, int> >; _Compare = LeftCollision; _Alloc = std::allocator<std::pair<int, int> >; _Const_Link_type = const std::_Rb_tree_node<std::pair<int, int> >*]':
/usr/include/c++/14/bits/stl_tree.h:2116:47:   required from 'std::pair<std::_Rb_tree_node_base*, std::_Rb_tree_node_base*> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_get_insert_unique_pos(const key_type&) [with _Key = std::pair<int, int>; _Val = std::pair<int, int>; _KeyOfValue = std::_Identity<std::pair<int, int> >; _Compare = LeftCollision; _Alloc = std::allocator<std::pair<int, int> >; key_type = std::pair<int, int>]'
 2116 |           __comp = _M_impl._M_key_compare(__k, _S_key(__x));
      |                                                ~~~~~~^~~~~
/usr/include/c++/14/bits/stl_tree.h:2169:4:   required from 'std::pair<std::_Rb_tree_iterator<_Val>, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(_Arg&&) [with _Arg = const std::pair<int, int>&; _Key = std::pair<int, int>; _Val = std::pair<int, int>; _KeyOfValue = std::_Identity<std::pair<int, int> >; _Compare = LeftCollision; _Alloc = std::allocator<std::pair<int, int> >]'
 2169 |         = _M_get_insert_unique_pos(_KeyOfValue()(__v));
      |           ^~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_set.h:514:25:   required from 'std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(const value_type&) [with _Key = std::pair<int, int>; _Compare = LeftCollision; _Alloc = std::allocator<std::pair<int, int> >; typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator = std::_Rb_tree<std::pair<int, int>, std::pair<int, int>, std::_Identity<std::pair<int, int> >, LeftCollision, std::allocator<std::pair<int, int> > >::const_iterator; typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other = std::allocator<std::pair<int, int> >; typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key> = __gnu_cxx::__alloc_traits<std::allocator<std::pair<int, int> >, std::pair<int, int> >::rebind<std::pair<int, int> >; typename _Alloc::value_type = std::pair<int, int>; value_type = std::pair<int, int>]'
  514 |           _M_t._M_insert_unique(__x);
      |           ~~~~~~~~~~~~~~~~~~~~~^~~~~
a.cc:45:18:   required from here
   45 |         SL.insert(p);
      |         ~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_tree.h:768:15: error: static assertion failed: comparison object must be invocable as const
  768 |               is_invocable_v<const _Compare&, const _Key&, const _Key&>,
      |               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h:768:15: note: 'std::is_invocable_v<const LeftCollision&, const std::pair<int, int>&, const std::pair<int, int>&>' evaluates to false
/usr/include/c++/14/bits/stl_tree.h: In instantiation of 'static const _Key& std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_S_key(_Const_Link_type) [with _Key = std::pair<int, int>; _Val = std::pair<int, int>; _KeyOfValue = std::_Identity<std::pair<int, int> >; _Compare = DownCollision; _Alloc = std::allocator<std::pair<int, int> >; _Const_Link_type = const std::_Rb_tree_node<std::pair<int, int> >*]':
/usr/include/c++/14/bits/stl_tree.h:2116:47:   required from 'std::pair<std::_Rb_tree_node_base*, std::_Rb_tree_node_base*> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_get_insert_unique_pos(const key_type&) [with _Key = std::pair<int, int>; _Val = std::pair<int, int>; _KeyOfValue = std::_Identity<std::pair<int, int> >; _Compare = DownCollision; _Alloc = std::allocator<std::pair<int, int> >; key_type = std::pair<int, int>]'
 2116 |           __comp = _M_impl._M_key_compare(__k, _S_key(__x));
      |                                                ~~~~~~^~~~~
/usr/include/c++/14/bits/stl_tree.h:2169:4:   required from 'std::pair<std::_Rb_tree_iterator<_Val>, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(_Arg&&) [with _Arg = const std::pair<int, int>&; _Key = std::pair<int, int>; _Val = std::pair<int, int>; _KeyOfValue = std::_Identity<std::pair<int, int> >; _Compare = DownCollision; _Alloc = std::allocator<std::pair<int, int> >]'
 2169 |         = _M_get_insert_unique_pos(_KeyOfValue()(__v));
      |           ^~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_set.h:514:25:   required from 'std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(const value_type&) [with _Key = std::pair<int, int>; _Compare = DownCollision; _Alloc = std::allocator<std::pair<int, int> >; typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator = std::_Rb_tree<std::pair<int, int>, std::pair<int, int>, std::_Identity<std::pair<int, int> >, DownCollision, std::allocator<std::pair<int, int> > >::const_iterator; typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other = std::allocator<std::pair<int, int> >; typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key> = __gnu_cxx::__alloc_traits<std::allocator<std::pair<int, int> >, std::pair<int, int> >::rebind<std::pair<int, int> >; typename _Alloc::value_type = std::pair<int, int>; value_type = std::pair<int, int>]'
  514 |           _M_t._M_insert_unique(__x);
      |           ~~~~~~~~~~~~~~~~~~~~~^~~~~
a.cc:47:18:   required from here
   47 |         SD.insert(p);
      |         ~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_tree.h:768:15: error: static assertion failed: comparison object must be invocable as const
  768 |               is_invocable_v<const _Compare&, const _Key&, const _Key&>,
      |               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h:768:15: note: 'std::is_invocable_v<const DownCollision&, const std::pair<int, int>&, const std::pair<int, int>&>' evaluates to false