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: Wrong Answer)

/*{{{*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include<bitset>
#include<vector>
#include<limits.h>
#include<assert.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++)
#define MP make_pair
#define PB emplace_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<PII> VPII;
typedef pair<LL,LL> PLL;
typedef vector<PLL> VPLL;
template<class T> void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(int64_t &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); }
template<class T> void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const int64_t &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T,class U> void _W(const pair<T,U> &x) {_W(x.F); putchar(' '); _W(x.S);}
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }
#ifdef HOME
 #define DEBUG(...) {printf("[DEBUG] ");W(__VA_ARGS__);}
#else
 #define DEBUG(...)
#endif
int MOD = 1e9+7;
void ADD(LL& x,LL v){x=(x+v)%MOD;if(x<0)x+=MOD;}
/*}}}*/
const int SIZE = 1<<19;
int x[SIZE],y[SIZE],d[SIZE];
VPII AA[SIZE];
void my_clear(){
    REP(i,SIZE)AA[i].clear();
}
int an=MOD;
void update(int mul){
    REP(i,SIZE){
        sort(ALL(AA[i]));
        REPP(j,1,SZ(AA[i])){
            if(AA[i][j-1].S==0&&AA[i][j].S==1)an=min(an,(AA[i][j].F-AA[i][j-1].F)*mul);
        }
    }
}
void solve() {
    int N;
    R(N);
    REP(i,N){
        R(x[i],y[i]);
        char c[4];
        RS(c);
        if(c[0]=='U')d[i]=0;
        else if(c[0]=='R')d[i]=1;
        else if(c[0]=='D')d[i]=2;
        else d[i]=3;
    }
    my_clear();
    REP(i,N){
        if(d[i]&1){
            AA[y[i]].PB(MP(x[i],d[i]>>1));
        }
    }
    update(1);
    my_clear();
    REP(i,N){
        if(d[i]%2==0){
            AA[x[i]].PB(MP(y[i],d[i]>>1));
        }
    }
    update(1);
    my_clear();
    REP(i,N){
        if(d[i]==1){
            AA[x[i]+y[i]].PB(MP(x[i],0));
        } else if(d[i] == 0) {
            AA[x[i]+y[i]].PB(MP(x[i],1));
        }
    }
    update(2);
    my_clear();
    REP(i,N){
        if(d[i]==2){
            AA[x[i]+y[i]].PB(MP(x[i],0));
        } else if(d[i] == 3) {
            AA[x[i]+y[i]].PB(MP(x[i],1));
        }
    }
    update(2);
    my_clear();
    REP(i,N){
        if(d[i]==0){
            AA[x[i]-y[i]+200000].PB(MP(x[i],0));
        } else if(d[i] == 3) {
            AA[x[i]-y[i]+200000].PB(MP(x[i],1));
        }
    }
    update(2);
    my_clear();
    REP(i,N){
        if(d[i]==1){
            AA[x[i]-y[i]+200000].PB(MP(x[i],0));
        } else if(d[i] == 2) {
            AA[x[i]-y[i]+200000].PB(MP(x[i],1));
        }
    }
    if(an==MOD)W("SAFE");
    else W(an*5);
}
int main(){
#define MULTITEST 0
#if MULTITEST    
    CASET{
        solve();
    }
#else
    solve();
#endif
    return 0;
}