#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define dump(...)
#endif
#define endl "\n"
#define ll long long
#define int ll
#define BUF 1e5
#define INF 1 << 30
#define vi vector<int>
#define vvi vector<vector<int>>
#define all(a) (a).begin(), (a).end()
#define bit(n) (1LL << (n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
constexpr ll mod = 1e9 + 7;
ll A, B, C, D, G, H, N, M, L, K, P, Q, R, W, X, Y, Z;
string S, T;
ll ans = 0;

template <typename T>
istream &operator>>(istream &is, vector<T> &vec) {
    for (T &x : vec)
        is >> x;
    return is;
}

class Graph {
    struct edge {
        int from, to, cost;
        edge(int from, int to, int cost) : from(from), to(to), cost(cost) {}
    };

public:
    int verN;
    int edgeN = 0;
    vector<edge> edge_set;
    vector<vector<int>> adj_list;
    vector<vector<int>> dist;
    Graph(int verN) : verN(verN), adj_list(verN), dist(verN, vector<int>(verN, INF)) {}

    void add_edge(int from, int to, int cost = 1) {
        edge_set.push_back(edge(from, to, cost));
        adj_list[from].push_back(to);
        dist[from][to] = cost;
        edgeN++;
    }

    bool bellmanford(int start) {
        vector<int> &d = dist[start];
        for (int i = 0; i < verN; i++) {
            d[i] = INF;
        }
        d[start] = 0;
        for (int i = 0; i < verN; i++) {
            for (int j = 0; j < edgeN; j++) {
                edge e = edge_set[j];
                if (d[e.from] != INF && d[e.to] > d[e.from] + e.cost) {
                    d[e.to] = d[e.from] + e.cost;
                    if (i == verN - 1) {
                        return false;
                    }
                }
            }
        }
        return true;
    }

    void dijkstra(int start) {
        vector<int> &d = dist[start];
        d[start] = 0;
        priority_queue<int, vector<int>, greater<int>> pq;
        vector<int> used(verN, 0);
        pq.push(0);
        while (!pq.empty()) {
            int now = pq.top();
            pq.pop();
            used[now] = 1;
            for (int adj : adj_list[now]) {
                if (used[adj])
                    continue;
                if (d[adj] > d[now] + dist[now][adj]) {
                    d[adj] = d[now] + dist[now][adj];
                    pq.push(adj);
                }
            }
        }
    }

    void warshallfloyd() {
        for (int k = 0; k < verN; k++) {         // 経由する頂点
            for (int i = 0; i < verN; i++) {     // 始点
                for (int j = 0; j < verN; j++) { // 終点
                    dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
                }
            }
        }
    }
};

signed main() {
    cin >> N >> M >> P;
    Graph g(N);
    for (int i = 0; i < M; i++) {
        cin >> A >> B >> C;
        A--, B--;
        C = -(C - P);
        g.add_edge(B, A, C);
    }
    set<int> set;
    vi used(N, 0);
    queue<int> q;
    q.push(N - 1);
    set.insert(N - 1);
    used[N - 1] = 1;
    dump(g.adj_list);
    while (!q.empty()) {
        int now = q.front();
        q.pop();
        for (auto adj : g.adj_list[now]) {
            set.insert(adj);
            if (used[adj])
                continue;
            q.push(adj);
            used[adj] = 1;
        }
    }
    dump(set);
    Graph g1(N);
    for (int i = 0; i < M; i++) {
        if (set.count(g.edge_set[i].from) && set.count(g.edge_set[i].to)) {
            g1.add_edge(g.edge_set[i].to, g.edge_set[i].from, g.edge_set[i].cost);
        }
    }
    dump(g1.adj_list);
    if (g1.bellmanford(0)) {
        dump(g1.dist);
        cout << max(-g1.dist[0][N - 1], 0LL) << endl;
    } else {
        cout << -1 << endl;
    }
}

Measured memory_bytes: 98944