Sanggeun is sailing the Doughnut Sea. The sea has the shape of a torus and is divided into N×M cells.

(Image by YassineMrabet from Wikimedia Commons, licensed under CC BY-SA 3.0.)
Every cell has coordinates (n,m) with 0≤n<N and 0≤m<M. Coordinates are always kept as remainders modulo N and M, so the sea wraps around in both directions. Increasing the first coordinate from N−1 gives 0, and decreasing it from 0 gives N−1. The second coordinate wraps the same way modulo M.
Sanggeun starts at (0,0) and wants to reach (x,y). He moves once per day. From (n,m) he goes to ((n+1)modN,(m+1)modM) or to ((n−1)modN,(m−1)modM), each with probability 1/2. Here amodb is the remainder in [0,b).
Write a program that computes the expected number of days until Sanggeun first reaches (x,y).