#include <bits/stdc++.h>
using namespace std;
struct Edge {
  int u, v, next;
  long long cost, cap;
} e[2000];
int source, sink, edgenum, first[300], par[300];
int rec[300], vis[300];
long long dist[300], C;
void add(int u, int v, long long f, long long c) {
  e[edgenum].u = u;
  e[edgenum].v = v;
  e[edgenum].cost = c;
  e[edgenum].cap = f;
  e[edgenum].next = first[u];
  first[u] = edgenum;
  edgenum++;
}
void add_edge(int u, int v, long long f, long long c) {
  add(u, v, f, c);
  add(v, u, 0, -c);
}
void SPFA(int node) {
  deque<int> q;
  for (int i = 0; i <= node; i++) dist[i] = LLONG_MAX;
  dist[source] = 0;
  vis[source] = 1;
  q.push_front(source);
  while (!q.empty()) {
    int u = q.front();
    vis[u] = 0;
    q.pop_front();
    for (int k = first[u]; k != -1; k = e[k].next) {
      int v = e[k].v;
      if (e[k].cap > 0 && (dist[u] + e[k].cost) < dist[v]) {
        par[v] = u;
        dist[v] = dist[u] + e[k].cost;
        rec[v] = k;
        if (!vis[v]) {
          vis[v] = 1;
          if (q.size() && dist[q.front()] > dist[v])
            q.push_front(v);
          else
            q.push_back(v);
        }
      }
    }
  }
  return;
}
long long mincost_maxflow(int node) {
  long long F = 0, mn;
  C = 0;
  memset(vis, 0, sizeof(vis));
  memset(par, -1, sizeof(par));
  while (1) {
    SPFA(node);
    if (dist[sink] == LLONG_MAX) break;
    mn = LLONG_MAX;
    for (int k = sink; k != source; k = par[k]) {
      int r = rec[k];
      mn = min(mn, max(0LL, e[r].cap));
    }
    for (int k = sink; k != source; k = par[k]) {
      int r = rec[k];
      e[r].cap -= mn;
      e[r ^ 1].cap += mn;
      e[r ^ 1].cost = -e[r].cost;
    }
    F += mn;
    C += mn * dist[sink];
  }
  return F;
}
int n, m;
int getid(int i, int j) { return i * m + j; }
int dx[] = {0, -1, 1, 0};
int dy[] = {1, 0, 0, -1};
bool valid(int x, int y) { return (x >= 0 && x < n && y >= 0 && y < m); }
int mat[100][100];
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      scanf("%d", &mat[i][j]);
    }
  }
  edgenum = 0;
  memset(first, -1, sizeof(first));
  source = n * m + 1;
  sink = n * m + 2;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if ((i + j) & 1) {
        add_edge(source, getid(i, j), 1, 0);
        for (int k = 0; k < 4; k++) {
          int x = i + dx[k];
          int y = j + dy[k];
          if (valid(x, y)) {
            if (mat[i][j] == mat[x][y])
              add_edge(getid(i, j), getid(x, y), 1, 0);
            else
              add_edge(getid(i, j), getid(x, y), 1, 1);
          }
        }
      } else
        add_edge(getid(i, j), sink, 1, 0);
    }
  }
  mincost_maxflow(n * m + 4);
  printf("%lld\n", C);
  return 0;
}
