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 PyPy3 (7.3.0) (judge verdict: Runtime Error)

import sys
sys.setrecursionlimit(10**7)

readline = sys.stdin.buffer.readline
def readstr():return readline().rstrip().decode()
def readstrs():return list(readline().decode().split())
def readint():return int(readline())
def readints():return list(map(int,readline().split()))
def printrows(x):print('\n'.join(map(str,x)))
def printline(x):print(' '.join(map(str,x)))

from operator import itemgetter

n = readint()

ans = 10**10

u = []
d = []
l = []
r = []
uu = [[]for i in range(2*10**5+1)]
dd = [[]for i in range(2*10**5+1)]
ll = [[]for i in range(2*10**5+1)]
rr = [[]for i in range(2*10**5+1)]
for i in range(n):
    x,y,a = readstrs()
    x = int(x)
    y = int(y)
    if a=='U':
        u.append([x,y,x+y,x-y])
        uu[x].append(y)
    elif a == 'D':
        d.append([x,y,x+y,x-y])
        dd[x].append(y)
    elif a == 'L':
        l.append([x,y,x+y,x-y])
        ll[y].append(x)
    elif a == 'R':
        r.append([x,y,x+y,x-y])
        rr[y].append(x)

for i in range(2*10**5+1):
    if uu[i] and dd[i]:
        uu[i].sort()
        dd[i].sort()
        lenu = len(uu[i])
        lend = len(dd[i])
        k = 0
        j = 0
        while k<lenu and j<lend:
            if dd[i][j]>uu[i][k]:
                ans = min(ans,(dd[i][j]-uu[i][k])//2)
                k += 1
            else:
                j += 1
    if ll[i] and rr[i]:
        ll[i].sort()
        rr[i].sort()
        lenl = len(ll[i])
        lenr = len(rr[i])
        k = 0
        j = 0
        while k<lenl and j<lenr:
            if ll[i][j]>rr[i][k]:
                ans = min(ans,(ll[i][j]-rr[i][k])//2)
                k += 1
            else:
                j += 1

uu = [[]for i in range(4*10**5+1)]
dd = [[]for i in range(4*10**5+1)]
ll = [[]for i in range(4*10**5+1)]
rr = [[]for i in range(4*10**5+1)]

for a in u:
    uu[a[3]].append(a[0])
for a in d:
    dd[a[3]].append(a[0])    
for a in l:
    ll[a[3]].append(a[0])
for a in r:
    rr[a[3]].append(a[0])

for i in range(4*10**5+1):
    if uu[i] and ll[i]:
        uu[i].sort()
        ll[i].sort()
        lenu = len(uu[i])
        lenl = len(ll[i])
        k = 0
        j = 0
        while k<lenu and j<lenl:
            if ll[i][j]>uu[i][k]:
                ans = min(ans,ll[i][j]-uu[i][k])
                k += 1
            else:
                j += 1
    if dd[i] and rr[i]:
        dd[i].sort()
        rr[i].sort()
        lend = len(dd[i])
        lenr = len(rr[i])
        k = 0
        j = 0
        while j<lend and k<lenr:
            if dd[i][j]>rr[i][k]:
                ans = min(ans,dd[i][j]-rr[i][k])
                k += 1
            else:
                j += 1

        
uu = [[]for i in range(4*10**5+1)]
dd = [[]for i in range(4*10**5+1)]
ll = [[]for i in range(4*10**5+1)]
rr = [[]for i in range(4*10**5+1)]

for a in u:
    uu[a[2]].append(a[0])
for a in d:
    dd[a[2]].append(a[0])    
for a in l:
    ll[a[2]].append(a[0])
for a in r:
    rr[a[2]].append(a[0])

for i in range(4*10**5+1):
    if uu[i] and rr[i]:
        uu[i].sort()
        rr[i].sort()
        lenu = len(uu[i])
        lenr = len(rr[i])
        k = 0
        j = 0
        while k<lenu and j<lenr:
            if rr[i][j]<uu[i][k]:
                ans = min(ans,-rr[i][j]+uu[i][k])
                j += 1
            else:
                k += 1
    if dd[i] and ll[i]:
        dd[i].sort()
        ll[i].sort()
        lend = len(dd[i])
        lenl = len(ll[i])
        k = 0
        j = 0
        while j<lend and k<lenl:
            if dd[i][j]<ll[i][k]:
                ans = min(ans,-dd[i][j]+ll[i][k])
                j += 1
            else:
                k += 1

if ans == 10**10:
    print('SAFE')
else:
    print(ans*10)