#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
int n, m, ans;
vector<int> v[maxn];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) v[i].resize(m + 1);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) scanf("%d", &v[i][j]);
  for (int j = 1; j <= m; j++) {
    vector<int> t(n + 1);
    fill(t.begin(), t.end(), 0);
    for (int i = 1; i <= n; i++) {
      if (v[i][j] <= n * m && (v[i][j] - j) % m == 0) {
        t[((i - (((v[i][j] - j) / m) + 1)) + n) % n]++;
      }
    }
    int Min = n - t[0];
    for (int i = 0; i < n; i++) {
      Min = min(Min, i + n - t[i]);
    }
    ans += Min;
  }
  cout << ans << endl;
  return 0;
}
