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;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL;
typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii; typedef long long ll;
template<typename T, typename U> static void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> static void amax(T &x, U y) { if(x < y) x = y; }

template<typename T>T gcd(T x, T y) { if(y == 0)return x; else return gcd(y, x%y); }
int nCr_saturated(int n, int k, int infinity) {
	const int MaxK = 30;
	if(k > n) return 0;
	if(k > n / 2) k = n - k;
	if(k == 0) return 1;
	assert(0 <= k && k <= MaxK);
	int factors[MaxK];
	rep(i, k)
		factors[i] = n - i;
	rep(i, k) {
		int x = i + 1;
		rep(j, k) {
			int g = gcd(factors[j], x);
			factors[j] /= g;
			x /= g;
		}
		assert(x == 1);
	}
	int product = 1;
	rep(i, k) {
		long long P = (long long)product * factors[i];
		if(P >= infinity)
			return infinity;
		else
			product = (int)P;
	}
	return product;
}

vector<int> trans(const vector<int> &a, int t, bool naive = false) {
	assert(t >= 0);
	const int logT = 30;
	int M = (int)a.size();
	vector<int> res = a;
	if(t <= logT || naive) {
		rep(k, t) {
			rep(i, M - 1)
				amin((res[i + 1] += res[i]), INF);
		}
	} else {
		int X = min(logT, M);
		rep(i, X) {
			int sum = 0;
			for(int j = i; j >= 0; -- j) {
				int coeff = nCr_saturated((i - j) + t - 1, i - j, INF);
				long long S = sum + (long long)coeff * a[j];
				if(S > INF) {
					sum = INF;
					break;
				} else {
					sum = (int)S;
				}
			}
			res[i] = sum;
		}
		reu(i, X, M)
			res[i] = INF;
	}
	return res;
}

int solve(const vector<int> &a, const vector<int> &b) {
	int M = (int)a.size();
	if(b < a)
		return 0;
	if(b[0] > a[0])
		return INF;
	int t = (b[1] - a[1] + a[0] - 1) / a[0];
	vector<int> c = trans(a, t);
	return b < c ? t : t + 1;
}

int main() {
	int N; int M;
	while(~scanf("%d%d", &N, &M)) {
		vector<vector<int> > A(N, vector<int>(M));
		for(int i = 0; i < N; ++ i) for(int j = 0; j < M; ++ j)
			scanf("%d", &A[i][j]);
		if(M == 1) {
			bool ok = true;
			rep(i, N - 1)
				ok &= A[i][0] < A[i + 1][0];
			puts(!ok ? "-1" : "0");
			continue;
		}
		vector<int> g(N - 1);
		bool ok = true;
		rep(i, N - 1) {
			int x = solve(A[i + 1], A[i]);
			if(x == INF) {
				ok = false;
				break;
			} else if(x > 0) {
				g[i] = x;
			} else {
				int y = solve(A[i], A[i + 1]);
				if(y > 0 && y < INF && trans(A[i], y - 1) == A[i + 1])
					-- y;
				g[i] = -y + 1;
			}
		}
		if(!ok) {
			puts("-1");
			continue;
		}
		ll ans = 0;
		ll curt = 0;
		rep(i, N - 1) {
			curt = max(curt + g[i], 0LL);
			ans += curt;
		}
		printf("%lld\n", ans);
	}
	return 0;
}