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: Time Limit Exceeded)

/*******************************************************************************
 * Copyright (c) 2016-2017, xehoth
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name xehoth, nor the names of its contributors may be used
 *       to endorse or promote products derived from this software without
 *       specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY XEHOTH AND CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL XEHOTH AND CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ******************************************************************************/
#include <bits/stdc++.h>

namespace IO {

inline char read() {
    static const int IN_LEN = 1000000;
    static char buf[IN_LEN], *s, *t;
    s == t ? t = (s = buf) + fread(buf, 1, IN_LEN, stdin) : 0;
    return s == t ? -1: *s++;
}

template<typename T>
inline void read(T &x) {
    static char c;
    static bool iosig;
    for (c = read(), iosig = false; !isdigit(c); c = read()) {
        if (c == -1) return;
        c == '-' ? iosig = true : 0;
    }
    for (x = 0; isdigit(c); c = read())
        x = (x + (x << 2) << 1) + (c ^ '0');
    iosig ? x = -x : 0;
}

const int OUT_LEN = 1000000;

char obuf[OUT_LEN], *oh = obuf;

inline void print(char c) {
    oh == obuf + OUT_LEN ? (fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf) : 0;
    *oh++ = c;
}

template<typename T>
inline void print(T x) {
    static int buf[30], cnt;
    if (x == 0) {
        print('0');
    } else {
        x < 0 ? (print('-'), x = -x) : 0;
        for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 | 48;
        while (cnt) print((char)buf[cnt--]);
    }
}

inline void print(const char *s) {
    for (; *s; s++) print(*s);
}

inline void flush() {
    fwrite(obuf, 1, oh - obuf, stdout);
}
}

namespace Task {

#define long long long

long ans = LLONG_MAX;
long min = LLONG_MAX, max = LLONG_MIN;

inline void update(long p, long w, long h) {
    if (p == 0) return;
    min = p * h, max = p * h;
    register int l = (w - p) / 2;
    if (l > 0) 
        ans = std::min(ans, std::max(max, std::max(l * h, (w - p - l) * h)) - 
                        std::min(min, std::min(l * h, (w - p - l) * h)));
    l = (w - p) / 2 - 1;
    if (l > 0)
        ans = std::min(ans, std::max(max, std::max(l * h, (w - p - l) * h)) - 
                        std::min(min, std::min(l * h, (w - p - l) * h)));
    l = (w - p) / 2 + 1;
    if (l <= w)
        ans = std::min(ans, std::max(max, std::max(l * h, (w - p - l) * h)) - 
                        std::min(min, std::min(l * h, (w - p - l) * h)));
    w -= p, l = h / 2;
    if (l > 0) 
        ans = std::min(ans, std::max(max, std::max(l * w, (h - l) * w)) - 
                        std::min(min, std::min(l * w, (h - l) * w)));
    l = h / 2 - 1;
    if (l > 0)
        ans = std::min(ans, std::max(max, std::max(l * w, (h - l) * w)) - 
                        std::min(min, std::min(l * w, (h - l) * w)));
    l = h / 2 + 1;
    if (l <= w)
        ans = std::min(ans, std::max(max, std::max(l * w, (h - l) * w)) - 
                        std::min(min, std::min(l * w, (h - l) * w)));
}

inline void solve() {
    using namespace IO;
    register long h, w;
    read(h), read(w);
    if (h % 3 == 0 || w % 3 == 0) {
        print("0\n");
    } else if (w == 2 || h == 2) {
        print("1\n");
    } else {
        while (true) {
            h++;
        }
        std::cout << h << std::endl;
        register long area = h * w / 3;
        register long ph = area % h, pw = area % w;
        update(pw, w, h), update(pw - 1, w, h), update(pw + 1, w, h);
        update(ph, h, w), update(ph - 1, h, w), update(ph + 1, h, w);
        print(ans), print('\n');
    }
}
}
int main() {
#ifdef DBG
    freopen("in.in", "r", stdin);
#endif
    Task::solve();
    IO::flush();
    return 0;
}