#!/usr/bin/env ruby

require 'set'

class Hash
  def push(key, value)
    self[key] = [] if self[key] == nil
    self[key].push(value)
  end
end

class Array
  def lower_bound(value)
    left = -1;
    right = self.length;
    while left + 1 < right
      mid = left + (right - left) / 2;
      if self[mid] >= value
        right = mid
      else
        left = mid
      end
    end
    right
  end

  def unique
    res = [self.first]
    each_cons(2) do |a, b|
      if a != b
        res.push(b)
      end
    end
    res
  end
end

def get_ints
  gets.chomp.split.map(&:to_i)
end

def get_ints_minus_one
  get_ints.map { |x| x - 1 }
end

def get_int
  gets.chomp.to_i
end

N = get_int

up = []
down = []
right = []
left = []
N.times do |i|
  x, y, u = gets.chomp.split
  x = x.to_i
  y = y.to_i
  case u
  when 'U'
    up << [x, y]
  when 'R'
    right << [x, y]
  when 'L'
    left << [x, y]
  when 'D'
    down << [x, y]
  end
end

crash_length = []
crash = false

up_and_right = Hash.new { |h, k| h[k] = [] }
up.each do |x, y|
  if y > 0
    x = x + y
  elsif y < 0
    x = x - y
  end
  up_and_right[x] << y
end
up_and_right.transform_values! { |v| v.sort }

right.each do |x, y|
  if y > 0
    x = x + y
  else
    x = x - y
  end
  if up_and_right[x].length > 0
    index = up_and_right[x].bsearch_index { |x| x >= y }
    next if index == 0 # 位置的にupのほうが最初から上にいたならぶつからない

    crash = true
    sa = if index == nil
      (up_and_right[x].last - y).abs
    else
      [up_and_right[x][index], up_and_right[x][index-1]].min
    end
    crash_length << sa * 10
  end
end

up_and_left = Hash.new { |h, k| h[k] = [] }
up.each do |x, y|
  if y > 0
    x = x - y
  elsif y < 0
    x = x + y
  end
  up_and_left[x] << y
end
up_and_left.transform_values! { |v| v.sort }

left.each do |x, y|
  if y > 0
    x = x - y
  else
    x = x + y
  end
  if up_and_left[x].length > 0
    index = up_and_left[x].bsearch_index { |x| x >= y }
    next if index == 0 # 位置的にupのほうが最初から上にいたならぶつからない

    crash = true
    sa = if index == nil
      (up_and_left[x].last - y).abs
    else
      [up_and_left[x][index], up_and_left[x][index-1]].min
    end
    crash_length << sa * 10
  end
end

down_and_right = Hash.new { |h, k| h[k] = [] }
down.each do |x, y|
  if y > 0
    x = x - y
  elsif y < 0
    x = x + y
  end
  down_and_right[x] << y
end
down_and_right.transform_values! { |v| v.sort }

right.each do |x, y|
  if y > 0
    x = x - y
  else
    x = x + y
  end
  if down_and_right[x].length > 0
    index = down_and_right[x].bsearch_index { |x| x >= y }
    next if index == nil # 位置的にdownのほうが最初から下にいたならぶつからない

    crash = true
    sa = if index == 0
      (down_and_right[x].first - y).abs
    else
      [down_and_right[x][index], down_and_right[x][index-1]].min
    end
    crash_length << sa * 10
  end
end

down_and_left = Hash.new { |h, k| h[k] = [] }
down.each do |x, y|
  if y > 0
    x = x + y
  elsif y < 0
    x = x - y
  end
  down_and_left[x] << y
end
down_and_left.transform_values! { |v| v.sort }

left.each do |x, y|
  if y > 0
    x = x + y
  else
    x = x - y
  end
  if down_and_left[x].length > 0
    index = down_and_left[x].bsearch_index { |x| x >= y }
    next if index == nil # 位置的にdownのほうが最初から下にいたならぶつからない

    crash = true
    sa = if index == 0
      (down_and_left[x].first - y).abs
    else
      [down_and_left[x][index], down_and_left[x][index-1]].min
    end
    crash_length << sa * 10
  end
end

up_and_down = Hash.new { |h, k| h[k] = [] }
up.each do |x, y|
  up_and_down[x] << y
end
up_and_down.transform_values! { |v| v.sort }

down.each do |x, y|
  if up_and_down[x].length > 0
    index = up_and_down[x].bsearch_index { |v| v >= y }
    next if index == 0 # downのほうが最初から上、もしくは差が2の倍数じゃないならぶつからない

    crash = true
    index = up_and_down[x].length - 1 if index.nil?
    sa = (up_and_down[x][index]*10 - y*10).abs / 2
    crash_length << sa
  end
end

left_and_right = Hash.new { |h, k| h[k] = [] }
left.each do |x, y|
  left_and_right[y] << x
end
left_and_right.transform_values! { |v| v.sort }

right.each do |x, y|
  if left_and_right[y].length > 0
    index = left_and_right[y].bsearch_index { |v| v >= x }
    next if index.nil? # leftのほうが最初から左、もしくは差が2の倍数じゃないならぶつからない

    crash = true
    sa = (left_and_right[y][index] * 10 - x * 10).abs / 2
    crash_length << sa
  end
end

if crash
  puts crash_length.sort.first
else
  puts 'SAFE'
end
