Scanf.scanf "%d %d %d" (fun n m p ->
    let nei = Array.make n [] in
    let uvw = Array.init m (fun _ ->
        Scanf.scanf " %d %d %d" (fun a b c ->
            let a = a - 1 in let b = b - 1 in
            nei.(a) <- b :: nei.(a);
            a, b, c - p
        )
    )
    in

    let sentinel = min_int / 10 in
    let bellman start =
        let cost = Array.make n sentinel in
        cost.(start) <- 0;
        for i = 0 to n - 1 do
            Array.iter (fun (u, v, w) ->
                if cost.(v) < cost.(u) + w then (
                    cost.(v) <- cost.(u) + w;
                )
            ) uvw
        done;
        cost
    in

    let reachable n1 n2 =
        let visited = Array.make n false in

        let rec visit cur =
            visited.(cur) <- true;
            List.iter (fun next -> if not visited.(next) then visit next) nei.(cur)
        in
        visit n1;
        visited.(n2)
    in

    let cost = bellman 0 in

    let flag = Array.fold_left (fun acc (u, v, w) ->
        if cost.(u) + w > cost.(v) && reachable 0 u && reachable u (n - 1) then true else acc) false uvw
    in
    Printf.printf "%d\n" @@ if flag then (-1) else max 0 cost.(n - 1)
)