Smart Beaver is careful about his appearance and pays special attention to shoes so he has a huge number of pairs of shoes from the most famous brands of the forest. He's trying to handle his shoes carefully so that each pair stood side by side. But by the end of the week because of his very active lifestyle in his dressing room becomes a mess.

Smart Beaver from ABBYY is not only the brightest beaver in the area, but he also is the most domestically oriented. For example, on Mondays the Smart Beaver cleans everything in his home.

It's Monday morning. Smart Beaver does not want to spend the whole day cleaning, besides, there is much in to do and it’s the gym day, so he wants to clean up as soon as possible. Now the floors are washed, the dust is wiped off — it’s time to clean up in the dressing room. But as soon as the Smart Beaver entered the dressing room, all plans for the day were suddenly destroyed: chaos reigned there and it seemed impossible to handle, even in a week. Give our hero some hope: tell him what is the minimum number of shoes need to change the position to make the dressing room neat.

The dressing room is rectangular and is divided into *n* × *m* equal squares, each square contains exactly one shoe. Each pair of shoes has a unique number that is integer from 1 to <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/ab8b2a5b0ca09697178f2939d983946945df958d.png" style="max-width: 100.0%;max-height: 100.0%;"/>, more formally, a square with coordinates (*i*, *j*) contains an integer number of the pair which is lying on it. The Smart Beaver believes that the dressing room is neat only when each pair of sneakers lies together. We assume that the pair of sneakers in squares (*i*<sub class="lower-index">1</sub>, *j*<sub class="lower-index">1</sub>) and (*i*<sub class="lower-index">2</sub>, *j*<sub class="lower-index">2</sub>) lies together if |*i*<sub class="lower-index">1</sub> - *i*<sub class="lower-index">2</sub>| + |*j*<sub class="lower-index">1</sub> - *j*<sub class="lower-index">2</sub>| = 1.
The first line contains two space-separated integers *n* and *m*. They correspond to the dressing room size. Next *n* lines contain *m* space-separated integers each. Those numbers describe the dressing room. Each number corresponds to a snicker. 

It is guaranteed that: 
 -  *n*·*m* is even. -  All numbers, corresponding to the numbers of pairs of shoes in the dressing room, will lie between 1 and <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/ab8b2a5b0ca09697178f2939d983946945df958d.png" style="max-width: 100.0%;max-height: 100.0%;"/>. -  Each number from 1 to <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/ab8b2a5b0ca09697178f2939d983946945df958d.png" style="max-width: 100.0%;max-height: 100.0%;"/> will occur exactly twice. 
The input limits for scoring 30 points are (subproblem C1): 
 -  2 ≤ *n*, *m* ≤ 8. 
The input limits for scoring 100 points are (subproblems C1+C2): 
 -  2 ≤ *n*, *m* ≤ 80.
import bisect
import copy
import decimal
import fractions
import heapq
import itertools
import math
import random
import sys
from collections import Counter,deque,defaultdict
from functools import lru_cache,reduce
from heapq import heappush,heappop,heapify,heappushpop,_heappop_max,_heapify_max
def _heappush_max(heap,item):
    heap.append(item)
    heapq._siftdown_max(heap, 0, len(heap)-1)
def _heappushpop_max(heap, item):
    if heap and item < heap[0]:
        item, heap[0] = heap[0], item
        heapq._siftup_max(heap, 0)
    return item
from math import gcd as GCD
read=sys.stdin.read
readline=sys.stdin.readline
readlines=sys.stdin.readlines
from typing import NamedTuple, Optional, List, Tuple, cast

