시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 512 MB1022100.000%

문제

A famous logical problem is that of connecting 9 dots on a paper by drawing 4 line segments with a pencil, while never lifting the pencil from the paper. While this is easy enough (although it requires some thinking outside of the box), Simone has recently been building a game called “Connect the Dots” around a generalisation of the concept.

In Connect the Dots, you are presented with a 4 × 4 regular grid of dots. Each dot is given a unique number between 1 and 16. The task is then to connect the dots in order by their numbers, starting with dot 1 and ending with dot 16. The dots should be connected using as few line segments as possible, starting at dot 1, with the end of each segment being the start point of the next. The segments are allowed to intersect and overlap one another. Additionally, it is allowed to pass through other points while trying to connect the current point. This means, for example, that visiting the first four points in the order 1, 4, 2, 3, 2, 4, . . . is acceptable. Formally, the sequence 1, 2, . . . , 15, 16 must be a subsequence of the sequence of dots visited.

Figure C.1: A solution to the first sample.

Simone asked you to try the puzzle out, while betting you a balloon that it would be too hard. Prove her wrong by writing a program that solves the puzzle for you!

입력

The input consists of:

• 4 lines, each with 4 integers, the numbers of the dots in the grid. The jth number on the ith line is the number of the jth dot in the ith row of the grid of dots.

The 16 numbers in the input are all between 1 and 16 (inclusive) and pairwise distinct.

출력

Output the minimum number of line segments needed to connect all the dots in order.

예제 입력 1

1 2 3 4
10 11 12 5
9 16 6 13
8 7 15 14


예제 출력 1

6


예제 입력 2

1 2 3 4
8 9 10 11
7 15 16 12
6 14 13 5


예제 출력 2

7


출처

• 문제를 만든 사람: Johan Sannemo