Java p1 solution