First attempt at problem 461. Works for example case but running very slowly for larger case. Need a more efficient algorithm.