Under construction

Still being built. Things may change

Gridev's Protocol

Time limit2sMemory limit512 MB

Summary
Given M error cells on an N by N grid, find the fewest row or column clears (each allowed when the line has at most K errors) that remove every error, or report -1.
Level

Medium6 of 10

Topics
BFS, Graph, Greedy
Solved
No attempts yet

Problem

Seongjun is a robot that solves any problem with ease. Geungsu, jealous of him, damaged his circuit board while he was asleep.

Fortunately, Seongjun has a self-repair protocol, and he plans to use it to fix himself. Seongjun's circuit board is an N×NN \times N grid, and currently there are errors at MM coordinates (xi,yi)(x_i, y_i). Seongjun can repeat the following two kinds of actions.

  1. For every row with at most KK errors, remove all errors in that row. The state of a row with more than KK errors does not change.
  2. For every column with at most KK errors, remove all errors in that column. The state of a column with more than KK errors does not change.

Seongjun wants to repair himself quickly and get revenge on Geungsu. Find the way to remove all errors with the fewest actions.

Input

The first line contains the size of the board NN, the number of errors MM, and the maximum number of errors KK that can be repaired in one row or column, separated by spaces. (1≤N,M,K≤2×1051 \le N, M, K \le 2\times10^5)

From the second line to the (M+1)(M+1)-th line, the positions of the errors (xi,yi)(x_i, y_i) are given, separated by spaces. (1≤xi,yi≤N1 \le x_i, y_i \le N)

No error position is given more than once.

Output

Print the minimum number of actions needed to remove all errors. If there is no way to remove all errors, print -1.

Examples3

  1. Example 1

    Input
    3 4 1
    1 1
    2 1
    2 2
    3 2
    
    Expected output
    2
    
  2. Example 2

    Input
    2 4 1
    1 1
    2 1
    1 2
    2 2
    
    Expected output
    -1
    
  3. Example 3

    Input
    1 1 1
    1 1
    
    Expected output
    1