Winning Tetris (tetris) 20/100

Buonasera, stavo cercando di risolvere il problema “tetris”, ma non riesco a superare 4 testcase (12, 13, 20, 24).
Questo è il codice:

#include <bits/stdc++.h>
using namespace std;

int main(){   
	int N, M;
		// H   W

	ifstream cin("input.txt");
	ofstream cout("output.txt");
	cin >> N >> M;  
	if(N == 2 && M == 2){
		cout << "1\n0 0\n0 0";
		return 0;
	}
	if(N % 4 != 0 && M % 4 != 0){
		cout << -1;
		return 0;
	}

	if(N % 4 == 0) 
	    cout << M * N / 4 << '\n';
	else 
	    cout << N * M / 4 << '\n';
	
	int b = 0;

	for(int i = 0; i < N; ++i){
		if(N % 4 == 0 && M % 4 != 0){
			if(i % 4 == 0) b += M;
		}
			
		for(int j = 0; j < M; ++j){
			if(M % 4 == 0){
				if(j % 4 == 0) ++b;
				cout << b - 1 << " ";
			}
			else if(N % 4 == 0) 
				cout << b + j - M << " ";
		}
		cout << '\n';
	}

	return 0;
}

Sicuro che se N e M non sono divisibili per 4 allora non c’è soluzione? Cosa succede per l’input 6 6?

Grazie mille, mi è bastato solo l’input per capire l’errore!