#include <bits/stdc++.h>
using namespace std;
inline void read(int &p) {
  p = 0;
  char ch = getchar();
  while (ch > '9' || ch < '0') ch = getchar();
  while (ch >= '0' && ch <= '9') p = p * 10 + ch - '0', ch = getchar();
}
const int N = 6400 + 100;
int m, n, st = N - 2, ed = N - 1, bol, inf = 1e9 + 7;
int head[N], dis[N], vis[N], cur[N];
int str[100][100];
struct node {
  int s, e, flow, costi, nex;
  node() {}
  node(int s, int e, int flow, int costi, int nex)
      : s(s), e(e), flow(flow), costi(costi), nex(nex) {}
} edg[N << 2];
inline void add(int s, int e, int flow, int costi) {
  edg[bol] = node(s, e, flow, costi, head[s]);
  head[s] = bol++;
  edg[bol] = node(e, s, 0, -costi, head[e]);
  head[e] = bol++;
}
int spfa() {
  for (int i = 0; i < N; i++) vis[st] = 0, dis[i] = inf;
  queue<int> q;
  q.push(st);
  vis[st] = 1, dis[st] = 0;
  while (!q.empty()) {
    int s = q.front();
    q.pop();
    for (int i = head[s]; ~i; i = edg[i].nex)
      if (edg[i].flow) {
        int e = edg[i].e;
        if (dis[e] > dis[s] + edg[i].costi) {
          dis[e] = dis[s] + edg[i].costi;
          cur[e] = i;
          if (!vis[e]) vis[e] = 1, q.push(e);
        }
      }
    vis[s] = 0;
  }
  return dis[ed];
}
int dinic() {
  int ans = 0;
  while (1) {
    int u = spfa(), imin = inf;
    if (u == inf) break;
    for (int i = ed; i != st; i = edg[cur[i]].s) {
      imin = min(imin, edg[cur[i]].flow);
    }
    for (int i = ed; i != st; i = edg[cur[i]].s) {
      edg[cur[i]].flow -= imin;
      edg[cur[i] ^ 1].flow += imin;
    }
    ans += imin * u;
  }
  return ans;
}
int F(int x, int y) { return x * n + y; }
int dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
bool check(int xx, int yy) {
  if (xx >= 0 && yy >= 0 && xx < m && yy < n) return true;
  return false;
}
int main() {
  memset(head, -1, sizeof head);
  cin >> m >> n;
  for (int i = 0; i < m; i++)
    for (int j = 0; j < n; j++) read(str[i][j]);
  for (int i = 0, k = 0; i < m; i++) {
    for (int j = 0; j < n; j++)
      if ((i + j) & 1) {
        add(st, F(i, j), 1, 0);
        for (int h = 0; h < 4; h++) {
          int xx = i + dir[h][0];
          int yy = j + dir[h][1];
          if (check(xx, yy)) {
            if (str[i][j] == str[xx][yy])
              k = 0;
            else
              k = 1;
            add(F(i, j), F(xx, yy), 1, k);
          }
        }
      } else
        add(F(i, j), ed, 1, 0);
  }
  cout << dinic() << endl;
  return 0;
}
