Problem: There is a directed graph with N vertices numbered 1 to N and M edges. The i-th edge is directed from Vertex A_i to Vertex B_i, and there are C_i coins placed along that edge. Additionally, there is a button on Vertex N.

We will play a game on this graph. You start the game on Vertex 1 with zero coins, and head for Vertex N by traversing the edges while collecting coins. It takes one minute to traverse an edge, and you can collect the coins placed along the edge each time you traverse it. As usual in games, even if you traverse an edge once and collect the coins, the same number of coins will reappear next time you traverse that edge, which you can collect again.

When you reach Vertex N, you can end the game by pressing the button. (You can also choose to leave Vertex N without pressing the button and continue traveling.) However, when you end the game, you will be asked to pay T \times P coins, where T is the number of minutes elapsed since the start of the game. If you have less than T \times P coins, you will have to pay all of your coins instead.

Your score will be the number of coins you have after this payment. Determine if there exists a maximum value of the score that can be obtained. If the answer is yes, find that maximum value.

Constraints

* 2 \leq N \leq 2500
* 1 \leq M \leq 5000
* 1 \leq A_i, B_i \leq N
* 1 \leq C_i \leq 10^5
* 0 \leq P \leq 10^5
* All values in input are integers.
* Vertex N can be reached from Vertex 1.

Input

Input is given from Standard Input in the following format:


N M P
A_1 B_1 C_1
:
A_M B_M C_M


Output

If there exists a maximum value of the score that can be obtained, print that maximum value; otherwise, print `-1`.

Examples

Input

3 3 10
1 2 20
2 3 30
1 3 45


Output

35


Input

2 2 10
1 2 100
2 2 100


Output

-1


Input

4 5 10
1 2 1
1 4 1
3 4 1
2 2 100
3 3 100


Output

0

Solution (in JAVA):

Solution:
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Set;

