macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let mut s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

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

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

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

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

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

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


#[cfg(debug_assertions)]
macro_rules! mydbg {
    //($arg:expr) => (dbg!($arg))
    ($arg:expr) => (println!("{:?}",$arg));
}


#[cfg(not(debug_assertions))]
macro_rules! mydbg {
    ($($arg:expr),*) => {};
}


use std::cmp;
use std::collections::HashMap;
use std::cell::RefCell;


use std::ops::{ Add, Sub , Div , Mul };
static global_mod : i64 = 1000000007;

#[derive(Debug,Copy,Clone)]
pub struct ModInt {
    x : i64
}

impl ModInt{
    pub fn  new (p:i64) -> Self {
        let a = (p % global_mod + global_mod)% global_mod;     
        return ModInt{x:a};
    }

    pub fn inv (self) -> Self{
        return self.pow(global_mod-2);

    }

    pub fn pow (self , t:i64) -> Self{
        if(t==0 ) {return ModInt::new(1)};
        let mut a = self.pow(t>>1);
        a = a*a;
        if(t&1 != 0) {a = a* self};
        return a;
    }
}
impl Add for ModInt {
    type Output = ModInt;
    fn add(self,other : ModInt) -> ModInt{
        let ret  = self.x + other.x ;
        return ModInt::new(ret);
    }
}
impl Sub for ModInt {
    type Output = ModInt;
    fn sub(self,other : ModInt) -> ModInt{
        let ret  = self.x - other.x;
        return ModInt::new(ret);
    }
}
impl Mul for ModInt {
    type Output = ModInt;
    fn mul(self,other : ModInt) -> ModInt{
        let ret  = self.x * other.x ;
        return ModInt::new(ret);
    }
}

impl Div for ModInt {
    type Output = ModInt;
    fn div(self,other : ModInt) -> ModInt{
        let ret  = self.x * other.inv().x;
       return ModInt::new(ret);
    }
}

impl std::string::ToString for ModInt{
    fn to_string(&self) -> String{
        return self.x.to_string();
    }
}


pub struct Combination{
    fact : Vec<ModInt>,
    ifact : Vec<ModInt>
}

impl Combination{
    pub fn new(n:i32)->Self{
        let mut fact = vec![ModInt::new(0);(n+1) as usize];
        let mut ifact = vec![ModInt::new(0);(n+1) as usize];
        fact[0] = ModInt::new(1);
        for i in 1..n+1 { fact[i as usize] = fact[(i-1) as usize] * ModInt::new(i as i64) };
        ifact[n as usize] = fact[n as usize].inv();
        for i in (1..n+1).rev(){ ifact[(i-1) as usize] = ifact[i as usize]* ModInt::new(i as i64) }


        let a = Combination{
            fact : fact,
            ifact : ifact
        };
        return a;
    }

    pub fn gen(self , n:i32 , k:i32)-> ModInt{
        mydbg!(self.fact[n as usize]);
        mydbg!(self.ifact[k as usize]);
        mydbg!(self.ifact[(n-k) as usize]);
        if (k < 0 || k > n) {return ModInt::new(0 as i64)};
        return self.fact[n as usize]*self.ifact[k as usize]*self.ifact[(n-k) as usize];

    }
}




fn main() {
    

  input! { 
         n: i32,
         m: i32,
         p: i64,
         nodes : [(i32,i32,i64);m],
    }


    let INF = 999999999;
    let mut nodes2 = vec![(0,0,0);m as usize];
    let mut edges = vec![INF;n as usize];
    let mut to = vec![vec![];n as usize];
    let mut from = vec! [ vec![]; n as usize];
    //let mut canFrom1 = RefCell::new(vec![false;n as usize]);
    let mut canFrom1 = vec![false;(n+1) as usize];
    //let mut canToN = RefCell::new(vec![false;n as usize]);
    let mut canToN = vec![false;(n+1) as usize];


    for i in 0..m{
        let a = nodes[i as usize];
        nodes2[i as usize] = (a.0 -1,a.1 -1 ,(a.2 - p ) * -1);
        to[(a.0-1) as usize].push(a.1 -1);
        from[(a.1-1) as usize].push(a.0 -1);
    }

    fn dfs(index :i32, state: &mut Vec<bool>, to : &Vec<Vec<i32>>){
        if state[index as usize] {return;}
        state[index as usize] = true;
        for item in &to[index as usize]{
            dfs(*item,state, to);
        }
    }
    fn rdfs(index :i32, state: &mut Vec<bool>, from : &Vec<Vec<i32>>){
        if state[index as usize] {return;}
        state[index as usize] = true;
        for item in &from[index as usize]{
            rdfs(*item,state,from);
        }
    }

    dfs(0, &mut canFrom1,&to);
    rdfs(n-1, &mut canToN , &from);

    mydbg!(canFrom1);
    mydbg!(canToN);
    edges[0] = 0;


    mydbg!(nodes2);
    for j in 0..n+1{
        for node in &nodes2{
            if !(canFrom1[node.1  as usize] && canToN[node.1  as usize]) { continue;}
            edges[node.1 as usize] =  cmp::min( edges[node.0 as usize] +  node.2  , edges[node.1 as usize]);
        }
        mydbg!(edges);
    }

    let mut heiro = false;
    
    for node in &nodes2{
        if !(canFrom1[node.1  as usize] && canToN[node.1  as usize]) { continue;}
        if( edges[node.0 as usize] +  node.2  < edges[node.1 as usize]){
            heiro = true;
            mydbg!("heiro");
            break;
        }
    }

    let mut ans = 0;
    if(heiro){ans=-1}
    else{
        ans = edges[(n-1) as usize] *-1 ;
        mydbg!(ans);
        ans = cmp::max(ans,0);
    }

    println!("{}",ans);

}


Measured memory_bytes: 4476