#include <bits/stdc++.h>
using namespace std;
struct Edge {
  int v, cap, cost, next;
} edge[600000];
int tot, flow, cost, n, src, des;
int head[7000], d[7000], q[7000], inq[7000], pre[7000], a[7000][7000];
int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};
void addedge(int u, int v, int cap, int cost) {
  edge[tot].v = v;
  edge[tot].cap = cap;
  edge[tot].cost = cost;
  edge[tot].next = head[u];
  head[u] = tot++;
  edge[tot].v = u;
  edge[tot].cap = 0;
  edge[tot].cost = -cost;
  edge[tot].next = head[v];
  head[v] = tot++;
}
void init() {
  memset(head, -1, sizeof(head));
  tot = flow = cost = 0;
}
bool spfa() {
  for (int i = 0; i < n; i++) d[i] = 1e6;
  int t = 0, h = 0;
  q[t++] = src;
  d[src] = 0;
  while (t != h) {
    int u = q[h++];
    h %= 7000;
    inq[u] = 0;
    for (int e = head[u]; e != -1; e = edge[e].next) {
      int v = edge[e].v;
      if (edge[e].cap > 0 && d[v] > d[u] + edge[e].cost) {
        d[v] = d[u] + edge[e].cost;
        pre[v] = e;
        if (!inq[v]) {
          inq[v] = 1;
          q[t++] = v;
          t %= 7000;
        }
      }
    }
  }
  return d[des] != 1e6;
}
void end() {
  int u, e, mi = 1e6;
  for (u = des; u != src; u = edge[e ^ 1].v) {
    e = pre[u];
    mi = min(mi, edge[e].cap);
  }
  for (u = des; u != src; u = edge[e ^ 1].v) {
    e = pre[u];
    edge[e].cap -= mi;
    edge[e ^ 1].cap += mi;
    cost += mi * edge[e].cost;
  }
}
int main() {
  int N, M;
  scanf("%d%d", &N, &M);
  n = N * M + 2;
  for (int i = 0; i < N; i++)
    for (int j = 0; j < M; j++) scanf("%d", &a[i][j]);
  init();
  src = N * M, des = src + 1;
  for (int i = 0; i < N; i++)
    for (int j = 0; j < M; j++) {
      if ((i + j) % 2 == 0)
        addedge(src, i * M + j, 1, 0);
      else
        addedge(i * M + j, des, 1, 0);
      if ((i + j) % 2 == 1) continue;
      for (int k = 0; k < 4; k++) {
        int nx = i + dx[k];
        int ny = j + dy[k];
        if (nx >= 0 && nx < N && ny >= 0 && ny < M) {
          if (a[nx][ny] == a[i][j]) {
            addedge(i * M + j, nx * M + ny, 1, 0);
          } else {
            addedge(i * M + j, nx * M + ny, 1, 1);
          }
        }
      }
    }
  while (spfa()) end();
  printf("%d\n", cost);
}
