#include <bits/stdc++.h>
using namespace std;
short cap[80 * 80 + 2][80 * 80 + 2];
short cost[80 * 80 + 2][80 * 80 + 2];
short fnet[80 * 80 + 2][80 * 80 + 2], deg[80 * 80 + 2];
vector<int> adj[80 * 80 + 2];
int par[80 * 80 + 2], d[80 * 80 + 2], q[80 * 80 + 2], inq[80 * 80 + 2], qs;
int pi[80 * 80 + 2];
bool dijkstra(int n, int s, int t) {
  memset(d, 0x3f, sizeof(d));
  memset(par, -1, sizeof(par));
  memset(inq, -1, sizeof(inq));
  d[s] = qs = 0;
  inq[q[qs++] = s] = 0;
  par[s] = n;
  while (qs) {
    int u = q[0];
    inq[u] = -1;
    q[0] = q[--qs];
    if (qs) inq[q[0]] = 0;
    for (int i = 0, j = 2 * i + 1, t; j < qs; i = j, j = 2 * i + 1) {
      if (j + 1 < qs && d[q[j + 1]] < d[q[j]]) j++;
      if (d[q[j]] >= d[q[i]]) break;
      {
        t = q[i];
        q[i] = q[j];
        q[j] = t;
        t = inq[q[i]];
        inq[q[i]] = inq[q[j]];
        inq[q[j]] = t;
      };
    }
    for (int k = 0, v = adj[u][k]; k < deg[u]; v = adj[u][++k]) {
      if (fnet[v][u] && d[v] > (d[u] + pi[u] - pi[v]) - cost[v][u])
        d[v] = (d[u] + pi[u] - pi[v]) - cost[v][par[v] = u];
      if (fnet[u][v] < cap[u][v] && d[v] > (d[u] + pi[u] - pi[v]) + cost[u][v])
        d[v] = (d[u] + pi[u] - pi[v]) + cost[par[v] = u][v];
      if (par[v] == u) {
        if (inq[v] < 0) {
          inq[q[qs] = v] = qs;
          qs++;
        }
        for (int i = inq[v], j = (i - 1) / 2, t; d[q[i]] < d[q[j]];
             i = j, j = (i - 1) / 2) {
          t = q[i];
          q[i] = q[j];
          q[j] = t;
          t = inq[q[i]];
          inq[q[i]] = inq[q[j]];
          inq[q[j]] = t;
        };
      }
    }
  }
  for (int i = 0; i < n; i++)
    if (pi[i] < 0x7fffffff / 2) pi[i] += d[i];
  return par[t] >= 0;
}
int mcmf4(int n, int s, int t, int &fcost) {
  memset(deg, 0, sizeof(deg));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      if (cap[i][j] || cap[j][i]) {
        adj[i].push_back(j);
        deg[i]++;
      }
  memset(fnet, 0, sizeof(fnet));
  memset(pi, 0, sizeof(pi));
  int flow = fcost = 0;
  while (dijkstra(n, s, t)) {
    int bot = 0x7fffffff;
    for (int v = t, u = par[v]; v != s; u = par[v = u])
      bot = min(bot, fnet[v][u] ? fnet[v][u] : (cap[u][v] - fnet[u][v]));
    for (int v = t, u = par[v]; v != s; u = par[v = u])
      if (fnet[v][u]) {
        fnet[v][u] -= bot;
        fcost -= bot * cost[v][u];
      } else {
        fnet[u][v] += bot;
        fcost += bot * cost[u][v];
      }
    flow += bot;
  }
  return flow;
}
int n, m, arr[100][100];
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) cin >> arr[i][j];
  int d[4][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}}, X, Y;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      if (((i + j) & 1) == 0) {
        for (int k = 0; k < 4; k++) {
          X = i + d[k][0];
          Y = j + d[k][1];
          if (X >= n || Y >= m) continue;
          cap[i * m + j][X * m + Y] = 1;
          cost[i * m + j][X * m + Y] = 1 - (arr[i][j] == arr[X][Y]);
        }
      }
      if ((i + j) & 1) {
        cap[i * m + j][n * m + 1] = 1;
        cost[i * m + j][n * m + 1] = 0;
      } else {
        cap[n * m][i * m + j] = 1;
        cost[n * m][i * m + j] = 0;
      }
    }
  int u;
  mcmf4(n * m + 2, n * m, n * m + 1, u);
  cout << u << endl;
  return 0;
}
