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 {{{
#include <iostream>
#include <cassert>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <bitset>
#include <vector>
#include <complex>
#include <algorithm>
using namespace std;
// }}}
// #define {{{
typedef long long ll;
typedef double db;
typedef pair<int,int> pii;
typedef vector<int> vi;
#define de(x) cout << #x << "=" << x << endl
#define rep(i,a,b) for(int i=a;i<(b);++i)
#define per(i,a,b) for(int i=(b)-1;i>=(a);--i)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
// }}}

const int N = 1e3 + 10;
const ll inf = 1e9 + 7;
int n , m , a[N][N];
ll times[N];

ll solve(vector<ll> A,vector<ll> B,int sig = 0) {
  if(A >= B) {
    int times = 0;
    while(true) {
      rep(i,1,sz(B)) {
        B[i] += B[i-1];
        B[i] = min(B[i] , inf);
        if(B[i] == inf)
          break;
      }
      times++;
      if(sig == 0) {
        if(A < B) return times;
        if(sz(B) == 2) return times + (A[1] - B[1] + B[0]) / B[0];
        if(B[2] == inf) return times + (A[1] - B[1] + B[0] - 1) / B[0];
      } else {
        if(A <= B) return times;
        if(sz(B) == 2 || B[2] == inf) return times + (A[1] - B[1] + B[0] - 1) / B[0];
      }
    }
  } else return -(solve(B , A , 1) - 1);
}

int main(){
  scanf("%d%d",&n,&m);
  rep(i,0,n) rep(j,0,m) scanf("%d",a[i]+j);
  bool error = false;
  rep(i,1,n) error |= a[i][0] < a[i-1][0];
  if(m == 1) rep(i,1,n) error |= a[i][0] <= a[i-1][0];
  if(error) return printf("-1\n") , 0;
  rep(i,1,n) {
    if(a[i][0] > a[i-1][0]) {
      times[i] = 0;
      continue;
    }
    vector<ll> A , B;
    rep(j,0,m) A.pb(a[i-1][j]) , B.pb(a[i][j]);
    ll del = solve(A,B);
    times[i] = max(0ll , times[i-1] + del);
  }
  ll ans = 0;
  rep(i,0,n) ans += times[i];
  printf("%lld\n",ans);
  return 0;
}