import java.util.*; import java.io.*; public class Main { static ContestScanner sc = new ContestScanner(System.in); static ContestPrinter pw = new ContestPrinter(System.out); static StringBuilder sb = new StringBuilder(); static long mod = (long) 1e9 + 7; static ArrayList> G; static boolean[] used; static int[] depth; static int[] my = {-1,0,0,1}; static int[] mx = {0,-1,1,0}; public static void main(String[] args) throws Exception { //int T = sc.nextInt(); //for(int i = 0; i < T; i++)solve(); solve(); pw.flush(); } static ArrayList> map; public static void solve() { map = new ArrayList<>(); int N = sc.nextInt(); int M = sc.nextInt(); long X = sc.nextInt(); for(int i = 0; i < N; i++){ map.add(new ArrayList<>()); } for(int i = 0; i < M; i++){ int u = sc.nextInt()-1; int v = sc.nextInt()-1; int a = sc.nextInt(); int b = sc.nextInt(); map.get(u).add(new int[]{v,a,b}); map.get(v).add(new int[]{u,a,b}); } int left = 0; int right = (int)1e9+1; while(right - left > 1){ int mid = (right+left)/2; if(bfs(X,mid)){ left = mid; }else{ right = mid; } } pw.println(left == 0 ? -1 : left); } static boolean bfs(long timeLimit, int width){ int N = map.size(); long[] minTime = new long[N]; Arrays.fill(minTime,timeLimit+1); minTime[0] = 0; PriorityQueue pq = new PriorityQueue<>(new ArrayComparator()); pq.add(new long[]{0L,0L}); while(pq.size() > 0){ long[] now = pq.poll(); long time = now[0]; int v = (int)now[1]; if(v == N-1) return true; if(minTime[v] < time) continue; for(int[] edge : map.get(v)){ int nextV = edge[0]; long plusT = edge[1]; int maxW = edge[2]; if(minTime[nextV] > plusT+time && maxW >= width){ minTime[nextV] = plusT+time; pq.add(new long[]{plusT+time,nextV}); } } } return false; } static class ArrayComparator implements Comparator { @Override public int compare(long[] a1, long[] a2) { for (int i = 0; i < a1.length; i++) { if (a1[i] < a2[i]) { return -1; } else if (a1[i] > a2[i]) { return 1; } } return 0; } } static class GeekInteger { public static void save_sort(int[] array) { shuffle(array); Arrays.sort(array); } public static int[] shuffle(int[] array) { int n = array.length; Random random = new Random(); for (int i = 0, j; i < n; i++) { j = i + random.nextInt(n - i); int randomElement = array[j]; array[j] = array[i]; array[i] = randomElement; } return array; } public static void save_sort(long[] array) { shuffle(array); Arrays.sort(array); } public static long[] shuffle(long[] array) { int n = array.length; Random random = new Random(); for (int i = 0, j; i < n; i++) { j = i + random.nextInt(n - i); long randomElement = array[j]; array[j] = array[i]; array[i] = randomElement; } return array; } } } /** * refercence : https://github.com/NASU41/AtCoderLibraryForJava/blob/master/ContestIO/ContestScanner.java */ class ContestScanner { private final java.io.InputStream in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private static final long LONG_MAX_TENTHS = 922337203685477580L; private static final int LONG_MAX_LAST_DIGIT = 7; private static final int LONG_MIN_LAST_DIGIT = 8; public ContestScanner(java.io.InputStream in){ this.in = in; } public ContestScanner(java.io.File file) throws java.io.FileNotFoundException { this(new java.io.BufferedInputStream(new java.io.FileInputStream(file))); } public ContestScanner(){ this(System.in); } private boolean hasNextByte() { if (ptr < buflen) { return true; }else{ ptr = 0; try { buflen = in.read(buffer); } catch (java.io.IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte(); } public String next() { if (!hasNext()) throw new java.util.NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while(isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new java.util.NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { int digit = b - '0'; if (n >= LONG_MAX_TENTHS) { if (n == LONG_MAX_TENTHS) { if (minus) { if (digit <= LONG_MIN_LAST_DIGIT) { n = -n * 10 - digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("%d%s... is not number", n, Character.toString(b)) ); } } } else { if (digit <= LONG_MAX_LAST_DIGIT) { n = n * 10 + digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("%d%s... is not number", n, Character.toString(b)) ); } } } } throw new ArithmeticException( String.format("%s%d%d... overflows long.", minus ? "-" : "", n, digit) ); } n = n * 10 + digit; }else if(b == -1 || !isPrintableChar(b)){ return minus ? -n : n; }else{ throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next()); } public long[] nextLongArray(int length){ long[] array = new long[length]; for(int i=0; i array) { this.printArray(array, " "); } public void printArray(ArrayList array, String separator) { int n = array.size() - 1; if (n == -1) return; for (int i = 0; i < n; i++) { super.print(array.get(i).toString()); super.print(separator); } super.println(array.get(n).toString()); } public void printlnArray(ArrayList array) { int n = array.size(); for (int i = 0; i < n; i++) super.println(array.get(i).toString()); } public void printlnArray(ArrayList array, java.util.function.IntUnaryOperator map) { int n = array.size(); for (int i = 0; i < n; i++) super.println(map.applyAsInt(array.get(i))); } public void printlnArray(ArrayList array, java.util.function.LongUnaryOperator map) { int n = array.size(); for (int i = 0; i < n; i++) super.println(map.applyAsLong(array.get(i))); } public void printArray(int[][] array) { for (int[] a : array) this.printArray(a); } public void printArray(int[][] array, java.util.function.IntUnaryOperator map) { for (int[] a : array) this.printArray(a, map); } public void printArray(long[][] array) { int n = array.length; if (n == 0) return; int m = array[0].length - 1; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) super.print(array[i][j] + " "); super.println(array[i][m]); } } public void printArray(long[][] array, java.util.function.LongUnaryOperator map) { int n = array.length; if (n == 0) return; int m = array[0].length - 1; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { super.print(map.applyAsLong(array[i][j])); super.print(" "); } super.println(map.applyAsLong(array[i][m])); } } public void printArray(boolean[][] array) { int n = array.length; if (n == 0) return; int m = array[0].length - 1; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) super.print(array[i][j] ? "○ " : "× "); super.println(array[i][m] ? "○" : "×"); } } public void printArray(char[][] array) { int n = array.length; if (n == 0) return; int m = array[0].length; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) super.print(array[i][j]); super.println(); } } }