#include <bits/stdc++.h>
using namespace std;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1}, N = 101000;
int n, m, tot, ans, y[N], nxt[N], fst[N], f[N], c[N], dis[N], q[N], inq[N],
    prd[N];
int a[90][90], grid[90][90];
void add(int u, int v, int w, int ww) {
  tot++;
  y[tot] = v;
  nxt[tot] = fst[u];
  f[tot] = w;
  c[tot] = ww;
  fst[u] = tot;
  tot++;
  y[tot] = u;
  nxt[tot] = fst[v];
  f[tot] = 0;
  c[tot] = -ww;
  fst[v] = tot;
}
void spfa() {
  memset(dis, 0x30, sizeof(dis));
  memset(inq, 0, sizeof(inq));
  int t = 1;
  q[t] = dis[0] = 0;
  inq[0] = 1;
  for (int i = 0; i < t; i++) {
    int u = q[i];
    for (int j = fst[u]; j; j = nxt[j])
      if (f[j] && dis[y[j]] > dis[u] + c[j]) {
        prd[y[j]] = j;
        dis[y[j]] = dis[u] + c[j];
        if (!inq[y[j]]) q[t++] = y[j], inq[y[j]] = 1;
      }
    inq[u] = 0;
  }
  ans += dis[n * m + 1];
  for (int p = prd[n * m + 1]; p; p = prd[y[p ^ 1]]) --f[p], ++f[p ^ 1];
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) scanf("%d", &a[i][j]), grid[i][j] = ++tot;
  tot = 1;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if ((i + j) & 1)
        for (int k = 0; k < 4; k++) {
          int x = i + dx[k], y = j + dy[k];
          if (x < 0 || x >= n || y < 0 || y >= m) continue;
          add(grid[i][j], grid[x][y], 1, a[i][j] != a[x][y]);
        }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if ((i + j) & 1)
        add(0, grid[i][j], 1, 0);
      else
        add(grid[i][j], n * m + 1, 1, 0);
  for (int i = 0; i < n * m / 2; i++) spfa();
  printf("%d\n", ans);
}
