
object Main extends App {

  import scala.io.StdIn._

  case class Cloth(a: Int, b: Int, c: Int)
  val Array(d, n) = readLine.trim.split(' ').map(_.toInt)
  val temp = List.tabulate(d){_ ⇒ readLine.trim.toInt}
  val clothes = Array.tabulate(n){_ ⇒
    val Array(a, b, c) = readLine.trim.split(' ').map(_.toInt)
    Cloth(a, b, c)
  }
  println(
    calMax(temp, clothes)
  )
  def calMax(temp: List[Int], clothes: Array[Cloth]): Int = {
    def sub(t: List[Int], cl: Array[Cloth], last: Int)(implicit memo: Array[Array[Option[Int]]]): Int = {
      memo(t.length)(last) match {
        case Some(r) ⇒ r
        case None ⇒ {
          (t match {
            case Nil ⇒ 0
            case h::r ⇒ cl.withFilter(c ⇒ (c.a to c.b).contains(h)).map(c ⇒ sub(r, cl, c.c) + (c.c - last).abs).max
          }).also{it ⇒ memo(t.length)(last) = Some(it)}
        }
      }
    }
    implicit val memo = Array.tabulate(temp.length){_ ⇒ Array.tabulate(101){_ ⇒ None: Option[Int]}}
    temp match {
      case Nil ⇒ 0
      case h::t ⇒ clothes.withFilter(c ⇒ (c.a to c.b).contains(h)).map(c ⇒ sub(t, clothes, c.c)).max
    }
  }
  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
    }
  }
}

/*
 */
