Stone Bridge

Given two stick powers A and B, find the fewest moves from stone N to stone M using +-1, +-A, +-B, and multiply-by-A/B moves on a bridge of stones 0 to 100000.

Medium6BFSGraphImplementationNo attempts yetTime limit1sMemory limit128 MB

Problem

Donggyu and Jumi are on a stone bridge that runs in a straight line. The stones are numbered 0 through 100,000. Donggyu stands on stone NN and Jumi stands on stone MM.

To reach Jumi quickly, Donggyu brought two pogo sticks, one with power AA and one with power BB. Crossing the bridge is turn based. In one turn Donggyu picks one of the following eight moves from his current stone xx.

  • Walk to x1x - 1 or x+1x + 1.
  • Hop with a pogo stick to xAx - A, x+Ax + A, xBx - B, or x+Bx + B.
  • Gather his strength for an instant and move to stone x×Ax \times A or stone x×Bx \times B.

For example, if Donggyu is on stone 7 and a pogo stick has power 8, he can hop to stone 15, or gather his strength and land on stone 56.

No stone is numbered below 0 or above 100,000, so he cannot move to such a position. He may use the same move as many times as he wants, and every input is a case where Jumi can be reached.

Input

The first line contains the pogo stick powers AA and BB, Donggyu's position NN, and Jumi's position MM, separated by spaces. (2A,B302 \le A, B \le 30, 0N,M100,0000 \le N, M \le 100{,}000)

Output

Print the minimum number of moves Donggyu needs to reach Jumi.