846_D. Monitor_8870
Recently Luba bought a monitor. Monitor is a rectangular matrix of size n × m. But then she started to notice that some pixels cease to work properly. Luba thinks that the monitor will become broken the first moment when it contains a square k × k consisting entirely of broken pixels. She knows that q pixels are already broken, and for each of them she knows the moment when it stopped working. Help Luba to determine when the monitor became broken (or tell that it's still not broken even after all q pixels stopped working).

Input

The first line contains four integer numbers n, m, k, q (1 ≤ n, m ≤ 500, 1 ≤ k ≤ min(n, m), 0 ≤ q ≤ n·m) — the length and width of the monitor, the size of a rectangle such that the monitor is broken if there is a broken rectangle with this size, and the number of broken pixels.

Each of next q lines contain three integer numbers xi, yi, ti (1 ≤ xi ≤ n, 1 ≤ yi ≤ m, 0 ≤ t ≤ 109) — coordinates of i-th broken pixel (its row and column in matrix) and the moment it stopped working. Each pixel is listed at most once.

We consider that pixel is already broken at moment ti.

Output

Print one number — the minimum moment the monitor became broken, or "-1" if it's still not broken after these q pixels stopped working.

Examples

Input

2 3 2 5
2 1 8
2 2 8
1 2 1
1 3 4
2 3 2


Output

8


Input

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


Output

-1
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.Comparator;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        D solver = new D();
        solver.solve(1, in, out);
        out.close();
    }

    static class D {
        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int rows = in.readInt();
            int cols = in.readInt();
            int k = in.readInt();
            int q = in.readInt();
            Pixel[] pixels = new Pixel[q];
            for (int i = 0; i < pixels.length; i++) {
                pixels[i] = new Pixel();
                pixels[i].row = in.readInt() - 1;
                pixels[i].col = in.readInt() - 1;
                pixels[i].t = in.readInt();
            }
            Arrays.sort(pixels, Comparator.comparing((Pixel p) -> p.t));
            int low = 0, high = pixels.length - 1;
            while (low < high) {
                int mid = (low + high) / 2;
                if (broken(pixels, mid, k, rows, cols)) {
                    high = mid;
                } else {
                    low = mid + 1;
                }
            }
            if (low < pixels.length && broken(pixels, low, k, rows, cols)) {
                out.printLine(pixels[low].t);
            } else {
                out.printLine(-1);
            }
        }

        long query(int row, int col, int k, long[][] sums) {
            int highRow = row + k;
            int highCol = col + k;
            return sums[highRow][highCol] - sums[highRow][col] - sums[row][highCol] + sums[row][col];
        }

        private boolean broken(Pixel[] pixels, int pixelEnd, int k, int rows, int cols) {
            int[][] hit = new int[rows][cols];
            for (int i = 0; i <= pixelEnd; i++) {
                Pixel pixel = pixels[i];
                hit[pixel.row][pixel.col] = 1;
            }
            long[][] partialSums = ArrayUtils.partialSums(hit);
            for (int row = 0; row + k <= rows; row++) {
                for (int col = 0; col + k <= cols; col++) {
                    if (query(row, col, k, partialSums) == k * k) {
                        return true;
                    }
                }
            }
            return false;
        }

        class Pixel {
            int row;
            int col;
            int t;

        }

    }

    static class OutputWriter {
        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void close() {
            writer.close();
        }

        public void printLine(int i) {
            writer.println(i);
        }

    }

    static class ArrayUtils {
        public static long[][] partialSums(int[][] array) {
            int height = array.length;
            int width = array[0].length;
            long[][] result = new long[height + 1][width + 1];
            for (int i = 1; i <= height; i++) {
                for (int j = 1; j <= width; j++) {
                    result[i][j] = result[i][j - 1] + result[i - 1][j] - result[i - 1][j - 1] + array[i - 1][j - 1];
                }
            }
            return result;
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputReader.SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int readInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }
}


				    		  	 		 			  	 	 	   	