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: Wrong Answer)

#include <iostream>
#include <cmath>
#include <vector>
#include <bitset>
#include <algorithm>
#include <stack>
#include <queue>
#include <map>
#include <climits>
#include <set>
#include <map>
#include <iomanip>
#include <cassert>
#include <functional>
#include <cstring>

using namespace std;

typedef pair<int, int> P;
#define mp make_pair
#define FOR(i, a, b) for(int (i)=a;(i)<(b);++(i))
#define rep(i, n)  FOR(i,0,n)
#define rrep(i,m,n) for(int i=(int)(m); i>=(int)(n); i--)
#define vsort(v) sort((v).begin(), (v).end());
#define rvsort(v) sort(v.begin(), v.end(),greater<int>());
#define YES cout<<"YES"<< endl
#define NO cout<<"NO"<<endl
#define Yes cout<<"Yes"<<endl
#define No cout<<"No"<<endl  
#define yes cout<<"yes"<<endl
#define no cout<<"no"<<endl
#define all(c) (c).begin(),(c).end()
#define ll long long
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vvb vector<vector<bool>>
#define vs vector<string>
#define vc vector<char>
#define vvc vector<vector<char>>
#define Print(p) cout<<(p)<<endl
#define F first
#define S second
#define pb push_back
#define mod 1000000007LL
#define INF 1050000000000

const int maxn = 1e5 + 10;


int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	ll h, w;
	cin >> h >> w;
	if (h < w) swap(h, w);

	if (h % 3 == 0 || w % 3 == 0) {
		Print(0); return 0;
	}

	ll sa = INF;
	rep(i,h)
	{
		if (i < 1) continue;

		ll nokori = h - i;
		ll ue = w * i;
		ll min, max;
		if (w % 2 == 1)
		{
			min = nokori * (w / 2);
			max = nokori * (w / 2 + 1);
			min = min < ue ? min : ue;
			max = max > ue ? max : ue;
		}
		else
		{
			int t = nokori * w / 2;
			min = t < ue ? t : ue;
			max = t > ue ? t : ue;

		}
		sa = sa < max - min ? sa : max - min;
	}
	
	rep(i, w)
	{
		if (i < 1) continue;

		ll nokori = w - i;
		ll yoko = h * i;
		ll min, max;
		if (h % 2 == 1)
		{
			min = nokori * (h / 2);
			max = nokori * (h / 2 + 1);
			min = min < yoko ? min : yoko;
			max = max > yoko ? max : yoko;
		}
		else
		{
			int t = nokori * w / 2;
			min = t < yoko ? t : yoko;
			max = t > yoko ? t : yoko;

		}
		sa = sa < max - min ? sa : max - min;
	}

	cout << sa << endl;
	return 0;
	
}