#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T &x) {
  x = 0;
  int f(1);
  char c(getchar());
  for (; !isdigit(c); c = getchar())
    if (c == '-') f = -f;
  for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + (c - '0');
  x *= f;
}
template <typename T>
void write(T x) {
  if (x < 0) putchar('-'), x = -x;
  if (x / 10) write(x / 10), x %= 10;
  putchar(x + '0');
}
const int maxn = 84, saxn = 6405;
struct Edge {
  int v, w, f, nt;
} e[saxn * 6];
int num, hd[saxn];
void add_edge(int u, int v, int w, int f) {
  ++num, e[num].v = v, e[num].w = w, e[num].f = f, e[num].nt = hd[u],
         hd[u] = num;
}
void add(int u, int v, int w, int f) {
  add_edge(u, v, w, f);
  add_edge(v, u, 0, -f);
}
int S, T, q[saxn], dis[saxn], flow[saxn], last[saxn], pre[saxn], in[saxn];
int spfa(void) {
  memset(dis, 0x3f, sizeof dis);
  flow[T] = 0;
  flow[S] = 0x3f3f3f3f;
  int fro = 0, bac = 0;
  in[q[bac++] = S] = true;
  dis[S] = 0;
  while (fro != bac) {
    int u = q[fro++];
    in[u] = false;
    if (fro == saxn) fro = 0;
    for (int i = hd[u]; i; i = e[i].nt) {
      int v = e[i].v;
      if (e[i].w && dis[v] > dis[u] + e[i].f) {
        dis[v] = dis[u] + e[i].f;
        flow[v] = min(flow[u], e[i].w);
        last[v] = i;
        pre[v] = u;
        if (!in[v]) {
          q[bac++] = v;
          in[v] = true;
          if (bac == saxn) bac = 0;
        }
      }
    }
  }
  return flow[T];
}
void solve(void) {
  int maxflow = 0, mincost = 0;
  while (spfa()) {
    maxflow += flow[T];
    mincost += flow[T] * dis[T];
    int now = T;
    while (now != S) {
      e[last[now]].w -= flow[T];
      e[(last[now] - 1 ^ 1) + 1].w += flow[T];
      now = pre[now];
    }
  }
  write(mincost), putchar('\n');
}
int txt[maxn][maxn];
const int di[4] = {0, 0, 1, -1}, dj[4] = {1, -1, 0, 0};
int main() {
  int n, m;
  read(n), read(m);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) read(txt[i][j]);
  S = n * m + 1;
  T = S + 1;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      int no = j + (i - 1) * m;
      if (i + j & 1) {
        for (int t = 0; t < 4; ++t) {
          int ti = i + di[t], tj = j + dj[t], to = tj + (ti - 1) * m;
          if (ti >= 1 && ti <= n && tj >= 1 && tj <= m) {
            add(no, to, 1, txt[ti][tj] != txt[i][j]);
          }
        }
        add(S, no, 1, 0);
      } else
        add(no, T, 1, 0);
    }
  }
  solve();
  return 0;
}
