n = gets.to_i
up, left, right, down = [], [], [], []
n.times do
  x, y, u = gets.split
  x, y = x.to_i, y.to_i
  t = [x, y, u]
  case u
    when ?U; up << t
    when ?D; down << t
    when ?L; left << t
    when ?R; right << t
  end
end

INF = 1000000000
fastest = INF
hu = {}
up.each { |t| (hu[t[0]] || (hu[t[0]] = [])) << t }
hd = {}
down.each { |t| (hd[t[0]] || (hd[t[0]] = [])) << t}
hu.keys.each do |y|
  if hd.key? y
    hu[y].each do |(_, x1, _)|
      hd[y].each do |(_, x2, _)|
        fastest = [fastest, (x1 - x2).abs * 5].min if x1 <= x2
      end
    end
  end
end

hu = {}
left.each { |t| (hu[t[1]] || (hu[t[1]] = [])) << t }
hd = {}
right.each { |t| (hd[t[1]] || (hd[t[1]] = [])) << t}
hu.keys.each do |y|
  if hd.key? y
    hu[y].each do |(x1, _, _)|
      hd[y].each do |(x2, _, _)|
        fastest = [fastest, (x1 - x2).abs * 5].min if x1 >= x2
      end
    end
  end
end

if fastest === INF
  puts "SAFE"
else
  puts fastest
end