There is a directed graph with N vertices numbered 1 to N and M edges.
The i-th edge is directed from Vertex A_i to Vertex B_i, and there are C_i coins placed along that edge.
Additionally, there is a button on Vertex N.
We will play a game on this graph.
You start the game on Vertex 1 with zero coins, and head for Vertex N by traversing the edges while collecting coins.
It takes one minute to traverse an edge, and you can collect the coins placed along the edge each time you traverse it.
As usual in games, even if you traverse an edge once and collect the coins, the same number of coins will reappear next time you traverse that edge, which you can collect again.
When you reach Vertex N, you can end the game by pressing the button. (You can also choose to leave Vertex N without pressing the button and continue traveling.)
However, when you end the game, you will be asked to pay T \times P coins, where T is the number of minutes elapsed since the start of the game. If you have less than T \times P coins, you will have to pay all of your coins instead.
Your score will be the number of coins you have after this payment.
Determine if there exists a maximum value of the score that can be obtained. If the answer is yes, find that maximum value.

-----Constraints-----
 - 2 \leq N \leq 2500
 - 1 \leq M \leq 5000
 - 1 \leq A_i, B_i \leq N
 - 1 \leq C_i \leq 10^5
 - 0 \leq P \leq 10^5
 - All values in input are integers.
 - Vertex N can be reached from Vertex 1.

-----Input-----
Input is given from Standard Input in the following format:
N M P
A_1 B_1 C_1
:
A_M B_M C_M

-----Output-----
If there exists a maximum value of the score that can be obtained, print that maximum value; otherwise, print -1.

-----Sample Input-----
3 3 10
1 2 20
2 3 30
1 3 45

-----Sample Output-----
35


There are two ways to travel from Vertex 1 to Vertex 3:
 - Vertex 1 \rightarrow 2 \rightarrow 3: You collect 20 + 30 = 50 coins on the way. After two minutes from the start of the game, you press the button, pay 2 \times 10 = 20 coins, and you have 50 - 20 = 30 coins left.
 - Vertex 1 \rightarrow 2: You collect 45 coins on the way. After one minute from the start of the game, you press the button, pay 1 \times 10 = 10 coins, and you have 45 - 10 = 35 coins left.
Thus, the maximum score that can be obtained is 35.

Difficulty: MEDIUM

Solutions:
def BF(v, start, edges):
	INF = 10 ** 18
	nega = False
	d = [INF] * v
	d[start] = 0
	for _ in range(v):
		f = False
		for (a, b, c) in edges:
			cost = d[a] + c
			if cost < d[b]:
				d[b] = cost
				f = True
		if not f:
			break
	else:
		nega = True
	return (d, nega)
from collections import deque

def BFS(N, graph, start):
	d = [-1 for i in range(N)]
	Q = deque([])
	Q.append(start)
	d[start] = 0
	while Q:
		v = Q.popleft()
		for u in graph[v]:
			if d[u] == -1:
				d[u] = d[v] + 1
				Q.append(u)
	return d
(N, M, P) = (int(i) for i in input().split())
graph = [[] for i in range(N)]
graphr = [[] for i in range(N)]
edges = []
for i in range(M):
	(A, B, C) = (int(i) for i in input().split())
	graph[A - 1].append(B - 1)
	graphr[B - 1].append(A - 1)
	edges.append((A - 1, B - 1, P - C))
d_start = BFS(N, graph, 0)
d_end = BFS(N, graphr, N - 1)
nodes_start = [i for i in range(N) if d_start[i] >= 0]
nodes_end = [i for i in range(N) if d_end[i] >= 0]
new_edges = []
for (A, B, C) in edges:
	if A in nodes_start and B in nodes_start and (A in nodes_end) and (B in nodes_end):
		new_edges.append((A, B, C))
(d, Nega) = BF(N, 0, new_edges)
if Nega == True:
	print(-1)
else:
	print(max(-d[N - 1], 0))


