#include <bits/stdc++.h>
using namespace std;
const int inf = 987654321;
struct MCMF {
  int V, m, tf, tc;
  vector<int> last, nxt, oppo, capa, cost;
  void init(int V_) {
    V = V_;
    m = tf = tc = 0;
    last.clear();
    nxt.clear();
    oppo.clear();
    capa.clear();
    cost.clear();
    last = vector<int>(V, -1);
  }
  void add_edge(int u, int v, int ca, int co) {
    nxt.push_back(last[u]);
    last[u] = m;
    oppo.push_back(v);
    capa.push_back(ca);
    cost.push_back(co);
    m++;
  }
  bool spfa(int s, int t) {
    queue<int> q;
    vector<int> inq(V, 0), dist(V, inf), fe(V, -1);
    q.push(s);
    dist[s] = 0;
    inq[s] = 1;
    while (!q.empty()) {
      int fq = q.front();
      q.pop();
      inq[fq] = 0;
      for (int i = last[fq]; i != -1; i = nxt[i]) {
        int j = oppo[i];
        if (capa[i] && dist[j] > dist[fq] + cost[i]) {
          dist[j] = dist[fq] + cost[i];
          fe[j] = i;
          if (!inq[j]) {
            q.push(j);
            inq[j] = 1;
          }
        }
      }
    }
    if (dist[t] == inf) return false;
    tf++;
    tc += dist[t];
    int now = t;
    while (now != s) {
      int e = fe[now];
      --capa[e];
      ++capa[e ^ 1];
      now = oppo[e ^ 1];
    }
    return true;
  }
} mcmf;
int N, M;
vector<vector<int> > G;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int S, T;
void get_nw() {
  mcmf.init(N * M + 2);
  S = N * M;
  T = N * M + 1;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      if ((i + j) % 2) {
        mcmf.add_edge(i * M + j, T, 1, 0);
        mcmf.add_edge(T, i * M + j, 0, 0);
        continue;
      } else {
        mcmf.add_edge(S, i * M + j, 1, 0);
        mcmf.add_edge(i * M + j, S, 0, 0);
        for (int k = 0; k < 4; k++) {
          int nx = j + dx[k];
          int ny = i + dy[k];
          if (nx < 0 || M <= nx) continue;
          if (ny < 0 || N <= ny) continue;
          int t = G[i][j] == G[ny][nx] ? 0 : 1;
          mcmf.add_edge(i * M + j, ny * M + nx, 1, t);
          mcmf.add_edge(ny * M + nx, i * M + j, 0, -t);
        }
      }
    }
  }
}
int main() {
  scanf("%d %d", &N, &M);
  G = vector<vector<int> >(N, vector<int>(M));
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      scanf("%d", &G[i][j]);
    }
  }
  get_nw();
  for (int i = 0; i < N * M / 2; i++) {
    mcmf.spfa(S, T);
  }
  printf("%d", mcmf.tc);
}
