Driving Exam

No attempts yetTime limit1sMemory limit128 MB

Problem

The driving licence exam in Byteotia takes place on a grid of streets. There are nn straight, parallel, one-way streets running north, meaning you may only drive from south to north on them. Each of these north-south streets is exactly mm meters long, and they all begin and end at the same latitude. They are numbered 11 to nn from west to east.

There are also pp one-way horizontal streets, each perpendicular to the north-south streets. Every horizontal street connects a pair of adjacent north-south streets and is oriented either east or west. Where an east-oriented and a west-oriented horizontal street coincide, they form a two-way connection.

Sample testing area

A sample testing area (n=4n=4, m=3m=3, p=5p=5).

The examiner picks one north-south street as the start (the exam begins at its southern end) and another as the finish. The candidate must drive from the start to the finish while respecting every one-way direction.

The examiner may only start the exam on a street from which it is possible to reach the northern end of every north-south street. Call such a street a valid starting street.

There are usually only a few valid starting streets, which makes the examiners' work tedious. The board wants to build at most kk new horizontal streets (each oriented east or west and connecting a pair of adjacent north-south streets) so as to create as many new valid starting streets as possible. The original layout may or may not already contain valid starting streets.

Write a program that reads the layout and the number kk, then outputs the largest number of new valid starting streets that can be created by building at most kk new horizontal streets.

Input

The first line contains four integers nn, mm, pp, and kk (2n1000002 \le n \le 100000, 1m,k1000001 \le m, k \le 100000, 0p1000000 \le p \le 100000): the number of north-south streets, their length, the number of existing horizontal streets, and the maximum number of new horizontal streets that may be built. The north-south streets are numbered 11 to nn from west to east.

Each of the next pp lines contains three integers nin_i, mim_i, and did_i (1nin11 \le n_i \le n-1, 0mim0 \le m_i \le m, di{0,1}d_i \in \{0, 1\}) describing one horizontal street. It connects north-south streets nin_i and ni+1n_i+1, meeting each of them mim_i meters from its southern end. If di=0d_i = 0 the street is oriented east (from street nin_i toward street ni+1n_i+1); if di=1d_i = 1 it is oriented west (from street ni+1n_i+1 toward street nin_i).

Output

Print a single integer: the largest number of new valid starting streets that can be created by building at most kk new horizontal streets. A new horizontal street may meet the north-south streets at a non-integer distance from their southern end, and new horizontal streets may overlap to form two-way connections.

Hint

Hint figure

In the sample layout, the southern ends of streets 11 and 33 can be turned into valid starting streets, for example.