CF316C1.Tidying Up

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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×mn×m equal squares, each square contains exactly one shoe. Each pair of shoes has a unique number that is integer from 11 to , more formally, a square with coordinates (i,j)(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 (i1,j1)(i_{1},j_{1}) and (i2,j2)(i_{2},j_{2}) lies together if i1i2+j1j2=1|i_{1}-i_{2}|+|j_{1}-j_{2}|=1 .

输入格式

The first line contains two space-separated integers nn and mm . They correspond to the dressing room size. Next nn lines contain mm space-separated integers each. Those numbers describe the dressing room. Each number corresponds to a snicker.

It is guaranteed that:

  • nmn·m is even.
  • All numbers, corresponding to the numbers of pairs of shoes in the dressing room, will lie between 11 and .
  • Each number from 11 to will occur exactly twice.

The input limits for scoring 30 points are (subproblem C1):

  • 2<=n,m<=82<=n,m<=8 .

The input limits for scoring 100 points are (subproblems C1+C2):

  • 2<=n,m<=802<=n,m<=80 .

输出格式

Print exactly one integer — the minimum number of the sneakers that need to change their location.

输入输出样例

  • 输入#1

    2 3
    1 1 2
    2 3 3
    

    输出#1

    2
    
  • 输入#2

    3 4
    1 3 2 6
    2 1 5 6
    4 4 5 3
    

    输出#2

    4
    

说明/提示

The second sample.

首页