(n, m, p) = map(int, input().split())
edge = []
inv_link = [[] for _ in range(n)]
link = [[] for _ in range(n)]
for i in range(m):
	(a, b, cost) = list(map(int, input().split()))
	edge.append([a - 1, b - 1, p - cost])
	link[a - 1].append(b - 1)
	inv_link[b - 1].append(a - 1)

def bell(edges, start, num_v):
	cost = [float('inf')] * num_v
	cost[start] = 0
	for _ in range(num_v):
		updated = False
		for (a, b, c) in edges:
			if cost[b] > cost[a] + c:
				cost[b] = cost[a] + c
				updated = True
		if not updated:
			break
	else:
		return (False, cost)
	return (True, cost)
from collections import deque
Q = deque()
Q.append([n - 1, 0])
visited = [-1] * n
visited[n - 1] = 0
while Q:
	(now, cnt) = Q.popleft()
	for nxt in inv_link[now]:
		if visited[nxt] != -1:
			continue
		visited[nxt] = cnt + 1
		Q.append([nxt, cnt + 1])
new_edge = []
for (a, b, c) in edge:
	if visited[a] == -1 or visited[b] == -1:
		continue
	new_edge.append([a, b, c])
(ret, cost) = bell(new_edge, 0, n)
if ret:
	print(max(0, -cost[-1]))
else:
	print(-1)


(n, m, p) = map(int, input().split())
G = []
for i in range(m):
	(a, b, c) = map(int, input().split())
	G.append([a, b, c - p])

def bellmanFord(G, start_vertex):
	distance = [-float('inf') for i in range(n + 1)]
	distance[start_vertex] = 0
	for i in range(2 * n):
		for (node, neighbor, cost) in G:
			if distance[neighbor] < distance[node] + cost:
				if i < n:
					distance[neighbor] = distance[node] + cost
				else:
					distance[neighbor] = float('inf')
		if i == n - 1:
			prev_ans = distance[n]
	if prev_ans != distance[n]:
		return -1
	else:
		return distance[n]
distance = bellmanFord(G, 1)
if distance == -1:
	ans = -1
else:
	ans = max(0, distance)
print(ans)


import sys
sys.setrecursionlimit(1000000000)
ii = lambda : int(input())
miis = lambda : map(int, input().split())
lmiis = lambda : list(miis())

def reachable_nodeset(start, inc):
	reachable = set()
	reachable.add(start)
	nodes = [start]
	while nodes:
		n = nodes.pop()
		for i in inc[n]:
			if i not in reachable:
				nodes.append(i)
				reachable.add(i)
	return reachable

def bellmanford(num, start, goal, edges):
	cost = [float('inf')] * num
	cost[start] = 0
	for _ in range(num):
		updated = False
		for (a, b, c) in edges:
			if cost[b] > cost[a] + c:
				cost[b] = cost[a] + c
				updated = True
		if not updated:
			break
	else:
		return -1
	return max(0, -cost[goal])

def solve():
	(N, M, P) = miis()
	to = [[] for _ in range(N)]
	ot = [[] for _ in range(N)]
	edges = []
	for _ in range(M):
		(a, b, c) = miis()
		a -= 1
		b -= 1
		c = -(c - P)
		to[a].append(b)
		ot[b].append(a)
		edges.append((a, b, c))
	reachableFromZero = reachable_nodeset(0, to)
	reachableToN = reachable_nodeset(N - 1, ot)
	ok = reachableFromZero.intersection(reachableToN)
	edges = tuple(((a, b, c) for (a, b, c) in edges if a in ok and b in ok))
	print(bellmanford(N, 0, N - 1, edges))

def __starting_point():
	solve()
__starting_point()


(n, m, p) = map(int, input().split())
abc = []
edge = [[] for _ in range(n)]
rev_edge = [[] for _ in range(n)]
for _ in range(m):
	(a, b, c) = map(int, input().split())
	abc.append((a - 1, b - 1, p - c))
	edge[a - 1].append(b - 1)
	rev_edge[b - 1].append(a - 1)

