Language: C++

#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <stdio.h>
#include <cmath>
#include <sstream>
#include <algorithm>
#include <iomanip>

#define ll long long
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define vi vector<int>
#define vl vector<long long>
#define sz size()
#define x first
#define y second
#define pii pair<int, int>
#define pll pair<ll, ll>

using namespace std;

ll n, m;
ll ans = (int)1e6;

int main() {
                        
	cin >> n >> m;	
	
	for(int i = 1; i <= m; i++) {
		{
			ll part1 = n * i;
			ll left = n * (m - i);		
		     ll part2, part3;			
			part2 = (m - i) / 2  * n;
			part3 = n * m - part1 - part2;						
		     if(part1 == 0 || part2 == 0 || part3 == 0) continue;
			assert(part1 + part2 + part3 == n * m);
			ans = min(ans, max(abs(part1 - part2), max(abs(part1 - part3), abs(part3 - part2))));
		}
		
		{
			ll part1 = n * i;
			ll left = n * (m - i);		
		     ll part2, part3;
			part2 = n/2 * (m - i);
			part3 = n * m - part1 - part2;
			if(part1 == 0 || part2 == 0 || part3 == 0) continue;
			assert(part1 + part2 + part3 == n * m);			
			ans = min(ans, max(abs(part1 - part2), max(abs(part1 - part3), abs(part3 - part2))));
		}
	}


	for(int i = 1; i <= n; i++) {
		{
			ll part1 = m * i;
			ll left = m * (n - i);		
		     ll part2, part3;			
			part2 = (n - i) / 2  * m;
			part3 = n * m - part1 - part2;						
		     if(part1 == 0 || part2 == 0 || part3 == 0) continue;
			assert(part1 + part2 + part3 == n * m);
			ans = min(ans, max(abs(part1 - part2), max(abs(part1 - part3), abs(part3 - part2))));
		}
		
		{
			ll part1 = m * i;
			ll left = m * (n - i);		
		     ll part2, part3;
			part2 = m/2 * (n - i);
			part3 = n * m - part1 - part2;
			if(part1 == 0 || part2 == 0 || part3 == 0) continue;
			assert(part1 + part2 + part3 == n * m);			
			ans = min(ans, max(abs(part1 - part2), max(abs(part1 - part3), abs(part3 - part2))));
		}
	}
	//cout << ans << endl;
	

	cout << ans;
	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:44:25: error: 'assert' was not declared in this scope
   44 |                         assert(part1 + part2 + part3 == n * m);
      |                         ^~~~~~
a.cc:13:1: note: 'assert' is defined in header '<cassert>'; this is probably fixable by adding '#include <cassert>'
   12 | #include <iomanip>
  +++ |+#include <cassert>
   13 | 
a.cc:55:25: error: 'assert' was not declared in this scope
   55 |                         assert(part1 + part2 + part3 == n * m);
      |                         ^~~~~~
a.cc:55:25: note: 'assert' is defined in header '<cassert>'; this is probably fixable by adding '#include <cassert>'
a.cc:69:25: error: 'assert' was not declared in this scope
   69 |                         assert(part1 + part2 + part3 == n * m);
      |                         ^~~~~~
a.cc:69:25: note: 'assert' is defined in header '<cassert>'; this is probably fixable by adding '#include <cassert>'
a.cc:80:25: error: 'assert' was not declared in this scope
   80 |                         assert(part1 + part2 + part3 == n * m);
      |                         ^~~~~~
a.cc:80:25: note: 'assert' is defined in header '<cassert>'; this is probably fixable by adding '#include <cassert>'