#include <bits/stdc++.h>
using namespace std;
template <typename T>
T abs(T a) {
  return a < 0 ? -a : a;
}
template <typename T>
T sqr(T a) {
  return a * a;
}
const int INF = (int)1e9;
const long double EPS = 1e-9;
const long double PI = 3.1415926535897932384626433832795;
const int N = 100;
const int M = N * N;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};
int n, m;
int a[N][N];
struct edge {
  int to, f, c, p, rev;
};
vector<edge> g[M];
void addEdge(int fr, int to, int c, int p) {
  edge a = {to, 0, c, p, int((g[to]).size())};
  edge b = {fr, 0, 0, -p, int((g[fr]).size())};
  g[fr].push_back(a);
  g[to].push_back(b);
}
int s, t;
int d[M], tr[M], trE[M];
bool inQ[M];
int H, T;
int q[M];
int cost;
bool enlarge() {
  memset(d, 63, sizeof(int) * (t + 1));
  d[s] = 0;
  H = T = 0;
  q[T++] = s;
  while (H != T) {
    int v = q[H++];
    inQ[v] = false;
    if (H == M) H = 0;
    for (int i = 0; i < int(int((g[v]).size())); ++i) {
      const edge& cur = g[v][i];
      if (cur.f < cur.c && d[cur.to] > d[v] + cur.p) {
        d[cur.to] = d[v] + cur.p;
        tr[cur.to] = v;
        trE[cur.to] = i;
        if (!inQ[cur.to]) {
          inQ[cur.to] = true;
          q[T++] = cur.to;
          if (T == M) T = 0;
        }
      }
    }
  }
  if (d[t] > (INF >> 1)) return false;
  cost += d[t];
  for (int b = t; b != s; b = tr[b]) {
    g[tr[b]][trE[b]].f++;
    g[b][g[tr[b]][trE[b]].rev].f--;
  }
  return true;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < int(n); ++i) {
    for (int j = 0; j < int(m); ++j) {
      cin >> a[i][j];
    }
  }
  s = n * m;
  t = s + 1;
  for (int i = 0; i < int(n); ++i)
    for (int j = 0; j < int(m); ++j) {
      if ((i + j) & 1) {
        addEdge(s, i * m + j, 1, 0);
        for (int k = 0; k < int(4); ++k) {
          int ni = i + dx[k], nj = j + dy[k];
          int p = (a[i][j] != a[ni][nj]) ? 1 : 0;
          if (0 <= ni && ni < n && 0 <= nj && nj < m) {
            addEdge(i * m + j, ni * m + nj, 1, p);
          }
        }
      } else {
        addEdge(i * m + j, t, 1, 0);
      }
    }
  while (enlarge())
    ;
  cout << cost << endl;
  return 0;
}
