using System;
using System.Text;
using System.Collections.Generic;
class Solve{
    int N,M;
    long[,] A;
    public Solve(){}
    StringBuilder sb;
    public static int Main(){
        new Solve().Run();
        return 0;
    }
    void Run(){
        sb = new StringBuilder();
        Read();
        Calc();
        Console.Write(sb.ToString());
    }
    void Calc(){
        long count = 0;
        long b = 0;
        if(M == 1){
            for(int i=1;i<N;i++){
                if(A[i,0] <= A[i-1,0]){
                    count = -1;
                    break;
                }
            }
        }
        else{
            for(int i=1;i<N;i++){
                if(A[i,0] > A[i-1,0]){
                    b = 0;
                    continue;
                }
                else if(A[i,0] < A[i-1,0]){
                    count = -1;
                    break;
                }
                else{
                    long c;
                    if(A[i,1] > A[i-1,1]){
                        c = (A[i,1] - A[i-1,1] - 1)/A[i-1,0] + 1;
                        if(c > b){
                            b = 0;
                        }
                        else if(c * A[i-1,0] != A[i,1] - A[i-1,1]){
                            count += b - c + 1;
                            b -= c - 1;
                        }
                        else{
                            if(c < 30){
                                long[] temp1 = new long[M];
                                long[] temp2 = new long[M];
                                int j = 1;
                                for(int k=0;k<M;k++){
                                    if(k == 0){
                                        temp2[k] = A[i-1,0];
                                    }
                                    else{
                                        temp2[k] = A[i-1,k] + temp2[k-1];
                                    }
                                }
                                for(;j<c;j++){
                                    if(j%2 == 1){
                                        for(int k=0;k<M;k++){
                                            if(k == 0){
                                                temp1[k] = temp2[k];
                                            }
                                            else{
                                                temp1[k] = temp1[k-1] + temp2[k];
                                            }
                                        }
                                    }
                                    else{
                                        for(int k=0;k<M;k++){
                                            if(k == 0){
                                                temp2[k] = temp1[k];
                                            }
                                            else{
                                                temp2[k] = temp2[k-1] + temp1[k];
                                            }
                                        }
                                    }
                                }
                                if(c%2 == 0){
                                    for(int k=0;k<M;k++){
                                        if(temp1[k] > A[i,k]){
                                            break;
                                        }
                                        else if(temp1[k] < A[i,k]){
                                            c++;
                                            break;
                                        }
                                    }
                                }
                                else{
                                    for(int k=0;k<M;k++){
                                        if(temp2[k] > A[i,k]){
                                            break;
                                        }
                                        else if(temp2[k] < A[i,k]){
                                            c++;
                                            break;
                                        }
                                    }
                                }
                            }
                            else{
                                Conv co = new Conv(M,c-1);
                                for(int k=0;k<M;k++){
                                    long sum = 0;
                                    for(int j=0;j<=k;j++){
                                        long x = co.C(k-j);
                                        if(x == -1){
                                            sum = -1;
                                            break;
                                        }
                                        sum += x * A[i-1,j];
                                        if(sum > 1000000000){
                                            sum = -1;
                                            break;
                                        }
                                    }
                                    if(sum == -1 || sum > A[i,k]){
                                        break;
                                    }
                                    else if(sum < A[i,k]){
                                        c++;
                                        break;
                                    }
                                }
                            }
                            if(c > b){
                                b = 0;
                            }
                            else{
                                b -= c - 1;
                                count += b;
                            }
                        }
                    }
                    else if(A[i,1] < A[i-1,1]){
                        c = (A[i-1,1] - A[i,1] - 1)/A[i,0] + 1;
                        if(c * A[i,0] != A[i-1,1] - A[i,1]){
                            b += c;
                            count += b;
                        }
                        else{
                            if(c < 30){
                                long[] temp1 = new long[M];
                                long[] temp2 = new long[M];
                                int j = 1;
                                for(int k=0;k<M;k++){
                                    if(k == 0){
                                        temp2[k] = A[i,0];
                                    }
                                    else{
                                        temp2[k] = A[i,k] + temp2[k-1];
                                    }
                                }
                                for(;j<c;j++){
                                    if(j%2 == 1){
                                        for(int k=0;k<M;k++){
                                            if(k == 0){
                                                temp1[k] = temp2[k];
                                            }
                                            else{
                                                temp1[k] = temp1[k-1] + temp2[k];
                                            }
                                        }
                                    }
                                    else{
                                        for(int k=0;k<M;k++){
                                            if(k == 0){
                                                temp2[k] = temp1[k];
                                            }
                                            else{
                                                temp2[k] = temp2[k-1] + temp1[k];
                                            }
                                        }
                                    }
                                }
                                if(c%2 == 0){
                                    int k;
                                    for(k=0;k<M;k++){
                                        if(temp1[k] > A[i-1,k]){
                                            break;
                                        }
                                        else if(temp1[k] < A[i-1,k]){
                                            c++;
                                            break;
                                        }
                                    }
                                    if(k == M){
                                        c++;
                                    }
                                }
                                else{
                                    int k;
                                    for(k=0;k<M;k++){
                                        if(temp2[k] > A[i-1,k]){
                                            break;
                                        }
                                        else if(temp2[k] < A[i-1,k]){
                                            c++;
                                            break;
                                        }
                                    }
                                    if(k == M){
                                        c++;
                                    }
                                }
                            }
                            else{
                                Conv co = new Conv(M,c-1);
                                int k;
                                for(k=0;k<M;k++){
                                    long sum = 0;
                                    for(int j=0;j<=k;j++){
                                        long x = co.C(k-j);
                                        if(x == -1){
                                            sum = -1;
                                            break;
                                        }
                                        sum += x * A[i,j];
                                        if(sum > 1000000000){
                                            sum = -1;
                                            break;
                                        }
                                    }
                                    if(sum == -1 || sum > A[i-1,k]){
                                        break;
                                    }
                                    else if(sum < A[i-1,k]){
                                        c++;
                                        break;
                                    }
                                }
                                if(k == M){
                                    c++;
                                }
                            }
                            b += c;
                            count += b;
                        }
                    }
                    else{
                        int j;
                        for(j=1;j<M;j++){
                            if(A[i-1,j] < A[i,j]){
                                count += b;
                                break;
                            }
                            else if(A[i-1,j] > A[i,j]){
                                b++;
                                count += b;
                                break;
                            }
                        }
                        if(j == M){
                            b++;
                            count += b;
                        }
                    }
                }
            }
        }
        sb.Append(count+"\n");
    }
    void Read(){
        string[] str = Console.ReadLine().Split(' ');
        N = int.Parse(str[0]);
        M = int.Parse(str[1]);
        A = new long[N,M];
        for(int i=0;i<N;i++){
            str = Console.ReadLine().Split(' ');
            for(int j=0;j<M;j++){
                A[i,j] = Int64.Parse(str[j]);
            }
        }
    }    
}
class Conv{
    public int n;
    public long m;
    public long[] c;
    public Conv(int n0,long m0){
        n = n0;
        m = m0;
        c = new long[n];
        for(int i=0;i<n;i++){
            if(i == 0){
                c[i] = 1;
            }
            else{
                c[i] = c[i-1]*(m+i)/i;
                if(c[i] > 1000000000){
                    c[i] = -1;
                    break;
                }
            }
        }
    }
    public long C(int N){
        return c[N];
    }
}

Measured memory_bytes: 39372