Go south, east and north, come to the same place
CJam, 62 chars

{4e4/:Y\9e-3*:X90-P180/:Q*mc/XY2$m](/mCQ/+_90&lt;\@XY@m[)/mCQ/-?}


This is a block (subroutine) which takes x y on the stack and leaves the solution on the stack.

Online demo

I'm interpreting the spec to allow returning 90 as an "impossible value" if there is no other solution. If that interpretation is disallowed, I think the best fix would be

{4e4/:Y\9e-3*:X90-P180/:Q*mc/[XY2$m](/mCQ/+\XY@m[)/mCQ/--91]{90&lt;}=}