Chef and Rectangle Array CHSQARR

All submissions for this problem are available.<h3> Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.</h3>

Chef has a two-dimensional matrix A of dimensions N × M, (N rows and M columns).

He calls the matrix A beautiful if there exist an a×b submatrix, such that all of its elements are equal. In one minute Chef can increase one element of the matrix A by 1. Now he asks you to find out minimum time he will need to make the matrix A beautiful?

Please note that sub-matrix of a matrix is a continuous rectangular block of the matrix. It can be denoted by two pair of indices (x1, y1) and (x2, y2) where x1 ≤ x2, y1 ≤ y2. The content of the submatrix will be all the cells (i, j) such that x1 ≤ i ≤ x2 and y1 ≤ j ≤ y2.

Input

Output

  • For each question, output a single line containing the minimum time that Chef needs to make an matrix A beautiful (for parameters a and b from question)
  • Constraints

    Subtasks

    Subtask #1 (13 pts)

    Subtask #2 (35 pts)

    Subtask #3 (52 pts)

    Example

    Input:
    3 4
    1 8 3 4
    5 2 3 1
    3 6 2 2
    4
    1 1
    2 2
    2 3
    3 2
    
    Output:
    0
    4
    15
    9
    

    Explanation

    Question #1:
    Chef can choose any 1 × 1 submatrix and it will take his 0 minutes to make it beautiful.

    Question #2:
    The best variant is submatrix

    3 1
    2 2
    

    Question #3:
    The next submatrix Chef can make equal in 15 minutes

    5 2 3
    3 6 2
    

    Question #4:

    3 4
    3 1
    2 2