#include <bits/stdc++.h>
using namespace std;
const int INF = 1000 * 1000 * 1000 + 7;
const double EPS = 1e-9;
const double PI = acos(-1.0);
template <class T>
int chmin(T &t, T f) {
  return (t > f) ? (t = f, 1) : 0;
}
template <class T>
int chmax(T &t, T f) {
  return (t < f) ? (t = f, 1) : 0;
}
inline int getint() {
  int a;
  return scanf("%d", &a) ? a : (fprintf(stderr, "trying to read\n"), -1);
}
inline double getdouble() {
  double a;
  return scanf("%lf", &a) ? a : (fprintf(stderr, "trying to read\n"), -1);
}
const int N = 81;
int board[N][N], m, n;
short c[N * N][N * N], caps[N * N][N * N];
int di[4] = {-1, 0, 1, 0};
int dj[4] = {0, 1, 0, -1};
inline int onboard(int i, int j) { return i >= 0 && j >= 0 && i < m && j < n; }
inline int id(int i, int j) { return i * n + j; }
int MCMF(int s, int t, vector<vector<int> > &a) {
  int ret = 0, n = ((int)(a).size());
  while (true) {
    int p[n];
    int d[n];
    for (int i = (int)0; i < (int)n; i++) d[i] = INF;
    d[s] = 0;
    while (true) {
      int updated = 0;
      for (int i = (int)0; i < (int)n; i++)
        if (d[i] < INF) {
          for (int j = (int)0; j < (int)((int)(a[i]).size()); j++)
            if (caps[i][a[i][j]] > 0 && d[i] + c[i][a[i][j]] < d[a[i][j]]) {
              d[a[i][j]] = d[i] + c[i][a[i][j]];
              p[a[i][j]] = i;
              updated = 1;
            }
        }
      if (!updated) break;
    }
    if (d[t] >= INF) break;
    ret += d[t];
    int cr = t;
    while (cr != s) {
      int pr = p[cr];
      --caps[pr][cr];
      ++caps[cr][pr];
      cr = pr;
    }
  }
  return ret;
}
void myCode() {
  m = getint(), n = getint();
  for (int i = (int)0; i < (int)m; i++)
    for (int j = (int)0; j < (int)n; j++) board[i][j] = getint();
  vector<vector<int> > a(m * n + 2);
  for (int i = (int)0; i < (int)m; i++)
    for (int j = (int)0; j < (int)n; j++)
      if ((i + j) & 1) {
        a[m * n].push_back(id(i, j));
        caps[m * n][id(i, j)] = 1;
        for (int k = (int)0; k < (int)4; k++)
          if (onboard(i + di[k], j + dj[k])) {
            int first = id(i, j), second = id(i + di[k], j + dj[k]);
            a[first].push_back(second);
            a[second].push_back(first);
            caps[first][second] = 1;
            if (board[i][j] == board[i + di[k]][j + dj[k]])
              c[first][second] = c[second][first] = 0;
            else {
              c[first][second] = 1;
              c[second][first] = -1;
            }
          }
      } else {
        a[id(i, j)].push_back(m * n + 1);
        caps[id(i, j)][m * n + 1] = 1;
      }
  printf("%d\n", MCMF(m * n, m * n + 1, a));
}
int main() {
  myCode();
  return 0;
}
