#include <bits/stdc++.h>
using namespace std;
void task();
int main() {
  task();
  return 0;
}
const int N = 6400 + 2;
const int S = N - 2;
const int T = N - 1;
struct Rib {
  int to;
  int cap;
  int flow;
  int back;
  int cost;
};
const int INF = 0x3f3f3f3f;
vector<Rib> g[N];
int n, m;
int a[81][81];
const int shift[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
pair<int, int> par[N];
int d[N];
int in[N];
int ids[N];
int ok(int i, int j) { return i >= 0 && i < n && j >= 0 && j < m; }
void add_rib(int a, int b, int cap, int cost) {
  g[a].push_back(Rib{b, cap, 0, ((int)((g[b]).size())), cost});
  g[b].push_back(Rib{a, 0, 0, ((int)((g[a]).size())) - 1, -cost});
}
int get_id(int i, int j) { return ids[i * m + j]; }
int spfa() {
  memset(d, 0x3f, sizeof(d));
  d[S] = 0;
  deque<int> q;
  q.push_back(S);
  in[S] = 1;
  while (!q.empty()) {
    int v = q.front();
    q.pop_front();
    in[v] = 0;
    int i = 0;
    for (Rib& r : g[v]) {
      if (r.cap - r.flow && d[v] + r.cost < d[r.to]) {
        d[r.to] = d[v] + r.cost;
        par[r.to] = pair<int, int>{v, i};
        if (!in[r.to]) q.push_back(r.to);
        in[r.to] = 1;
      }
      ++i;
    }
  }
  return d[T] != INF;
}
int mcmf() {
  int res = 0;
  while (spfa()) {
    ++res;
    int v = T;
    while (v != S) {
      Rib& r = g[par[v].first][par[v].second];
      r.flow++;
      g[r.to][r.back].flow--;
      v = par[v].first;
    }
  }
  return res;
}
void task() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) scanf("%d", &a[i][j]);
  for (int i = 0; i < 6400; ++i) ids[i] = i;
  random_shuffle(ids, ids + 6400);
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) {
      if ((i + j) % 2) {
        add_rib(get_id(i, j), T, 1, 0);
      } else {
        add_rib(S, get_id(i, j), 1, 0);
        for (int h = 0; h < 4; ++h) {
          int ii = i + shift[h][0];
          int jj = j + shift[h][1];
          if (!ok(ii, jj)) continue;
          add_rib(get_id(i, j), get_id(ii, jj), 1, a[i][j] != a[ii][jj]);
        }
      }
    }
  mcmf();
  int ans = 0;
  for (int i = 0; i < N; ++i)
    for (Rib& r : g[i])
      if (r.flow > 0) ans += r.cost;
  printf("%d", ans);
}
