import scala.collection.mutable

object Main extends App {

  import scala.io.StdIn._

  val Array(l, n) = readLine.trim.split(' ').map(_.toInt)
  val circles = Array.tabulate(n){_ ⇒
    val Array(p, d, t) = readLine.trim.split(' ').map(_.toLong)
    (p, d, t)
  }.sortBy(_._1)
  val position = circles.map(_._1) :+ l
  val minTime = position.map{i ⇒ i}
  for (i ← circles.indices){
    val (p, d, t) = circles(i)
    lowerBound(position, p + d) match {
      case pos if minTime(pos) > minTime(i) + t + position(pos) - p - d ⇒ minTime(pos) = minTime(i) + t + position(pos) - p - d
      case _ ⇒
    }
  }
  println(minTime.last)
  def lowerBound[T](array: Array[T], target: T)(implicit ord: Ordering[T]): Int = {
    var left = 0
    var right = array.length
    while(left < right){
      val mid = (left + right) / 2
      if (ord.compare(array(mid), target) < 0){
        left = mid + 1
      }else {
        right = mid
      }
    }
    right
  }
  implicit class Extend[T](value: T){
    def also(func: T ⇒ Unit): T = {func(value); value}
    def let[A](func: T ⇒ A): A = func(value)
  }
}
