import sys

sys.setrecursionlimit(10 ** 9)



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

X = [list(map(int, input().split())) for _ in range(M)]



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

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

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



for a, b, c in X:

    a -= 1

    b -= 1

    c = -(c - P)

    edges[a].append((b, c))

    to[a].append(b)

    ot[b].append(a)



ok_1 = [False] * N

ok_n = [False] * N



# Reachable

def dfs(u):

    if ok_1[u]:

        return

    

    ok_1[u] = True

    for v in to[u]:

        dfs(v)

        

def rdfs(u):

    if ok_n[u]:

        return

    

    ok_n[u] = True

    for v in ot[u]:

        rdfs(v)

        

dfs(0)

rdfs(N - 1)

flag = [ok_1[i] & ok_n[i] for i in range(N)]



# Bellman ford

INF = 10 ** 9 + 7

d = [INF] * N

d[0] = 0

cnt = 0

updated = True

while updated and cnt <= N:

    updated = False

    cnt += 1

    for u in range(N):

        if not flag[u]:

            continue

        for v, c in edges[u]:

            if not flag[v]:

                continue



            value = d[u] + c

            if d[v] > d[u] + c:

                d[v] = value

                updated = True



if cnt > N:

    print((-1))

else:

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

from collections import deque

import sys

sys.setrecursionlimit(10 ** 9)



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

X = [list(map(int, input().split())) for _ in range(M)]



to = [[] for _ in range(N + 1)]

ot = [[] for _ in range(N + 1)]

edges = [[] for _ in range(N + 1)]



for a, b, c in X:

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

    to[a].append(b)

    ot[b].append(a)



ok_1 = [False] * (N + 1)

ok_n = [False] * (N + 1)



def dfs_1(u):

    if ok_1[u]:

        return

    

    ok_1[u] = True

    for v in to[u]:

        dfs_1(v)

        

def dfs_n(u):

    if ok_n[u]:

        return

    

    ok_n[u] = True

    for v in ot[u]:

        dfs_n(v)



dfs_1(1)

dfs_n(N)



INF = 10 ** 9 + 7

d = [INF] * (N + 1)

d[1] = 0

updated = True

cnt = 0

while updated and cnt <= N:

    updated = False

    cnt += 1

    for u in range(1, N + 1):

        if not ok_1[u] or not ok_n[u]:

            continue

            

        for v, c in edges[u]:

            if not ok_1[v] or not ok_n[v]:

                continue

                

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

                d[v] = d[u] - c

                updated = True



if cnt > N:

    print((-1))

else:

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

import sys

sys.setrecursionlimit(10**9)
N, M, P = list(map(int, input().split()))
X = [list(map(int, input().split())) for _ in range(M)]
edges = [[] for _ in range(N)]
to = [[] for _ in range(N)]
ot = [[] for _ in range(N)]
for a, b, c in X:
    a -= 1
    b -= 1
    c = -(c - P)
    edges[a].append((b, c))
    to[a].append(b)
    ot[b].append(a)
ok_1 = [False] * N
ok_n = [False] * N
# Reachable
def dfs(u):
    if ok_1[u]:
        return
    ok_1[u] = True
    for v in to[u]:
        dfs(v)


def rdfs(u):
    if ok_n[u]:
        return
    ok_n[u] = True
    for v in ot[u]:
        rdfs(v)


dfs(0)
rdfs(N - 1)
flag = [ok_1[i] & ok_n[i] for i in range(N)]
# Bellman ford
INF = 10**9 + 7
d = [INF] * N
d[0] = 0
cnt = 0
updated = True
while updated and cnt <= N:
    updated = False
    cnt += 1
    for u in range(N):
        if not flag[u]:
            continue
        for v, c in edges[u]:
            if not flag[v]:
                continue
            value = d[u] + c
            if d[v] > d[u] + c:
                d[v] = value
                updated = True
if cnt > N:
    print((-1))
else:
    print((max(0, -d[-1])))

from collections import deque
import sys

sys.setrecursionlimit(10**9)
N, M, P = list(map(int, input().split()))
X = [list(map(int, input().split())) for _ in range(M)]
to = [[] for _ in range(N + 1)]
ot = [[] for _ in range(N + 1)]
edges = [[] for _ in range(N + 1)]
for a, b, c in X:
    edges[a].append((b, c - P))
    to[a].append(b)
    ot[b].append(a)
ok_1 = [False] * (N + 1)
ok_n = [False] * (N + 1)


def dfs_1(u):
    if ok_1[u]:
        return
    ok_1[u] = True
    for v in to[u]:
        dfs_1(v)


def dfs_n(u):
    if ok_n[u]:
        return
    ok_n[u] = True
    for v in ot[u]:
        dfs_n(v)


dfs_1(1)
dfs_n(N)
INF = 10**9 + 7
d = [INF] * (N + 1)
d[1] = 0
updated = True
cnt = 0
while updated and cnt <= N:
    updated = False
    cnt += 1
    for u in range(1, N + 1):
        if not ok_1[u] or not ok_n[u]:
            continue
        for v, c in edges[u]:
            if not ok_1[v] or not ok_n[v]:
                continue
            if d[v] > d[u] - c:
                d[v] = d[u] - c
                updated = True
if cnt > N:
    print((-1))
else:
    print((max(0, -d[-1])))

+from collections import deque -edges = [[] for _ in range(N)] -to = [[] for _ in range(N)] -ot = [[] for _ in range(N)] +to = [[] for _ in range(N + 1)] +ot = [[] for _ in range(N + 1)] +edges = [[] for _ in range(N + 1)] -    a -= 1 -    b -= 1 -    c = -(c - P) -    edges[a].append((b, c)) +    edges[a].append((b, c - P)) -ok_1 = [False] * N -ok_n = [False] * N -# Reachable -def dfs(u): +ok_1 = [False] * (N + 1) +ok_n = [False] * (N + 1) + + +def dfs_1(u): -        dfs(v) +        dfs_1(v) -def rdfs(u): +def dfs_n(u): -        rdfs(v) +        dfs_n(v) -dfs(0) -rdfs(N - 1) -flag = [ok_1[i] & ok_n[i] for i in range(N)] -# Bellman ford +dfs_1(1) +dfs_n(N) -d = [INF] * N -d[0] = 0 +d = [INF] * (N + 1) +d[1] = 0 +updated = True -updated = True -    for u in range(N): -        if not flag[u]: +    for u in range(1, N + 1): +        if not ok_1[u] or not ok_n[u]: -            if not flag[v]: +            if not ok_1[v] or not ok_n[v]: -            value = d[u] + c -            if d[v] > d[u] + c: -                d[v] = value +            if d[v] > d[u] - c: +                d[v] = d[u] - c