Problem:
Score : 400 points

Problem Statement

There is a bar of chocolate with a height of H blocks and a width of W blocks.
Snuke is dividing this bar into exactly three pieces.
He can only cut the bar along borders of blocks, and the shape of each piece must be a rectangle.

Snuke is trying to divide the bar as evenly as possible.
More specifically, he is trying to minimize S_{max} - S_{min}, where S_{max} is the area (the number of blocks contained) of the largest piece, and S_{min} is the area of the smallest piece.
Find the minimum possible value of S_{max} - S_{min}.

Constraints

2 ≤ H, W ≤ 10^5

Input

Input is given from Standard Input in the following format:

H W

Output

Print the minimum possible value of S_{max} - S_{min}.

Sample Input 1

3 5

Sample Output 1

0

In the division below, S_{max} - S_{min} = 5 - 5 = 0.

Sample Input 2

4 5

Sample Output 2

2

In the division below, S_{max} - S_{min} = 8 - 6 = 2.

Sample Input 3

5 5

Sample Output 3

4

In the division below, S_{max} - S_{min} = 10 - 6 = 4.

Sample Input 4

100000 2

Sample Output 4

1

Sample Input 5

100000 100000

Sample Output 5

50000

Solution in C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;

namespace AtCoder
{
    static class Solver
    {
        static IO io = new IO();

        public static void Solve()
        {
            io.ReadSplit();
            int H = io.Int();
            int W = io.Int();

            io.WriteLine(Math.Min(Ans(H, W), Ans(W, H)).ToString());
        }
        static int Ans(int H, int W)
        {
            List<int> ans = new List<int>();
            if (W % 3 == 0)
            {
                ans.Add(0);
            }
            else if (W % 3 == 1)
            {
                if (H % 2 == 0)
                {
                    ans.Add(H / 2);
                }
                else
                {
                    int h = (H - 1) / 2;
                    int w = (W - 1) / 3;
                    ans.Add(H);
                    ans.Add(Math.Max(w + h + 1, 2 * w + 1));
                    ans.Add(Math.Max(w + 2 * h + 1, 2 * w));
                }
            }
            else
            {
                if (H % 2 == 0)
                {
                    ans.Add(H / 2);
                }
                else
                {
                    int h = (H - 1) / 2;
                    int w = (W - 2) / 3;
                    ans.Add(H);
                    ans.Add(Math.Max(2 * w + 2, w + 2 * h + 2));
                    ans.Add(Math.Max(2 * w + 1, w + h + 1));
                    ans.Add(Math.Max(Math.Max(2 * w, w - 4 * h - 2), 4 * h + w + 2));
                }
            }
            return ans.Min();
        }
    }
}
namespace AtCoder
{
    class Program
    {
        static void Main(string[] args)
        {
            Solver.Solve();
        }
    }
    class IO
    {
        private string[] _Inputs;
        private int _Index;
        public void Read()
        {
            _Inputs = new string[1];
            _Inputs[0] = Console.ReadLine();
            _Index = 0;
        }
        public void ReadSplit()
        {
            _Inputs = Console.ReadLine().Split(' ');
            _Index = 0;
        }
        public string String()
        {
            return _Inputs[_Index++];
        }
        public string[] String(int xCount)
        {
            string[] rv = new string[xCount];
            Array.Copy(_Inputs, _Index, rv, 0, xCount);
            _Index += xCount;
            return rv;
        }
        public int Int()
        {
            return int.Parse(_Inputs[_Index++]);
        }
        public int[] Int(int xCount)
        {
            int[] rv = new int[xCount];
            for (int i = 0; i < xCount; ++i)
            {
                rv[i] = int.Parse(_Inputs[_Index++]);
            }
            return rv;
        }
        public long Long()
        {
            return long.Parse(_Inputs[_Index++]);
        }
        public long[] Long(int xCount)
        {
            long[] rv = new long[xCount];
            for (int i = 0; i < xCount; ++i)
            {
                rv[i] = long.Parse(_Inputs[_Index++]);
            }
            return rv;
        }
        public void Write(string xStr)
        {
            Console.Write(xStr);
        }
        public void WriteLine(string xStr)
        {
            Console.WriteLine(xStr);
        }
    }
}