
object Main extends App {

  import scala.io.StdIn._
  import scala.math._

  val Array(n, x, m) = readLine.trim.split(' ').map(_.toInt)
  val gages = Array.tabulate(n + 1){_ ⇒ new Gage}
  for (_ ← 0 until m){
    val Array(l, r, s) = readLine.trim.split(' ').map(_.toInt)
    gages(l - 1).addRelation(r, s)
    gages(r).addRelation(l - 1, -s)
  }
  implicit val suspect = Array.tabulate(n + 1){_ ⇒ None: Option[Position]}
  var counter = 0
  val isNonParadoxical = (for (i ← suspect.indices) yield {
    suspect(i) match {
      case None ⇒
        suspect(i) = Some(Absolute(counter))
        counter += 1
        searchSub(gages, suspect, i)
      case _ ⇒ true
    }
  }).forall(i ⇒ i)

  val nonOptionalSuspects = suspect.flatten
  val rangesOfOrigin = Array.tabulate(counter){_ ⇒ Nil: List[(Int, Int)]}.also{arr ⇒
    for (i ← 1 until arr.length){
      // i - x <= 0
      // 0 - 0 <= i
      arr(i) ::= (0, 0)
  }}
  for (i ← 1 to n){
    val left = nonOptionalSuspects(i - 1)
    val right = nonOptionalSuspects(i)
    val (lOrient, lDiff) = left match {
      case Absolute(id) ⇒ (id, 0)
      case Relation(orient, diff) ⇒ (nonOptionalSuspects(orient).asInstanceOf[Absolute].id, diff)
    }
    val (rOrient, rDiff) = right match {
      case Absolute(id) ⇒ (id, 0)
      case Relation(orient, diff) ⇒ (nonOptionalSuspects(orient).asInstanceOf[Absolute].id, diff)
    }
    // 0 <= rOrient + rDiff - lOrient - lDiff <= x
    //lOrient <= rOrient + rDiff - lDiff
    //rOrient <= lOrient + lDiff - rDiff + x
    rangesOfOrigin(rOrient) ::= (lOrient, rDiff - lDiff)
    rangesOfOrigin(lOrient) ::= (rOrient, lDiff - rDiff + x)
  }
  val memo = Array.tabulate(counter){_ ⇒ Int.MaxValue.toLong}.also{_(0) = 0L}
  var isChanged = false
  for (_ ← 0 to counter){
    isChanged = false
    for (from ← rangesOfOrigin.indices){
      for ((to, diff) ← rangesOfOrigin(from)){
        if (memo(from) + diff < memo(to)){
          isChanged = true
          memo(to) = memo(from) + diff
        }
      }
    }
  }
  val res = nonOptionalSuspects.map{
    case Absolute(id) ⇒ memo(id)
    case Relation(orient, diff) ⇒ memo(nonOptionalSuspects(orient).asInstanceOf[Absolute].id) + diff
  }.let{arr ⇒ for (i ← 1 until arr.length) yield {arr(i) - arr(i - 1)}}
  println(
    if (!isNonParadoxical || res.exists(i ⇒ i < 0 || x < i) || isChanged) -1
    else {
      res.mkString(" ")
    }
  )

  def searchSub(gages: Array[Gage], suspects: Array[Option[Position]], current: Int): Boolean = {
    val (orient, diff) = suspects(current) match {
      case Some(Absolute(_)) ⇒ (current, 0)
      case Some(Relation(o, d)) ⇒ (o, d)
      case None ⇒ ???
    }
    (for (relation ← gages(current).relations) yield {
      suspects(relation.orient) match {
        case Some(r@Relation(_, _)) ⇒ orient == r.orient && diff + relation.diff == r.diff
        case Some(Absolute(_)) ⇒ relation.orient == orient && diff + relation.diff == 0
        case None ⇒
          suspects(relation.orient) = Some(Relation(orient, diff + relation.diff))
          searchSub(gages, suspects, relation.orient)
      }
    }).let{it ⇒ it.isEmpty || it.forall(i ⇒ i)}
  }
  sealed trait Position {
    def getAbsolute(implicit s: Array[Option[Position]]): Option[Int] = {
      this match {
        case Absolute(_) ⇒ Option(0)
        case Relation(orient, diff) ⇒ s(orient).flatMap{_.getAbsolute(s).map{_ + diff}}
      }
    }
  }
  case class Relation(orient: Int, diff: Int) extends Position
  case class Absolute(id: Int) extends Position
  class Gage{
    private var mRelations: List[Relation] = Nil
    def addRelation(to: Int, diff: Int): Unit = {
      mRelations ::= Relation(to, diff)
    }
    def relations: List[Relation] = mRelations
  }
  implicit class Extenion[T](val value :T){
    def also(func: T ⇒ Unit): T = {
      func(value)
      value
    }
    def let[R](func: T ⇒ R): R = func(value)
  }
}
/*
https://onlinejudge.u-aizu.ac.jp/challenges/sources/VPC/KUPC/2545

S(i) 1からi番目までの檻の中のライオンの合計
l r s <=> S(r) - S(l - 1) == s

 */
