#include <bits/stdc++.h>
using namespace std;
struct edge {
  int a, b, cap, flow, cost;
};
vector<edge> e;
vector<int> g[20001];
int q[20001 * 10], inqueue[20001], n, m, a[101][101], d[20001], id[101][101],
    cnt, p[20001];
void add_edge(int a, int b, int cost) {
  edge e1 = {a, b, 1, 0, cost};
  edge e2 = {b, a, 0, 0, -cost};
  g[a].push_back((int)e.size());
  e.push_back(e1);
  g[b].push_back((int)e.size());
  e.push_back(e2);
}
void fordBellman() {
  for (int i = 1; i <= n * m + 1; ++i) d[i] = 1000000001, inqueue[i] = 0;
  d[0] = 0;
  int l = 0, r = 1;
  q[r] = 0;
  while (l < r) {
    int v = q[++l];
    inqueue[v] = 0;
    for (int i = 0; i < g[v].size(); ++i) {
      int id = g[v][i], to = e[id].b;
      if (d[v] < 1000000001 && d[to] > d[v] + e[id].cost &&
          e[id].cap > e[id].flow) {
        d[to] = d[v] + e[id].cost;
        p[to] = id;
        if (!inqueue[to]) q[++r] = to, inqueue[to] = 1;
      }
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) scanf("%d", &a[i][j]);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) id[i][j] = ++cnt;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) {
      if (i > 1) add_edge(id[i][j], id[i - 1][j], (a[i][j] != a[i - 1][j]));
      if (j > 1) add_edge(id[i][j], id[i][j - 1], (a[i][j] != a[i][j - 1]));
      if (i < n) add_edge(id[i][j], id[i + 1][j], (a[i][j] != a[i + 1][j]));
      if (j < m) add_edge(id[i][j], id[i][j + 1], (a[i][j] != a[i][j + 1]));
    }
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      if ((i + j) % 2 == 0)
        add_edge(0, id[i][j], 0);
      else
        add_edge(id[i][j], n * m + 1, 0);
  int total = 0;
  while (1) {
    fordBellman();
    if (d[n * m + 1] == 1000000001) break;
    int v = n * m + 1, f = 1000000001;
    while (v != 0) {
      f = min(f, e[p[v]].cap - e[p[v]].flow);
      v = e[p[v]].a;
    }
    v = n * m + 1;
    while (v != 0) {
      e[p[v]].flow += f;
      e[p[v] ^ 1].flow -= f;
      total += e[p[v]].cost * f;
      v = e[p[v]].a;
    }
  }
  printf("%d ", total);
}
