Done. Not bad, takes like 15 seconds. I can't prove it's right, though.
it just happens to be accepted as an answer.