Problem:
Score: 600 points

Problem Statement

M-kun is a brilliant air traffic controller.

On the display of his radar, there are N airplanes numbered 1, 2, ..., N, all flying at the same altitude.

Each of the airplanes flies at a constant speed of 0.1 per second in a constant direction. The current coordinates of the airplane numbered i are (X_i, Y_i), and the direction of the airplane is as follows:

if U_i is U, it flies in the positive y direction;

if U_i is R, it flies in the positive x direction;

if U_i is D, it flies in the negative y direction;

if U_i is L, it flies in the negative x direction.

To help M-kun in his work, determine whether there is a pair of airplanes that will collide with each other if they keep flying as they are now.

If there is such a pair, find the number of seconds after which the first collision will happen.

We assume that the airplanes are negligibly small so that two airplanes only collide when they reach the same coordinates simultaneously.

Constraints

1 \leq N \leq 200000

0 \leq X_i, Y_i \leq 200000

U_i is U, R, D, or L.

The current positions of the N airplanes, (X_i, Y_i), are all distinct.

N, X_i, and Y_i are integers.

Input

Input is given from Standard Input in the following format:

N
X_1 Y_1 U_1
X_2 Y_2 U_2
X_3 Y_3 U_3
:
X_N Y_N U_N

Output

If there is a pair of airplanes that will collide with each other if they keep flying as they are now, print an integer representing the number of seconds after which the first collision will happen.

If there is no such pair, print SAFE.

Sample Input 1

2
11 1 U
11 47 D

Sample Output 1

230

If the airplanes keep flying as they are now, two airplanes numbered 1 and 2 will reach the coordinates (11, 24) simultaneously and collide.

Sample Input 2

4
20 30 U
30 20 R
20 10 D
10 20 L

Sample Output 2

SAFE

No pair of airplanes will collide.

Sample Input 3

8
168 224 U
130 175 R
111 198 D
121 188 L
201 116 U
112 121 R
145 239 D
185 107 L

Sample Output 3

100

Solution in C++ (GCC 9.2.1) (judge verdict: Accepted)

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>
#include <climits>
#include <cassert>

#include <iostream>

#include <vector>
#include <queue>
#include <list>
#include <stack>
#include <deque>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

#include <algorithm>
#include <utility>
#include <memory>
#include <bitset>
#include <string>
#include <tuple>
#include <functional>
using namespace std;

#define lowbit(x) ((x) & -(x))
#define lson l,mid,id<<1
#define rson mid+1,r,id<<1|1
#define ls id<<1
#define rs id<<1|1
#define MID(l,r) ((l)+(((r)-(l))>>1))
#define fi first
#define se second
#define mk make_pair
#define mt make_tuple
#define pb push_back
#define epb emplace_back
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()

using LL = long long;
//using LL = __int64;
using pii = pair<int, int>;
using pdd = pair<double, double>;
using pLL = pair<LL, LL>;

const int INF = 0x3f3f3f3f;
const LL LINF = 0x3f3f3f3f3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
const int e5 = 100000; // 1e5
const int e6 = 1000000; //1e6;
const int MOD_1e9 = 1000000007; //1e9 + 7
const int MOD_998 = 998244353;
const int MOD = MOD_1e9;

template< typename T > inline void get_min(T &x, T y) {if(y < x) x = y;}
template< typename T > inline void get_max(T &x, T y) {if(x < y) x = y;}
template< typename T > inline void get_unique(vector<T> &vec) {sort(all(vec)); vec.erase(unique(all(vec)), vec.end());}

inline int sig(double x) {return x < -eps ? -1 : eps < x;}
LL fp(LL a, LL n, LL mod = MOD)
{
    if(n < 0) a = fp(a, mod - 2, mod), n = -n;
    LL res = 1;
    for(; n; n >>= 1, a = a * a % mod)
        if(n & 1)
            res = res * a % mod;
    return res;
}

