Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process.

There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems.

You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day.

Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days.

Input

The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). 

The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost.

Output

Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities.

If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes).

Examples

Input

2 6 5
1 1 0 5000
3 2 0 5500
2 2 0 6000
15 0 2 9000
9 0 1 7000
8 0 2 6500


Output

24500


Input

2 4 5
1 2 0 5000
2 1 0 4500
2 1 0 3000
8 0 1 6000


Output

-1

Note

The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more.

In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
import java.io.*;
import java.util.*;
import java.math.BigInteger;
import java.util.Map.Entry;

import static java.lang.Math.*;

public class B extends PrintWriter {

    class Flight implements Comparable<Flight> {
        final int d, f, t, c;

        public Flight(int d, int f, int t, int c) {
            this.d = d;
            this.f = f;
            this.t = t;
            this.c = c;
        }

        @Override
        public int compareTo(Flight flight) {
            return Integer.compare(d, flight.d);
        }
    }

    void run() {
        final int n = nextInt(), m = nextInt(), k = nextInt();

        final int w = 1_000_003;

        Flight[] f = new Flight[m];

        for (int j = 0; j < m; j++) {
            f[j] = new Flight(nextInt(), nextInt() - 1, nextInt() - 1, nextInt());
        }

        Arrays.sort(f);

        final long inf = (long) 1e15;

        long[] l = new long[w], r = new long[w];
        long ans = inf;

        Arrays.fill(l, inf);
        Arrays.fill(r, inf);

        long[] b = new long[n];
        {
            int cnt = 0;
            Arrays.fill(b, inf);
            long cur = 0;
            int p = 0;

            for (int d = 0; d < w; d++) {
                while (p < m && f[p].d < d) {
                    Flight flight = f[p++];

                    int u = flight.f;
                    int v = flight.t;

                    if (u != -1 && v == -1) {
                        long c = flight.c;

                        if (c < b[u]) {
                            if (b[u] == inf) {
                                ++cnt;
                                cur += c;
                            } else {
                                cur -= b[u];
                                cur += c;
                            }
                            b[u] = c;
                        }
                    }
                }

                if (cnt == n) {
                    l[d] = cur;
                }
            }
        }

        {
            int cnt = 0;
            Arrays.fill(b, inf);
            long cur = 0;
            int p = m - 1;

            for (int d = w - 1; d >= 0; d--) {
                while (p >= 0 && d < f[p].d) {
                    Flight flight = f[p--];

                    int v = flight.f;
                    int u = flight.t;

                    if (u != -1 && v == -1) {
                        long c = flight.c;

                        if (c < b[u]) {
                            if (b[u] == inf) {
                                ++cnt;
                                cur += c;
                            } else {
                                cur -= b[u];
                                cur += c;
                            }
                            b[u] = c;
                        }
                    }
                }

                if (cnt == n) {
                    r[d] = cur;
                }
            }
        }

        for (int to = k - 1; to < w; to++) {
            int from = to - k + 1;
            ans = min(ans, l[from] + r[to]);

        }

        if (ans < inf) {
            println(ans);
        } else {
            print(-1);
        }
    }

    void skip() {
        while (hasNext()) {
            next();
        }
    }

    int[][] nextMatrix(int n, int m) {
        int[][] matrix = new int[n][m];
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
                matrix[i][j] = nextInt();
        return matrix;
    }

    String next() {
        while (!tokenizer.hasMoreTokens())
            tokenizer = new StringTokenizer(nextLine());
        return tokenizer.nextToken();
    }

    boolean hasNext() {
        while (!tokenizer.hasMoreTokens()) {
            String line = nextLine();
            if (line == null) {
                return false;
            }
            tokenizer = new StringTokenizer(line);
        }
        return true;
    }

    int[] nextArray(int n) {
        int[] array = new int[n];
        for (int i = 0; i < n; i++) {
            array[i] = nextInt();
        }
        return array;
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    String nextLine() {
        try {
            return reader.readLine();
        } catch (IOException err) {
            return null;
        }
    }

    public B(OutputStream outputStream) {
        super(outputStream);
    }

    static BufferedReader reader;
    static StringTokenizer tokenizer = new StringTokenizer("");
    static Random rnd = new Random();
    static boolean OJ;

    public static void main(String[] args) throws IOException {
        OJ = System.getProperty("ONLINE_JUDGE") != null;
        B solution = new B(System.out);
        if (OJ) {
            reader = new BufferedReader(new InputStreamReader(System.in));
            solution.run();
        } else {
            reader = new BufferedReader(new FileReader(new File(B.class.getName() + ".txt")));
            long timeout = System.currentTimeMillis();
            while (solution.hasNext()) {
                solution.run();
                solution.println();
                solution.println("----------------------------------");
            }
            solution.println("time: " + (System.currentTimeMillis() - timeout));
        }
        solution.close();
        reader.close();
    }
}