def dfs(e, s):
	v_set = {s}
	stack = [s]
	while stack:
		v = stack.pop()
		for next_v in e[v]:
			if next_v in v_set:
				continue
			v_set.add(next_v)
			stack.append(next_v)
	return v_set

def bellman_ford(sub_abc, n):
	inf = float('inf')
	dist = [inf] * n
	dist[0] = 0
	for _ in range(n):
		updated = False
		for (u, v, d) in sub_abc:
			if dist[u] + d < dist[v]:
				dist[v] = dist[u] + d
				updated = True
		if not updated:
			break
	else:
		return -1
	return max(-dist[n - 1], 0)
use = dfs(edge, 0) & dfs(rev_edge, n - 1)
print(bellman_ford([(a, b, c) for (a, b, c) in abc if a in use and b in use], n))


import sys
input = sys.stdin.readline
(n, m, p) = [int(j) for j in input().split()]

def bellman_ford(v, s, e):
	INF = 10 ** 18
	d = [INF] * v
	d[s] = 0
	for _ in range(v):
		f = False
		for (a, b, c) in e:
			cost = d[a] + c
			if cost < d[b]:
				d[b] = cost
				f = True
		if not f:
			break
	else:
		print(-1)
		exit()
	print(max(-d[-1], 0))

def dfs(edge, s):
	use = {s}
	q = [s]
	while q:
		v = q.pop()
		for w in edge[v]:
			if w in use:
				continue
			use.add(w)
			q.append(w)
	return use
l = [[] for i in range(n)]
ll = [[] for i in range(n)]
e = []
for i in range(m):
	(a, b, c) = [int(j) for j in input().split()]
	l[a - 1].append(b - 1)
	ll[b - 1].append(a - 1)
	e.append((a - 1, b - 1, p - c))
use = dfs(l, 0) & dfs(ll, n - 1)
e = [(a, b, c) for (a, b, c) in e if a in use and b in use]
bellman_ford(n, 0, e)


import collections
(N, M, P) = [int(_) for _ in input().split()]
ABC = [[int(_) for _ in input().split()] for _ in range(M)]
cd = collections.defaultdict
Ga = cd(set)
Gb = cd(set)
G = []
ok = cd(int)
for (a, b, c) in ABC:
	Ga[a].add(b)
	Gb[b].add(a)
for pair in [[1, Ga], [N, Gb]]:
	S = pair
	Gn = pair.pop()
	visited = [0] * (N + 1)
	while S:
		s = S.pop()
		if not visited[s]:
			visited[s] = 1
			ok[s] += 1
			for ns in Gn[s]:
				S += [ns]
for (a, b, c) in ABC:
	if ok[a] == ok[b] == 2:
		G += [(a, b, P - c)]

def bellman_ford(V, G, i):
	INF = float('inf')
	D = [INF] * V
	D[i] = 0
	for _ in range(V):
		update = False
		for (s, t, d) in G:
			if D[s] != INF and D[t] > D[s] + d:
				D[t] = D[s] + d
				update = True
		if not update:
			return D
		if _ == V:
			return False
a = bellman_ford(N + 1, G, 1)
try:
	print(max(-a[-1], 0))
except:
	print(-1)


def dfs(edge, s):
	use = {s}
	q = [s]
	while q:
		v = q.pop()
		for w in edge[v]:
			if w in use:
				continue
			use.add(w)
			q.append(w)
	return use

def bellman_ford(v, s, t, e):
	d = [10 ** 18] * v
	d[s] = 0
	for _ in range(v):
		f = False
		for (a, b, c) in e:
			cost = d[a] + c
			if cost < d[b]:
				d[b] = cost
				f = True
		if not f:
			break
	else:
		return -1
	return max(-d[t], 0)

