Hristenko Oleg

아직 제출이 없습니다시간 제한2초메모리 제한512 MB

문제

You are given an n×mn \times m grid of integers.

Consider the following graph. Each cell of the grid is considered as a vertex. Two cells are connected by an edge if they are in the same row or column and the cost of the edge is equal to absolute difference of numbers in its endpoints.

Consider the minimal cost spanning tree of this graph (the cost of the spanning tree is the sum of costs of edges in it). Find its cost.

입력

The first line contains two integers nn and mm (1nm1051 \leq n \cdot m \leq 10^5), the number of the rows and columns in the grid, respectively.

nn lines follow. ii-th of them contains mm integers a_i,ja\_{i,j} (0a_i,j<1090 \leq a\_{i,j} < 10^9), the elements of the grid.

출력

Print a single integer --- the cost of the minimal cost spanning tree.