#include <bits/stdc++.h>
using namespace std;
int H, W;
const int MW = 100;
int orig[MW][MW];
const int MN = 2 * MW * MW;
vector<int> conn[MN];
const int MM = 8 * MN;
int to[MM];
int cost[MM];
int cap[MN];
int K = 0;
void join(int a, int b, int c, int d) {
  conn[a].push_back(K);
  conn[b].push_back(K + 1);
  to[K] = b;
  to[K + 1] = a;
  cost[K] = d;
  cost[K + 1] = -d;
  cap[K] = c;
  cap[K + 1] = 0;
  K += 2;
}
int dist[MN];
int froms[MN];
int N;
int res = 0;
bool bford() {
  for (int i = 0; i < N; ++i) dist[i] = 1e9;
  dist[0] = 0;
  froms[0] = -1;
  for (int i = 0; i < 2 + W * H; ++i) {
    for (int j = 0; j < K; ++j) {
      if (!cap[j]) continue;
      int f = to[j ^ 1];
      int d = dist[f];
      if (d < 0) continue;
      int t = to[j];
      int dd = d + cost[j];
      if (dd < dist[t]) {
        dist[t] = dd;
        froms[t] = j;
      }
    }
  }
  if (dist[1] > 1e8) return 0;
  int c = 1;
  while (froms[c] >= 0) {
    int e = froms[c];
    --cap[e];
    ++cap[e ^ 1];
    res += cost[e];
    c = to[e ^ 1];
  }
  return 1;
}
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
int main() {
  cin >> H >> W;
  for (int i = 0; i < H; ++i)
    for (int j = 0; j < W; ++j) cin >> orig[i][j];
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      int id = 2 + W * i + j;
      if ((i + j) & 1) {
        join(id, 1, 1, 0);
      } else {
        join(0, id, 1, 0);
        for (int k = 0; k < 4; ++k) {
          int x = j + dx[k], y = i + dy[k];
          if (x < 0 || y < 0 || x >= W || y >= H) continue;
          int to = 2 + W * y + x;
          join(id, to, 1, orig[i][j] != orig[y][x]);
        }
      }
    }
  }
  N = 2 + W * H;
  while (bford()) {
  }
  cout << res << '\n';
}
