PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#DSU0000061. Dò mìn phản ứng dây chuyền (Unusual Minesweeper)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Dò mìn phản ứng dây chuyền (Unusual Minesweeper)

Unusual Minesweeper

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn mines at distinct integer coordinates in the plane. Mine ii is located at (xi,yi)(x_i,y_i) and automatically explodes at integer time tit_i if it has not exploded earlier.

When a mine explodes, every unexploded mine on the same row or column at distance at most kk explodes immediately. Newly triggered mines can cause further explosions, and the entire chain reaction takes no additional time. In addition to automatic explosions, you may manually detonate at most one mine at each integer time, starting at time 00.

Find the earliest integer time by which every mine can have exploded, assuming you choose the times and positions of manual detonations optimally.

Input

The first line contains n,kn,k. The following nn lines contain xi,yi,tix_i,y_i,t_i.

Output

Print the minimum completion time in integer seconds.

Subtasks

  • Subtask 1 (20 points): 1≤n≤201\le n\le20, 0≤k≤200\le k\le20, ∣xi∣,∣yi∣≤100|x_i|,|y_i|\le100, 0≤ti≤200\le t_i\le20; All coordinate pairs (xi,yi)(x_i,y_i) are distinct.
  • Subtask 2 (30 points): 1≤n≤30001\le n\le3000, 0≤k≤1050\le k\le10^5, ∣xi∣,∣yi∣≤105|x_i|,|y_i|\le10^5, 0≤ti≤1050\le t_i\le10^5; All coordinate pairs (xi,yi)(x_i,y_i) are distinct.
  • Subtask 3 (50 points): 1≤n≤2⋅1051\le n\le2\cdot10^5, 0≤k≤1090\le k\le10^9, ∣xi∣,∣yi∣≤109|x_i|,|y_i|\le10^9, 0≤ti≤1090\le t_i\le10^9; All coordinate pairs (xi,yi)(x_i,y_i) are distinct.

Examples

Input

5 0
0 0 1
0 1 4
1 0 2
1 1 3
2 2 9

Output

2

Explanation

With k=0k=0 and distinct positions, no explosion triggers another mine. Manually detonate the mines with deadlines 99, 44, and 33 at times 00, 11, and 22, respectively; the other two detonate automatically at times 11 and 22. Finishing before time 22 is impossible because only two manual detonations can occur by the end of time 11, while three mines have later automatic explosion times.