#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
struct edge {
  int b, l, c, f;
} g[10005 * 8];
int h[10005], t, dis[10005], n, ldis[10005], vis[10005], fl[10005], pre[10005];
int mp[82][82];
int arc(int i) { return i + (i & 1 ? 1 : -1); }
void addedge(int a, int b, int c, int f) {
  g[++t] = (edge){b, h[a], c, f};
  h[a] = t;
  g[++t] = (edge){a, h[b], 0, -f};
  h[b] = t;
}
bool spfa(int x, int y, int &flow, int &cost) {
  int i, u;
  deque<int> q;
  for (i = 1; i <= n; i++) dis[i] = 1e8;
  dis[x] = pre[x] = 0;
  fl[x] = 1e8;
  q.push_back(x);
  while (q.size()) {
    u = q.front();
    q.pop_front();
    vis[u] = 0;
    for (i = h[u]; i; i = g[i].l)
      if (g[i].c && dis[g[i].b] + ldis[g[i].b] > dis[u] + ldis[u] + g[i].f) {
        dis[g[i].b] = dis[u] + ldis[u] - ldis[g[i].b] + g[i].f;
        fl[g[i].b] = min(fl[u], g[pre[g[i].b] = i].c);
        if (!vis[g[i].b]++)
          if (q.empty() || dis[g[i].b] > dis[q.front()])
            q.push_back(g[i].b);
          else
            q.push_front(g[i].b);
      }
  }
  if (dis[y] == 1e8) return 0;
  for (i = 1; i <= n; i++)
    if (dis[i] < 1e8) ldis[i] += dis[i];
  flow += fl[y];
  cost += ldis[y] * fl[y];
  for (i = pre[y]; i; i = pre[g[arc(i)].b]) {
    g[i].c -= fl[y];
    g[arc(i)].c += fl[y];
  }
  return 1;
}
pair<int, int> solve(int x, int y) {
  int flow = 0, cost = 0;
  while (spfa(x, y, flow, cost))
    ;
  return make_pair(flow, cost);
}
void prepareGraph(int m) {
  int i, x, y, nx, ny, idx, idy, dire[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
  queue<int> q;
  for (x = 0; x < n; x++)
    for (y = 0; y < m; y++) {
      idx = x * m + y + 3;
      if (x + y & 1) {
        addedge(1, idx, 1, 0);
        for (i = 0; i < 4; i++) {
          nx = x + dire[i][0];
          ny = y + dire[i][1];
          if (nx >= 0 && nx < n && ny >= 0 && ny < m) {
            idy = nx * m + ny + 3;
            addedge(idx, idy, 1e8, mp[nx][ny] != mp[x][y]);
          }
        }
      } else
        addedge(idx, 2, 1, 0);
    }
  n = 2 + n * m;
}
int main() {
  int m, i, j;
  scanf("%d%d", &n, &m);
  for (i = 0; i < n; i++)
    for (j = 0; j < m; j++) scanf("%d", &mp[i][j]);
  prepareGraph(m);
  pair<int, int> ans = solve(1, 2);
  printf("%d", ans.second);
  return 0;
}
