This page is still under construction.

Parts of this page are still being built. What you see may change.

Ladder Manipulation

Interview

Time limit2sMemory limit512 MB

Summary
Given a ladder with N vertical lines, H rows, and M existing rungs, find the minimum number of rungs to add so every walk from column i ends at column i, or report -1 if more than 3.
Level

Hard8 of 10

Topics
Backtracking, Brute force, Implementation, Simulation
Solved
No attempts yet

Problem

A ladder game is built from NN vertical lines and MM horizontal lines. A horizontal line can be placed between two adjacent vertical lines. Every vertical line has HH positions that can hold a horizontal line, and those positions are the same on every vertical line. The picture below shows N=5N = 5, H=6H = 6 with no horizontal line.

The green lines are the vertical lines, and each point where a green line meets a dotted line can hold a horizontal line. A horizontal line must connect two adjacent vertical lines. Two horizontal lines must not be consecutive and must not touch each other. A horizontal line must also lie on a dotted line.

This picture has 5 horizontal lines. Each horizontal line connects two adjacent vertical lines, and it connects positions that can hold a horizontal line.

The ladder game runs separately for each vertical line and moves from the top of that vertical line downward. When the walk meets a horizontal line, it crosses to the neighboring vertical line along that horizontal line and then continues downward on the new vertical line.

In this picture, 1 ends at 3, 2 ends at 2, 3 ends at 5, 4 ends at 1, and 5 ends at 4. The two pictures below show how 1 and 2 move.

Vertical line 1Vertical line 2

You want to change the outcome of the game by adding horizontal lines to the ladder. A walk that starts at vertical line ii must end at vertical line ii. Write a program that finds the minimum number of horizontal lines you have to add.

Input

The first line contains the number of vertical lines NN, the number of horizontal lines MM, and the number of positions HH on each vertical line that can hold a horizontal line. (2≤N≤102 \le N \le 10, 1≤H≤301 \le H \le 30, 0≤M≤(N−1)×H0 \le M \le (N-1) \times H)

Each of the next MM lines describes one horizontal line with two integers aa and bb. (1≤a≤H1 \le a \le H, 1≤b≤N−11 \le b \le N-1) It means that vertical line bb and vertical line b+1b+1 are connected at dotted line position aa.

The topmost dotted line is number 1, and the number grows by 1 for each step down. The leftmost vertical line is number 1, and the number grows by 1 for each step to the right.

No two horizontal lines given in the input are consecutive.

Output

Print the minimum number of horizontal lines you have to add so that a walk starting at vertical line ii ends at vertical line ii. If the answer is greater than 3, print -1. If it is impossible, print -1 as well.

Hint

The ladder with N=5N = 5, M=5M = 5, H=6H = 6The same ladder after 3 horizontal lines are added
The ladder with N=5N = 5, M=6M = 6, H=6H = 6The same ladder after 2 horizontal lines are added

Examples5

  1. Example 1

    Input
    2 0 3
    
    Expected output
    0
    
  2. Example 2

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

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

    Input
    6 5 6
    1 1
    3 2
    1 3
    2 5
    5 5
    
    Expected output
    3
    
  5. Example 5

    Input
    5 6 6
    1 1
    3 1
    5 2
    4 3
    2 3
    1 4
    
    Expected output
    2