def main():
	import sys
	input = sys.stdin.readline
	(n, m, p) = map(int, input().split())
	edge = [[] for _ in range(n)]
	edge_rev = [[] for _ in range(n)]
	e = []
	for _ in range(m):
		(a, b, c) = map(int, input().split())
		edge[a - 1].append(b - 1)
		edge_rev[b - 1].append(a - 1)
		e.append((a - 1, b - 1, p - c))
	use = dfs(edge, 0) & dfs(edge_rev, n - 1)
	e = [(a, b, c) for (a, b, c) in e if a in use and b in use]
	print(bellman_ford(n, 0, n - 1, e))

def __starting_point():
	main()
__starting_point()


import collections
(n, m, p) = map(int, input().split())
dist1 = []
edges1 = [[] for _ in range(n)]
edges2 = [[] for _ in range(n)]
for _ in range(m):
	(a, b, c) = map(int, input().split())
	a -= 1
	b -= 1
	dist1.append((a, b, (c - p) * -1))
	edges1[a].append(b)
	edges2[b].append(a)

def dfs(edges, start):
	stack = [start]
	use = {start}
	while stack:
		x = stack.pop()
		for y in edges[x]:
			if y in use:
				continue
			stack.append(y)
			use.add(y)
	return use

def bellman_ford(dist):
	v = [float('inf')] * n
	v[0] = 0
	for _ in range(n):
		updated = False
		for (a, b, c) in dist:
			if v[a] + c < v[b]:
				updated = True
				v[b] = v[a] + c
		if not updated:
			return max(0, v[n - 1] * -1)
	return -1
use = dfs(edges1, 0) & dfs(edges2, n - 1)
dist2 = [(a, b, c) for (a, b, c) in dist1 if a in use and b in use]
print(bellman_ford(dist2))


import sys
sys.setrecursionlimit(10 ** 9)

def BF(edges, n, s):
	inf = float('inf')
	d = [inf for i in range(n + 1)]
	d[s] = 0
	update = True
	for i in range(n + 1):
		update = False
		for (v, u, cost) in edges:
			if cand[v] != 1 or cand[u] != 1:
				continue
			if d[v] != inf and d[u] > d[v] + cost:
				d[u] = d[v] + cost
				update = True
		if update == False:
			break
		if i == n:
			return inf
	return -d[-1]

def DFS_1toN(v):
	for u in to[v]:
		if toflag[u] == 0:
			toflag[u] = 1
			DFS_1toN(u)

def DFS_Nto1(v):
	for u in ot[v]:
		if otflag[u] == 0:
			otflag[u] = 1
			DFS_Nto1(u)
(n, m, p) = map(int, input().split())
g = []
to = [[] for _ in range(n + 1)]
ot = [[] for _ in range(n + 1)]
for _ in range(m):
	(a, b, c) = map(int, input().split())
	g.append([a, b, -(c - p)])
	to[a].append(b)
	ot[b].append(a)
toflag = [0] * (n + 1)
toflag[1] = 1
DFS_1toN(1)
otflag = [0] * (n + 1)
otflag[n] = 1
DFS_Nto1(n)
cand = [0] * (n + 1)
for i in range(1, n + 1):
	cand[i] = toflag[i] * otflag[i]
ans = BF(g, n, 1)
if ans == float('inf'):
	print(-1)
else:
	print(max(ans, 0))


import sys
from collections import deque
input = sys.stdin.readline

