Problem: 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.

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.

Examples

Input

3 3
2 3 1
2 1 2
2 6 3


Output

1


Input

3 3
3 2 10
10 5 4
9 1 9


Output

-1


Input

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


Output

11

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int H,W;
vector<ll> A[2020];
ll G[2020],T[2020];

void dodo(vector<ll>& V) {
	int i;
	for(i=1;i<V.size();i++) V[i]+=V[i-1];
}

ll hoge(vector<ll> A,vector<ll> B,bool eq_ok) {
	if(A[0]<B[0]) {
		cout<<-1<<endl;
		exit(0);
	}
	if(A[0]>B[0]) return 0;
	
	ll t=(B[1]-A[1]+A[0]-1)/A[0];
	if(A[0]*t+A[1]>B[1]) return t;
	
	int mapos=B.size();
	ll ov=1LL<<30;
	int i,x,y;
	FOR(i,t) {
		if(mapos==2) {
			A[1]+=A[0]*(t-i);
			break;
		}
		for(x=1;x<mapos;x++) {
			A[x]+=A[x-1];
			if(A[x]>=ov) {
				mapos=x;
				break;
			}
		}
	}
	
	if(eq_ok && A>=B) return t;
	if(!eq_ok && A>B) return t;
	return t+1;
}
 
void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>H>>W;
	FOR(y,H) FOR(x,W) cin>>i, A[y].push_back(i);
	
	if(W==1) {
		FOR(y,H-1) if(A[y][0]>=A[y+1][0]) return _P("-1\n");
		return _P("0\n");
	}
	
	FOR(y,H-1) {
		if(A[y][0]>A[y+1][0]) return _P("-1\n");
		if(A[y][0]<A[y+1][0]) {
			G[y+1]=-1LL<<30;
			continue;
		}
		
		if(A[y]<A[y+1]) {
			G[y+1]=-hoge(A[y],A[y+1],true)+1;
		}
		else {
			G[y+1]=hoge(A[y+1],A[y],false);
		}
	}
	
	ll ret=0;
	for(i=1;i<H;i++) {
		T[i]=max(0LL,T[i-1]+G[i]);
		ret+=T[i];
	}
	cout<<ret<<endl;
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}