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;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Globalization;
using System.Diagnostics;



class Myon
{
    public Myon() { }
    public static int Main()
    {
        new Myon().calc();
        return 0;
    }
    

    Scanner cin;
    

    void calc()
    {
        cin = new Scanner();
        int H = cin.nextInt();
        int W = cin.nextInt();
        Console.WriteLine(calc(H, W));
    }

    long calc(int H, int W)
    {
        long H3 = H / 3;
        long W3 = W / 3;
        long ans = long.MaxValue;
        for (int i = -10; i < 10; i++)
        {
            long H2 = H3 + i;
            if (H2 <= 0 || H2 >= H) continue;
            ans = Math.Min(ans, calc2(H - H2, W, H2 * W));
        }
        for (int i = -10; i < 10; i++)
        {
            long W2 = W3 + i;
            if (W2 <= 0 || W2 >= W) continue;
            ans = Math.Min(ans, calc2(H, W - W2, H * W2));
        }
        return ans;
    }

    long calc2(long H, long W, long pre)
    {
        long H2 = H / 2;
        long W2 = W / 2;
        long A1 = H2 * W;
        long A2 = H * W - A1;
        long ans1 = calcDiff(A1, A2, pre);
        long B1 = W2 * H;
        long B2 = H * W - B1;
        long ans2 = calcDiff(B1, B2, pre);
        return Math.Min(ans1, ans2);
    }

    long calcDiff(long a, long b, long c)
    {
        long max = Math.Max(a, b);
        max = Math.Max(max, c);
        long min = Math.Min(a, b);
        min = Math.Min(min, c);
        if (min == 0) return long.MaxValue / 2;
        return max - min;
    }
    
}



class Scanner
{
    string[] s;
    int i;

    char[] cs = new char[] { ' ' };

    public Scanner()
    {
        s = new string[0];
        i = 0;
    }

    public string next()
    {
        if (i < s.Length) return s[i++];
        string st = Console.ReadLine();
        while (st == "") st = Console.ReadLine();
        s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
        if (s.Length == 0) return next();
        i = 0;
        return s[i++];
    }

    public int nextInt()
    {
        return int.Parse(next());
    }
    public int[] ArrayInt(int N, int add = 0)
    {
        int[] Array = new int[N];
        for (int i = 0; i < N; i++)
        {
            Array[i] = nextInt() + add;
        }
        return Array;
    }

    public long nextLong()
    {
        return long.Parse(next());
    }

    public long[] ArrayLong(int N, long add = 0)
    {
        long[] Array = new long[N];
        for (int i = 0; i < N; i++)
        {
            Array[i] = nextLong() + add;
        }
        return Array;
    }

    public double nextDouble()
    {
        return double.Parse(next());
    }


    public double[] ArrayDouble(int N, double add = 0)
    {
        double[] Array = new double[N];
        for (int i = 0; i < N; i++)
        {
            Array[i] = nextDouble() + add;
        }
        return Array;
    }
}