n = gets.to_i
arr = n.times.map do
  x, y, u = gets.split(" ")
  [x.to_i, y.to_i, u]
end

ud_or_rl = {
  "U" => "D",
  "R" => "L",
  "D" => "U",
  "L" => "R",
}
move = {
  "U" => [0, 1],
  "R" => [1, 0],
  "D" => [0, -1],
  "L" => [-1, 0],
}

l = arr.length
min_time = nil # 1
arr.each_with_index do |(x, y, u), i|
  (l - i - 1).times do |j|
    xj, yj, uj = arr[i + j + 1]
    if u == ud_or_rl[uj]
      # 上下 or 左右が一致 => 逆向き
      if x == xj
        t = (y - yj).abs / 2
        _, dyi = move[u]
        _, dyj = move[uj]
        min_time = t if (y + t * dyi == yj + t * dyj) && (min_time.nil? || t < min_time)
      elsif y == yj
        t = (x - xj).abs / 2
        dxi, _ = move[u]
        dxj, _ = move[uj]
        min_time = t if (x + t * dxi == xj + t * dxj) && (min_time.nil? || t < min_time)
      end
    elsif u != uj && u != ud_or_rl[uj]
      # それ以外 => 座標差が一致かつ移動軸が別かつ近づく方向
      xt = x - xj
      yt = y - yj
      next if xt.abs != yt.abs
      
      t = xt.abs
      dxi, dyi = move[u]
      dxj, dyj = move[uj]
      if (x + t * dxi == xj + t * dxj) && (y + t * dyi == yj + t * dyj)
        min_time = t if min_time.nil? || t < min_time
      end
    end
  end
end
puts min_time.nil? ? "SAFE" : 10 * min_time