#include <bits/stdc++.h>
using namespace std;
const int N = 85 * 85 * 2 + 10;
const int M = 85 * 85 * 4 * 2 + 10;
const int INF = 0x3f3f3f3f;
struct Edge {
  int from, to, next, cap, cost;
  Edge() {}
  Edge(int From, int To, int Next, int Cap, int Cost) {
    from = From, to = To, next = Next, cap = Cap, cost = Cost;
  }
} e[M];
int head[N];
int total;
int dis[N], vis[N];
int path[N], low[N];
void init() {
  total = 0;
  memset(head, -1, sizeof(head));
}
void adde(int from, int to, int cap, int cost) {
  e[total] = Edge(from, to, head[from], cap, cost);
  head[from] = total++;
  e[total] = Edge(to, from, head[to], 0, -cost);
  head[to] = total++;
}
int Spfa(int s, int t) {
  memset(vis, 0, sizeof(vis));
  memset(dis, 0x3f, sizeof(dis));
  memset(low, 0, sizeof(low));
  queue<int> qq;
  qq.push(s);
  vis[s] = 1;
  low[s] = INF;
  dis[s] = 0;
  while (!qq.empty()) {
    int u = qq.front();
    qq.pop();
    vis[u] = 0;
    for (int i = head[u]; i != -1; i = e[i].next) {
      int v = e[i].to;
      if (e[i].cap > 0 && (dis[v] > dis[u] + e[i].cost)) {
        path[v] = i;
        low[v] = min(low[u], e[i].cap);
        dis[v] = dis[u] + e[i].cost;
        if (!vis[v]) {
          vis[v] = 1;
          qq.push(v);
        }
      }
    }
  }
  return low[t];
}
int _mcmf(int s, int t) {
  int flow = 0;
  int cost = 0;
  while (Spfa(s, t)) {
    int f = low[t];
    for (int i = t; i != s; i = e[path[i]].from) {
      e[path[i]].cap -= f;
      e[path[i] ^ 1].cap += f;
    }
    flow += f;
    cost += dis[t] * f;
  }
  return cost;
}
int aa[85][85];
int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int main() {
  int n, m;
  while (scanf("%d%d", &n, &m) != EOF) {
    init();
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        scanf("%d", &aa[i][j]);
      }
    }
    int s = 0, t = n * m * 2 + 1;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        if ((i + j) % 2 == 0) {
          adde(s, (i - 1) * m + j, 1, 0);
          for (int k = 0; k < 4; k++) {
            int ii = i + dir[k][0], jj = j + dir[k][1];
            if (ii >= 1 && ii <= n && jj >= 1 && jj <= m) {
              if (aa[i][j] == aa[ii][jj])
                adde((i - 1) * m + j, n * m + (ii - 1) * m + jj, 1, 0);
              else
                adde((i - 1) * m + j, n * m + (ii - 1) * m + jj, 1, 1);
            }
          }
        } else {
          adde(n * m + (i - 1) * m + j, t, 1, 0);
        }
      }
    }
    int cost = _mcmf(s, t);
    printf("%d\n", cost);
  }
  return 0;
}
