Solved 31. Needs optimizations. Runs in 5 seconds or so.