N,M,P = list(map(int,input().split()))

ABC = [tuple(map(int,input().split())) for i in range(M)]



es = [[] for _ in range(N)]

bs = [[] for _ in range(N)]

for a,b,c in ABC:

    a,b = a-1,b-1

    es[a].append((b,c-P))

    bs[b].append((a,c-P))



go_set = set([0])

stack = [0]

while stack:

    v = stack.pop()

    for to,_ in es[v]:

        if to in go_set: continue

        go_set.add(to)

        stack.append(to)



bk_set = set([N-1])

stack = [N-1]

while stack:

    v = stack.pop()

    for to,_ in bs[v]:

        if to in bk_set: continue

        bk_set.add(to)

        stack.append(to)



use = go_set & bk_set

K = len(use)

INF = float('inf')

d = [INF] * N

d[0] = 0

for i in range(K):

    for v in range(N):

        if v not in use: continue

        for to,c in es[v]:

            if to not in use: continue

            if d[to] > d[v] - c:

                d[to] = d[v] - c

                if i==K-1:

                    print((-1))

                    exit()

print((max(0, -d[-1])))
import sys

input = sys.stdin.readline

N,M,P = list(map(int,input().split()))

ABC = [tuple(map(int,input().split())) for i in range(M)]



es = [[] for _ in range(N)]

rs = [[] for _ in range(N)]

for a,b,c in ABC:

    a,b = a-1,b-1

    es[a].append(b)

    rs[b].append(a)



stack = [0]

vis_go = set([0])

while stack:

    v = stack.pop()

    for to in es[v]:

        if to in vis_go: continue

        vis_go.add(to)

        stack.append(to)

stack = [N-1]

vis_bk = set([N-1])

while stack:

    v = stack.pop()

    for to in rs[v]:

        if to in vis_bk: continue

        vis_bk.add(to)

        stack.append(to)

use = vis_go & vis_bk



INF = float('inf')

dist = [INF] * N

dist[0] = 0

for i in range(len(use)):

    for a,b,c in ABC:

        a,b,c = a-1,b-1,P-c

        if a not in use or b not in use: continue

        if dist[b] > dist[a] + c:

            dist[b] = dist[a] + c

            if i==len(use)-1:

                print((-1))

                exit()

print((max(0,-dist[-1])))
N, M, P = list(map(int, input().split()))
ABC = [tuple(map(int, input().split())) for i in range(M)]
es = [[] for _ in range(N)]
bs = [[] for _ in range(N)]
for a, b, c in ABC:
    a, b = a - 1, b - 1
    es[a].append((b, c - P))
    bs[b].append((a, c - P))
go_set = set([0])
stack = [0]
while stack:
    v = stack.pop()
    for to, _ in es[v]:
        if to in go_set:
            continue
        go_set.add(to)
        stack.append(to)
bk_set = set([N - 1])
stack = [N - 1]
while stack:
    v = stack.pop()
    for to, _ in bs[v]:
        if to in bk_set:
            continue
        bk_set.add(to)
        stack.append(to)
use = go_set & bk_set
K = len(use)
INF = float("inf")
d = [INF] * N
d[0] = 0
for i in range(K):
    for v in range(N):
        if v not in use:
            continue
        for to, c in es[v]:
            if to not in use:
                continue
            if d[to] > d[v] - c:
                d[to] = d[v] - c
                if i == K - 1:
                    print((-1))
                    exit()
print((max(0, -d[-1])))

import sys

input = sys.stdin.readline
N, M, P = list(map(int, input().split()))
ABC = [tuple(map(int, input().split())) for i in range(M)]
es = [[] for _ in range(N)]
rs = [[] for _ in range(N)]
for a, b, c in ABC:
    a, b = a - 1, b - 1
    es[a].append(b)
    rs[b].append(a)
stack = [0]
vis_go = set([0])
while stack:
    v = stack.pop()
    for to in es[v]:
        if to in vis_go:
            continue
        vis_go.add(to)
        stack.append(to)
stack = [N - 1]
vis_bk = set([N - 1])
while stack:
    v = stack.pop()
    for to in rs[v]:
        if to in vis_bk:
            continue
        vis_bk.add(to)
        stack.append(to)
use = vis_go & vis_bk
INF = float("inf")
dist = [INF] * N
dist[0] = 0
for i in range(len(use)):
    for a, b, c in ABC:
        a, b, c = a - 1, b - 1, P - c
        if a not in use or b not in use:
            continue
        if dist[b] > dist[a] + c:
            dist[b] = dist[a] + c
            if i == len(use) - 1:
                print((-1))
                exit()
print((max(0, -dist[-1])))

+import sys + +input = sys.stdin.readline -bs = [[] for _ in range(N)] +rs = [[] for _ in range(N)] -    es[a].append((b, c - P)) -    bs[b].append((a, c - P)) -go_set = set([0]) +    es[a].append(b) +    rs[b].append(a) +vis_go = set([0]) -    for to, _ in es[v]: -        if to in go_set: +    for to in es[v]: +        if to in vis_go: -        go_set.add(to) +        vis_go.add(to) -bk_set = set([N - 1]) +vis_bk = set([N - 1]) -    for to, _ in bs[v]: -        if to in bk_set: +    for to in rs[v]: +        if to in vis_bk: -        bk_set.add(to) +        vis_bk.add(to) -use = go_set & bk_set -K = len(use) +use = vis_go & vis_bk -d = [INF] * N -d[0] = 0 -for i in range(K): -    for v in range(N): -        if v not in use: +dist = [INF] * N +dist[0] = 0 +for i in range(len(use)): +    for a, b, c in ABC: +        a, b, c = a - 1, b - 1, P - c +        if a not in use or b not in use: -        for to, c in es[v]: -            if to not in use: -                continue -            if d[to] > d[v] - c: -                d[to] = d[v] - c -                if i == K - 1: -                    print((-1)) -                    exit() -print((max(0, -d[-1]))) +        if dist[b] > dist[a] + c: +            dist[b] = dist[a] + c +            if i == len(use) - 1: +                print((-1)) +                exit() +print((max(0, -dist[-1])))