public class Main {
    static final class Vertex {
        final int id;
        long distance;
        int previous;
        Vertex(int id, long distance, int previous) {
            if (id < 0 || previous < -1) {
                throw new IllegalArgumentException();
            }
            this.id = id;
            this.distance = distance;
            this.previous = previous;
        }
        @Override
        public int hashCode() {
            return Objects.hash(distance, id, previous);
        }
        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj == null) {
                return false;
            }
            if (!(obj instanceof Vertex)) {
                return false;
            }
            Vertex other = (Vertex)obj;
            return distance == other.distance
                   && id == other.id
                   && previous == other.previous;
        }
        @Override
        public String toString() {
            return "Vertex [id="
                   + id
                   + ", distance="
                   + distance
                   + ", previous="
                   + previous
                   + "]";
        }
    }
    static final class Edge {
        final int src;
        final int dest;
        final long length;
        Edge(int src, int dest, long length) {
            if (src < 0 || dest < 0) {
                throw new IllegalArgumentException();
            }

            this.src = src;
            this.dest = dest;
            this.length = length;
        }
        @Override
        public int hashCode() {
            return Objects.hash(dest, src, length);
        }
        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj == null) {
                return false;
            }
            if (!(obj instanceof Edge)) {
                return false;
            }
            Edge other = (Edge)obj;
            return dest == other.dest
                   && src == other.src
                   && length == other.length;
        }
        @Override
        public String toString() {
            return "Edge [src="
                   + src
                   + ", dest="
                   + dest
                   + ", length="
                   + length
                   + "]";
        }
    }
    static final class BellmanFord {
        static final class NegativeCircleException extends Exception {
            private static final long serialVersionUID = 1L;
        }
        private static void dfs(Map<Integer, List<Integer>> m,
                                Set<Integer> s,
                                int i) {
            if (s.contains(i)) {
                return;
            }
            s.add(i);
            for (Integer j : m.getOrDefault(i, Collections.emptyList())) {
                dfs(m, s, j);
            }
        }
        static List<Edge> reachableEdgesFromBothDir(Collection<Edge> edges,
                                                    int src,
                                                    int dest) {
            Map<Integer, List<Integer>> srcToDest = new HashMap<>();
            Map<Integer, List<Integer>> destToSrc = new HashMap<>();
            for (Edge edge : edges) {
                srcToDest.computeIfAbsent(edge.src, k -> new ArrayList<>())
                         .add(edge.dest);
                destToSrc.computeIfAbsent(edge.dest, k -> new ArrayList<>())
                         .add(edge.src);
            }
            Set<Integer> reachableFromSrc = new HashSet<>();
            Set<Integer> reachableFromDest = new HashSet<>();
            dfs(srcToDest, reachableFromSrc, src);
            dfs(destToSrc, reachableFromDest, dest);

            List<Edge> result = new ArrayList<>();
            for (Edge edge : edges) {
                if (reachableFromSrc.contains(edge.src)
                    && reachableFromSrc.contains(edge.dest)
                    && reachableFromDest.contains(edge.src)
                    && reachableFromDest.contains(edge.dest)) {
                    result.add(edge);
                }
            }
            return result;
        }
        static List<Vertex> exec(int vertexNumber,
                                 Collection<Edge> edges,
                                 int start) throws NegativeCircleException {
            List<Vertex> vertexes =
                    new ArrayList<>(Collections.nCopies(vertexNumber, null));
            vertexes.set(start, new Vertex(start, 0, -1));

            for (int i = 0; i < vertexNumber; i++) {
                for (Edge edge : edges) {
                    Vertex src = vertexes.get(edge.src);
                    if (src == null) {
                        continue;
                    }
                    long newDistance = src.distance + edge.length;
                    Vertex dest = vertexes.get(edge.dest);
                    if (dest == null) {
                        vertexes.set(edge.dest,
                                     new Vertex(edge.dest,
                                                newDistance,
                                                src.id));
                    } else if (dest.distance > newDistance) {
                        dest.distance = newDistance;
                        dest.previous = src.id;
                    } else {
                        continue;
                    }
                    if (i == vertexNumber - 1) {
                        throw new NegativeCircleException();
                    }
                }
            }
            for (int i = 0; i < vertexes.size(); i++) {
                if (vertexes.get(i) == null) {
                    vertexes.set(i, new Vertex(i, Long.MAX_VALUE, -1));
                }
            }
            return vertexes;
        }
    }
    static void solve(MyScanner in, MyWriter out) {
        int N = in.nextInt();
        int M = in.nextInt();
        int P = in.nextInt();
        List<Edge> edges = new ArrayList<>(M);
        for (int i = 0; i < M; i++) {
            int A = in.nextInt();
            int B = in.nextInt();
            int C = in.nextInt();
            edges.add(new Edge(A - 1, B - 1, -(C - P)));
        }
        List<Edge> reachable =
                BellmanFord.reachableEdgesFromBothDir(edges, 0, N - 1);
        try {
            List<Vertex> result = BellmanFord.exec(N, reachable, 0);
            out.println(Math.max(0, -result.get(N - 1).distance));
        } catch (BellmanFord.NegativeCircleException e) {
            out.println(-1);
        }
    }

    public static void main(String[] args) {
        MyWriter w = new MyWriter(System.out);
        solve(new MyScanner(System.in), w);
        w.flush();
    }

    static final class MyScanner {
        private final InputStream in;
        MyScanner(InputStream in) {
            this.in = new BufferedInputStream(in);
        }
        private int readByte() {
            try {
                return in.read();
            } catch (IOException e) {
                throw new AssertionError(null, e);
            }
        }
        private static boolean isPrintableCharExceptSpace(int c) {
            return 33 <= c && c <= 126;
        }
        String next() {
            int c = readByte();
            while (!(c == -1 || isPrintableCharExceptSpace(c))) {
                c = readByte();
            }
            if (c == -1) {
                throw new NoSuchElementException();
            }
            StringBuilder b = new StringBuilder();
            do {
                b.appendCodePoint(c);
                c = readByte();
            } while (c != -1 && isPrintableCharExceptSpace(c));
            return b.toString();
        }
        long nextLong() {
            int c = readByte();
            while (!(c == -1 || isPrintableCharExceptSpace(c))) {
                c = readByte();
            }
            if (c == -1) {
                throw new NoSuchElementException();
            }
            boolean minus = false;
            if (c == '-') {
                minus = true;
                c = readByte();
            }
            long result = 0L;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                result = result * 10L + (c - '0');
                c = readByte();
            } while (c != -1 && isPrintableCharExceptSpace(c));
            return minus ? -result : result;
        }
        int nextInt() {
            long n = nextLong();
            if (n < Integer.MIN_VALUE || n > Integer.MAX_VALUE) {
                throw new InputMismatchException();
            }
            return (int)n;
        }
        double nextDouble() {
            return Double.parseDouble(next());
        }
        int[] nextIntArray(int n) {
            int[] result = new int[n];
            for (int i = 0; i < n; i++) {
                result[i] = nextInt();
            }
            return result;
        }
        private static boolean allSameLength(int[] a, int[] b, int[]... c) {
            if (a.length != b.length) {
                return false;
            }
            for (int[] element : c) {
                if (a.length != element.length) {
                    return false;
                }
            }
            return true;
        }
        private static boolean allSameLength(char[] a, char[] b, char[]... c) {
            if (a.length != b.length) {
                return false;
            }
            for (char[] element : c) {
                if (a.length != element.length) {
                    return false;
                }
            }
            return true;
        }
        void nextVerticalIntArrays(int[] a, int[] b, int[]... c) {
            if (!allSameLength(a, b, c)) {
                throw new IllegalArgumentException();
            }
            for (int i = 0; i < a.length; i++) {
                a[i] = nextInt();
                b[i] = nextInt();
                for (int[] d : c) {
                    d[i] = nextInt();
                }
            }
        }
        long[] nextLongArray(int n) {
            long[] result = new long[n];
            for (int i = 0; i < n; i++) {
                result[i] = nextLong();
            }
            return result;
        }
        char nextChar() {
            int c = readByte();
            while (!(c == -1 || isPrintableCharExceptSpace(c))) {
                c = readByte();
            }
            if (c == -1) {
                throw new NoSuchElementException();
            }
            return (char)c;
        }
        char[] nextCharArray(int n) {
            char[] result = new char[n];
            for (int i = 0; i < n; i++) {
                result[i] = nextChar();
            }
            return result;
        }
        char[][] next2dCharArray(int n, int m) {
            char[][] result = new char[n][m];
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    result[i][j] = nextChar();
                }
            }
            return result;
        }
        void nextVerticalCharArrays(char[] a, char[] b, char[]... c) {
            if (!allSameLength(a, b, c)) {
                throw new IllegalArgumentException();
            }
            for (int i = 0; i < a.length; i++) {
                a[i] = nextChar();
                b[i] = nextChar();
                for (char[] d : c) {
                    d[i] = nextChar();
                }
            }
        }
    }
    static final class MyWriter extends PrintWriter {
        MyWriter(OutputStream out) {
            super(out);
        }
        void joinAndPrintln(int[] x) {
            joinAndPrintln(x, " ");
        }
        void joinAndPrintln(int[] x, String delimiter) {
            StringBuilder b = new StringBuilder();
            if (x.length > 0) {
                b.append(x[0]);
                for (int i = 1; i < x.length; i++) {
                    b.append(delimiter).append(x[i]);
                }
            }
            println(b.toString());
        }
        void joinAndPrintln(Iterable<?> iterable) {
            joinAndPrintln(iterable, " ");
        }
        void joinAndPrintln(Iterable<?> iterable, String delimiter) {
            StringBuilder b = new StringBuilder();
            for (Iterator<?> i = iterable.iterator(); i.hasNext();) {
                b.append(i.next());
                while (i.hasNext()) {
                    b.append(delimiter).append(i.next());
                }
            }
            println(b.toString());
        }
    }
}