class Bellman_Ford:

	def __init__(self, v=None, e=None, lis=None, start=None, end=None, inf=float('inf')):
		self.V = v
		self.E = e
		self.lis = lis
		self.start = start if start else 0
		self.end = end if end else self.V - 1
		self.inf = inf
		self.close_minus = False

	def getlist(self, lis):
		self.lis = lis

	def def_start(self, s):
		self.start = s

	def def_end(self, e):
		self.end = e

	def def_inf(self, inf):
		self.inf = inf

	def def_vertice(self, v):
		self.V = v

	def def_edge(self, e):
		self.E = e

	def prepare(self):
		self.cost = [self.inf] * self.V
		self.cost[self.start] = 0

	def search(self):
		for i in range(self.V):
			update = False
			for (x, y, z) in self.lis:
				if self.cost[y] > self.cost[x] + z:
					self.cost[y] = self.cost[x] + z
					update = True
			if not update:
				break
			if i == self.V - 1:
				self.close_minus = True
				return False
		return True

	def main(self):
		self.prepare()
		self.search()

	def cost_all(self):
		return self.cost

def main():
	(n, m, p) = map(int, input().split())
	graph = [None] * m
	tree_forth = [[] for _ in range(n)]
	tree_back = [[] for _ in range(n)]
	for i in range(m):
		(a, b, c) = map(int, input().split())
		tree_forth[a - 1].append(b - 1)
		tree_back[b - 1].append(a - 1)
		c = p - c
		graph[i] = (a - 1, b - 1, c)
	(already1, already2) = ([True] * n, [True] * n)
	for (already, tree, start) in zip([already1, already2], [tree_forth, tree_back], [0, n - 1]):
		not_yet = deque([start])
		already[start] = False
		while not_yet:
			key = not_yet.pop()
			for v in tree[key]:
				if already[v] == False:
					continue
				already[v] = False
				not_yet.append(v)
	graph_new = []
	for i in range(m):
		(a, b, c) = graph[i]
		if already1[a] or already1[b] or already2[a] or already2[b]:
			continue
		graph_new.append((a, b, c))
	bf = Bellman_Ford(v=n, e=m, lis=graph_new)
	bf.main()
	if bf.close_minus:
		print(-1)
	else:
		print(max(0, -bf.cost[n - 1]))

def __starting_point():
	main()
__starting_point()


(N, M, P) = map(int, input().split())
edges = []
edges1 = [[] for _ in range(N)]
for i in range(M):
	(a, b, c) = map(int, input().split())
	edges1[b - 1].append(a - 1)
	edges.append((a - 1, b - 1, -c + P))
from collections import deque
que = deque([N - 1])
flag = [True] * N
flag[-1] = False
while que:
	v = que.pop()
	for e in edges1[v]:
		if flag[e]:
			que.append(e)
			flag[e] = False

def f(n, w, es):
	d = [float('inf')] * n
	d[0] = 0
	for i in range(n):
		for j in range(w):
			e = es[j]
			if flag[e[1]]:
				continue
			if d[e[1]] > d[e[0]] + e[2]:
				d[e[1]] = d[e[0]] + e[2]
				if i == n - 1:
					return False
	return d
d = f(N, M, edges)
if d:
	print(max(-d[-1], 0))
else:
	print(-1)


from collections import deque
import sys

class mEdge:

	def __init__(self, _from, _to, _cost):
		self.From = _from
		self.to = _to
		self.cost = _cost

class bellman_ford:

	def __init__(self, V):
		self.G = []
		self._V = N
		self._E = 0

	def add(self, _from, _to, _cost):
		self.G.append(mEdge(_from, _to, _cost))
		self._E += 1

	def shortest_path(self, s):
		d = [10 ** 20] * (self._V + 1)
		d[s] = 0
		for _ in range(self._V):
			flag = False
			for edge in self.G:
				newlen = d[edge.From] + edge.cost
				if newlen < d[edge.to]:
					flag = True
					d[edge.to] = newlen
			if not flag:
				break
		return d

	def have_negative_circle(self):
		d = [10 ** 20] * (self._V + 1)
		d[1] = 0
		for i in range(1, self._V + 1):
			flag = False
			for edge in self.G:
				newlen = d[edge.From] + edge.cost
				if newlen < d[edge.to]:
					flag = True
					d[edge.to] = newlen
			if not flag:
				break
			if i == self._V:
				return True
		return False
