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



  val Array(n, m) = readLine.trim.split(' ').map(_.toInt)
  val toys = Array.tabulate(n){_ ⇒ readLine.trim.toInt}.let(i ⇒ (1 to m).map(a ⇒ i.map(t ⇒ if (t == a) 1 else 0).also(ar ⇒ for (idx ← 1 until ar.length) ar(idx) += ar(idx - 1)))).toArray
  println(
    minMove(toys, (1 << m) - 1)
  )
  def minMove(toys: Array[Array[Int]], usage: Int, rest: Int = 0)(implicit memo: Array[Option[Long]] = Array.tabulate(1 << toys.length){_ ⇒ None}): Long = {
    memo(usage) match {
      case Some(r) ⇒ r
      case None if usage == 0 ⇒ 0
      case None ⇒ {
        usage.asIterable.map{t ⇒
          minMove(toys, usage ^ (1 << t), rest + toys(t).last) + toys(t).last - toys(t)(rest + toys(t).last - 1) + (if (rest != 0) toys(t)(rest - 1) else 0)
        }.min.also(r ⇒ memo(usage) = Some(r))
      }
    }
  }
  implicit class AsFlag(val value: Int){
    def asIterable: Iterable[Int] = {
      var i = 0
      var v = value
      var res = Nil: List[Int]
      while (v != 0){
        if (v % 2 == 1){
          res ::= i
        }
        i += 1
        v /= 2
      }
      res
    }
  }
  implicit class ScopeFunc[T](val value: T){
    def let[A](func: T ⇒ A): A = func(value)
    def also(func: T ⇒ Unit): T = {
      func(value)
      value
    }
  }
}
