#include <bits/stdc++.h>
using namespace std;
int n, m, tot, s, t, maxf, minc;
int imap[205][205];
struct node {
  int end, cost, next, wei;
} edge[60005];
int head[60005], dis[60005], mark[60005], pre[60005], fi[60005];
int fx[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
vector<int> ss, tt;
void add(int u, int v, int w, int c) {
  edge[tot].end = v;
  edge[tot].next = head[u];
  edge[tot].cost = c;
  edge[tot].wei = w;
  head[u] = tot++;
  edge[tot].end = u;
  edge[tot].next = head[v];
  edge[tot].cost = -c;
  edge[tot].wei = 0;
  head[v] = tot++;
}
int bfs() {
  memset(pre, -1, sizeof(pre));
  memset(mark, 0, sizeof(mark));
  memset(dis, 0x3f3f3f3f, sizeof(dis));
  dis[s] = 0;
  queue<int> q;
  q.push(s);
  mark[s] = 1;
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    mark[x] = 0;
    for (int i = head[x]; i != -1; i = edge[i].next) {
      if (edge[i].wei == 0) continue;
      int end = edge[i].end;
      int cost = edge[i].cost;
      fi[end] = min(fi[x], edge[i].wei);
      if (dis[end] > dis[x] + cost) {
        dis[end] = dis[x] + cost;
        pre[end] = i;
        if (!mark[end]) {
          mark[end] = 1;
          q.push(end);
        }
      }
    }
  }
  if (dis[t] == 0x3f3f3f3f) return 0;
  return 1;
}
void updata() {
  int u = t;
  while (u != s) {
    int i = pre[u];
    edge[i].wei -= fi[t];
    edge[i ^ 1].wei += fi[t];
    u = edge[i ^ 1].end;
  }
  maxf += fi[t];
  minc += dis[t] * fi[t];
}
int main() {
  tot = 0;
  maxf = 0;
  minc = 0;
  memset(head, -1, sizeof(head));
  memset(fi, 0x3f3f3f3f, sizeof(fi));
  ss.clear();
  tt.clear();
  scanf("%d%d", &n, &m);
  s = n * m;
  t = s + 1;
  int ju = 0;
  for (int i = 0; i < n; i++) {
    ju = i % 2;
    for (int j = 0; j < m; j++) {
      scanf("%d", &imap[i][j]);
      if (ju == 0) {
        add(s, i * m + j, 1, 0);
        ss.push_back(i * m + j);
      } else {
        add(i * m + j, t, 1, 0);
        tt.push_back(i * m + j);
      }
      ju ^= 1;
    }
  }
  for (int i = 0; i < ss.size(); i++) {
    int x = ss[i] / m;
    int y = ss[i] % m;
    for (int j = 0; j < 4; j++) {
      int nx = x + fx[j][0];
      int ny = y + fx[j][1];
      if (0 <= nx && nx < n && 0 <= ny && ny < m) {
        int c = imap[x][y] == imap[nx][ny] ? 0 : 1;
        add(ss[i], nx * m + ny, 1, c);
      }
    }
  }
  while (bfs()) updata();
  printf("%d\n", minc);
}
