#include <bits/stdc++.h>
using namespace std;
const int N = 101010;
struct MCMF {
  int h[N], dis[N], ing[N], pre[N], s, t, n;
  int to[100010], ne[100010], cap[100010], cost[100010], e;
  void ini() {
    fill(h, h + N, -1);
    e = 0;
  }
  void liu(int u, int v, int c, int w) {
    to[e] = v, ne[e] = h[u], cap[e] = c, cost[e] = w;
    h[u] = e++;
  }
  void link(int u, int v, int c, int w) {
    liu(u, v, c, w);
    liu(v, u, 0, -w);
  }
  bool spfa() {
    queue<int> Q;
    fill(ing, ing + n, 0);
    fill(pre, pre + n, -1);
    fill(dis, dis + n, 0x37373737);
    ing[s] = true, dis[s] = 0;
    Q.push(s);
    while (!Q.empty()) {
      int c = Q.front();
      Q.pop();
      ing[c] = false;
      for (int k = h[c]; ~k; k = ne[k]) {
        int v = to[k];
        if (cap[k] <= 0) continue;
        if (dis[c] + cost[k] < dis[v]) {
          dis[v] = dis[c] + cost[k];
          pre[v] = k;
          if (!ing[v]) Q.push(v), ing[v] = true;
        }
      }
    }
    return dis[t] != 0x37373737;
  }
  int flow, mincost;
  pair<int, int> run(int _s, int _t, int _n) {
    s = _s, t = _t, n = _n;
    flow = mincost = 0;
    while (spfa()) {
      int pl = 0x37373737, p, k;
      for (p = t; p != s; p = to[k ^ 1]) {
        k = pre[p];
        pl = min(pl, cap[k]);
      }
      for (p = t; p != s; p = to[k ^ 1]) {
        k = pre[p];
        cap[k] -= pl;
        cap[k ^ 1] += pl;
      }
      mincost += pl * dis[t];
      flow += pl;
    }
    return make_pair(flow, mincost);
  }
};
MCMF mcmf;
int n, m, i, j, a[100][100], s, t;
int o(int x, int y) { return (x - 1) * m + y; }
int main() {
  scanf("%d%d", &n, &m);
  mcmf.ini();
  s = 0;
  t = n * m + 1;
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++) {
      scanf("%d", &a[i][j]);
      if ((i + j) % 2 == 0)
        mcmf.link(s, o(i, j), 1, 0);
      else
        mcmf.link(o(i, j), t, 1, 0);
    }
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++)
      if ((i + j) % 2 == 0) {
        if (i + 1 <= n)
          mcmf.link(o(i, j), o(i + 1, j), 1, a[i][j] == a[i + 1][j] ? 0 : 1);
        if (i - 1 > 0)
          mcmf.link(o(i, j), o(i - 1, j), 1, a[i][j] == a[i - 1][j] ? 0 : 1);
        if (j + 1 <= m)
          mcmf.link(o(i, j), o(i, j + 1), 1, a[i][j] == a[i][j + 1] ? 0 : 1);
        if (j - 1 > 0)
          mcmf.link(o(i, j), o(i, j - 1), 1, a[i][j] == a[i][j - 1] ? 0 : 1);
      }
  printf("%d\n", mcmf.run(s, t, n * m + 10).second);
}
