#GYM104663C. Don't Let Them Pass
Don't Let Them Pass
Description
A grid with $N$ rows and $M$ columns. Each column contains only one block, other cells are empty. A block can be moved up or down only. In each operation you can select one block and move it up or down by only one cell. The enemy can enter the grid using any empty cell from the top side and reach you(at the bottom of the grid). An enemy can move to any adjacent cell if it's empty. Find the minimum number of operations needed to reorganize the blocks of the grid so that it is impossible to pass the grid.

The first line contains $N(1 \le N \le 1000)$, number of rows and $M(1 \le M \le 1000)$, number of columns. The next $N$ line contains an array of size $M$ each. Each element of the array is either 0(empty) or 1(block). There is exactly 1 block in each column.
Print one integer, minimum number of operations needed to reorganize the blocks of the grid so that it is impossible to pass the grid.
Input
The first line contains $N(1 \le N \le 1000)$, number of rows and $M(1 \le M \le 1000)$, number of columns. The next $N$ line contains an array of size $M$ each. Each element of the array is either 0(empty) or 1(block). There is exactly 1 block in each column.
Output
Print one integer, minimum number of operations needed to reorganize the blocks of the grid so that it is impossible to pass the grid.
7 5
1 0 0 0 0
0 0 1 0 0
0 1 0 0 1
0 0 0 0 0
0 0 0 1 0
0 0 0 0 0
0 0 0 0 0
3