#!/usr/bin/env python3

#ABC137 E



import sys

import math

import bisect

sys.setrecursionlimit(1000000000)

from heapq import heappush, heappop

from collections import defaultdict

from itertools import accumulate

from collections import Counter

from collections import deque

from operator import itemgetter

from itertools import permutations

mod = 10**9 + 7

def I(): return int(sys.stdin.readline())

def LI(): return list(map(int,sys.stdin.readline().split()))



def Bellman_Ford(edges,num_v, start):

    #start → iの最短距離

    #start:始点, num_v:頂点数, edges:[辺の始点, 辺の終点, 辺のコスト]

    dist = [-float('inf') for _ in range(num_v)]

    #dist[i] := s→iの最短距離

    dist[start] = 0

    flg = 0

    for i in range(num_v):

        update = False

        for edge in edges:

            e_from, e_to, e_cost = edge

            if e_from != -float('inf') and visited[e_to] and dist[e_to] < dist[e_from] + e_cost:

                dist[e_to] = dist[e_from] + e_cost

                update = True

        if not update:

            flg = 1

            break

    return dist, flg



n,m,p = LI()

edge = []

f = defaultdict(dict)

for _ in range(m):

    a,b,c = LI()

    edge.append([a-1,b-1,c-p])

    f[b-1][a-1] = 1

visited = [False]*n

def dfs(x):

    visited[x] = True

    for t in list(f[x].keys()):

        if not visited[t]:

            dfs(t)

dfs(n-1)

d,f = Bellman_Ford(edge,n,0)

if not f:

    print((-1))

else:

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

#!/usr/bin/env python3



import sys

import math

from bisect import bisect_right as br

from bisect import bisect_left as bl

sys.setrecursionlimit(2147483647)

from heapq import heappush, heappop,heappushpop

from collections import defaultdict

from itertools import accumulate

from collections import Counter

from collections import deque

from operator import itemgetter

from itertools import permutations

mod = 10**9 + 7

inf = float('inf')

def I(): return int(sys.stdin.readline())

def LI(): return list(map(int,sys.stdin.readline().split()))



n, m, p = LI()

abc = [LI() for _ in range(m)]

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

inv_edges = [[] for _ in range(n)]

for a, b, c in abc:

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

    inv_edges[b-1].append(a-1)



visited = [False] * n

stack = [n-1]

while stack:

    v = stack.pop()

    visited[v] = True

    for u in inv_edges[v]:

        if visited[u]:

            continue

        stack.append(u)



dp = [-float('inf')] * n

dp[0] = 0

tmp = -1

for _ in range(n+1):

    flag = False

    for v in range(n):

        score = dp[v]

        for u, c in edges[v]:

            if dp[u] < score + c - p and visited[u]:

                dp[u] = score + c - p

                flag = True

if flag:

    print((-1))

else:

    print((max(0, dp[n-1])))
#!/usr/bin/env python3
# ABC137 E
import sys
import math
import bisect

sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations

mod = 10**9 + 7


def I():
    return int(sys.stdin.readline())


def LI():
    return list(map(int, sys.stdin.readline().split()))


def Bellman_Ford(edges, num_v, start):
    # start → iの最短距離
    # start:始点, num_v:頂点数, edges:[辺の始点, 辺の終点, 辺のコスト]
    dist = [-float("inf") for _ in range(num_v)]
    # dist[i] := s→iの最短距離
    dist[start] = 0
    flg = 0
    for i in range(num_v):
        update = False
        for edge in edges:
            e_from, e_to, e_cost = edge
            if (
                e_from != -float("inf")
                and visited[e_to]
                and dist[e_to] < dist[e_from] + e_cost
            ):
                dist[e_to] = dist[e_from] + e_cost
                update = True
        if not update:
            flg = 1
            break
    return dist, flg


n, m, p = LI()
edge = []
f = defaultdict(dict)
for _ in range(m):
    a, b, c = LI()
    edge.append([a - 1, b - 1, c - p])
    f[b - 1][a - 1] = 1
visited = [False] * n


def dfs(x):
    visited[x] = True
    for t in list(f[x].keys()):
        if not visited[t]:
            dfs(t)


dfs(n - 1)
d, f = Bellman_Ford(edge, n, 0)
if not f:
    print((-1))
else:
    print((max(0, d[-1])))

#!/usr/bin/env python3
import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl

sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop, heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations

mod = 10**9 + 7
inf = float("inf")


def I():
    return int(sys.stdin.readline())


def LI():
    return list(map(int, sys.stdin.readline().split()))


n, m, p = LI()
abc = [LI() for _ in range(m)]
edges = [[] for _ in range(n)]
inv_edges = [[] for _ in range(n)]
for a, b, c in abc:
    edges[a - 1].append((b - 1, c))
    inv_edges[b - 1].append(a - 1)
visited = [False] * n
stack = [n - 1]
while stack:
    v = stack.pop()
    visited[v] = True
    for u in inv_edges[v]:
        if visited[u]:
            continue
        stack.append(u)
dp = [-float("inf")] * n
dp[0] = 0
tmp = -1
for _ in range(n + 1):
    flag = False
    for v in range(n):
        score = dp[v]
        for u, c in edges[v]:
            if dp[u] < score + c - p and visited[u]:
                dp[u] = score + c - p
                flag = True
if flag:
    print((-1))
else:
    print((max(0, dp[n - 1])))

-# ABC137 E -import bisect +from bisect import bisect_right as br +from bisect import bisect_left as bl -sys.setrecursionlimit(1000000000) -from heapq import heappush, heappop +sys.setrecursionlimit(2147483647) +from heapq import heappush, heappop, heappushpop +inf = float("inf") -def Bellman_Ford(edges, num_v, start): -    # start → iの最短距離 -    # start:始点, num_v:頂点数, edges:[辺の始点, 辺の終点, 辺のコスト] -    dist = [-float("inf") for _ in range(num_v)] -    # dist[i] := s→iの最短距離 -    dist[start] = 0 -    flg = 0 -    for i in range(num_v): -        update = False -        for edge in edges: -            e_from, e_to, e_cost = edge -            if ( -                e_from != -float("inf") -                and visited[e_to] -                and dist[e_to] < dist[e_from] + e_cost -            ): -                dist[e_to] = dist[e_from] + e_cost -                update = True -        if not update: -            flg = 1 -            break -    return dist, flg - - -edge = [] -f = defaultdict(dict) -for _ in range(m): -    a, b, c = LI() -    edge.append([a - 1, b - 1, c - p]) -    f[b - 1][a - 1] = 1 +abc = [LI() for _ in range(m)] +edges = [[] for _ in range(n)] +inv_edges = [[] for _ in range(n)] +for a, b, c in abc: +    edges[a - 1].append((b - 1, c)) +    inv_edges[b - 1].append(a - 1) - - -def dfs(x): -    visited[x] = True -    for t in list(f[x].keys()): -        if not visited[t]: -            dfs(t) - - -dfs(n - 1) -d, f = Bellman_Ford(edge, n, 0) -if not f: +stack = [n - 1] +while stack: +    v = stack.pop() +    visited[v] = True +    for u in inv_edges[v]: +        if visited[u]: +            continue +        stack.append(u) +dp = [-float("inf")] * n +dp[0] = 0 +tmp = -1 +for _ in range(n + 1): +    flag = False +    for v in range(n): +        score = dp[v] +        for u, c in edges[v]: +            if dp[u] < score + c - p and visited[u]: +                dp[u] = score + c - p +                flag = True +if flag: -    print((max(0, d[-1]))) +    print((max(0, dp[n - 1])))