Fall Guys-Perfect Match
题号:NC241124
时间限制:C/C++/Rust/Pascal 12秒,其他语言24秒
空间限制:C/C++/Rust/Pascal 1024 M,其他语言2048 M
64bit IO Format: %lld

题目描述

In the famous game "Fall Guys:Ultimate Knockout", there's a level called "Perfect Match". We have simplified the rule of the game as follows: You stand on an grid and there are m kinds of fruits. Also, each grid is labeled with some kind of fruit. It is guaranteed that each kind of fruit appears on at least 1 and at most 20 grids. You can choose to stand on any grid initially, then a fruit will appear on the big screen in the game, you need to run to a grid such that the fruit labeled on it is the same as the fruit on the screen, within the countdown. In the game, you can only move horizontally or vertically, i.e. if you stand on (x_1,y_1) and move to (x_2,y_2), the distance you move is .



You want to select a grid to stand on initially, to minimize the distance you need to move in the worst case of all m possible fruits to appear on the big screen, so that hopefully you will not be knocked out even in the worst case! Calculate this minimized distance.

输入描述:

The first line contains two integers , denoting the size of the grid and the kinds of fruits, respectively.

The n lines follow, where the i-th line contains n integers , denoting the fruits on the i-th row of the grid.

It is guaranteed that for each , x appears at least 1 and at most 20 times in the matrix a.

输出描述:

Output an integer in one line, denoting the answer.
示例1

输入

复制
2 3
1 2
1 3

输出

复制
1
示例2

输入

复制
3 9
1 2 3
4 5 6
7 8 9

输出

复制
2