// -*- coding:utf-8-unix -*- use std::{cmp::Reverse, collections::BinaryHeap, io::prelude::*}; fn main() { let stdin = std::io::stdin(); let mut lines = stdin.lock().lines(); let (n, m, x) = { let l = lines.next().unwrap().unwrap(); let mut t = l.split_ascii_whitespace(); let n = t.next().unwrap().parse::().unwrap(); let m = t.next().unwrap().parse::().unwrap(); let x = t.next().unwrap().parse::().unwrap(); (n, m, x) }; let n1 = n - 1; let mut g = (0..n) .map(|_| Vec::<(u32, u32, u32)>::new()) .collect::>(); let mut yokan = (0u32, 1000000000u32); let mut queue = BinaryHeap::with_capacity(n + m); for _ in 0..m { let l = lines.next().unwrap().unwrap(); let mut t = l.split_ascii_whitespace(); let u = t.next().unwrap().parse::().unwrap() - 1; let v = t.next().unwrap().parse::().unwrap() - 1; let a = t.next().unwrap().parse::().unwrap(); let b = t.next().unwrap().parse::().unwrap(); g[u as usize].push((v, a, b)); g[v as usize].push((u, a, b)); } while yokan.0 < yokan.1 { let pivot = (yokan.1 - yokan.0 + 1) / 2 + yokan.0; let mut dists = vec![1u64 << 60; n]; dists[0] = 0; queue.clear(); queue.push(Reverse((0u64, 0usize))); while let Some(Reverse((pd, i))) = queue.pop() { if dists[i] < pd { continue; } for &(j, a, b) in g[i].iter() { let ju = j as usize; let nd = pd + (a as u64); if b < pivot || nd > x || dists[ju] <= nd { continue; } dists[ju] = nd; if ju == n1 { queue.clear(); break; } queue.push(Reverse((nd, ju))); } } if dists[n1] <= x { yokan.0 = pivot; } else { yokan.1 = pivot - 1; } } println!( "{}", if yokan.0 == 0 { "-1".to_string() } else { yokan.0.to_string() } ); }