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)

#define ll long long
#define ffor(i,a,b) for (int i=(a);i<(b);i++)
#define rfor(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define rep(i,n) for (int i=0;i<(n);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<cmath>
#include<string>
#include<stack>
#include<queue>
#include<vector>
#include<map>
#define SIZE 100001
#define MOD 1000000007
#define INF 100000000
using namespace std;

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	ll h,w;
	cin >> h >> w;
	if(h%3 == 0 || w%3 == 0){
		cout << 0 << endl;
		return 0;
	}
	ll p1 = INF,p2=INF,p3=INF,p4=INF;

	//p1,p2
	rep(i,w){
		ll s_left = h*i;
		ll s_right = h*w - s_left;

		//p1
		ll h_div = h/2;
		ll s_up_1 = h_div*(w-i);
		ll s_down_1 = s_right - s_up_1;
		//p2
		ll w_div = (w-i)/2;
		ll s_right_l = w_div*h;
		ll s_right_r = s_right - s_right_l;

		ll p1_max = max(s_left,max(s_up_1,s_down_1));
		ll p1_min = min(s_left,min(s_up_1,s_down_1));
		if(p1 > p1_max - p1_min) p1 = p1_max - p1_min;

		ll p2_max = max(s_left,max(s_right_l,s_right_r));
		ll p2_min = min(s_left,min(s_right_l,s_right_r));
		if(p2 > p2_max - p2_min) p2 = p2_max - p2_min;
	}

	//p3,p4
	rep(i,h){
		ll up = w*i;
		ll down = h*w - up;
		//p3
		ll h_div = (h-i)/2;
		ll down_u = w*h_div;
		ll down_d = down - down_u;
		//p4
		ll w_div = w/2;
		ll down_l = w_div*(h-i);
		ll down_r = down - down_l;


		ll p3_max = max(up,max(down_u,down_d));
		ll p3_min = min(up,min(down_u,down_d));
		if(p3 > p3_max - p3_min) p3 = p3_max - p3_min;

		ll p4_max = max(up,max(down_l,down_r));
		ll p4_min = min(up,min(down_l,down_r));
		if(p4 > p4_max - p4_min) p4 = p4_max - p4_min;
	}

	cout << min(p1,min(p2,min(p3,p4))) << endl;

	return 0;
}