template< int mod > struct Mint
{
    int x;
    Mint():x(0) {}
    Mint(int _x): x(_x) {if(x < 0 || x >= mod) x %= mod; if(x < 0) x += mod;}
    Mint(LL _x) {if(_x < 0 || _x >= mod) _x %= mod; if(_x < 0) _x += mod; x = _x;}
    Mint operator - () const {return Mint(mod - x);}
    Mint operator + (const Mint &rhs) const {return Mint(x + rhs.x >= mod ? x + rhs.x - mod : x + rhs.x);}
    Mint operator - (const Mint &rhs) const {return Mint(x - rhs.x < 0 ? x - rhs.x + mod : x - rhs.x);}
    Mint operator * (const Mint &rhs) const {return Mint((LL)x * rhs.x % mod);}
    Mint operator / (const Mint &rhs) const {return Mint((LL)x * fp(rhs.x, -1, mod) % mod);}
    bool operator == (const Mint &rhs) const {return x == rhs.x;}
    bool operator != (const Mint &rhs) const {return x != rhs.x;}
    Mint &operator += (const Mint &rhs) {x += rhs.x; if(x >= mod) x -= mod; return *this;}
    Mint &operator -= (const Mint &rhs) {x -= rhs.x; if(x < 0) x += mod; return *this;}
    Mint &operator *= (const Mint &rhs) {x = (LL)x * rhs.x % mod; return *this;}
    Mint &operator /= (const Mint &rhs) {x = (LL)x * fp(rhs.x, -1, mod) % mod; return *this;}
    friend ostream &operator << (ostream &out, const Mint &rhs) {return out << to_string(rhs.x);}
    Mint inv() {return Mint(fp(x, -1, mod));}
    Mint pow(int k) {return Mint(fp(x, k, mod));}
    Mint pow(const Mint &t) {return Mint(fp(x, t.x, mod));}
};

class fast_reader
{
private:
    int buff_size;
    char *buff_ptr;
    char *ptr;
    char *tail;
    bool is_open;
    fast_reader(const fast_reader &) = delete;
    fast_reader &operator = (const fast_reader &) = delete;

    inline bool is_digit() const {return '0' <= *ptr && *ptr <= '9';}
    inline bool is_blank() const {return *ptr == ' ' || *ptr == '\n' || *ptr == '\t' || *ptr == '\0';}
    template <typename T> inline void read_i(T &x)
    {
        bool f = false;
        x = 0;
        while(ptr < tail && !is_digit()) {if(*ptr == '-') f = true; ptr++;}
        while(ptr < tail && is_digit()) {x = (x << 1) + (x << 3) + (*ptr & 0xf); ptr++;}
        if(f) x = -x;
    }
    template <typename T> inline void read_f(T &x)
    {
        static char t_buff[1 << 6];
        read(t_buff);
        x = atof(t_buff);
    }
public:
    fast_reader(): buff_size(0), buff_ptr(nullptr), ptr(nullptr), tail(nullptr), is_open(false) {}
    ~fast_reader() {if(is_open) free(buff_ptr);}
    template <typename T> fast_reader &operator >> (T &x) {read(x); return *this;}

    inline void open_alter(int _base_size = 1 << 24) //2^24B = 16MB
    {
        if(is_open)
            throw runtime_error("fast reader is reopened!");

        int input_size = 0;
        int call_size = _base_size;
        buff_ptr = (char *) malloc(call_size);
        input_size += fread(buff_ptr, 1, call_size, stdin);
        while(feof(stdin) == 0)
        {
            int recall_size = call_size;
            buff_ptr = (char *) realloc(buff_ptr, call_size + recall_size);
            tail = buff_ptr + call_size;
            input_size += fread(tail, 1, recall_size, stdin);
            call_size += recall_size;
        }
        buff_ptr = (char *) realloc(buff_ptr, input_size);
        ptr = buff_ptr;
        tail = buff_ptr + input_size;
        buff_size = input_size;
        is_open = true;
    }

    template<typename T, typename ...Args> inline void read(T &x, Args &...args) {read(x); read(args...);}
    inline void read(int &x) {read_i(x);}
    inline void read(unsigned &x) {read_i(x);}
    inline void read(long long &x) {read_i(x);}
    inline void read(unsigned long long &x) {read_i(x);}
    inline void read(float &x) {read_f(x);}
    inline void read(double &x) {read_f(x);}
    inline void read(char *str)
    {
        while(ptr < tail && is_blank()) ptr++;
        while(ptr < tail && !is_blank()) *str = *ptr++, str++;
        *str = '\0';
    }
    inline void read(string &s)
    {
        while(ptr < tail && is_blank()) ptr++;
        char *pre = ptr;
        while(ptr < tail && !is_blank()) ptr++;
        s = std::move(string(pre, ptr)); //C++11 #include<utility> -> move return a rvalue
    }

