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")