sys.setrecursionlimit(10 ** 7)
(N, M, P) = map(int, input().split())
graph = []
d = deque()
from_1 = [[] for i in range(N + 1)]
from_N = [[] for i in range(N + 1)]
for _ in range(M):
	(a, b, c) = map(int, input().split())
	from_1[a].append(b)
	from_N[b].append(a)
	d.append((a, b, P - c))
accessible_1 = [False for i in range(N + 1)]
accessible_N = [False for i in range(N + 1)]

def dfs_1(s):
	accessible_1[s] = True
	for node in from_1[s]:
		if accessible_1[node] == False:
			dfs_1(node)

def dfs_N(s):
	accessible_N[s] = True
	for node in from_N[s]:
		if accessible_N[node] == False:
			dfs_N(node)
dfs_1(1)
dfs_N(N)
isok = [False for i in range(N + 1)]
for i in range(N + 1):
	if accessible_1[i] and accessible_N[i]:
		isok[i] = True
G = bellman_ford(N)
while d:
	(p, q, weight) = d.popleft()
	if isok[p] and isok[q]:
		G.add(p, q, weight)
if G.have_negative_circle():
	print(-1)
else:
	print(max(0, -G.shortest_path(1)[N]))


from collections import deque

def solve():
	(N, M, P) = map(int, input().split())
	G = [[] for i in range(N)]
	RG = [[] for i in range(N)]
	for i in range(M):
		(a, b, c) = map(int, input().split())
		G[a - 1].append((b - 1, P - c))
		RG[b - 1].append((a - 1, P - c))

	def chk(s, G):
		P = [0] * N
		que = deque([s])
		used = [0] * N
		used[s] = 1
		while que:
			v = que.popleft()
			P[v] = 1
			for (w, c) in G[v]:
				if used[w]:
					continue
				que.append(w)
				used[w] = 1
		return P
	P0 = chk(0, G)
	P1 = chk(N - 1, RG)
	F = [P0[i] == P1[i] == 1 for i in range(N)]
	I = [i for i in range(N) if F[i]]
	E0 = []
	for v in I:
		for (w, c) in G[v]:
			if not F[w]:
				continue
			E0.append((v, w, c))
	dist = [10 ** 18] * N
	dist[0] = 0
	L = len(I)
	for i in range(L):
		update = 0
		for (v, w, c) in E0:
			x = c + dist[v]
			if x < dist[w]:
				dist[w] = x
				update = 1
		if not update:
			break
	else:
		print('-1')
		return
	print(max(-dist[N - 1], 0))
solve()


import time
t1 = time.time()
(N, M, P) = list(map(int, input().split()))
ABC = [tuple(map(int, input().split())) for _ in range(M)]
to_w = [set() for _ in range(N)]
for (a, b, c) in ABC:
	to_w[a - 1].add((b - 1, P - c))
reachable_from_start = {0}
q = {0}
while q:
	u = q.pop()
	for (v, _) in to_w[u]:
		if v not in reachable_from_start:
			reachable_from_start.add(v)
			q.add(v)
from_w = [set() for _ in range(N)]
for (a, b, c) in ABC:
	from_w[b - 1].add((a - 1, P - c))
reachable_to_goal = {N - 1}
q = {N - 1}
while q:
	u = q.pop()
	for (v, _) in from_w[u]:
		if v not in reachable_to_goal:
			reachable_to_goal.add(v)
			q.add(v)
reachable = reachable_from_start & reachable_to_goal
to_w = [set() for _ in range(N)]
for (a, b, c) in ABC:
	if a - 1 in reachable and b - 1 in reachable:
		to_w[a - 1].add((b - 1, P - c))
d = [float('inf')] * N
d[0] = 0
q = {0}
while q:
	u = q.pop()
	for (v, w) in to_w[u]:
		tmp = d[u] + w
		if tmp < d[v]:
			d[v] = tmp
			if v not in q:
				q.add(v)
		if time.time() > 1.9 + t1:
			d[-1] = -float('inf')
			break
	else:
		continue
	break
