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 <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <iomanip>
#include <cassert>
#include <bitset>
using namespace std;

typedef pair<int, int> P;
#define rep(i, n) for (int i=0; i<(n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define index(xs, x) (int)(lower_bound(all(xs), x) - xs.begin())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919LL
#define MOD 1000000007

int N, M;
int A[1000][1000];
long long F[1000][1000][31];
long long K[1000];

long long f(int i, int pos, long long k) {
  //if (k <= 30 || pos > 12) return F[i][pos][min(k, 30LL)];
  if (k == 0 || pos > 12) return F[i][pos][min(k, 30LL)];
  long long s = A[i][pos], p = 1;
  long long n = k, r = 1;
  for (int x=pos-1; x>=0; x--) {
    assert((p*n)%r == 0);
    p = (p*n)/r;
    n++, r++;
    s += 1LL*p*A[i][x];
    if (p >= INF || s >= INF) return INF;
  }
  if (k <= 30) assert(s == F[i][pos][k]);
  return s;
}

signed main() {
  ios::sync_with_stdio(false); cin.tie(0);
  cin >> N >> M;
  rep(i, N) rep(j, M) cin >> A[i][j];
  rep(i, N-1) if (A[i][0] > A[i+1][0]) {
    cout << -1 << "\n";
    return 0;
  }
  if (M == 1) {
    //assert(false);
    bool ok = true;
    rep(i, N-1) if (A[i][0] >= A[i+1][0]) ok = false;
    cout << (ok?0:-1) << "\n";
    return 0;
  }
  rep(i, N) {
    rep(j, M) F[i][j][0] = A[i][j];
    rep(f, 30) rep(j, M) F[i][j][f+1] = min(F[i][j][f] + (j>0?F[i][j-1][f+1]:0), INF);
  }
  rep(i, N-1) {
    if (A[i][0] != A[i+1][0]) continue;
    int a = A[i][0];
    long long cur_sec = 1LL*K[i]*A[i][0]+A[i][1];
    if (A[i+1][1] < cur_sec) {
      long long X = (cur_sec-A[i+1][1]+a-1)/a;
      K[i+1] += X;
    }
    if (cur_sec == 1LL*K[i+1]*A[i+1][0]+A[i+1][1]) {
      // if a[i] >= a[i+1]: do +1
      bool ord = false;
      for (int pos=2; pos<M; pos++) {
        long long k1 = K[i], k2 = K[i+1];
        long long a = -1, b = -1;
        if (k1 <= k2) {
          // <=> A[i]^0[pos] < A[i+1]^(k2-k1)[pos]
          a = A[i][pos];
          b = f(i+1, pos, k2-k1);
        }
        else {
          // <=> A[i]^(k1-k2)[pos] < A[i+1]^0[pos]
          a = f(i, pos, k1-k2);
          b = A[i+1][pos];
        }
        if (a < b) {
          ord = true;
          break;
        }
        if (a > b) break;
      }
      //cout<<"i="<<i<<"-"<<i+1<<": ord="<<ord<<"\n";
      if (!ord) K[i+1]++;
    }
  }
  /*
  rep(i, N) {
    rep(j, M) cout <<f(i, j, K[i])<<",";cout<<"\n";
  }
  */
  long long m = 0;
  rep(i, N) m += K[i];
  cout << m << "\n";
  return 0;
}