object Main extends App {
  import scala.io.StdIn._

  val Array(n, m) = readLine.trim.split(' ').map(_.toInt)
  val borders = List.tabulate(m){_ ⇒ readInt - 1}
  val goal = borders.foldLeft((1 until n).reduce{(a, b) ⇒ a * 10 + b}){(a, b) ⇒ a.swapToNext(b)}
  val piles = Array.tabulate(m){_ ⇒ Array.tabulate(n){_ ⇒ false}}

  println(
    borders.permutations.map(countHeights).min
  )

  def countHeights(border: List[Int]): Int = {
    def sub(border: List[Int], depth: Int, result: Int): Int = {
      border match {
        case Nil if result == goal ⇒
          depth + 1
        case Nil ⇒ m
        case h :: _ if piles(depth)(h) || piles(depth)(h + 1) || (h > 0 && piles(depth)(h - 1)) ⇒ sub(border, depth + 1, result)
        case h :: t ⇒
          piles(depth)(h) = true
          val res = sub(t, depth, result.swapToNext(h))
          piles(depth)(h) = false
          res
      }
    }
    sub(border, 0, (1 until n).reduce{(a, b) ⇒ a * 10 + b})
  }
  object WrappedInt{
    private val digit = Array(1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000)
  }
  implicit class WrappedInt(val value: Int) extends AnyVal {
    import WrappedInt._
    def swapToNext(position: Int): Int = (takeNth(position) - takeNth(position + 1)) * digit(position) * 9 + value
    private[this] def takeNth(position: Int): Int = value / digit(position) % 10
  }
  implicit class Scope[T](val value: T) extends AnyVal {
    @inline
    def let[A](func: T ⇒ A): A = func(value)
    @inline
    def also(func: T ⇒ Unit): T = {
      func(value)
      value
    }
  }
}
