p | 1 4 -2 1
1 p p2+4p | p3+4p2-2p
1 p+4 p2+4p-2 | p3+4p2-2p+1 is the remainder.
p | 1 3 -1 7
1 p p2+3p | p3+3p2-p
1 p+3 p2+3p-1 | p3+3p2-p+7 is the remainder.
The two remainders are the same so p3+4p2-2p+1=p3+3p2-p+7,
p2-p-6=0=(p-3)(p+2), so p=3 or -2.