#include <bits/stdc++.h>
using namespace std;
const int inf = 111111, maxn = 66000, maxm = 660000;
int hea[maxn], nex[maxm], vv[maxm];
int cost[maxm], cap[maxm];
int n, m, s, t, add, num, d[maxn], inq[maxn], pre[maxn];
inline void adj(int a, int b, int c) {
  vv[add] = b;
  nex[add] = hea[a];
  cap[add] = 1;
  cost[add] = c;
  hea[a] = add++;
  vv[add] = a;
  nex[add] = hea[b];
  cap[add] = 0;
  cost[add] = -c;
  hea[b] = add++;
}
bool spfa() {
  queue<int> q;
  int u, v, e;
  for (int i = 1; i <= num; ++i) d[i] = inf;
  d[s] = 0;
  pre[s] = -1;
  inq[s] = 1;
  q.push(s);
  while (!q.empty()) {
    u = q.front();
    q.pop();
    inq[u] = 0;
    for (e = hea[u]; e != -1; e = nex[e])
      if (cap[e] > 0 && d[vv[e]] > d[u] + cost[e]) {
        v = vv[e];
        pre[v] = e ^ 1;
        d[v] = d[u] + cost[e];
        if (!inq[v]) {
          inq[v] = 1;
          q.push(v);
        }
      }
  }
  return d[t] < inf;
}
int mincost() {
  int u, v, e, ans = 0;
  while (spfa()) {
    for (e = pre[t]; e != -1; e = pre[vv[e]]) {
      cap[e]++;
      cap[e ^ 1]--;
    }
    ans += d[t];
  }
  return ans;
}
int ss[88][88], dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
void init() {
  memset(hea, -1, sizeof(hea));
  s = n * m + 1;
  t = s + 1;
  add = 0;
  num = t;
  for (int i = 0; i < n; ++i) {
    for (int j = 1; j <= m; ++j)
      if ((i + j) & 1) {
        for (int k = 0; k < 4; ++k) {
          int ii = i + dx[k], jj = j + dy[k];
          if (ii < 0 || ii >= n) continue;
          if (jj < 1 || jj > m) continue;
          adj(i * m + j, ii * m + jj, 1 - (ss[i][j] == ss[ii][jj]));
        }
      }
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 1; j <= m; ++j) {
      if ((i + j) & 1)
        adj(s, i * m + j, 0);
      else
        adj(i * m + j, t, 0);
    }
  }
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; ++i)
    for (int j = 1; j <= m; ++j) scanf("%d", &ss[i][j]);
  init();
  printf("%d\n", mincost());
  return 0;
}
