Under construction

Still being built. Things may change

Torus Sea

Time limit2sMemory limit512 MB

Summary
On a torus of size N by M, a random walk moves diagonally each day; find the expected number of days to first reach (x, y), or -1 if unreachable.
Level

Medium6 of 10

Topics
Probability, Graph, Math
Solved
No attempts yet

Problem

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

(Image by YassineMrabet from Wikimedia Commons, licensed under CC BY-SA 3.0.)

Every cell has coordinates (n,m)(n, m) with 0≤n<N0 \le n < N and 0≤m<M0 \le m < M. Coordinates are always kept as remainders modulo NN and MM, so the sea wraps around in both directions. Increasing the first coordinate from N−1N-1 gives 00, and decreasing it from 00 gives N−1N-1. The second coordinate wraps the same way modulo MM.

Sanggeun starts at (0,0)(0, 0) and wants to reach (x,y)(x, y). He moves once per day. From (n,m)(n, m) he goes to ((n+1) mod N,(m+1) mod M)((n+1) \bmod N, (m+1) \bmod M) or to ((n−1) mod N,(m−1) mod M)((n-1) \bmod N, (m-1) \bmod M), each with probability 1/21/2. Here a mod ba \bmod b is the remainder in [0,b)[0, b).

Write a program that computes the expected number of days until Sanggeun first reaches (x,y)(x, y).

Input

The first line contains NN, MM, xx, yy, separated by spaces. (2≤N,M≤102 \le N, M \le 10, 0≤x≤N−10 \le x \le N-1, 0≤y≤M−10 \le y \le M-1)

(x,y)(x, y) is not (0,0)(0, 0).

Output

Print the expected number of days until Sanggeun first reaches (x,y)(x, y). This expected value is always an integer, so print a single integer with no decimal point and no trailing digits.

If (x,y)(x, y) cannot be reached, print -1.

Examples4

  1. Example 1

    Input
    2 2 1 1
    
    Expected output
    1
    
  2. Example 2

    Input
    2 2 0 1
    
    Expected output
    -1
    
  3. Example 3

    Input
    3 3 1 1
    
    Expected output
    2
    
  4. Example 4

    Input
    4 6 1 3
    
    Expected output
    27