    inline bool is_end()
    {
        while(ptr < tail && is_blank()) ptr++;
        return ptr >= tail;
    }
}reader;

const int maxn = (int) 1e4 + 20;
const int maxm = (int) 2e6 + 20;

void work()
{
    int n;
    reader >>n;

    map<string, vector<pii>> mp;
    for(int i = 0; i < n; i++)
    {
        int x, y;
        string s;
        reader >>x >>y >>s;
        mp[s].epb(mk(x, y));
    }

    int ans = INF;

    auto find_minmax = [](int x, const vector<int> &arr) -> int
    {
        int l = 0, r = arr.size() - 1, mid, ans = -1;
        while(l <= r)
        {
            mid = MID(l, r);
            if(arr[mid] <= x)
            {
                ans = arr[mid];
                l = mid + 1;
            }
            else r = mid - 1;
        }
        return ans;
    };
    auto find_maxmin = [](int x, const vector<int> &arr) -> int
    {
        int l = 0, r = arr.size() - 1, mid, ans = -1;
        while(l <= r)
        {
            mid = MID(l, r);
            if(x <= arr[mid])
            {
                ans = arr[mid];
                r = mid - 1;
            }
            else l = mid + 1;
        }
        return ans;
    };

    {
        map<int, vector<int>> tar;
        for(auto pr : mp["U"])
            tar[pr.fi].epb(pr.se);

        for(auto &pr : tar)
            sort(all(pr.se));

        for(auto pr : mp["D"])
        {
            int ret = find_minmax(pr.se, tar[pr.fi]);
//            cout <<ret <<endl;
            if(ret != -1)
            {
                int c = pr.se - ret;
                if(c & 1) c = c / 2 * 10 + 5;
                else c = c / 2 * 10;
                get_min(ans, c);
            }
        }

        tar.clear();
        for(auto pr : mp["R"])
            tar[pr.se].epb(pr.fi);

        for(auto &pr : tar)
            sort(all(pr.se));

        for(auto pr : mp["L"])
        {
            int ret = find_minmax(pr.fi, tar[pr.se]);
//            cout <<ret <<endl;
            if(ret != -1)
            {
                int c = pr.fi - ret;
                if(c & 1) c = c / 2 * 10 + 5;
                else c = c / 2 * 10;
                get_min(ans, c);
            }
        }
    }

    {
        map<int, vector<int>> L1, L2, R1, R2;

        for(auto pr : mp["L"])
        {
            int x = pr.fi, y = pr.se;
            L1[y + x].epb(x);
            L2[y - x].epb(x);
        }
        for(auto pr : mp["R"])
        {
            int x = pr.fi, y = pr.se;
            R1[y + x].epb(x);
            R2[y - x].epb(x);
        }

        for(auto &pr : L1)
            sort(all(pr.se));

        for(auto &pr : L2)
            sort(all(pr.se));

        for(auto &pr : R1)
            sort(all(pr.se));

        for(auto &pr : R2)
            sort(all(pr.se));

        for(auto pr : mp["U"])
        {
            int x = pr.fi, y = pr.se;
            int ret;
            ret = find_maxmin(x, L2[y - x]);
//            cout <<ret <<endl;
            if(ret != -1)
            {
                int c = ret - x;
                get_min(ans, c * 10);
            }

            ret = find_minmax(x, R1[y + x]);
//            cout <<ret <<endl;
            if(ret != -1)
            {
                int c = x - ret;
                get_min(ans, c * 10);
            }
        }

        for(auto pr : mp["D"])
        {
            int x = pr.fi, y = pr.se;
            int ret;
            ret = find_maxmin(x, L1[y + x]);
//            cout <<ret <<endl;
            if(ret != -1)
            {
                int c = ret - x;
                get_min(ans, c * 10);
            }

            ret = find_minmax(x, R2[y - x]);
//            cout <<ret <<endl;
            if(ret != -1)
            {
                int c = x - ret;
                get_min(ans, c * 10);
            }
        }
    }

    if(ans >= INF) cout <<"SAFE" <<endl;
    else cout <<ans <<endl;
}

int main()
{
#ifdef yukihana0416
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif // yukihana0416

    reader.open_alter();

    int tc = 1;
//    reader >> tc;
    for(int ca = 1; ca <= tc; ca++)
    {
//        printf("Case #%d: ", ca);
//        printf("Case #%d:\n", ca);
        work();
    }
    return 0;
}