#include <bits/stdc++.h>
using namespace std;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
struct MCMF {
  enum { MAXN = 6500 };
  struct Edge {
    int x, y;
    int cap, cost;
  };
  vector<Edge> E;
  vector<int> adj[MAXN];
  int N, prev[MAXN];
  int dist[MAXN], phi[MAXN];
  MCMF(int NN) : N(NN) {}
  void add(int x, int y, int cap, int cost) {
    Edge e1 = {x, y, cap, cost}, e2 = {y, x, 0, -cost};
    adj[e1.x].push_back(E.size());
    E.push_back(e1);
    adj[e2.x].push_back(E.size());
    E.push_back(e2);
  }
  vector<queue<int> > Q;
  void mcmf(int s, int t, int &flowVal, int &flowCost) {
    int x;
    flowVal = flowCost = 0;
    memset(phi, 0, sizeof(phi));
    Q.resize(6500);
    while (true) {
      for (x = 0; x < N; x++) prev[x] = -1;
      for (x = 0; x < N; x++) dist[x] = 1000000000;
      dist[s] = prev[s] = 0;
      Q[dist[s]].push(s);
      for (int i = 0; i < 6400; i++) {
        while (!Q[i].empty()) {
          x = Q[i].front();
          Q[i].pop();
          if (dist[x] < i) continue;
          vector<int>::iterator it;
          for (it = adj[x].begin(); it != adj[x].end(); it++) {
            const Edge &e = E[*it];
            if (e.cap <= 0) continue;
            int cc = e.cost + phi[x] - phi[e.y];
            if (dist[x] + cc < dist[e.y]) {
              dist[e.y] = dist[x] + cc;
              prev[e.y] = *it;
              Q[dist[e.y]].push(e.y);
            }
          }
        }
      }
      if (prev[t] == -1) break;
      int z = 1000000000;
      for (x = t; x != s; x = E[prev[x]].x) z = min(z, E[prev[x]].cap);
      for (x = t; x != s; x = E[prev[x]].x) {
        E[prev[x]].cap -= z;
        E[prev[x] ^ 1].cap += z;
      }
      flowVal += z;
      flowCost += z * (dist[t] - phi[s] + phi[t]);
      for (x = 0; x < N; x++)
        if (prev[x] != -1) phi[x] += dist[x];
    }
  }
} * mc;
int a[90][90];
int main() {
  int i, j, k, n, m;
  scanf("%d %d", &n, &m);
  mc = new MCMF(n * m + 10);
  for (i = 0; i < n; i++)
    for (j = 0; j < m; j++) cin >> a[i][j];
  int src = n * m + 1;
  int dest = n * m + 2;
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      if ((i + j) % 2 == 0)
        mc->add(src, i * m + j, 1, 0);
      else {
        mc->add(i * m + j, dest, 1, 0);
        continue;
      }
      for (k = 0; k < 4; k++) {
        int ni = i + dx[k];
        int nj = j + dy[k];
        if (ni < n && nj < m && ni >= 0 && nj >= 0) {
          mc->add(i * m + j, ni * m + nj, 1, a[i][j] != a[ni][nj]);
        }
      }
    }
  }
  int fval, fcost;
  mc->mcmf(src, dest, fval, fcost);
  cout << fcost << endl;
  return 0;
}
