A solution to POJ 1330.