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

    way_lists =
      for _ <- 1..m do
        IO.gets("")
        |> String.trim()
        |> String.split()
        |> Enum.map(&String.to_integer/1)
        |> Enum.map(&(&1 - 1))
      end

    good_peaks(peaks, peaks, 0, way_lists, [])
  end

  defp good_peaks([head | []], peaks, search_index, way_lists, fin) do
    target_ways = Enum.filter(way_lists, fn [x, y] -> x == search_index or y == search_index end)

    if Enum.all?(target_ways, fn [x, y] ->
         target_index = Enum.find([x, y], fn num -> num != search_index end)
         head > Enum.at(peaks, target_index)
       end) do
      (fin ++ [head])
      |> Enum.count()
      |> IO.inspect()
    else
      fin
      |> Enum.count()
      |> IO.inspect()
    end
  end

  defp good_peaks([head | tail], peaks, search_index, way_lists, fin) do
    target_ways = Enum.filter(way_lists, fn [x, y] -> x == search_index or y == search_index end)

    if Enum.all?(target_ways, fn [x, y] ->
         target_index = Enum.find([x, y], fn num -> num != search_index end)
         head > Enum.at(peaks, target_index)
       end) do
      good_peaks(tail, peaks, search_index + 1, way_lists, fin ++ [head])
    else
      good_peaks(tail, peaks, search_index + 1, way_lists, fin)
    end
  end
end