#include <bits/stdc++.h>
using namespace std;
const int maxn = 300 + 5;
const int inf = (1e9) + 10;
const long double eps = 1e-12;
struct rib {
  int b, u, c, f, back;
};
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
vector<rib> g[maxn];
int n, m;
int s, t;
int V;
void add_rib(int a, int b, int u, int c) {
  rib r1 = {b, u, c, 0, (int)g[b].size()};
  rib r2 = {a, 0, -c, 0, (int)g[a].size()};
  g[a].push_back(r1);
  g[b].push_back(r2);
}
int mcmf() {
  int flow = 0, cost = 0;
  while (flow < inf) {
    vector<int> id(V, 0);
    vector<int> d(V, inf);
    vector<int> q(V);
    vector<int> p(V);
    vector<int> p_rib(V);
    int qh = 0, qt = 0;
    q[qt++] = s;
    d[s] = 0;
    while (qh != qt) {
      int v = q[qh++];
      id[v] = 2;
      if (qh == V) qh = 0;
      for (int i = 0; i < g[v].size(); ++i) {
        rib& r = g[v][i];
        if (r.f < r.u && d[v] + r.c < d[r.b]) {
          d[r.b] = d[v] + r.c;
          if (id[r.b] == 0) {
            q[qt++] = r.b;
            if (qt == V) qt = 0;
          } else if (id[r.b] == 2) {
            if (--qh == -1) qh = V - 1;
            q[qh] = r.b;
          }
          id[r.b] = 1;
          p[r.b] = v;
          p_rib[r.b] = i;
        }
      }
    }
    if (d[t] == inf) break;
    int addflow = inf - flow;
    for (int v = t; v != s; v = p[v]) {
      int pv = p[v];
      int pr = p_rib[v];
      addflow = min(addflow, g[pv][pr].u - g[pv][pr].f);
    }
    for (int v = t; v != s; v = p[v]) {
      int pv = p[v];
      int pr = p_rib[v], r = g[pv][pr].back;
      g[pv][pr].f += addflow;
      g[v][r].f -= addflow;
      cost += g[pv][pr].c * addflow;
    }
    flow += addflow;
  }
  return cost;
}
int grid[maxn][maxn];
bool valid(int i, int j) {
  if (i < 1 || j < 1 || i > n || j > m) return false;
  return true;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> m;
  for (int i = (int)1; i <= (int)n; i++) {
    for (int j = (int)1; j <= (int)m; j++) {
      cin >> grid[i][j];
    }
  }
  s = 0;
  t = n * m + 1;
  V = t + 1;
  for (int i = (int)1; i <= (int)n; i++) {
    for (int j = (int)1; j <= (int)m; j++) {
      if ((i + j) % 2) {
        add_rib((i - 1) * m + j, t, 1, 0);
      } else {
        add_rib(s, (i - 1) * m + j, 1, 0);
        for (int k = (int)0; k <= (int)3; k++) {
          int tx = i + dx[k];
          int ty = j + dy[k];
          if (!valid(tx, ty)) continue;
          add_rib((i - 1) * m + j, (tx - 1) * m + ty, 1,
                  grid[i][j] != grid[tx][ty]);
        }
      }
    }
  }
  cout << mcmf() << endl;
  return 0;
}
