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

n=int(input())
ver=[]
hor=[]
diagr=[]
diagl=[]
INF=10**9
for i in range(200001):
  ver.append([[],[]])
  hor.append([[],[]])
for i in range(-200000,200001):
  diagr.append([[],[],[],[]])
  diagl.append([[],[],[],[]])
for i in range(n):
  x,y,u=map(str,input().split())
  x=int(x)
  y=int(y)
  if u=='U':
    ver[x][0].append(y)
    diagr[x-y+200000][0].append(x)
    diagl[x+y][0].append(x)
  if u=='D':
    ver[x][1].append(y)
    diagr[x-y+200000][1].append(x)
    diagl[x+y][1].append(x)
  if u=='R':
    hor[y][0].append(x)
    diagr[x-y+200000][2].append(x)
    diagl[x+y][2].append(x)
  if u=='L':
    hor[y][1].append(x)
    diagr[x-y+200000][3].append(x)
    diagl[x+y][3].append(x)
ans=INF
for i in range(200001):
  if len(ver[i][0])>0 and len(ver[i][1])>0:
    ver[i][0].sort()
    ver[i][1].sort()
    if ver[i][0][0]>ver[i][1][-1]:
      continue
    pointer1=0
    pointer=0
    s=len(ver[i][1])
    t=len(ver[i][0])
    while pointer1<t:
      while pointer<s and pointer1<t and ver[i][1][pointer]<ver[i][0][pointer1]:
        pointer+=1
      if pointer==s:
        break
      ans=min(ans,(ver[i][1][pointer]-ver[i][0][pointer1])//2)
      pointer1+=1
for i in range(200001):
  if len(hor[i][0])>0 and len(hor[i][1])>0:
    hor[i][0].sort()
    hor[i][1].sort()
    if hor[i][0][0]>hor[i][1][-1]:
      continue
    pointer1=0
    pointer=0
    s=len(hor[i][1])
    t=len(hor[i][0])
    while pointer1<t:
      while pointer<s and pointer1<t and hor[i][1][pointer]<hor[i][0][pointer1]:
        pointer+=1
      if pointer==s:
        break
      ans=min(ans,(hor[i][1][pointer]-hor[i][0][pointer1])//2)
      pointer1+=1
for i in range(400001):
  if len(diagl[i][0])>0 and len(diagl[i][2])>0:
    diagl[i][0].sort()
    diagl[i][2].sort()
    if diagl[i][2][0]>diagl[i][0][-1]:
      continue
    pointer1=0
    pointer=0
    s=len(diagl[i][0])
    t=len(diagl[i][2])
    while pointer1<t:
      while pointer<s and pointer1<t and diagl[i][0][pointer]<diagl[i][2][pointer1]:
        pointer+=1
      if pointer==s:
        break
      ans=min(ans,(diagl[i][0][pointer]-diagl[i][2][pointer1]))
      pointer1+=1
  if len(diagl[i][1])>0 and len(diagl[i][3])>0:
    diagl[i][1].sort()
    diagl[i][3].sort()
    if diagl[i][1][0]>diagl[i][3][-1]:
      continue
    pointer1=0
    pointer=0
    s=len(diagl[i][3])
    t=len(diagl[i][1])
    while pointer1<t:
      while pointer<s and pointer1<t and diagl[i][3][pointer]<diagl[i][1][pointer1]:
        pointer+=1
      if pointer==s:
        break
      ans=min(ans,(diagl[i][3][pointer]-diagl[i][1][pointer1]))
      pointer1+=1
for i in range(400001):
  if len(diagr[i][0])>0 and len(diagr[i][3])>0:
    diagr[i][0].sort()
    diagr[i][3].sort()
    if diagr[i][0][0]>diagr[i][3][-1]:
      continue
    pointer1=0
    pointer=0
    s=len(diagr[i][3])
    t=len(diagr[i][0])
    while pointer1<t:
      while pointer<s and pointer1<t and diagr[i][3][pointer]<diagr[i][0][pointer1]:
        pointer+=1
      if pointer==s:
        break
      ans=min(ans,(diagr[i][3][pointer]-diagr[i][0][pointer1]))
      pointer1+=1
  if len(diagr[i][1])>0 and len(diagr[i][2])>0:
    diagr[i][1].sort()
    diagr[i][2].sort()
    if diagr[i][2][0]>diagr[i][1][-1]:
      continue
    pointer1=0
    pointer=0
    s=len(diagr[i][1])
    t=len(diagr[i][2])
    while pointer1<t:
      while pointer<s and pointer1<t and diagr[i][1][pointer]<diagr[i][2][pointer1]:
        pointer+=1
      if pointer==s:
        break
      ans=min(ans,(diagr[i][1][pointer]-diagr[i][2][pointer1]))
      pointer1+=1
if ans==INF:
  print('SAFE')
else:
  print(ans*10)