Problem:
Score : 1200 points

Problem Statement

Takahashi is a magician. He can cast a spell on an integer sequence (a_1,a_2,...,a_M) with M terms, to turn it into another sequence (s_1,s_2,...,s_M), where s_i is the sum of the first i terms in the original sequence.

One day, he received N integer sequences, each with M terms, and named those sequences A_1,A_2,...,A_N. He will try to cast the spell on those sequences so that A_1 < A_2 < ... < A_N will hold, where sequences are compared lexicographically.
Let the action of casting the spell on a selected sequence be one cast of the spell. Find the minimum number of casts of the spell he needs to perform in order to achieve his objective.

Here, for two sequences a = (a_1,a_2,...,a_M), b = (b_1,b_2,...,b_M) with M terms each, a < b holds lexicographically if and only if there exists i (1 ≦ i ≦ M) such that a_j = b_j (1 ≦ j < i) and a_i < b_i.

Constraints

1 ≦ N ≦ 10^3

1 ≦ M ≦ 10^3

Let the j-th term in A_i be A_{(i,j)}, then 1 ≦ A_{(i,j)} ≦ 10^9.

Partial Scores

In the test set worth 200 points, Takahashi can achieve his objective by at most 10^4 casts of the spell, while keeping the values of all terms at most 10^9.

In the test set worth another 800 points, A_{(i,j)} ≦ 80.

Input

The input is given from Standard Input in the following format:

N M
A_{(1,1)} A_{(1,2)} … A_{(1,M)}
A_{(2,1)} A_{(2,2)} … A_{(2,M)}
:
A_{(N,1)} A_{(N,2)} … A_{(N,M)}

Output

Print the minimum number of casts of the spell Takahashi needs to perform. If he cannot achieve his objective, print -1 instead.

Sample Input 1

3 3
2 3 1
2 1 2
2 6 3

Sample Output 1

1

Takahashi can achieve his objective by casting the spell on A_2 once to turn it into (2 , 3 , 5).

Sample Input 2

3 3
3 2 10
10 5 4
9 1 9

Sample Output 2

-1

In this case, Takahashi cannot achieve his objective by casting the spell any number of times.

Sample Input 3

5 5
2 6 5 6 9
2 6 4 9 10
2 6 8 6 7
2 1 7 3 8
2 1 4 8 3

Sample Output 3

11

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<cassert>
#define PB push_back
#define MP make_pair
#define sz(v) (in((v).size()))
#define forn(i,n) for(in i=0;i<(n);++i)
#define forv(i,v) forn(i,sz(v))
#define fors(i,s) for(auto i=(s).begin();i!=(s).end();++i)
#define all(v) (v).begin(),(v).end()
using namespace std;
typedef long long in;
typedef vector<in> VI;
typedef vector<VI> VVI;
void no(){
  cout<<-1<<endl;
  exit(0);
}
in m;
const in inf=1e9+9;
VI fc;
void rs(VI& a, in c){
  forn(z,c){
    for(in i=1;i<m;++i){
      a[i]=min(inf,a[i]+a[i-1]);
      if(a[i]==inf)
	return;
    }
  }
}
in md(VI a, VI b){
  if(a[0]>b[0])
    no();
  if(a[0]<b[0])
    return 0;
  if(m==1)
    no();
  in df=a[1]-b[1];
  if(df%a[0]){
    if(df<0)
      return -((-df)/a[0]);
    if(df>0)
      return (df+a[0]-1)/a[0];
  }
  in c=df/a[0];
  if(c<0){
    rs(a,-c);
  }
  else{
    rs(b,c);
  }
  in g=0;
  while(g<m && a[g]==b[g])
    ++g;
  assert(g>=2);
  if(g==m || a[g]>b[g])
    return c+1;
  return c;
}
VVI a;
int main(){
  ios::sync_with_stdio(0);
  cin.tie(0);
  in n;
  cin>>n>>m;
  fc.PB(1);
  for(in i=1;fc.back()<4e16;++i)
    fc.PB(fc.back()*i);
  a.resize(n,VI(m));
  forn(i,n){
    forn(j,m)
      cin>>a[i][j];
  }
  in minc=0;
  in sm=0;
  in nc;
  for(in i=1;i<n;++i){
    in cc=md(a[i-1],a[i]);
    nc=max(0LL,minc+cc);
    sm+=nc;
    minc=nc;
  }
  cout<<sm<<endl;
  return 0;
}