#include <bits/stdc++.h>
const int N = 645, M = N * N, u[] = {1, 0, -1, 0}, v[4] = {0, 1, 0, -1};
bool b[N];
int n, m, S, T, l = 1, ans, a[85][85], d[N], f[M], ed[M], st[M], next[M],
                cost[M], data[M], son[N], pre[N];
void add(int x, int y, int z) {
  st[++l] = x, ed[l] = y, data[l] = 1, cost[l] = z, next[l] = son[x],
  son[x] = l;
  st[++l] = y, ed[l] = x, data[l] = 0, cost[l] = -z, next[l] = son[y],
  son[y] = l;
}
int main() {
  scanf("%d%d", &n, &m), S = n * m, T = S + 1;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      scanf("%d", a[i] + j);
      if (i + j & 1)
        add(S, i * m + j, 0);
      else
        add(i * m + j, T, 0);
    }
  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 + u[k], y = j + v[k];
          if (x >= 0 && x < n && y >= 0 && y < m)
            add(i * m + j, x * m + y, a[i][j] != a[x][y]);
        }
  for (;;) {
    int h = 0, t = 1;
    f[1] = S;
    memset(d, 6, sizeof(d)), d[S] = 0;
    while (h < t) {
      int x = f[++h];
      b[x] = 0;
      for (int p = son[x]; p; p = next[p])
        if (data[p]) {
          int y = ed[p];
          if (d[x] + cost[p] < d[y]) {
            d[y] = d[x] + cost[p], pre[y] = p;
            if (!b[y]) b[y] = 1, f[++t] = y;
          }
        }
    }
    if (d[T] >= 1 << 20) break;
    ans += d[T];
    for (int p = T; p != S; p = st[p]) p = pre[p], data[p]--, data[p ^ 1]++;
  }
  printf("%d\n", ans);
}
