#include <bits/stdc++.h>
using namespace std;
const int Inf = 1000000000;
const int MaxM = 80005;
const int MaxN = 8005;
struct Edge {
  int v, cap, cost;
  int nxt;
} buf[4 * MaxM];
int g[MaxN], eCnt;
int n, st, ed;
int d[MaxN], pre[MaxN], pree[MaxN];
void init() {
  for (int i = 0; i < n; i++) g[i] = -1;
  eCnt = 0;
}
void add(int u, int v, int cap, int cost) {
  buf[eCnt].v = v;
  buf[eCnt].cap = cap;
  buf[eCnt].cost = cost;
  buf[eCnt].nxt = g[u];
  g[u] = eCnt++;
  buf[eCnt].v = u;
  buf[eCnt].cap = 0;
  buf[eCnt].cost = -cost;
  buf[eCnt].nxt = g[v];
  g[v] = eCnt++;
}
const int MaxQ = 8005;
int q[MaxQ], front, rear;
bool in[MaxN];
void spfa() {
  memset(in, 0, sizeof(in));
  for (int i = 0; i < n; i++) d[i] = Inf;
  d[st] = 0;
  front = rear = 0;
  q[rear++] = st;
  in[st] = 1;
  while (front != rear) {
    int u = q[front];
    front = (front + 1) % MaxQ;
    in[u] = 0;
    for (int i = g[u]; i != -1; i = buf[i].nxt)
      if (buf[i].cap) {
        int v = buf[i].v, w = buf[i].cost;
        if (d[v] > d[u] + w) {
          d[v] = d[u] + w;
          pre[v] = u;
          pree[v] = i;
          if (!in[v]) {
            in[v] = 1;
            q[rear] = v;
            rear = (rear + 1) % MaxQ;
          }
        }
      }
  }
}
int Mincost_flow() {
  int flow = 0, cost = 0;
  while (true) {
    spfa();
    if (d[ed] >= Inf) break;
    int mx = Inf;
    for (int i = ed; i != st; i = pre[i]) {
      mx = min(mx, buf[pree[i]].cap);
    }
    for (int i = ed; i != st; i = pre[i]) {
      buf[pree[i]].cap -= mx;
      buf[pree[i] ^ 1].cap += mx;
      cost += mx * buf[pree[i]].cost;
    }
    flow += mx;
  }
  return cost;
}
int N, M;
int maap[90][90];
const int dir[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
bool ind(int i, int j) {
  if (i < 0 || j < 0 || i >= N || j >= M) return false;
  return true;
}
int main() {
  scanf("%d%d", &N, &M);
  for (int i = 0; i < N; i++)
    for (int j = 0; j < M; j++) scanf("%d", &maap[i][j]);
  st = N * M;
  ed = N * M + 1;
  n = ed + 1;
  init();
  for (int i = 0; i < N; i++)
    for (int j = 0; j < M; j++) {
      if ((i + j) % 2 == 0) {
        for (int k = 0; k < 4; k++) {
          int dx = i + dir[k][0];
          int dy = j + dir[k][1];
          if (ind(dx, dy)) {
            if (maap[i][j] == maap[dx][dy])
              add(i * M + j, dx * M + dy, 1, 0);
            else
              add(i * M + j, dx * M + dy, 1, 1);
          }
        }
        add(st, i * M + j, 1, 0);
      } else
        add(i * M + j, ed, 1, 0);
    }
  printf("%d\n", Mincost_flow());
}
