#include <bits/stdc++.h>
using namespace std;
int n, m;
int ans = 64;
int a[8][8];
bool used[8][8];
void solve(int i, int j, int change) {
  if (change >= ans) return;
  if (i == n - 1 && j == m - 1) {
    ans = change;
    return;
  }
  if (used[i][j]) {
    if (j == m - 1)
      solve(i + 1, 0, change);
    else
      solve(i, j + 1, change);
    return;
  }
  if (j + 1 < m && !used[i][j + 1]) {
    used[i][j + 1] = 1;
    solve(i, j + 1, change + (a[i][j] != a[i][j + 1]));
    used[i][j + 1] = 0;
  }
  if (i + 1 < n) {
    used[i + 1][j] = 1;
    if (j == m - 1)
      solve(i + 1, 0, change + (a[i][j] != a[i + 1][j]));
    else
      solve(i, j + 1, change + (a[i][j] != a[i + 1][j]));
    used[i + 1][j] = 0;
  }
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) cin >> a[i][j];
  solve(0, 0, 0);
  cout << ans;
}
