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 <image>, 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 (i1, j1) and (i2, j2) lies together if |i1 - i2| + |j1 - j2| = 1.

Input

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 <image>. 
  * Each number from 1 to <image> 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. 

Output

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

Examples

Input

2 3
1 1 2
2 3 3


Output

2


Input

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


Output

4

Note

<image> The second sample. 
import java.io.*;
import java.util.*;
import static java.lang.Math.abs;
 
public class pairs_in_matrix_ar
{
        static int MAXN = 81;
        static int MAXT = MAXN * MAXN;
        static int MAXK = MAXT * 8;
        static int INF = (int)1e+9 + 1;
 
        static int a[][] = new int[MAXN][MAXN];
 
        static int next[] = new int[MAXK];
        static int dst[] = new int[MAXK];
        static int src[] = new int[MAXK];
        static int flow[] = new int[MAXK];
        static int dist[] = new int[MAXK];
        static int last[] = new int[MAXT];
        static int phi[] = new int[MAXT];
        static int d[] = new int[MAXT];
        static int p[] = new int[MAXT];
 
        static int frs[] = new int[MAXK];
        static int lst[] = new int[MAXK];
        static int nxt[] = new int[MAXK];
        static int val[] = new int[MAXK];
 
        static int n, m, k, t, c = 0;
        static int ans;
        static int cnt;
 
        public static void adde(int x, int y, int f, int c)
        {
                k += 1;
                src[k] = x;
                dst[k] = y;
                flow[k] = f;
                dist[k] = c;
                next[k] = last[x];
                last[x] = k;
        }
 
        public static void adde(int x, int y, int c)
        {
                adde(x, y, 1, c);
                adde(y, x, 0, -c);
        }
       
        public static void eadd(int i, int v)
        {
                c += 1;
                if (frs[i] == 0) frs[i] = c;
                if (lst[i] != 0) nxt[lst[i]] = c;
                val[c] = v;
                lst[i] = c;
        }
 
        public static int dijkstra(int st)
        {
                for (int i = 1; i <= t; i++)
                {
                        d[i] = INF;
                        p[i] = -1;
                }
                for (int i = 0; i <= c; i++)
                {
                        frs[i] = 0;
                        lst[i] = 0;
                        nxt[i] = 0;
                }
                c = 0;
 
                p[st] = d[st] = 0;
                eadd(0, st);
                for (int l = 0; l < t; l++)
                        for (int j = frs[l]; j > 0; j = nxt[j])
                        {
                                int x = val[j];
                                if (d[x] != l)
                                        continue;
                                for (int i = last[x]; i > 0; i = next[i])
                                {
                                        cnt += 1;
                                        if (flow[i] == 0)
                                                continue;
                                        int y = dst[i];
                                        int v = l + dist[i] + phi[x] - phi[y];
                                        if (v < d[y])
                                        {
                                                d[y] = v;
                                                p[y] = i;
                                                eadd(v, y);
                                        }
                                }
                        }
                int res = d[t] + phi[t] - phi[st];
                for(int i = 1; i <= t; ++i)
                        phi[i] += p[i] != -1? d[i] : d[t];
                return res;
        }
 
        public static void main(String[] args) throws FileNotFoundException
        {
                InputStream inputStream = System.in;
                OutputStream outputStream = System.out;
                InputReader in = new InputReader(inputStream);
                OutputWriter out = new OutputWriter(outputStream);
                int n = in.readInt();
                int m = in.readInt();
                for (int i = 1; i <= n; i++)
                        for (int j = 1; j <= m; j++)
                                a[i][j] = in.readInt();
                k = 1;
                for (int i = 1; i <= n; i++)
                        for (int j = 1; j <= m; j++)
                                if (((i + j) & 1) != 0)
                                        for (int dx = -1; dx <= 1; dx += 1)
                                        {
                                                for (int dy = dx == 0? -1 : 0; dy <= 1; dy += 2)
                                                {
                                                        int x = (i - 1) * m + j;
                                                        int y = (i + dx - 1) * m + j + dy;
                                                        i += dx;
                                                        j += dy;
                                                        if (1 <= i && i <= n && 1 <= j && j <= m)
                                                        {
                                                                int c = a[i - dx][j - dy] != a[i][j]? 1 : 0;
                                                                adde(x, y, c);
                                                        }
                                                        i -= dx;
                                                        j -= dy;
                                                }
                                        }
                t = n * m + 2;
                for (int i = 1; i <= n; i++)
                        for (int j = 1; j <= m; j++)
                                if (((i + j) & 1) != 0)
                                        adde(t - 1, (i - 1) * m + j, 0);
                                else
                                        adde((i - 1) * m + j, t, 0);
 
 
                for (int i = 1; i <= n * m / 2; i++)
                {
                        int v = dijkstra(t - 1);
        //              cout << v << endl;
                        ans += v;
                        int x = t;
 
                        while (x != t - 1)
                        {
                                flow[p[x] ^ 0] -= 1;
                                flow[p[x] ^ 1] += 1;
                                x = src[p[x]];
                        }
                }
                out.printLine(ans);
                out.close();
        }
}
 
class InputReader {
 
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private SpaceCharFilter filter;
 
        public InputReader(InputStream stream) {
                this.stream = stream;
        }
 
        public int read() {
                if (numChars == -1)
                        throw new InputMismatchException();
                if (curChar >= numChars) {
                        curChar = 0;
                        try {
                                numChars = stream.read(buf);
                        } catch (IOException e) {
                                throw new InputMismatchException();
                        }
                        if (numChars <= 0)
                                return -1;
                }
                return buf[curChar++];
        }
 
        public int readInt() {
                int c = read();
                while (isSpaceChar(c))
                        c = read();
                int sgn = 1;
                if (c == '-') {
                        sgn = -1;
                        c = read();
                }
                int res = 0;
                do {
                        if (c < '0' || c > '9')
                                throw new InputMismatchException();
                        res *= 10;
                        res += c - '0';
                        c = read();
                } while (!isSpaceChar(c));
                return res * sgn;
        }
 
        public long readLong() {
                int c = read();
                while (isSpaceChar(c))
                        c = read();
                int sgn = 1;
                if (c == '-') {
                        sgn = -1;
                        c = read();
                }
                long res = 0;
                do {
                        if (c < '0' || c > '9')
                                throw new InputMismatchException();
                        res *= 10;
                        res += c - '0';
                        c = read();
                } while (!isSpaceChar(c));
                return res * sgn;
        }
 
        public boolean isSpaceChar(int c) {
                if (filter != null)
                        return filter.isSpaceChar(c);
                return isWhitespace(c);
        }
 
        public static boolean isWhitespace(int c) {
                return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }
 
        public interface SpaceCharFilter {
                public boolean isSpaceChar(int ch);
        }
}
 
class OutputWriter {
        private final PrintWriter writer;
 
        public OutputWriter(OutputStream outputStream) {
                writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }
 
        public OutputWriter(Writer writer) {
                this.writer = new PrintWriter(writer);
        }
 
        public void print(Object...objects) {
                for (int i = 0; i < objects.length; i++) {
                        if (i != 0)
                                writer.print(' ');
                        writer.print(objects[i]);
                }
        }
 
    public void printLine(Object...objects) {
                print(objects);
                writer.println();
        }
 
        public void close() {
                writer.close();
        }
 
        }