defmodule Main do
  def main do
    [x, _n] = IO.gets("") |> String.trim() |> String.split() |> Enum.map(&String.to_integer/1)

    list = IO.gets("") |> String.trim() |> String.split() |> Enum.map(&String.to_integer/1)

    case Enum.member?(list, x) do
      false ->
        IO.puts(x)

      true ->
        [bottom_target, bottom_count] = search_bottom(x - 1, list, 1)
        [top_target, top_count] = search_up(x + 1, list, 1)

        cond do
          top_count == bottom_count ->
            IO.puts(bottom_target)

          top_count > bottom_count ->
            IO.puts(bottom_target)

          true ->
            IO.puts(top_target)
        end
    end
  end

  defp search_bottom(target, list, count) do
    case Enum.member?(list, target) do
      true ->
        search_bottom(target - 1, list, count + 1)

      false ->
        [target, count]
    end
  end

  defp search_up(target, list, count) do
    case Enum.member?(list, target) do
      true ->
        search_up(target + 1, list, count + 1)

      false ->
        [target, count]
    end
  end
end