#include <bits/stdc++.h>
int a[85][85], begin[10005], next[100005], d[100005], f[100005], v[100005],
    tot = 1;
int p[10005], r[10005], b[10005], dis[10005], w[1000005], ans = 0, st, ed;
void add(int a, int b, int c, int e) {
  tot++;
  next[tot] = begin[a];
  begin[a] = tot;
  d[tot] = b;
  f[tot] = c;
  v[tot] = e;
  tot++;
  next[tot] = begin[b];
  begin[b] = tot;
  d[tot] = a;
  f[tot] = 0;
  v[tot] = -e;
  return;
}
bool find() {
  memset(dis, 63, sizeof(dis));
  dis[st] = 0;
  w[1] = st;
  b[st] = 1;
  int h = 0, t = 1, x, y, i, e = dis[ed];
  while (h < t) {
    h++;
    x = w[h];
    for (i = begin[x]; i; i = next[i]) {
      y = d[i];
      if (f[i] && dis[x] + v[i] < dis[y]) {
        p[y] = x;
        r[y] = i;
        dis[y] = dis[x] + v[i];
        if (b[y] == 0) {
          b[y] = 1;
          t++;
          w[t] = y;
        }
      }
    }
    b[x] = 0;
  }
  return dis[ed] != e;
}
void path() {
  ans = ans + dis[ed];
  int x = ed;
  while (x) {
    f[r[x]]--;
    f[r[x] ^ 1]++;
    x = p[x];
  }
  return;
}
int main() {
  int i, j, k, x, n, m;
  scanf("%d%d", &n, &m);
  st = 0;
  ed = n * m + 1;
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++) scanf("%d", &a[i][j]);
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++) {
      x = (i - 1) * m + j;
      if ((i + j) & 1) {
        add(x, ed, 1, 0);
        continue;
      }
      add(st, x, 1, 0);
      if (i != 1) add(x, x - m, 1, a[i][j] != a[i - 1][j]);
      if (i != n) add(x, x + m, 1, a[i][j] != a[i + 1][j]);
      if (j != 1) add(x, x - 1, 1, a[i][j] != a[i][j - 1]);
      if (j != m) add(x, x + 1, 1, a[i][j] != a[i][j + 1]);
    }
  while (find()) path();
  printf("%d\n", ans);
  return 0;
}
