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 long long LL;
typedef __int128 INT;
struct cww{cww(){
    ios::sync_with_stdio(false);cin.tie(0);
}}star;
#define fin "\n"
#define FOR(i,bg,ed) for(int i=(bg);i<(ed);i++)
#define REP(i,n) FOR(i,0,n)
#define fi first
#define se second
#define pb push_back
#define DEBUG if(0)
template <typename T>inline void chmin(T &l,T r){l=min(l,r);}
template <typename T>inline void chmax(T &l,T r){l=max(l,r);}
template <typename T>
istream& operator>>(istream &is,vector<T> &v){
    for(auto &it:v)is>>it;
    return is;
}
typedef vector<INT> V;
typedef vector<V> VV;

INT comb[1145140][11];
const INT INF=1e15;
struct latte{
    latte(){
        for(int i=0;i<1145140;i++)comb[i][0]=1;
        for(int i=1;i<1145140;i++)
            for(int j=1;j<=min(i,10);j++)
                comb[i][j]=min(INF,comb[i-1][j]+comb[i-1][j-1]);
    }
}malta;

LL f(V a,V b,bool eq){
    int m=b.size();
    LL cnt=0;
    int ub=114514;
    while(a>b){
        cnt++;
        FOR(i,1,m){
            b[i]+=b[i-1];
            chmin(b[i],INF);
            if(b[i]==INF)chmin(ub,i);
        }
        if(ub<10)break;
    }
    if(eq){
        if(a==b)return cnt+1;
        if(a<b)return cnt;
    }
    else{
        if(a==b)return -cnt+1;
        if(a<b)return -cnt+1;
    }
    LL k=(a[1]-b[1]+b[0]-1)/b[0];
    //if(k<0)return 0;
    if(b[0]*k+b[1]>a[1]){
        if(eq)
            return cnt+k;
        else{
            return -cnt-k+1;
        }
    }
    if(k==0){
        if(eq){
            return cnt+k+1;
        }
        else{
            return -cnt-k;
        }
    }
   
    
    if(k>=1000000){
        if(eq)
            return cnt+k;
        else return -cnt-k+1;
    }
    for(int id=2;id<b.size();id++){
        INT sum=b[id];
        if(sum>a[id]){
            if(eq)
                return cnt+k;
            else
                return -cnt-k+1;
        }
        REP(i,id){
            sum+=min(INF,b[i]*comb[k+id-1][id-i]);
        }
        if(sum>a[id]){
            if(eq)
                return cnt+k;
            else
                return -cnt-k+1;
        }
        if(sum<a[id]){
            if(eq)return cnt+k+1;
            else return -cnt-k;
        }
    }
    if(eq){
        return cnt+k+1;
    }
    else{
        return -cnt-k+1;
    }
}


int main(){
    int N,M;
    cin>>N>>M;
    VV A(N,V(M));
    REP(i,N)REP(j,M){
        int a;
        cin>>a;
        A[i][j]=a;
    }
    if(M==1){
        int ans=0;
        REP(i,N-1)if(A[i]>=A[i+1])ans=-1;
        cout<<ans<<endl;
        return 0;
    }
    vector<LL> B(N-1,0);
    REP(i,N-1){
        if(A[i]==A[i+1])B[i]=1;
        else if(A[i]>A[i+1]){
            if(A[i][0]>A[i+1][0]){
                cout<<-1<<endl;
                return 0;
            }
            B[i]=f(A[i],A[i+1],true);
        }
        else {
            if(A[i][0]<A[i+1][0])B[i]=-INF;
            else 
                B[i]=f(A[i+1],A[i],false);
        }
        //cout<<i<<" "<<B[i]<<endl;
    }
    LL res=0,now=0;
    REP(i,N-1){
        now=max(0ll,now+B[i]);
        res+=now;
    }
    cout<<res<<endl;
    return 0;
}