Xadiant, you are my hero! I've been trying to bruteforce this problem all day and I never thought to solve it with math. <3