#include <bits/stdc++.h>
using namespace std;
struct E {
  int s, t, next, cost, cap;
} edge[60000];
int ant, head[7000];
void add(int a, int b, int cap, int cost) {
  edge[ant].s = a;
  edge[ant].t = b;
  edge[ant].cap = cap;
  edge[ant].cost = cost;
  edge[ant].next = head[a];
  head[a] = ant++;
  edge[ant].s = b;
  edge[ant].t = a;
  edge[ant].cap = 0;
  edge[ant].cost = -cost;
  edge[ant].next = head[b];
  head[b] = ant++;
}
int s, t, ans, pre[7000], dp[7000], vis[7000];
int spfa() {
  int u, i, inf;
  memset(vis, 0, sizeof(vis));
  memset(dp, 32, sizeof(dp));
  queue<int> que;
  que.push(s);
  inf = dp[0];
  dp[s] = 0;
  vis[s] = 1;
  while (!que.empty()) {
    u = que.front();
    que.pop();
    for (i = head[u]; i != -1; i = edge[i].next) {
      if (edge[i].cap > 0 && edge[i].cost + dp[u] < dp[edge[i].t]) {
        pre[edge[i].t] = i;
        dp[edge[i].t] = edge[i].cost + dp[u];
        if (!vis[edge[i].t]) {
          vis[edge[i].t] = 1;
          que.push(edge[i].t);
        }
      }
    }
    vis[u] = 0;
  }
  if (dp[t] == inf) return 0;
  ans += dp[t];
  u = t;
  while (u != s) {
    edge[pre[u]].cap--;
    edge[pre[u] ^ 1].cap++;
    u = edge[pre[u]].s;
  }
  return 1;
}
int mat[100][100], op[4][2] = {1, 0, 0, 1, -1, 0, 0, -1};
int main() {
  int n, m, i, j, k, tx, ty;
  while (scanf("%d%d", &n, &m) != EOF) {
    ant = ans = 0;
    s = 0;
    t = (n + 1) * m + 1;
    memset(head, -1, sizeof(head));
    for (i = 1; i <= n; i++)
      for (j = 1; j <= m; j++) scanf("%d", &mat[i][j]);
    for (i = 1; i <= n; i++) {
      for (j = 1; j <= m; j++) {
        if ((i + j) & 1) {
          for (k = 0; k < 4; k++) {
            tx = i + op[k][0];
            ty = j + op[k][1];
            if (tx < 1 || tx > n || ty < 1 || ty > m) continue;
            add(i * m + j, tx * m + ty, 1, mat[i][j] != mat[tx][ty]);
          }
        }
        if ((i + j) & 1)
          add(s, i * m + j, 1, 0);
        else
          add(i * m + j, t, 1, 0);
      }
    }
    while (spfa())
      ;
    printf("%d\n", ans);
  }
  return 0;
}