if d[-1] == -float('inf'):
	print(-1)
else:
	print(max(0, -d[-1]))


import sys
from collections import deque

def dfs(x, s):
	used = {s}
	search = deque([s])
	while search:
		ss = search.pop()
		for sss in x[ss]:
			if sss in used:
				continue
			used.add(sss)
			search.append(sss)
	return used

def bellmanford(edges):
	coins = [-float('inf')] * N
	coins[0] = 0
	for _ in range(N):
		f = True
		for (u, v, c) in edges:
			if coins[u] + c > coins[v]:
				coins[v] = coins[u] + c
				f = False
		if f:
			return max(0, coins[-1])
	return -1
(N, M, P) = map(int, input().split())
ABC = []
Adake = [[] for _ in range(N)]
Bdake = [[] for _ in range(N)]
for _ in range(M):
	(A, B, C) = map(int, input().split())
	ABC.append((A - 1, B - 1, C - P))
	Adake[A - 1].append(B - 1)
	Bdake[B - 1].append(A - 1)
U = dfs(Adake, 0) & dfs(Bdake, N - 1)
ABC = [(a, b, c) for (a, b, c) in ABC if a in U and b in U]
print(bellmanford(ABC))


import sys
input = sys.stdin.readline
from collections import *

def bfs(s, g):
	q = deque([s])
	dist = [-1] * N
	dist[s] = 0
	while q:
		v = q.popleft()
		for nv in g[v]:
			if dist[nv] == -1:
				dist[nv] = dist[v] + 1
				q.append(nv)
	return dist

def bellman_ford():
	dist = [10 ** 18] * N
	dist[0] = 0
	for i in range(N):
		for (s, t, w) in edges:
			if d1[s] == -1 or d2[t] == -1:
				continue
			if dist[t] > dist[s] + w:
				dist[t] = dist[s] + w
				if i == N - 1:
					print(-1)
					exit()
	return -dist[N - 1]
(N, M, P) = map(int, input().split())
G = [[] for _ in range(N)]
rG = [[] for _ in range(N)]
edges = []
for _ in range(M):
	(A, B, C) = map(int, input().split())
	G[A - 1].append(B - 1)
	rG[B - 1].append(A - 1)
	edges.append((A - 1, B - 1, -(C - P)))
d1 = bfs(0, G)
d2 = bfs(N - 1, rG)
print(max(0, bellman_ford()))


(N, M, P) = map(int, input().split())
ABC = [list(map(int, input().split())) for i in range(M)]
c = [[] for i in range(N)]
v = [0] * N
for (a, b, _) in ABC:
	c[a - 1].append(b - 1)
q = [0]
while q:
	p = q.pop()
	if v[p] == 0:
		v[p] = 1
		for n in c[p]:
			if n != p:
				q.append(n)
inf = 10 ** 20
f = lambda i, j, w: [i - 1, j - 1, P - w]
edge = [f(i, j, w) for (i, j, w) in ABC if v[i - 1]]

def bf(N, s):
	d = [inf] * N
	d[s] = 0
	prev = [-1] * N
	for i in range(N - 1):
		f = 0
		for (i, j, w) in edge:
			if d[j] > d[i] + w:
				d[j] = d[i] + w
				prev[j] = i
				f = 1
		if f == 0:
			break
	while True:
		f = 0
		for (i, j, w) in edge:
			if d[j] > d[i] + w and (d[j] != -inf or d[i] != -inf):
				d[i] = d[j] = -inf
				f = 1
		if f == 0:
			break
	return d
d = bf(N, 0)
print(max(0, -d[-1]) if d[-1] > -inf else -1)


import sys
sys.setrecursionlimit(1000000000)
ii = lambda : int(input())
mis = lambda : list(map(int, input().split()))
lmis = lambda : list(mis())

