P1 solution in python