Let $p$ be a prime. Prove that the equation $x^2-py^2 = -1$ has integral solution if and only if $p=2$ or $p\equiv 1\pmod{4}$.