#include <bits/stdc++.h>
using namespace std;
inline long long IN() {
  long long x = 0;
  int ch = getchar(), f = 1;
  while (!isdigit(ch) && (ch != '-') && (ch != EOF)) ch = getchar();
  if (ch == '-') {
    f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = (x << 1) + (x << 3) + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline void OUT(long long x) {
  if (x < 0) putchar('-'), x = -x;
  if (x >= 10)
    OUT(x / 10), putchar(x % 10 + '0');
  else
    putchar(x + '0');
}
const int N = 6405, M = 64005;
int n, m;
const int Ps[4][2] = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};
int ID(int i, int j) { return (i - 1) * m + j; }
int Cnt, To[M], Nx[M], Cs[M], Fl[M], Hd[N];
void AddEdge(int u, int v, int cost, int f) {
  To[Cnt] = v;
  Nx[Cnt] = Hd[u];
  Cs[Cnt] = cost;
  Fl[Cnt] = f;
  Hd[u] = Cnt++;
  To[Cnt] = u;
  Nx[Cnt] = Hd[v];
  Cs[Cnt] = -cost;
  Fl[Cnt] = 0;
  Hd[v] = Cnt++;
}
int Pr[N], Ds[N], S, T, Ans, a[85][85];
bool Vs[N];
queue<int> Q;
bool SPFA() {
  memset(Ds, 60, sizeof Ds);
  Ds[S] = 0;
  Q.push(S);
  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    Vs[u] = 0;
    for (int i = Hd[u]; ~i; i = Nx[i]) {
      int v = To[i];
      if (Fl[i] && Ds[u] + Cs[i] < Ds[v]) {
        Ds[v] = Ds[u] + Cs[i], Pr[v] = i;
        if (!Vs[v]) {
          Q.push(v);
          Vs[v] = 1;
        }
      }
    }
  }
  return Ds[T] < 500000000;
}
void Edmonds_Karp() {
  while (SPFA()) {
    int Nw = T, Mx = (1 << 29);
    while (Nw != S) {
      Mx = min(Mx, Fl[Pr[Nw]]);
      Nw = To[Pr[Nw] ^ 1];
    }
    Ans += Mx * Ds[T];
    Nw = T;
    while (Nw != S) {
      Fl[Pr[Nw]] -= Mx;
      Fl[Pr[Nw] ^ 1] += Mx;
      Nw = To[Pr[Nw] ^ 1];
    }
  }
}
int main() {
  n = IN(), m = IN();
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) a[i][j] = IN();
  S = 0, T = n * m + 1;
  memset(Hd, -1, sizeof Hd);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      int Tp = ID(i, j);
      if ((i + j) & 1)
        AddEdge(Tp, T, 0, 1);
      else
        AddEdge(S, Tp, 0, 1);
      for (int k = 0; k <= 3; k++) {
        int Ni = i + Ps[k][0], Nj = j + Ps[k][1];
        if (Ni >= 1 && Ni <= n && Nj >= 1 && Nj <= m) {
          if (a[Ni][Nj] == a[i][j])
            AddEdge(Tp, ID(Ni, Nj), 0, 1);
          else
            AddEdge(Tp, ID(Ni, Nj), 1, 1);
        }
      }
    }
  Edmonds_Karp();
  OUT(Ans);
}
