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 Rust (1.15.1) (judge verdict: Accepted)

#![allow(unused_imports)]
#![allow(non_snake_case)]
#![allow(dead_code)]
#![allow(deprecated)]

use std::cell::RefCell;
use std::cmp::{max, min, Ordering};
use std::collections::*;
use std::fmt::{Debug, Formatter, Write as FmtWrite};
use std::io::{stderr, stdin, BufRead, Write};
use std::mem::{replace, swap};
use std::ops::*;
use std::rc::Rc;
use std::usize;

const MOD_10_9_7: u64 = 1_000_000_007;
const INF: i64 = 1_000_000_000_000;
const MIN_INF: i64 = -1_000_000_000_000;

/// FYI: https://github.com/vain0x/scan-bench
#[allow(unused_macros)]
macro_rules! read {
    ([$t:ty] ; $n:expr) =>
        ((0..$n).map(|_| read!([$t])).collect::<Vec<_>>());
    ($($t:ty),+ ; $n:expr) =>
        ((0..$n).map(|_| read!($($t),+)).collect::<Vec<_>>());
    ([$t:ty]) =>
        (rl().split_whitespace().map(|w| w.parse().unwrap()).collect::<Vec<$t>>());
    ($t:ty) =>
        (rl().parse::<$t>().unwrap());
    ($($t:ty),*) => {{
        let buf = rl();
        let mut w = buf.split_whitespace();
        ($(w.next().unwrap().parse::<$t>().unwrap()),*)
    }};
}

fn rl() -> String {
    let mut buf = String::new();
    stdin().read_line(&mut buf).unwrap();
    buf.trim_right().to_owned()
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        eprintln!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

fn main() {
    let (h, w): (i64, i64) = read!(i64, i64);

    let mut ans = INF;
    for i in 1..(w + h - 1) {
        // i <= w - 1 のときは x = i のところで1つ目の区切りを入れる
        if i <= w - 1 {
            let first_s = i * h;

            // 2つ目の区切りは縦方向の中央に入れるか横方向の中央に入れるかの2通り
            // 横方向に区切りを入れる場合
            let second_s_h = (w - i) * (h / 2);
            let third_s_h = h * w - first_s - second_s_h;
            // 縦方向に区切りを入れる場合
            let second_s_v = h * ((w - i) / 2);
            let third_s_v = h * w - first_s - second_s_v;

            let s_diff_h =
                max(first_s, max(second_s_h, third_s_h)) - min(first_s, min(second_s_h, third_s_h));
            let s_diff_v =
                max(first_s, max(second_s_v, third_s_v)) - min(first_s, min(second_s_v, third_s_v));
            let s_diff = min(s_diff_h, s_diff_v);
            ans = if s_diff < ans { s_diff } else { ans };
        } else {
            // i >= w のときは y = i - w + 1 のところで1つ目の区切りを入れる
            let first_s = (i - w + 1) * w;

            // 2つ目の区切りは縦方向の中央に入れるか横方向の中央に入れるかの2通り
            // 横方向に区切りを入れる場合
            let second_s_h = w * ((h - i + w - 1) / 2);
            let third_s_h = h * w - first_s - second_s_h;
            // 縦方向に区切りを入れる場合
            let second_s_v = (h - i + w - 1) * (w / 2);
            let third_s_v = h * w - first_s - second_s_v;

            let s_diff_h =
                max(first_s, max(second_s_h, third_s_h)) - min(first_s, min(second_s_h, third_s_h));
            let s_diff_v =
                max(first_s, max(second_s_v, third_s_v)) - min(first_s, min(second_s_v, third_s_v));
            let s_diff = min(s_diff_h, s_diff_v);
            ans = if s_diff < ans { s_diff } else { ans };
        }
    }
    println!("{}", &ans);
}