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 Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		new Main().solve();
	}
	int H,W;
	void solve(){
		Scanner sc=new Scanner(System.in);
		H=sc.nextInt();
		W=sc.nextInt();
		
		if(H%3==0 ||  W%3==0)System.out.println(0);
		else{
			int h1,h2,h3;
			long min;
			//1通りめ
			if(H%3==1){
			h1=H/3;
			h2=H/3;
			h3=H-h1-h2;
			}else{
				h1=H/3+1;
				h2=H/3+1;
				h3=H-h1-h2;
			}
			min=Math.abs(h3*W-h1*W);
			//2通り目
			int w1,w2,w3;
			if(W%3==1){
			w1=W/3;
			w2=W/3;
			w3=W-w1-w2;
			}else{
				w1=W/3+1;
				w2=W/3+1;
				w3=W-w1-w2;
			}
			min=Math.min(min, Math.abs(w3*H-w1*H));
			
			//3通り目:横を分割
			int a1,b1,b2,a2,a3;
			long S1,S2,S3;
			a1=W;
			b1=H/2;
			b2=H-b1;
			a2=W/2;
			a3=W-a2;
			S1=a1*b1;
			S2=a2*b2;
			S3=a3*b2;
			min=Math.min(min, calc(S1,S2,S3));
			
			a1=W;
			b1=H/3;
			b2=H-b1;
			a2=W/2;
			a3=W-a2;
			S1=a1*b1;
			S2=a2*b2;
			S3=a3*b2;
			min=Math.min(min, calc(S1,S2,S3));
			
			a1=W;
			b1=H/3+1;
			b2=H-b1;
			a2=W/2;
			a3=W-a2;
			S1=a1*b1;
			S2=a2*b2;
			S3=a3*b2;
			min=Math.min(min, calc(S1,S2,S3));
			
			//5通り目:たてを分割
			int c1,d1,d2,c2,c3;
			long T1,T2,T3;
			
			c1=H;
			d1=W/2;
			d2=W-d1;
			c2=H/2;
			c3=H-c2;
			T1=c1*d1;
			T2=c2*d2;
			T3=c3*d2;
			min=Math.min(min, calc(T1,T2,T3));
			
			c1=H;
			d1=W/3;
			d2=W-d1;
			c2=H/2;
			c3=H-c2;
			T1=c1*d1;
			T2=c2*d2;
			T3=c3*d2;
			min=Math.min(min, calc(T1,T2,T3));
			
			c1=H;
			d1=W/3+1;
			d2=W-d1;
			c2=H/2;
			c3=H-c2;
			T1=c1*d1;
			T2=c2*d2;
			T3=c3*d2;
			min=Math.min(min, calc(T1,T2,T3));
			
			System.out.println(min);
		}
		
	}
	long calc(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);
		
		return Max-Min;
	}
	
}