class MCFGraph:
    class Edge(NamedTuple):
        src: int
        dst: int
        cap: int
        flow: int
        cost: int

    class _Edge:
        def __init__(self, dst: int, cap: int, cost: int) -> None:
            self.dst = dst
            self.cap = cap
            self.cost = cost
            self.rev: Optional[MCFGraph._Edge] = None

    def __init__(self, n: int) -> None:
        self._n = n
        self._g: List[List[MCFGraph._Edge]] = [[] for _ in range(n)]
        self._edges: List[MCFGraph._Edge] = []

    def add_edge(self, src: int, dst: int, cap: int, cost: int) -> int:
        assert 0 <= src < self._n
        assert 0 <= dst < self._n
        assert 0 <= cap
        m = len(self._edges)
        e = MCFGraph._Edge(dst, cap, cost)
        re = MCFGraph._Edge(src, 0, -cost)
        e.rev = re
        re.rev = e
        self._g[src].append(e)
        self._g[dst].append(re)
        self._edges.append(e)
        return m

    def get_edge(self, i: int) -> Edge:
        assert 0 <= i < len(self._edges)
        e = self._edges[i]
        re = cast(MCFGraph._Edge, e.rev)
        return MCFGraph.Edge(
            re.dst,
            e.dst,
            e.cap + re.cap,
            re.cap,
            e.cost
        )

    def edges(self) -> List[Edge]:
        return [self.get_edge(i) for i in range(len(self._edges))]

    def flow(self, s: int, t: int,
             flow_limit: Optional[int] = None) -> Tuple[int, int]:
        return self.slope(s, t, flow_limit)[-1]

    def slope(self, s: int, t: int,
              flow_limit: Optional[int] = None) -> List[Tuple[int, int]]:
        assert 0 <= s < self._n
        assert 0 <= t < self._n
        assert s != t
        if flow_limit is None:
            flow_limit = cast(int, sum(e.cap for e in self._g[s]))

        dual = [0] * self._n
        prev: List[Optional[Tuple[int, MCFGraph._Edge]]] = [None] * self._n

        def refine_dual() -> bool:
            pq = [(0, s)]
            visited = [False] * self._n
            dist: List[Optional[int]] = [None] * self._n
            dist[s] = 0
            while pq:
                dist_v, v = heapq.heappop(pq)
                if visited[v]:
                    continue
                visited[v] = True
                if v == t:
                    break
                dual_v = dual[v]
                for e in self._g[v]:
                    w = e.dst
                    if visited[w] or e.cap == 0:
                        continue
                    reduced_cost = e.cost - dual[w] + dual_v
                    new_dist = dist_v + reduced_cost
                    dist_w = dist[w]
                    if dist_w is None or new_dist < dist_w:
                        dist[w] = new_dist
                        prev[w] = v, e
                        heapq.heappush(pq, (new_dist, w))
            else:
                return False
            dist_t = dist[t]
            for v in range(self._n):
                if visited[v]:
                    dual[v] -= cast(int, dist_t) - cast(int, dist[v])
            return True

        flow = 0
        cost = 0
        prev_cost_per_flow: Optional[int] = None
        result = [(flow, cost)]
        while flow < flow_limit:
            if not refine_dual():
                break
            f = flow_limit - flow
            v = t
            while prev[v] is not None:
                u, e = cast(Tuple[int, MCFGraph._Edge], prev[v])
                f = min(f, e.cap)
                v = u
            v = t
            while prev[v] is not None:
                u, e = cast(Tuple[int, MCFGraph._Edge], prev[v])
                e.cap -= f
                assert e.rev is not None
                e.rev.cap += f
                v = u
            c = -dual[s]
            flow += f
            cost += f * c
            if c == prev_cost_per_flow:
                result.pop()
            result.append((flow, cost))
            prev_cost_per_flow = c
        return result

N,M=map(int,readline().split())
S=[list(map(int,readline().split())) for i in range(N)]
MCFG=MCFGraph(N*M+2)
s=0
t=N*M+1
for i in range(N):
    for j in range(M):
        if i%2==j%2:
            MCFG.add_edge(s,i*M+j+1,1,0)
            for di,dj in ((0,1),(0,-1),(1,0),(-1,0)):
                if 0<=i+di<=N-1 and 0<=j+dj<=M-1:
                    if S[i][j]==S[i+di][j+dj]:
                        MCFG.add_edge(i*M+j+1,(i+di)*M+j+dj+1,1,0)
                    else:
                        MCFG.add_edge(i*M+j+1,(i+di)*M+j+dj+1,1,1)
        else:
            MCFG.add_edge(i*M+j+1,t,1,0)
ans=MCFG.flow(s,t)[1]
print(ans)