#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };

    ($next:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, bytes) => {
        read_value!($next, String).into_bytes()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

struct Solver {
    n: usize,
    graph: Vec<Vec<usize>>,
    visited: Vec<bool>,
    cache: Vec<Option<bool>>,
}

impl Solver {
    fn new(n: usize, graph: &Vec<Vec<usize>>) -> Solver {
        Solver {
            n: n,
            graph: graph.clone(),
            visited: vec![false; n],
            cache: vec![None; n],
        }
    }

    fn can_go(&mut self, pos: usize) -> bool {
        if self.cache[pos].is_some() {
            return self.cache[pos].unwrap();
        }

        self.visited = vec![false; self.n];
        self.cache[pos] = Some(self.dfs(pos));
        return self.cache[pos].unwrap();
    }

    fn dfs(&mut self, pos: usize) -> bool {
        if self.visited[pos] {
            return false;
        }
        if pos == self.n - 1 {
            return true;
        }

        self.visited[pos] = true;
        let mut next = vec![];

        for &to in &self.graph[pos] {
            if self.visited[to] {
                continue;
            }
            next.push(to);
        }

        for to in next {
            let res = self.dfs(to);
            if res {
                return true;
            }
        }

        false
    }
}

fn main() {
    input! {
        n: usize,
        m: usize,
        p: i64,
        edge: [(usize1, usize1, i64); m],
    }

    let mut dist: Vec<Option<i64>> = vec![None; n];
    dist[0] = Some(0);

    let mut graph = vec![vec![]; n];
    for &(f, t, _) in &edge {
        graph[f].push(t);
    }

    let mut solver = Solver::new(n, &graph);

    for i in 0..n {
        solver.can_go(i);
        // println!("{}, {}", i, solver.can_go(i));
    }

    for i in 0..(2 * n) {
        for &(from, to, cost) in &edge {
            if dist[from].is_none() {
                continue;
            }
            if dist[to].is_none() {
                dist[to] = Some(dist[from].unwrap() + cost - p);
            } else {
                let next = std::cmp::max(dist[from].unwrap() + cost - p, dist[to].unwrap());
                if i >= n && next > dist[to].unwrap() && solver.can_go(to) {
                    println!("{}", -1);
                    return;
                }
                dist[to] = Some(next);
            }
        }
    }
    if dist[n - 1].is_some() {
        println!("{}", std::cmp::max(dist[n - 1].unwrap(), 0));
    } else {
        println!("-1");
    }
}


Measured memory_bytes: 4476