#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
vector<int> vec[N];
int same[N], ans;
inline int read() {
  int Res = 0, Sign = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') Sign = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    Res = Res * 10 + ch - '0';
    ch = getchar();
  }
  return Res * Sign;
}
int main() {
  int n, m;
  n = read(), m = read();
  for (int i = 1; i <= n; i++) {
    vec[i].push_back(0);
    int tmp;
    for (int j = 1; j <= m; j++) {
      tmp = read();
      vec[i].push_back(tmp);
    }
  }
  for (int j = 1; j <= m; j++) {
    for (int i = 0; i < n; i++) same[i] = 0;
    for (int i = 1; i <= n; i++) {
      if (vec[i][j] > n * m || (vec[i][j] - j) % m != 0 || vec[i][j] < j)
        continue;
      int k = (vec[i][j] - j) / m + 1;
      same[(i - k + n) % n]++;
    }
    int total = n * m;
    for (int i = 0; i < n; i++) total = min(total, n + i - same[i]);
    ans += total;
  }
  printf("%d\n", ans);
  return 0;
}
