#include <bits/stdc++.h>
using namespace std;
struct Edge {
  int Data, Weight, Cost, Next;
};
int Get() {
  char c;
  while (c = getchar(), (c < '0' || c > '9') && (c != '-'))
    ;
  bool Flag = (c == '-');
  if (Flag) c = getchar();
  int X = 0;
  while (c >= '0' && c <= '9') {
    X = X * 10 + c - 48;
    c = getchar();
  }
  return Flag ? -X : X;
}
void Output(int X) {
  if (X < 0) {
    putchar('-');
    X = -X;
  }
  int Len = 0, Data[10];
  while (X) {
    Data[Len++] = X % 10;
    X /= 10;
  }
  if (!Len) Data[Len++] = 0;
  while (Len--) putchar(Data[Len] + 48);
  putchar('\n');
}
const int inf = 1000000000;
const int V[4][2] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};
int G[6402];
Edge E[40000];
void Add(int Number, int X, int Y, int W, int C) {
  E[Number * 2] = (Edge){Y, W, C, G[X]};
  G[X] = Number * 2;
  E[Number * 2 + 1] = (Edge){X, 0, -C, G[Y]};
  G[Y] = Number * 2 + 1;
}
int SPFA(int S, int T, int N) {
  static int Dist[6402], Pre[6402], Queue[6402];
  for (int i = 0; i < N; i++) Dist[i] = (i == S) ? 0 : inf;
  int Front = 0, Rear = 1;
  Queue[0] = S;
  static bool Flag[6402];
  memset(Flag, 0, sizeof(Flag));
  while (Front != Rear) {
    int V = Queue[Front++], P = G[V];
    Flag[V] = false;
    if (Front == N) Front = 0;
    while (P != -1) {
      int U = E[P].Data;
      if (E[P].Weight && Dist[V] + E[P].Cost < Dist[U]) {
        Dist[U] = Dist[V] + E[P].Cost;
        Pre[U] = P;
        if (!Flag[U]) {
          Flag[U] = true;
          Queue[Rear++] = U;
          if (Rear == N) Rear = 0;
        }
      }
      P = E[P].Next;
    }
  }
  int V = T;
  while (V != S) {
    E[Pre[V]].Weight--;
    E[Pre[V] ^ 1].Weight++;
    V = E[Pre[V] ^ 1].Data;
  }
  return Dist[T];
}
int main() {
  int M = Get(), N = Get();
  static int A[80][80];
  for (int i = 0; i < M; i++)
    for (int j = 0; j < N; j++) A[i][j] = Get();
  int S = M * N, T = M * N + 1, Counter = 0;
  memset(G, -1, sizeof(G));
  for (int i = 0; i < M; i++)
    for (int j = 0; j < N; j++)
      if ((i + j) & 1)
        Add(Counter++, S, i * N + j, 1, 0);
      else
        Add(Counter++, i * N + j, T, 1, 0);
  for (int i = 0; i < M; i++)
    for (int j = 0; j < N; j++)
      if ((i + j) & 1)
        for (int D = 0; D < 4; D++) {
          int x = i + V[D][0], y = j + V[D][1];
          if (x >= 0 && x < M && y >= 0 && y < N)
            Add(Counter++, i * N + j, x * N + y, 1, (A[i][j] != A[x][y]));
        }
  int Ans = 0;
  for (int i = 0; i < M * N / 2; i++) Ans += SPFA(S, T, M * N + 2);
  Output(Ans);
  return 0;
}
