my first, exhaustive solution

didn't pass test case 897654321