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: Accepted)

from collections import deque, Counter, defaultdict
from itertools import chain, combinations
import json
# import numpy as np
import bisect
import sys
import math
import bisect
from functools import lru_cache
import itertools

sys.setrecursionlimit(10 ** 8)

M = 10 ** 9 + 7
INF = 10 ** 17


def main():
    N = int(input())
    XYU = [
        [a for a in input().split()]
        for _ in range(N)
    ]

    # N, K = [int(a) for a in input().split()]
    # A = [int(a) for a in input().split()]
    # T1, T2, T3 = [int(a) for a in input().split()]

    up = []
    down = []
    right = []
    left = []
    for xi, yi, ui in XYU:
        if ui == 'U':
            up.append((int(xi), int(yi)))
            continue
        if ui == 'D':
            down.append((int(xi), int(yi)))
            continue
        if ui == 'R':
            right.append((int(xi), int(yi)))
            continue
        if ui == 'L':
            left.append((int(xi), int(yi)))
            continue

    def a():
        conflict = 10 ** 50
        xtable = {}
        for xi, yi in down:
            if xi not in xtable:
                xtable[xi] = []
            ind = bisect.bisect_left(xtable[xi], yi)
            xtable[xi].insert(ind, yi)
        for xi, yi in up:
            if xi in xtable:
                ind = bisect.bisect_left(xtable[xi], yi)
                if ind != len(xtable[xi]):
                    conflict = min(conflict, (xtable[xi][ind] - yi) * 5)
        ytable = {}
        for xi, yi in left:
            if yi not in ytable:
                ytable[yi] = []
            ind = bisect.bisect_left(ytable[yi], xi)
            ytable[yi].insert(ind, xi)
        for xi, yi in right:
            if yi in ytable:
                ind = bisect.bisect_left(ytable[yi], xi)
                if ind != len(ytable[yi]):
                    conflict = min(conflict, (ytable[yi][ind] - xi) * 5)
        return conflict

    left_l1 = {}
    left_l2 = {}
    for xi, yi in left:
        v1 = yi - xi
        if v1 not in left_l1:
            left_l1[v1] = []
        ind = bisect.bisect_left(left_l1[v1], xi)
        left_l1[v1].insert(ind, xi)

        v2 = xi + yi
        if v2 not in left_l2:
            left_l2[v2] = []
        ind = bisect.bisect_left(left_l2[v2], xi)
        left_l2[v2].insert(ind, xi)

    right_l1 = {}
    right_l2 = {}
    for xi, yi in right:
        v1 = yi - xi
        if v1 not in right_l1:
            right_l1[v1] = []
        ind = bisect.bisect_left(right_l1[v1], xi)
        right_l1[v1].insert(ind, xi)

        v2 = xi + yi
        if v2 not in right_l2:
            right_l2[v2] = []
        ind = bisect.bisect_left(right_l2[v2], xi)
        right_l2[v2].insert(ind, xi)

    conflict = a()

    for xi, yi in up:
        v1 = yi - xi
        v2 = xi + yi
        if v1 in left_l1:
            l = left_l1[v1]
            ind = bisect.bisect_left(l, xi)
            if ind != len(l):
                xx = l[ind]
                conflict = min(conflict, abs(xx - xi) * 10)
        if v2 in right_l2:
            l = right_l2[v2]
            ind = bisect.bisect_right(l, xi)
            if ind != 0:
                xx = l[ind - 1]
                conflict = min(conflict, abs(xx - xi) * 10)
    for xi, yi in down:
        v1 = yi - xi
        v2 = xi + yi
        if v1 in right_l1:
            l = right_l1[v1]
            ind = bisect.bisect_right(l, xi)
            if ind != 0:
                xx = l[ind - 1]
                conflict = min(conflict, abs(xx - xi) * 10)
        if v2 in left_l2:
            l = left_l2[v2]
            ind = bisect.bisect_left(l, xi)
            if ind != len(l):
                xx = l[ind]
                conflict = min(conflict, abs(xx - xi) * 10)
    if conflict == 10 ** 50:
        print('SAFE')
        return
    print(conflict)


if __name__ == "__main__":
    main()