#include <bits/stdc++.h>
using namespace std;
struct edge {
  int s, t, c, r, rev, next;
} in[6405 * 10];
int n, m, e, mat[81][81], first[6405], pre[6405], dis[6405], st, ed,
    dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
bool inq[6405];
void add(int x, int y, int c) {
  in[e].s = x;
  in[e].t = y;
  in[e].c = c;
  in[e].r = 1;
  in[e].rev = e + 1;
  in[e].next = first[x];
  first[x] = e++;
  in[e].s = y;
  in[e].t = x;
  in[e].c = -c;
  in[e].r = 0;
  in[e].rev = e - 1;
  in[e].next = first[y];
  first[y] = e++;
}
bool spfa() {
  int cur;
  queue<int> q;
  memset(inq, false, sizeof(inq));
  for (int i = 0; i <= ed; i++) dis[i] = 2000000000;
  dis[st] = 0;
  q.push(st);
  while (!q.empty()) {
    cur = q.front();
    q.pop();
    inq[cur] = false;
    for (int i = first[cur]; ~i; i = in[i].next)
      if (in[i].r > 0 && dis[cur] + in[i].c < dis[in[i].t]) {
        dis[in[i].t] = dis[cur] + in[i].c;
        pre[in[i].t] = i;
        if (!inq[in[i].t]) {
          q.push(in[i].t);
          inq[in[i].t] = true;
        }
      }
  }
  return dis[ed] != 2000000000;
}
int costflow() {
  int x, re = 0, flow;
  while (spfa()) {
    flow = 2000000000;
    x = ed;
    while (x != st) {
      flow = min(flow, in[pre[x]].r);
      x = in[pre[x]].s;
    }
    x = ed;
    while (x != st) {
      in[pre[x]].r -= flow;
      in[in[pre[x]].rev].r += flow;
      x = in[pre[x]].s;
    }
    re += flow * dis[ed];
  }
  return re;
}
int main() {
  while (~scanf("%d %d", &n, &m)) {
    e = 0;
    memset(first, -1, sizeof(first));
    for (int i = 0; i <= n - 1; i++)
      for (int j = 0; j <= m - 1; j++) scanf("%d", &mat[i][j]);
    for (int i = 0; i <= n - 1; i++)
      for (int j = 0; j <= m - 1; j++)
        for (int k = 0; k <= 3; k++)
          if ((i + j) & 1) {
            int x = i + dx[k], y = j + dy[k];
            if (x < 0 || y < 0 || x >= n || y >= m) continue;
            add(i * m + j, x * m + y, mat[i][j] != mat[x][y]);
          }
    st = n * m;
    ed = st + 1;
    for (int i = 0; i <= n - 1; i++)
      for (int j = 0; j <= m - 1; j++) {
        if ((i + j) & 1)
          add(st, i * m + j, 0);
        else
          add(i * m + j, ed, 0);
      }
    printf("%d\n", costflow());
  }
  return 0;
}
