A solution to POJ 2140