There’s a new art installation in town, and it inspires you... to play a childish game. The art installation consists of a floor with an matrix of square tiles. Each tile holds a single number from to . You want to play hopscotch on it. You want to start on some tile numbered , then hop to some tile numbered , then , and so on, until you reach some tile numbered . You are a good hopper, so you can hop any required distance. You visit exactly one tile of each number from to .
What’s the shortest possible total distance over a complete game of Hopscotch? Use the Manhattan distance: the distance between the tile at and the tile at is .
输入格式
The first line of input contains two space-separated integers () and (), where the art installation consists of an matrix with tiles having numbers from to .
Each of the next lines contains space-separated integers (). This is the art installation.
输出格式
Output a single integer, which is the total length of the shortest path starting from some tile and ending at some tile, or if it isn’t possible.