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++14 (GCC 5.4.1) (judge verdict: Accepted)

#include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<set>
#include<map>

#define INF 1000000007
#define LINF 1000000000000000007

using namespace std;

typedef long long Int;
typedef pair<Int, Int> P;

Int gcd(Int x, Int y){
	if(y == 0)
		return x;
	return gcd(y, x % y);
}

Int lcm(Int x, Int y){
	return x * y / gcd(x, y);
}

Int h, w;

int main(){
	cin >> h >> w;
	//if(h < w) swap(h,w);
	
	if(h % 3 == 0 || w % 3 == 0){
		cout << 0 << endl;
	}
	else{
		Int a, b, c;
		a = (h / 3 + 1) * w;
		b = w / 2 * (h - (h / 3 + 1));
		c = h * w - a - b;
		int ans1 = max(a, max(b, c)) - min(a, min(b, c));
		
		Int d, e, f;
		d = (h / 3) * w;
		e = w / 2 * (h - (h / 3));
		f = h * w - d - e;
		
		/*Int aa, bb, cc;
		aa = h / 3 * w;
		bb = (h / 3 + 1) * w;
		cc = h * w - aa - bb;*/
		//w
		int ans3 = w;
		
		int ans2 = max(d, max(e, f)) - min(d, min(e, f));
		
		Int a1, b1, c1;
		a1 = (w / 3 + 1) * h;
		b1 = h / 2 * (w - (w / 3 + 1));
		c1 = h * w - a1 - b1;
		int ans4 = max(a1, max(b1, c1)) - min(a1, min(b1, c1));
		
		Int d1, e1, f1;
		d1 = (w / 3) * h;
		e1 = h / 2 * (w - (w / 3));
		f1 = h * w - d1 - e1;
		
		/*Int aa, bb, cc;
		aa = h / 3 * w;
		bb = (h / 3 + 1) * w;
		cc = h * w - aa - bb;*/
		//w
		int ans6 = h;
		
		int ans5 = max(d1, max(e1, f1)) - min(d1, min(e1, f1));
		
		cout << min(min(ans1, min(ans2, ans3)),min(ans4, min(ans5, ans6))) << endl;
	}
	
	return 0;
}