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
#include <bits/stdc++.h>
      
#define FOR(i,a,b) for( ll i = (a); i < (ll)(b); i++ )
#define REP(i,n) FOR(i,0,n)
#define YYS(x,arr) for(auto& x:arr)
#define ALL(x) (x).begin(),(x).end()
#define SORT(x) sort( (x).begin(),(x).end() )
#define REVERSE(x) reverse( (x).begin(),(x).end() )
#define UNIQUE(x) (x).erase( unique( ALL( (x) ) ) , (x).end() )
#define PW(x) (1LL<<(x))
#define SZ(x) ((ll)(x).size())
#define SHOW(x) cout << #x << " = " << x << endl
     
#define pb emplace_back
#define fi first
#define se second
     
using namespace std;

typedef long double ld;
typedef long long int ll;
typedef pair<int,int> pi;
typedef pair<ll,ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<bool> vb;
typedef vector<ld> vd;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<vpl> gr;
typedef vector<vl> ml;
typedef vector<vd> md;
typedef vector<vi> mi;
     
const ll INF = (ll)1e9 + 10;
const ll INFLL = (ll)1e18 + 10;
const ld EPS = 1e-12;
const ll MOD = 1e9+7;
     
template<class T> T &chmin( T &a , const T &b ){ return a = min(a,b); }
template<class T> T &chmax( T &a , const T &b ){ return a = max(a,b); }
template<class T> inline T sq( T a ){ return a * a; }

ll in(){ ll x; scanf( "%lld" , &x ); return x; }
char yuyushiki[1000010]; string stin(){ scanf( "%s" , yuyushiki ); return yuyushiki; }

// head

ll dp[2010][2010];

ll comb( ll a , ll b ){ 
  if( b < 0 or b > a ){
    return 0;
  }
  if( b == 0 or b == a ){
    return 1;
  }
  if( a >= 2010 ){
    if( b == 1 || b == a - 1 ){
      return min( INF , a );
    } else if( b == 2 || b == a - 2 ){
      return min( INF , a * ( a - 1 ) / 2 );
    } else if( b == 3 ){
      return INF;
    } 
  }
  if( dp[a][b] != 0 ){
    return dp[a][b];
  }
  ll res = comb( a - 1 , b ) + comb( a - 1 , b - 1 );
  return dp[a][b] = min( res , INF );
}

int n, m;

pi calc( vi a , vi b ){
  assert( a < b );

  int x = ( b[1] - a[1] ) / a[0];

  FOR( i , 1 , m ){
    ll cur = a[i];
    FOR( j , 1 , i+1 ){
      cur += a[i-j] * comb( x + j - 1 ,  j );
      chmin( cur , INF );
    }
    if( cur > b[i] ){
      return make_pair( x , false );
    }
    if( cur < b[i] ){
      return make_pair( x + 1 , false );
    }
  }
  
  return make_pair( x , true );
}

mi a;

int main(){

  n = in();
  m = in();
  a = mi( n , vi( m ) );
  REP( i , n ){
    REP( j , m ){
      a[i][j] = in();
    }
  }

  if( m == 1 ){
    REP( i , n-1 ){
      if( a[i][0] >= a[i+1][0] ){
	puts( "-1" );
	return 0;
      }
    }
    puts( "0" );
    return 0;
  }
  
  REP( i , n-1 ){
    if( a[i][0] > a[i+1][0] ){
      puts( "-1" );
      return 0;
    }
  }

  ll cur = 0;
  ll ans = 0;
  REP( i , n-1 ){
    if( a[i][0] == a[i+1][0] ){
      if( a[i][1] < a[i+1][1] ){
	pi res = calc( a[i] , a[i+1] );
	cur = cur - res.fi + 1;
      } else if( a[i][1] > a[i+1][1] ){
	pi res = calc( a[i+1] , a[i] );
	if( res.se ){
	  cur = cur + res.fi + 1;
	} else {
	  cur = cur + res.fi;
	}
      } else {
	if( a[i] >= a[i+1] ){
	  cur++;
	}
      }
    } else {
      cur = 0;
    }
    chmax( cur , 0LL );
    ans += cur;
  }

  cout << ans << endl;
  
  return 0;
}
