Brute forcing all the combinations. I'm sure there's probably a smarter algorithm for this, as usual.