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
{
    class Program
    {
        private const int MODULO = 1000000007;
        private static string Read() { return Console.ReadLine(); }
        private static int ReadInt() { return int.Parse(Read()); }
        private static long ReadLong() { return long.Parse(Read()); }
        private static double ReadDouble() { return double.Parse(Read()); }
        private static int[] ReadInts() { return Array.ConvertAll(Read().Split(), int.Parse); }
        private static long[] ReadLongs() { return Array.ConvertAll(Read().Split(), long.Parse); }
        private static double[] ReadDoubles() { return Array.ConvertAll(Read().Split(), double.Parse); }

        static long Calculate(long a, long b, long c)
        {
            var s = new[] { a, b, c };
            long min = s.Min();
            long max = s.Max();
            return (min > 0) ? max - min : long.MaxValue;
        }
        static long Solve(long H, long W)
        {
            long min = long.MaxValue;
            long h1 = (H + 0) / 3;
            long h2 = (H + 1) / 3;
            long h3 = (H + 2) / 3;
            long ha = (H + 0) / 2;
            long hb = (H + 1) / 2;
            long w1 = (W + 0) / 3;
            long w2 = (W + 1) / 3;
            long w3 = (W + 2) / 3;
            long wa = (W + 0) / 2;
            long wb = (W + 1) / 2;
            min = Math.Min(Calculate(h1 * W, h2 * W, h3 * W), min);
            min = Math.Min(Calculate(w1 * H, w2 * H, w3 * H), min);
            min = Math.Min(Calculate(h1 * W, (H - h1) * wa, (H - h1) * wb), min);
            min = Math.Min(Calculate(h3 * W, (H - h3) * wa, (H - h3) * wb), min);
            min = Math.Min(Calculate(w1 * H, (W - w1) * ha, (W - w1) * hb), min);
            min = Math.Min(Calculate(w3 * H, (W - w3) * ha, (W - w3) * hb), min);
            return min;
        }
        static void Main(string[] args)
        {
            long[] values = ReadLongs();
            Console.WriteLine(Solve(values[0], values[1]));
        }
    }
}