#include <bits/stdc++.h>
using namespace std;
const int N = 6405;
const int INF = 1e9;
int n;
int w[N][N];
int m1[N], m2[N];
int l1[N], l2[N], p[N], slack[N];
bool blue[N];
int hungarian() {
  for (int i = 0; i < (n); ++i) {
    m1[i] = m2[i] = p[i] = -1;
    l1[i] = l2[i] = -INF;
  }
  for (int i = 0; i < (n); ++i)
    for (int j = 0; j < (n); ++j) {
      l1[i] = max(l1[i], w[i][j]);
      l2[j] = max(l2[j], w[i][j]);
    }
  for (int k = 0; k < (n); ++k) {
    for (int i = 0; i < (n); ++i) {
      slack[i] = INF;
      p[i] = -1;
    }
    for (int i = 0; i < (n); ++i)
      if (blue[i] = (m1[i] == -1))
        for (int j = 0; j < (n); ++j)
          slack[j] = min(slack[j], l1[i] + l2[j] - w[i][j]);
    int u;
    for (;;) {
      u = min_element(slack, slack + n) - slack;
      if (slack[u]) {
        int d = slack[u];
        for (int i = 0; i < (n); ++i) {
          if (blue[i]) l1[i] -= d;
          if (slack[i] == INF)
            l2[i] += d;
          else
            slack[i] -= d;
        }
      } else {
        for (int i = 0; i < (n); ++i)
          if (blue[i] && w[i][u] == l1[i] + l2[u]) {
            p[u] = i;
            break;
          }
        slack[u] = INF;
        if (m2[u] == -1) break;
        blue[m2[u]] = true;
        for (int i = 0; i < (n); ++i)
          if (slack[i] != INF)
            slack[i] = min(slack[i], l1[m2[u]] + l2[i] - w[m2[u]][i]);
      }
    }
    while (u != -1) {
      m2[u] = p[u];
      swap(u, m1[p[u]]);
    }
  }
  int result = 0;
  for (int i = 0; i < (n); ++i) result += w[i][m1[i]];
  return result;
}
int main() {
  int a, s;
  cin >> a >> s;
  n = a * s / 2;
  int T[a][s];
  for (int i = 0; i < a; i++)
    for (int j = 0; j < s; j++) cin >> T[i][j];
  pair<int, int> M[4] = {make_pair(0, 1), make_pair(0, -1), make_pair(1, 0),
                         make_pair(-1, 0)};
  for (int i = 0; i < a; i++)
    for (int j = 0; j < s; j++)
      if ((i + j) % 2 == 0)
        for (int k = 0; k < 4; k++) {
          int d1 = M[k].first, d2 = M[k].second;
          if (0 <= i + d1 && i + d1 < a && 0 <= j + d2 && j + d2 < s) {
            w[(j + s * i) / 2][(j + d2 + s * (i + d1)) / 2] =
                1e4 + (int)(T[i][j] == T[i + d1][j + d2]);
          }
        }
  int tmp = hungarian();
  cout << a * s / 2 - (tmp - 1e4 * a * s / 2) << endl;
  return 0;
}
