#include <bits/stdc++.h>
using namespace std;
int board[8][8];
int dyn[9][1 << 8];
int n, m;
void calc(int x, int y, int mask, int nextMask, int count) {
  if (x == n) {
    return;
  }
  if (y >= m) {
    dyn[x + 1][nextMask] = min(dyn[x + 1][nextMask], dyn[x][mask] + count);
    return;
  }
  int myMask = 1 << y;
  if (mask & myMask) {
    calc(x, y + 1, mask, nextMask, count);
  } else {
    if (x < n - 1) {
      int plus = board[x][y] == board[x + 1][y] ? 0 : 1;
      calc(x, y + 1, mask, nextMask | myMask, count + plus);
    }
    if (y < m - 1 && !(mask & (1 << (y + 1)))) {
      int plus = board[x][y] == board[x][y + 1] ? 0 : 1;
      calc(x, y + 2, mask, nextMask, count + plus);
    }
  }
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> board[i][j];
    }
  }
  for (int i = 0; i < n + 1; i++) {
    for (int j = 0; j < 1 << m; j++) {
      dyn[i][j] = n * m;
    }
  }
  dyn[0][0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 1 << m; j++) {
      calc(i, 0, j, 0, 0);
    }
  }
  cout << dyn[n][0] << endl;
  return 0;
}
