#include <bits/stdc++.h>
using namespace std;
const long long N = 1777;
long long n, m;
string templ;
long long cost[N][N], cap[N][N], f[N][N];
long long d[N], par[N], pot[N];
bool used[N];
long long s, t;
map<long long, long long> mp;
long long inf = 1e12;
long long a[100][100];
long long b[100][100];
long long nn;
long long dijicstra() {
  for (long long i = 0; i <= nn; i++) {
    d[i] = inf;
    used[i] = 0;
  }
  d[s] = 0;
  d[t] = inf;
  long long cnt = 0;
  while (true) {
    long long v = -1;
    for (long long j = 0; j <= nn; j++) {
      if (!used[j] && (v == -1 || d[j] < d[v])) {
        v = j;
      }
    }
    if (v == -1 || d[v] == inf) {
      break;
    }
    used[v] = 1;
    for (long long i = 0; i <= nn; i++) {
      if (used[i]) {
        continue;
      }
      long long w = cost[v][i] + pot[v] - pot[i];
      if (cap[v][i] && d[i] > d[v] + w) {
        d[i] = d[v] + w;
        par[i] = v;
      }
    }
  }
  return d[t] < inf;
}
long long solve() {
  long long ans = 0;
  while (dijicstra()) {
    for (long long i = 0; i <= nn; i++) {
      pot[i] = d[i];
    }
    long long flow = inf;
    for (long long v = t; v != s; v = par[v]) {
      flow = min(flow, cap[par[v]][v]);
    }
    if (flow == 0) {
      break;
    }
    for (long long v = t; v != s; v = par[v]) {
      cap[par[v]][v] -= flow;
      cap[v][par[v]] += flow;
      ans = ans + cost[par[v]][v] * flow;
    }
  }
  return ans;
}
vector<pair<long long, long long> > v;
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  v.push_back({0, 1});
  v.push_back({1, 0});
  v.push_back({-1, 0});
  v.push_back({0, -1});
  nn = m * n;
  s = 0, t = nn + 1;
  long long c1 = 0, c0 = 0;
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= m; j++) {
      cin >> a[i][j];
      if ((i + j) % 2 == 0) {
        c0++;
        b[i][j] = c0;
        cap[0][c0] = 1;
      } else {
        c1++;
        b[i][j] = c1 + nn / 2;
        long long ii = c1 + nn / 2;
        cap[ii][t] = 1;
      }
    }
  }
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= m; j++) {
      if ((i + j) % 2 != 0) {
        continue;
      }
      long long ii = b[i][j];
      for (long long t = 0; t < v.size(); t++) {
        long long x = v[t].first + i;
        long long y = v[t].second + j;
        if (x >= 1 && x <= n && y >= 1 && y <= m) {
          long long jj = b[x][y];
          if (a[x][y] == a[i][j]) {
            cost[ii][jj] = 0;
          } else {
            cost[ii][jj] = 1;
          }
          cap[ii][jj] = 1;
          cap[jj][ii] = 0;
          cost[jj][ii] = -cost[ii][jj];
        }
      }
    }
  }
  nn++;
  cout << solve();
  return 0;
}