def main():
	(N, M, P) = mis()
	to = [[] for _ in range(N)]
	ot = [[] for _ in range(N)]
	edges = []
	for i in range(M):
		(a, b, c) = mis()
		a -= 1
		b -= 1
		c = -(c - P)
		to[a].append(b)
		ot[b].append(a)
		edges.append((a, b, c))
	reachableFrom0 = set()
	reachableToN = set()
	reachableFrom0.add(0)
	nodes = [0]
	while nodes:
		node = nodes.pop()
		for i in to[node]:
			if i not in reachableFrom0:
				nodes.append(i)
				reachableFrom0.add(i)
	reachableToN.add(N - 1)
	nodes = [N - 1]
	while nodes:
		node = nodes.pop()
		for i in ot[node]:
			if i not in reachableToN:
				nodes.append(i)
				reachableToN.add(i)
	ok = reachableFrom0 & reachableToN
	edges = tuple(((a, b, c) for (a, b, c) in edges if a in ok and b in ok))
	V = [float('inf')] * N
	V[0] = 0

	def bf(n):
		for _ in range(n):
			upd = False
			for (a, b, c) in edges:
				if V[b] > V[a] + c:
					V[b] = V[a] + c
					upd = True
			if not upd:
				return False
		return True
	bf(N)
	if not bf(1):
		print(max(-V[-1], 0))
	else:
		print(-1)
main()


import sys
sys.setrecursionlimit(10000000)
f_inf = sys.maxsize
(N, M, P) = map(int, input().split())
ABC_d = {}
for m in range(M):
	(A, B, C) = map(int, input().split())
	if A in ABC_d:
		ABC_d[A].append([B, C - P])
	else:
		ABC_d[A] = [[B, C - P]]
cost = [-f_inf] * N
cost[0] = 0

def solve(pos, cnt):
	global cost
	if not pos in ABC_d:
		return
	if cnt > N:
		cost[pos - 1] = f_inf
	for (B, C) in ABC_d[pos]:
		if cost[B - 1] == f_inf or cost[B - 1] >= cost[pos - 1] + C:
			pass
		elif cost[pos - 1] == f_inf:
			cost[B - 1] = f_inf
			solve(B, cnt + 1)
		elif cost[B - 1] < cost[pos - 1] + C:
			cost[B - 1] = cost[pos - 1] + C
			solve(B, cnt + 1)
solve(1, 1)
if cost[-1] == sys.maxsize:
	print(-1)
else:
	print(max(0, int(cost[-1])))


import collections
import sys
import time
sys.setrecursionlimit(10 ** 5)
(N, M, P) = map(int, input().split())
e = collections.defaultdict(list)
st = time.time()
for _ in range(M):
	(u, v, c) = map(int, input().split())
	e[u].append((v, c))

def isConnected(n):
	if n == N:
		return True
	vis = [0] * (N + 1)
	que = collections.deque([])
	que.append(n)
	while que:
		nc = que.popleft()
		vis[nc] = 1
		for o in e[nc]:
			if o[0] == N:
				return True
			if vis[o[0]] == 0:
				que.append(o[0])
	return False
inf = 10 ** 10
visited = [0] * (N + 1)
earn = [-inf] * (N + 1)
earn[1] = 0

def dfs(now):
	global visited
	global earn
	nt = time.time() - st
	if nt > 1.95:
		print(max(earn[N], 0))
		exit(0)
	visited[now] = 1
	for n in e[now]:
		if visited[n[0]] == 0:
			if n[1] + earn[now] - P > earn[n[0]]:
				earn[n[0]] = n[1] + earn[now] - P
				dfs(n[0])
				visited[n[0]] = 0
		elif earn[n[0]] < n[1] + earn[now] - P:
			if isConnected(n[0]) == True:
				print(-1)
				exit(0)
dfs(1)
print(max(earn[N], 0))