S99 P18 Solution