#include <bits/stdc++.h>
using namespace std;
struct T {
  int i, v;
  T(int i = 0, int v = 0) : i(i), v(v) {}
};
bool operator<(T a, T b) { return a.v > b.v; }
int l, n, u[10010], w[10010], p[10010], a[2 * 100010], c[2 * 100010],
    e[2 * 100010];
vector<int> m[10010];
priority_queue<T> q;
void push(int i, int v, int j) {
  if (v < u[i]) {
    u[i] = v;
    p[i] = j;
    q.push(T(i, v));
  }
}
bool bfs(int b, int s) {
  int i, v, j, k;
  for (fill(u, u + n, 1000000000), push(b, 0, -1); !q.empty();) {
    i = q.top().i;
    v = q.top().v;
    q.pop();
    if (v == u[i])
      for (j = 0; j < m[i].size(); j++)
        if (a[k = m[i][j]]) push(e[k], v + c[k] + w[i] - w[e[k]], k);
  }
  return u[s] < 1000000000;
}
int mcmf(int b, int s) {
  int i, j, f;
  for (fill(w, w + n, 0), f = 0; bfs(b, s);) {
    for (j = 1000000000, i = s; i != b; j = min(j, a[p[i]]), i = e[p[i] ^ 1])
      ;
    for (i = s; i != b;
         a[p[i]] -= j, a[p[i] ^ 1] += j, f += c[p[i]] * j, i = e[p[i] ^ 1])
      ;
    for (i = 0; i < n; w[i] += (u[i] - 1000000000) & u[i], i++)
      ;
  }
  return f;
}
void add(int i, int j, int v, int p) {
  e[l] = j;
  a[l] = v;
  c[l] = p;
  m[i].push_back(l++);
  e[l] = i;
  a[l] = 0;
  c[l] = -p;
  m[j].push_back(l++);
}
int g[100][100], v[10000], n1, n2;
int main() {
  int i, j, k, r, c, di[] = {-1, 1, 0, 0}, dj[] = {0, 0, -1, 1}, ni, nj;
  for (scanf("%d%d", &r, &c), i = 0; i < r; i++)
    for (j = 0; j < c; scanf("%d", &g[i][j]), j++)
      ;
  n = r * c + 2;
  for (i = 0; i < r * c; i++)
    if ((i / c + i % c) % 2 == 0)
      add(n - 2, i, 1, 0);
    else
      add(i, n - 1, 1, 0);
  for (i = 0; i < r; i++)
    for (j = 0; j < c; j++)
      if ((i + j) % 2 == 0)
        for (k = 0; k < 4; k++) {
          ni = i + di[k];
          nj = j + dj[k];
          if (ni >= 0 && ni < r && nj >= 0 && nj < c)
            add(i * c + j, ni * c + nj, 1, g[i][j] != g[ni][nj]);
        }
  printf("%d\n", mcmf(n - 2, n - 1));
  return 0;
}
