#include <bits/stdc++.h>
using namespace std;
const int MaxN = 3300;
const short int inf = 30000;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
short int a[MaxN][MaxN], b[MaxN][MaxN];
int n, m;
int N, M;
short int w[2 * MaxN][2 * MaxN];
bool ok(int i, int j) { return (i > 0 && i <= n && j > 0 && j <= m); }
void makeGraph() {
  N = (n * m) / 2;
  for (int i = 1; i <= 2 * N; i++)
    for (int j = 1; j <= 2 * N; j++) w[i][j] = -inf;
  int c1 = 0, c2 = N;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      if ((i + j) % 2 == 0) {
        b[i][j] = ++c1;
      } else
        b[i][j] = ++c2;
    }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (b[i][j] <= N) {
        for (int k = 0; k < 4; k++) {
          int x = i + dx[k];
          int y = j + dy[k];
          if (ok(x, y)) {
            w[b[i][j]][b[x][y]] = (a[i][j] == a[x][y] ? 1 : 0);
            w[b[x][y]][b[i][j]] = (a[i][j] == a[x][y] ? 1 : 0);
          }
        }
      }
}
int Pair[2 * MaxN];
int l[2 * MaxN];
int p[2 * MaxN];
int slack[2 * MaxN], slackInd[2 * MaxN];
bool inS[2 * MaxN], inT[2 * MaxN];
bool inN_lS[2 * MaxN];
int Hungarian_Algorithm() {
  vector<int> N_lS;
  int i, j, u, v, alpha;
  bool AugmentingPathFound;
  for (int i = 1; i <= N; i++) {
    l[i] = 0;
    for (int j = N + 1; j <= 2 * N; j++)
      if (l[i] < w[i][j]) l[i] = w[i][j];
  }
  for (int i = N + 1; i <= 2 * N; i++) l[i] = 0;
  M = 0;
  memset(Pair, 0, sizeof(Pair));
  for (int i = 1; i <= N; i++) {
    int j = N + 1;
    while (Pair[i] == 0 && j <= 2 * N) {
      if (Pair[j] == 0 && l[i] + l[j] == w[i][j]) {
        M++;
        Pair[i] = j;
        Pair[j] = i;
      } else
        j++;
      if (M == N) break;
    }
  }
  while (M < N) {
    memset(inS, 0, sizeof(inS));
    memset(inT, 0, sizeof(inT));
    memset(inN_lS, 0, sizeof(inN_lS));
    N_lS.clear();
    u = 1;
    while (Pair[u] != 0) u++;
    inS[u] = true;
    for (v = N + 1; v <= 2 * N; v++)
      if (l[u] + l[v] == w[u][v]) {
        p[v] = u;
        inN_lS[v] = true;
        N_lS.push_back(v);
      }
    for (v = N + 1; v <= 2 * N; v++) {
      slack[v] = l[u] + l[v] - w[u][v];
      slackInd[v] = u;
    }
    AugmentingPathFound = false;
    while (!AugmentingPathFound) {
      i = 0;
      while (i < N_lS.size() && inT[N_lS[i]]) i++;
      if (i >= N_lS.size()) {
        alpha = inf;
        for (v = N + 1; v <= 2 * N; v++)
          if (!inT[v] && slack[v] < alpha) alpha = slack[v];
        for (v = N + 1; v <= 2 * N; v++)
          if (!inT[v]) slack[v] -= alpha;
        for (u = 1; u <= N; u++)
          if (inS[u]) l[u] -= alpha;
        for (v = N + 1; v <= 2 * N; v++)
          if (inT[v]) l[v] += alpha;
        for (v = N + 1; v <= 2 * N; v++)
          if (!inN_lS[v] && l[v] + l[slackInd[v]] == w[v][slackInd[v]]) {
            p[v] = slackInd[v];
            inN_lS[v] = true;
            N_lS.push_back(v);
          }
      } else {
        v = N_lS[i];
        if (Pair[v] != 0) {
          u = Pair[v];
          inS[u] = true;
          inT[v] = true;
          for (v = N + 1; v <= 2 * N; v++)
            if (!inT[v] && l[u] + l[v] - w[u][v] < slack[v]) {
              slack[v] = l[u] + l[v] - w[u][v];
              slackInd[v] = u;
            }
          for (v = N + 1; v <= 2 * N; v++)
            if (!inN_lS[v] && l[u] + l[v] == w[u][v]) {
              p[v] = u;
              inN_lS[v] = true;
              N_lS.push_back(v);
            }
        } else {
          AugmentingPathFound = true;
          do {
            u = p[v];
            Pair[v] = u;
            if (Pair[u] != 0) {
              u = Pair[u];
              Pair[Pair[u]] = v;
              v = u;
            } else {
              Pair[u] = v;
              u = 0;
            }
          } while (u != 0);
          M++;
        }
      }
    }
  }
  int weight = 0;
  for (int i = 1; i <= N; i++) weight += w[i][Pair[i]];
  return weight;
}
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]);
  makeGraph();
  int sol = N - Hungarian_Algorithm();
  printf("%d\n", sol);
  return 0;
}
