#include <bits/stdc++.h>
using namespace std;
const int N = 10000;
const int M = 1000000;
const int INF = 0x7f7f7f7f;
struct edge {
  int v, cap, cost, next;
  edge() {}
  edge(int a, int b, int c, int d) : v(a), cap(b), cost(c), next(d) {}
} e[M];
int head[N], sz;
int dis[N], pre[N], cur[N];
bool inq[N];
queue<int> q;
void graphinit() {
  memset(head, -1, sizeof(head));
  sz = 0;
}
void addedge(int u, int v, int cp, int ct) {
  e[sz] = edge(v, cp, ct, head[u]);
  head[u] = sz++;
  e[sz] = edge(u, 0, -ct, head[v]);
  head[v] = sz++;
}
int mcmf(int s, int t) {
  int mc = 0, mf = 0;
  while (true) {
    memset(pre, -1, sizeof(pre));
    memset(inq, 0, sizeof(inq));
    memset(dis, 0x7f, sizeof(dis));
    dis[s] = 0;
    q.push(s);
    while (!q.empty()) {
      int u = q.front();
      q.pop();
      inq[u] = false;
      for (int i = head[u]; i != -1; i = e[i].next) {
        int v = e[i].v;
        if (e[i].cap > 0 && dis[v] > dis[u] + e[i].cost) {
          dis[v] = dis[u] + e[i].cost;
          pre[v] = u;
          cur[v] = i;
          if (!inq[v]) {
            inq[v] = true;
            q.push(v);
          }
        }
      }
    }
    if (pre[t] == -1) break;
    int aug = INF;
    for (int i = t; i != s; i = pre[i]) aug = min(aug, e[cur[i]].cap);
    mf += aug;
    mc += dis[t] * aug;
    for (int i = t; i != s; i = pre[i]) {
      e[cur[i]].cap -= aug;
      e[cur[i] ^ 1].cap += aug;
    }
  }
  return mc;
}
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
int mz[100][100];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) scanf("%d", &mz[i][j]);
  graphinit();
  int s = n * m, t = s + 1;
  for (int x = 0; x < n; x++)
    for (int y = 0; y < m; y++) {
      int u = x * m + y;
      if ((x + y) % 2 == 0) {
        addedge(s, u, 1, 0);
        for (int d = 0; d < 4; d++) {
          int nx = x + dx[d], ny = y + dy[d];
          if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
          int v = nx * m + ny;
          int c = mz[x][y] == mz[nx][ny] ? 0 : 1;
          addedge(u, v, 1, c);
        }
      } else {
        addedge(u, t, 1, 0);
      }
    }
  int ans = mcmf(s, t);
  printf("%d\n", ans);
  return 0;
}
