#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int n, m;
  cin >> n >> m;
  long long int it[n][m];
  for (long long int i = 0; i < n; i++) {
    for (long long int j = 0; j < m; j++) {
      cin >> it[i][j];
    }
  }
  long long int mm = n * m - m;
  long long int tot = 0;
  for (long long int j = 0; j < m; j++) {
    map<long long int, long long int> aa;
    for (long long int i = 0; i < n; i++) {
      long long int k = i * m + j + 1 - it[i][j];
      if (aa.find(k) == aa.end()) {
        aa[k] = 1;
      } else {
        aa[k] += 1;
      }
    }
    long long int mi;
    if (aa.find(0) == aa.end()) {
      mi = n;
    } else {
      mi = n - aa[0];
    }
    for (long long int i = 0; i < n; i++) {
      long long int k = i * m + j + 1 - it[i][j];
      aa[k] -= 1;
      if (aa.find(k + mm + m) == aa.end()) {
        aa[k + mm + m] = 1;
      } else {
        aa[k + mm + m] += 1;
      }
      long long int cc;
      if (aa.find(i * m + m) == aa.end()) {
        cc = n + i + 1;
      } else {
        cc = n - aa[i * m + m] + i + 1;
      }
      mi = min(mi, cc);
    }
    tot += mi;
  }
  cout << tot << endl;
  return 0;
}
