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 Lua (LuaJIT 2.1.0) (judge verdict: Accepted)

local mmi = math.min
local mfl = math.floor

local function comp(a, b)
  return a < b
end

local function lower_bound(ary, x)
  local num = #ary
  if num == 0 then return 1 end
  if not comp(ary[1], x) then return 1 end
  if comp(ary[num], x) then return num + 1 end
  local min, max = 1, num
  while 1 < max - min do
    local mid = mfl((min + max) / 2)
    if comp(ary[mid], x) then
      min = mid
    else
      max = mid
    end
  end
  return max
end

local function upper_bound(ary, x)
  local num = #ary
  if num == 0 then return 1 end
  if comp(x, ary[1]) then return 1 end
  if not comp(x, ary[num]) then return num + 1 end
  local min, max = 1, num
  while 1 < max - min do
    local mid = mfl((min + max) / 2)
    if not comp(x, ary[mid]) then
      min = mid
    else
      max = mid
    end
  end
  return max
end

local n = io.read("*n", "*l")
local s = ""


local function create()
  local struct = {}
  struct.xpy = {}
  struct.xmy = {}
  struct.x = {}
  struct.y = {}
  return struct
end
local toleft = create()
local toright = create()
local toup = create()
local todown = create()

local function ins(tbl, x, y)
  local xpy, xmy = x + y, x - y
  if not tbl.xpy[xpy] then tbl.xpy[xpy] = {} end
  table.insert(tbl.xpy[xpy], x)
  if not tbl.xmy[xmy] then tbl.xmy[xmy] = {} end
  table.insert(tbl.xmy[xmy], x)
  if not tbl.x[x] then tbl.x[x] = {} end
  table.insert(tbl.x[x], y)
  if not tbl.y[y] then tbl.y[y] = {} end
  table.insert(tbl.y[y], x)
end
local function sortall(tbl)
  local z = {tbl.xpy, tbl.xmy, tbl.x, tbl.y}
  for i = 1, 4 do
    for k, t in pairs(z[i]) do
      table.sort(t)
    end
  end
end

local raw_x, raw_y, raw_d = {}, {}, {}
for i = 1, n do
  s = io.read()
  local x, y, d = s:match("(%d+) (%d+) (%w)")
  x, y = tonumber(x), tonumber(y)
  raw_x[i], raw_y[i], raw_d[i] = x, y, d
  if d == "L" then
    ins(toleft, x, y)
  elseif d == "R" then
    ins(toright, x, y)
  elseif d == "U" then
    ins(toup, x, y)
  else
    ins(todown, x, y)
  end
end
sortall(toleft)
sortall(toright)
sortall(toup)
sortall(todown)

local ans = false
local function solvelb(tbl, z, mul)
  if tbl then
    local p = lower_bound(tbl, z) - 1
    if 0 < p then
      local v = (z - tbl[p]) * mul
      if not ans or v < ans then
        ans = v
      end
    end
  end
end

for i = 1, n do
  local x, y, d = raw_x[i], raw_y[i], raw_d[i]
  local xpy, xmy = x + y, x - y
  if d == "L" then
    --toLeft vs toRight
    solvelb(toright.y[y], x, 5)
    --toLeft vs toUp
    solvelb(toup.xmy[xmy], x, 10)
    --toLeft vs toDown
    solvelb(todown.xpy[xpy], x, 10)
  elseif d == "R" then
  elseif d == "U" then
    -- toUp vs toRight
    solvelb(toright.xpy[xpy], x, 10)
  else
    -- toDown vs toRight
    solvelb(toright.xmy[xmy], x, 10)
    -- toDown vs toUp
    solvelb(toup.x[x], y, 5)
  end
end
print(ans and ans or "SAFE")