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 Fortran (GNU Fortran 9.2.1) (judge verdict: Accepted)

module mod_two_dim_merge_sort
  implicit none
  integer, private, parameter :: intkind = 4
contains
  logical function less(a, b) result(res)
    integer(intkind), intent(in) :: a(:), b(:)
    res = a(1) < b(1) .or. (a(1) == b(1) .and. a(2) <= b(2))
  end
  subroutine merge_sort(a)
    integer(intkind), intent(inout) :: a(:, :)
    integer :: n, m, l, i, u
    n = size(a, 1)
    m = n
    l = 1
    do while (m > 1)
      do i = 1, m / 2
        u = min(2 * i * l, n)
        call merger(a(2 * (i - 1) * l + 1:(2 * i - 1) * l, :), &
        a((2 * i - 1) * l + 1:u, :))
      end do
      l = 2 * l
      m = (m + 1) / 2
    end do
  end
  subroutine merger(a1, a2)
    integer(intkind), intent(inout) :: a1(:, :), a2(:, :)
    integer(intkind) :: a(size(a1, 1) + size(a2, 1), size(a1, 2))
    integer :: i1, i2, n1, n2
    i1 = 1
    i2 = 1
    n1 = size(a1, 1)
    n2 = size(a2, 1)
    do while (i1 <= n1 .and. i2 <= n2)
      if (less(a1(i1, :), a2(i2, :))) then
        a(i1 + i2 - 1, :) = a1(i1, :)
        i1 = i1 + 1
      else
        a(i1 + i2 - 1, :) = a2(i2, :)
        i2 = i2 + 1
       end if
    end do
    if (i1 <= n1) then
      a(i1 + i2 - 1:n1 + n2, :) = a1(i1:n1, :)
    else
      a(i1 + i2 - 1:n1 + n2, :) = a2(i2:n2, :)
    end if
    a1 = a(1:n1, :)
    a2 = a(n1 + 1:n1 + n2, :)
  end
end module mod_two_dim_merge_sort
program air_safety
  use mod_two_dim_merge_sort
  implicit none
  integer, parameter :: inf = 1e9
  integer :: n, x(200000) = 0, y(200000) = 0, u(200000) = 0, i, j, k, z
  integer :: p(200000, 2) = 0, m(0:400000) = 0, ans = inf
  character(1) :: s
  type t_list
    integer, allocatable :: at(:, :)
  end type
  type(t_list) :: list(0:400000)
  read(*,*) n
  do i = 1, n
    read(*,*) x(i), y(i), s
    select case (s)
    case ('U')
      u(i) = 1
    case ('R')
      u(i) = 2
    case ('D')
      u(i) = 3
    case ('L')
      u(i) = 4
    end select
  end do
  do k = 1, 4
    if (k == 1 .or. k == 2) then
      m = 0
      do i = 1, n
        if (mod(u(i), 2) == 1) then
          m(x(i)) = m(x(i)) + 1
        end if
      end do
      do i = 0, 200000
        if (m(i) == 0) cycle
        allocate(list(i)%at(m(i), 2))
      end do
      m = 0
      do i = 1, n
        if (mod(u(i), 2) == 1) then
          m(x(i)) = m(x(i)) + 1
          list(x(i))%at(m(x(i)), 1) = y(i)
          list(x(i))%at(m(x(i)), 2) = u(i)
        end if
      end do
      do i = 0, 200000
        if (m(i) == 0) cycle
        call merge_sort(list(i)%at(1:m(i), :))
        do j = 1, m(i) - 1
          if (list(i)%at(j + 1, 2) - list(i)%at(j, 2) == 2) then
            ans = min(ans, 5 * (list(i)%at(j + 1, 1) - list(i)%at(j, 1)))
          end if
        end do
        deallocate(list(i)%at)
      end do
    end if
    m = 0
    do i = 1, n
      if (u(i) == 1 .or. u(i) == 2) then
        z = x(i) + y(i)
        m(z) = m(z) + 1
      end if
    end do
    do i = 0, 400000
      if (m(i) == 0) cycle
      allocate(list(i)%at(m(i), 2))
    end do
    m = 0
    do i = 1, n
      if (u(i) == 1 .or. u(i) == 2) then
        z = x(i) + y(i)
        m(z) = m(z) + 1
        list(z)%at(m(z), 1) = y(i)
        list(z)%at(m(z), 2) = u(i)
      end if
    end do
    do i = 0, 400000
      if (m(i) == 0) cycle
      call merge_sort(list(i)%at(1:m(i), :))
      do j = 1, m(i) - 1
        if (list(i)%at(j + 1, 2) - list(i)%at(j, 2) == 1) then
          ans = min(ans, 10 * (list(i)%at(j + 1, 1) - list(i)%at(j, 1)))
        end if
      end do
      deallocate(list(i)%at)
    end do
    do i = 1, n
      u(i) = u(i) + 1
      if (u(i) == 5) u(i) = 1
      z = x(i)
      x(i) = y(i)
      y(i) = 200000 - z
    end do
  end do
  if (ans == inf) then
    write(*,'(a)') 'SAFE'
  else
    write(*,'(i0)') ans
  end if
end program air_safety