#include <bits/stdc++.h>
using namespace std;
const int maxn = 6500;
const int dx[5] = {0, 0, 1, -1};
const int dy[5] = {1, -1, 0, 0};
int n, m, sursa, dest;
int A[100][100], ind[100][100], cnt[2], type[maxn], C[maxn][maxn];
bool good[maxn][maxn];
vector<int> G[maxn];
bool uz[maxn];
int L[maxn], R[maxn], d[maxn];
inline int getCost(int a, int b) {
  if (a == 0 || b == 0 || a == dest || b == dest) return 0;
  if (type[a] == type[b]) return 0;
  if (good[a][b]) return 1;
  return -1;
}
int father[maxn];
bool BF() {
  for (int i = 1; i <= dest; ++i) {
    d[i] = 5 * maxn;
    father[i] = -1;
  }
  queue<int> Q;
  Q.push(0);
  father[0] = 0;
  d[0] = 0;
  for (; !Q.empty();) {
    int act = Q.front();
    Q.pop();
    for (auto a : G[act]) {
      if (C[act][a] == 1) {
        if (d[act] + getCost(act, a) < d[a]) {
          father[a] = act;
          d[a] = d[act] + getCost(act, a);
          Q.push(a);
        }
      }
    }
  }
  if (d[dest] != 5 * maxn) return true;
  return false;
}
int matching() {
  int ans = 0;
  for (; BF();) {
    for (int i = dest; i != sursa; i = father[i]) {
      C[father[i]][i]--;
      C[i][father[i]]++;
    }
    ans += d[dest];
  }
  return ans;
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) {
      cin >> A[i][j];
      ind[i][j] = ++cnt[0];
      type[ind[i][j]] = A[i][j];
    }
  dest = ++cnt[0];
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) {
      if ((i + j) % 2 == 0) {
        for (int k = 0; k <= 3; ++k) {
          int nowi = i + dx[k];
          int nowj = j + dy[k];
          if (nowi < 1 || nowi > n || nowj < 1 || nowj > m) continue;
          G[ind[i][j]].push_back(ind[nowi][nowj]);
          C[ind[i][j]][ind[nowi][nowj]] = 1;
          G[ind[nowi][nowj]].push_back(ind[i][j]);
          good[ind[i][j]][ind[nowi][nowj]] = true;
        }
        G[sursa].push_back(ind[i][j]);
        C[sursa][ind[i][j]] = 1;
      } else {
        G[ind[i][j]].push_back(dest);
        C[ind[i][j]][dest] = 1;
      }
    }
  cout << matching() << "\n";
  return 0;
}
