Here's code to solve the problem from Uva, checkIO...etc.