Attempt to solve UVa 507 problem