
import scala.annotation.tailrec

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 - 1, s)
    gages(r - 1).addRelation(l - 1, -s)
  }
  implicit val suspect = Array.tabulate(n){_ ⇒ None: Option[Position]}
  val searched = (for (i ← suspect.indices) yield {
    suspect(i) match {
      case None ⇒
        suspect(i) = Some(Absolute(0))
        val (smin, smax) = searchSub(gages, suspect, i)
        suspect(i) = Some(Absolute(x - smax))
        Option(smin, smax)
      case _ ⇒ None
    }
  }).flatten

  println(
    if (searched.exists{case (smin, smax) ⇒ smin + x < smax}) -1
    else {
      suspect.flatMap{_.map{_.getAbsolute}}.flatten.mkString(" ")
    }
  )

  def searchSub(gages: Array[Gage], suspects: Array[Option[Position]], current: Int): (Int, Int) = {
    val (orient, diff) = suspects(current) match {
      case Some(Absolute(_)) ⇒ (current, 0)
      case Some(Relation(o, d)) ⇒ (o, d)
      case None ⇒ ???
    }
    val sub = for (relation ← gages(current).relations) yield {
      suspects(relation.orient) match {
        case Some(r@Relation(_, _)) if orient == r.orient && diff + relation.diff == r.diff ⇒ None
        case Some(Absolute(o)) if o == orient ⇒ None
        case Some(_) ⇒ Some(Int.MinValue, Int.MaxValue)
        case None ⇒
          suspects(relation.orient) = Some(Relation(orient, diff + relation.diff))
          Some(searchSub(gages, suspects, relation.orient))
      }
    }
    sub.flatten.foldLeft(diff, diff) {
      case ((cmin, cmax), (smin, smax)) ⇒ (min(cmin, smin), max(cmax, smax))
    }
  }
  sealed trait Position {
    def getAbsolute(implicit s: Array[Option[Position]]): Option[Int] = {
      this match {
        case Absolute(count) ⇒ Option(count)
        case Relation(orient, diff) ⇒ s(orient).flatMap{_.getAbsolute(s).map{_ + diff}}
      }
    }
  }
  case class Relation(orient: Int, diff: Int) extends Position
  case class Absolute(count: 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)
  }
}
