#include <bits/stdc++.h>
using namespace std;
const int inf = 1 << 29;
const double dinf = 1e30;
const long long linf = 1LL << 55;
int n, m;
const int N = 6444;
int board[88][88];
int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};
bool check(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; }
int ans, anscost, eh[N], tot, low[N], p[N], dist[N];
bool visit[N];
int S, T;
struct Edge {
  int u, v, cost, cap, flow, next;
} et[N * 10];
bool spfa() {
  queue<int> que;
  memset(visit, false, sizeof(visit));
  memset(p, -1, sizeof(p));
  memset(low, 0, sizeof(low));
  fill(&dist[0], &dist[N], inf);
  visit[S] = true, low[S] = inf, dist[S] = 0;
  que.push(S);
  while (!que.empty()) {
    int u = que.front();
    que.pop();
    visit[u] = false;
    for (int i = eh[u]; i != -1; i = et[i].next) {
      int v = et[i].v, cost = et[i].cost, cap = et[i].cap, flow = et[i].flow;
      if (flow < cap && dist[u] + cost < dist[v]) {
        dist[v] = dist[u] + cost;
        p[v] = i;
        low[v] = min(low[u], cap - flow);
        if (!visit[v]) {
          visit[v] = true;
          que.push(v);
        }
      }
    }
  }
  return dist[T] != inf;
}
void costflow() {
  ans = 0, anscost = 0;
  while (spfa()) {
    int x = p[T];
    ans += low[T];
    anscost += low[T] * dist[T];
    while (x != -1) {
      et[x].flow += low[T];
      et[x ^ 1].flow -= low[T];
      et[x ^ 1].cost = -et[x].cost;
      x = p[et[x].u];
    }
  }
}
void add(int u, int v, int cost, int cap, int flow) {
  Edge e = {u, v, cost, cap, flow, eh[u]};
  et[tot] = e;
  eh[u] = tot++;
}
void addedge(int u, int v, int cost, int cap) {
  add(u, v, cost, cap, 0), add(v, u, -cost, 0, 0);
}
void init() {
  tot = 0;
  memset(eh, -1, sizeof(eh));
}
int main() {
  while (cin >> n >> m) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        cin >> board[i][j];
      }
    }
    S = n * m, T = S + 1;
    init();
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if ((i + j) % 2 == 0) {
          addedge(S, i * m + j, 0, 1);
          for (int k = 0; k < 4; k++) {
            int x = i + dx[k], y = j + dy[k];
            if (check(x, y)) {
              addedge(i * m + j, x * m + y, board[x][y] != board[i][j], 1);
            }
          }
        } else
          addedge(i * m + j, T, 0, 1);
      }
    }
    costflow();
    cout << anscost << endl;
  }
  return 0;
}
