#include <bits/stdc++.h>
using namespace std;
const int MX = 7005;
const int INF = 1e8;
vector<int> adj[MX];
char c[MX][MX], w[MX][MX];
int n, m, dist[MX], p[MX];
void addEdge(int a, int b, int waga, int x) {
  adj[a].push_back(b);
  adj[b].push_back(a);
  c[a][b] += waga;
  w[a][b] = x;
  w[b][a] = -x;
}
bool Bfs(int s, int t) {
  queue<int> Q;
  Q.push(s);
  for (int i = 1; i <= n; ++i) {
    dist[i] = INF;
  }
  dist[s] = 0;
  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    for (int i = 0; i < (int)adj[u].size(); ++i) {
      int v = adj[u][i];
      if (c[u][v] > 0) {
        if (dist[v] > dist[u] + w[u][v]) {
          dist[v] = dist[u] + w[u][v];
          p[v] = u;
          Q.push(v);
        }
      }
    }
  }
  return dist[t] != INF;
}
int koszt = 0;
int maxFlow(int s, int t) {
  int result = 0;
  while (Bfs(s, t)) {
    result++;
    int XD = t;
    while (XD != s) {
      c[XD][p[XD]]++;
      c[p[XD]][XD]--;
      koszt -= w[XD][p[XD]];
      XD = p[XD];
    }
  }
  return result;
}
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};
int a, b, TAB[100][100], nr[100][100];
bool ins(int x, int y) { return x >= 1 && x <= a && y >= 1 && y <= b; }
int main() {
  scanf("%d%d", &a, &b);
  for (int i = 1; i <= a; ++i) {
    for (int j = 1; j <= b; ++j) {
      scanf("%d", &TAB[i][j]);
    }
  }
  int NR = 0;
  int s = ++NR;
  for (int i = 1; i <= a; ++i) {
    for (int j = 1; j <= b; ++j) {
      if ((i + j) % 2 == 0) {
        nr[i][j] = ++NR;
      }
    }
  }
  for (int i = 1; i <= a; ++i) {
    for (int j = 1; j <= b; ++j) {
      if ((i + j) % 2 == 1) {
        nr[i][j] = ++NR;
      }
    }
  }
  int t = ++NR;
  n = t;
  for (int i = 1; i <= a; ++i) {
    for (int j = 1; j <= b; ++j) {
      if ((i + j) % 2 == 0) {
        addEdge(s, nr[i][j], 1, 0);
      } else {
        addEdge(nr[i][j], t, 1, 0);
      }
      for (int r = 0; r < 4; ++r) {
        int ip = i + dx[r], jp = j + dy[r];
        if (ins(ip, jp)) {
          if ((i + j) % 2) {
            addEdge(nr[ip][jp], nr[i][j], 1, (int)(TAB[i][j] != TAB[ip][jp]));
          }
        }
      }
    }
  }
  fprintf(stderr, "%d\n", maxFlow(s, t));
  printf("%d\n", koszt);
  return 0;
}
