Solved p3 in Java