#include <bits/stdc++.h>
using namespace std;
int n, m, ed, S, T, C, t, p[2][10000];
int a[99][99], out[99][99];
int v[10000], u[10000], f[10000], q[10000], l[10000];
int qd[400000], ql[400000], qc[400000], qn[400000];
int MCF(int n, int S, int T) {
  for (int i = 0; i < n; i++) v[i] = (1 << 30), u[i] = 0;
  v[S] = 0, q[0] = S;
  for (int i = 0, j = 0, s = 1; s; i++) {
    if (i == 10000) i = 0;
    u[q[i]] = 0;
    s--;
    for (int k = l[q[i]]; k; k = qn[k])
      if (qc[k] && v[q[i]] + ql[k] < v[qd[k]]) {
        v[qd[k]] = v[q[i]] + ql[k];
        f[qd[k]] = k;
        if (!u[qd[k]]) {
          if (++j == 10000) j = 0;
          s++;
          u[q[j] = qd[k]] = 1;
        }
      }
  }
  for (int i = T; i != S; i = qd[f[i] ^ 1]) qc[f[i]]--, qc[f[i] ^ 1]++;
  return v[T];
}
void bd(int x, int y, int c, int w) {
  qd[++ed] = y, qn[ed] = l[x], l[x] = ed, ql[ed] = w, qc[ed] = c;
  if (ed % 2 == 0) bd(y, x, 0, -w);
}
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
int main() {
  scanf("%d%d", &n, &m);
  memset(p, -1, sizeof(p));
  for (int i = (0); i < (n); i++)
    for (int j = (0); j < (m); j++) {
      scanf("%d", &a[i][j]), a[i][j]--;
    }
  S = 0;
  T = 1;
  t = 2;
  C = n * m / 2;
  ed = 1;
  for (int i = (0); i < (n); i++)
    for (int j = (0); j < (m); j++)
      if ((i + j) & 1) {
        out[i][j] = t;
        bd(t, T, 1, 0);
        ++t;
      }
  for (int i = (0); i < (n); i++)
    for (int j = (0); j < (m); j++) {
      if ((i + j) % 2 == 0) {
        for (int d = (0); d < (4); d++) {
          int ti, tj;
          ti = i + dx[d];
          tj = j + dy[d];
          if (ti < 0 || tj < 0 || ti == n || tj == m) continue;
          int c = 1;
          if (a[i][j] == a[ti][tj]) c = 0;
          bd(t, out[ti][tj], 1, c);
        }
      }
      bd(S, t, 1, 0);
      ++t;
    }
  int ret = 0;
  for (int i = (0); i < (C); i++) ret += MCF(t, S, T);
  printf("%d\n", ret);
  return 0;
}
