Move the Bishop Given an n × m chessboard, a bishop and two numbers a and b.Initially bishop is placed on the position (i, j) on the board.There can only be actions of the following types:
move the Bishop from position (x, y) on the board to position (x - a, y - b)
move the Bishop from position (x, y) on the board to position (x + a, y - b)
move the Bishop from position (x, y) on the board to position (x - a, y + b)
move the Bishop from position (x, y) on the board to position (x + a, y + b). 

I want to find the minimum number of allowed actions that are needed to be performed to move the bishop from the initial position (i, j) to one of the chessboard corners and when it is not possible?
 A: Consider the possible corners that a bishop can travel to, depending on the color pattern on the chessboard.  Then, if one can get to a corner $(x',y')$, then
$x\equiv x'\pmod{a}$,  and $y\equiv y'\pmod{b}$ $ ~~~~$(*)
The reason is that all four possible moves preserve the $x$ coordinate $\pmod{a}$, and the $y$ coordinate $\pmod{b}$.  So in order to get to $(x',y')$, the conditions of (*) must both hold.  (similarly for the other corner).
Now suppose that the conditions hold.  Then $x-x'=sa$ and $y-y'=tb$, for some integers $s,t$.  Note that the four moves all preserve the parity of $m+n$.  The final position has $s+t=0$, so if the initial position has $s+t$ odd, the position can't be solved.  If instead the initial position has $s+t$ even, then the position can be solved, using $\max(|s|,|t|)$ moves, as follows.  Without loss suppose that $|t|\ge |s|$, and that $t>0$.  Each move will decrease $t$ by one.  Initially, we move $s$ to approach $0$; once it gets there then we alternate moving $s$ back one step, then again to $0$, until $t$ finally arrives at $0$.

Worked example, as requested, for $n=5, m=7, i=1, j=3, a=2, b=2$

Suppose we color the board so that the bishop begins on a black square.  As it happens, all four corners are black.  Considering the $(1,1)$ corner, we have $i-1=0a$ and $j-1=1b$.  Since $0+1$ is odd, we cannot reach that corner.  Considering the $(7,1)$ corner, we have $i-7=-3a$ and $j-1=1b$.  Since $-3+1$ is even, we can reach the corner.  It takes 3 steps, where we increase the x-coordinate at each step.  The y-coordinate first goes down (to its minimum), then back up, then down again.  Considering the $(1,5)$ corner, we have $i-1=0a, j-5=-1b$.  Since $0+(-1)$ is odd, we cannot reach that corner.  Lastly, considering the $(7,5)$ corner, we have $i-7=-3a, j-5=-1b$.  Since $-3+(-1)$ is even, we can reach the corner, again in 3 steps.
