Vasya is reading a e-book. The file of the book consists of $n$ pages, numbered from $1$ to $n$. The screen is currently displaying the contents of page $x$, and Vasya wants to read the page $y$. There are two buttons on the book which allow Vasya to scroll $d$ pages forwards or backwards (but he cannot scroll outside the book). For example, if the book consists of $10$ pages, and $d = 3$, then from the first page Vasya can scroll to the first or to the fourth page by pressing one of the buttons; from the second page — to the first or to the fifth; from the sixth page — to the third or to the ninth; from the eighth — to the fifth or to the tenth.

Help Vasya to calculate the minimum number of times he needs to press a button to move to page $y$.


-----Input-----

The first line contains one integer $t$ ($1 \le t \le 10^3$) — the number of testcases.

Each testcase is denoted by a line containing four integers $n$, $x$, $y$, $d$ ($1\le n, d \le 10^9$, $1 \le x, y \le n$) — the number of pages, the starting page, the desired page, and the number of pages scrolled by pressing one button, respectively.


-----Output-----

Print one line for each test.

If Vasya can move from page $x$ to page $y$, print the minimum number of times he needs to press a button to do it. Otherwise print $-1$.


-----Example-----
Input
3
10 4 5 2
5 1 3 4
20 4 19 3

Output
4
-1
5



-----Note-----

In the first test case the optimal sequence is: $4 \rightarrow 2 \rightarrow 1 \rightarrow 3 \rightarrow 5$.

In the second test case it is possible to get to pages $1$ and $5$.

In the third test case the optimal sequence is: $4 \rightarrow 7 \rightarrow 10 \rightarrow 13 \rightarrow 16 \rightarrow 19$.

Difficulty: interview

Starter code:

Solutions:
for _ in range(int(input())):
	n, x, y, d = map(int, input().split())
	tt = abs(x - y)
	if tt % d != 0:
		ans = -1
		if (y - 1) % d == 0:
			ans = (x - 1 + d - 1) // d + (y - 1) // d
		if (n - y) % d == 0:
			cc = (n - x + d - 1) // d + (n - y) // d
			if ans == -1 or cc < ans:
				ans = cc
		print(ans)
	else:
		print(tt // d)

df = int(input())
for _ in range(df):
    n,x,y,d = list(map(int,input().split()))
    d2 = abs(x-y)
    ans = 3e9
    if d2 % d == 0:
        ans = d2 // d
    v = abs(x-1)
    v2  = abs(x-n)
    e = 0
    if v % d == 0:
        e = v // d
    else:
        e = v // d + 1
    to_b  = e
    e = 0
    if v2 % d == 0:
        e = v2 // d
    else:
        e = v2 // d + 1
    to_e = e
    v3 = abs(n-y)
    v4 = abs(1-y)
    if v3 % d == 0:
        ans = min(ans, v3 // d + to_e)
    if v4 % d == 0:
        ans = min(ans, v4 // d + to_b)
    if ans < 2e9:
        print(ans)
    else:
        print(-1)



from math import ceil
q = int(input())
for _ in range(q):
    n,x,y,d = list(map(int,input().split()))
    if (y-x)%d==0:
        print(abs(y-x)//d)
    elif (y-1)%d==0 and (n-y)%d==0:
        a = ceil((x-1)/d) + (y-1)//d
        b = ceil((n-x)/d) + (n-y)//d
        print(min(a,b))
    elif (y-1)%d==0:
        print(ceil((x-1)/d) + (y-1)//d)
    elif (n-y)%d==0:
        print(ceil((n-x)/d) + (n-y)//d)
    else:
        print(-1)


t = int(input())
for i in range(t):
    n, x, y, d = list(map(int, input().split()))
    if abs(y - x) % d == 0 or y == 1 or y == n:
        print((abs(x - y) + d - 1) // d)
    else:
        if abs(y - 1) % d == 0 and abs(n - y) % d == 0:
            print(min((x - 1 + d - 1) // d + (y - 1) // d, (n - x + d - 1) // d + (n - y) // d))
        elif abs(y - 1) % d != 0 and abs(n - y) % d != 0:
            print(-1)
        elif abs(y - 1) % d == 0:
            print((x - 1 + d - 1) // d + (y - 1) // d)
        else:
            print((n - x + d - 1) // d + (n - y) // d)


t = int(input())
INF = 10000000000
for t_num in range(t):
    n, start, end, d = list(map(int, input().split()))
    ans = INF
    if abs(end - start) % d == 0:
        ans = min(ans, abs(end - start) // d)
    if (n - end) % d == 0:
        ans = min(ans, (n - end) // d + (n - start + d - 1) // d)
    if (end - 1) % d == 0:
        ans = min(ans, (end - 1) // d + (start - 1 + d - 1) // d)

    if (ans == INF):
        print(-1)
    else:
        print(ans)


t = int(input())
for _ in range(t):
  n, x, y, d = list(map(int, input().split()))
  ans = 1<<64
  if abs(x - y) % d == 0:
    ans = abs(x - y) // d
  if abs(1 - y) % d == 0:
    ans = min(ans, (abs(x - 1) + d - 1) // d + abs(1 - y) // d)
  if abs(n - y) % d == 0:
    ans = min(ans, (abs(x - n) + d - 1) // d + abs(n - y) // d)
  print(ans if ans != (1<<64) else -1)


t=int(input())
for i in range(t):
    n,x,y,d=list(map(int,input().split()))
    if abs(x-y)%d==0:
        print(abs(x-y)//d)
    else:
        res=10000000000
        if (y-1)%d==0:
            res=min(res,-(-(x-1)//d)+(y-1)//d)
        if (n-y)%d==0:
            res=min(res,-(-(n-x)//d)+(n-y)//d)
        if res==10000000000:
            res=-1
        print(res)


def mi():
	return list(map(int, input().split()))
'''
3
10 4 5 2
5 1 3 4
20 4 19 3
'''
import math
for _ in range(int(input())):
	n,x,y,d = mi()
	if abs(x-y)%d==0:
		print(abs(x-y)//d)
	else:
		ans1 = math.ceil((x-1)/d)
		ans2 = math.ceil((n-x)/d)
		if abs(y-1)%d==0:
			ans1 += (y-1)//d
		else:
			ans1 = 1e19
		if abs(n-y)%d==0:
			ans2 += (n-y)//d
		else:
			ans2 = 1e19
		if min(ans1, ans2)==1e19:
			print(-1)
		else:
			print(min(ans1, ans2))


from math import ceil

def solve():
    n, x, y, d = [int(x) for x in input().split()]

    INF = 1e9
    sol1 = abs(x - y) // d if (x - y) % d == 0 else INF
    sol2 = ceil((x-1) / d) + ceil((y-1) / d) if (y-1) % d == 0 else INF
    sol3 = ceil((n - x) / d) + ceil((n - y) / d) if ((n - y) % d == 0) else INF

    ans = min(sol1, sol2, sol3)
    ans = ans if ans != INF else -1
    print(ans)

nt = int(input())
for _ in range(nt):
    solve()


t = int(input())
for _ in range(t):
    n, x, y, d = list(map(int, input().split()))
    x, y = x - 1, y - 1
    if x <= y and (y - x) % d == 0:
        print((y - x) // d)
        continue
    if y < x and (x - y) % d == 0:
        print((x - y) // d)
        continue
    if y % d == 0 and (n - 1 - y) % d == 0:
        print(min((x + y + d - 1) // d, (n - 1 - y + d - 1) // d + (n - 1 - x + d - 1) // d))
        continue
    if y % d == 0:
        print((x + y + d - 1) // d)
        continue
    if (n - 1 - y) % d == 0:
        print((n - 1 - y + d - 1) // d + (n - 1 - x + d - 1) // d)
        continue
    print(-1)


import math

for i in range(int(input())):
    n,x,y,d=list(map(int,input().split()))
    if y!=n and y!=1:
        if (abs(y-x))%d==0:
            print((abs(y-x))//d)
        else:
            if (y-1)%d==0:
                if (n-y)%d!=0:
                    print(math.ceil((x-1)/d)+(y-1)//d)
                else:
                    print(min(math.ceil((x-1)/d)+(y-1)//d, math.ceil((n-x)/d)+(n-y)//d))
            else:
                if (n-y)%d!=0:
                    print(-1)
                else:
                    print(math.ceil((n-x)/d)+(n-y)//d)
    else:
        if y==1:
            print(math.ceil((x-1)/d))
        else:
            print(math.ceil((n-x)/d))


t=int(input())
for i in range(t):
    n,x,y,d=list(map(int,input().split()))

    if abs(x-y)%d==0:
        print(abs(x-y)//d)

    else:
        ANS=10**10
        if abs(y-1)%d==0:
            ANS=min(ANS,abs(y-1)//d+(-(-abs(x-1)//d)))

        if abs(n-y)%d==0:
            ANS=min(ANS,abs(y-n)//d+(-(-abs(x-n)//d)))

        if ANS==10**10:
            print(-1)
        else:
            print(ANS)


#      
import collections, atexit, math, sys, bisect 

sys.setrecursionlimit(1000000)
def getIntList():
    return list(map(int, input().split()))    

try :
    #raise ModuleNotFoundError
    import numpy
    def dprint(*args, **kwargs):
        #print(*args, **kwargs, file=sys.stderr)
        # in python 3.4 **kwargs is invalid???
        print(*args,  file=sys.stderr)
    dprint('debug mode')
except Exception:
    def dprint(*args, **kwargs):
        pass



inId = 0
outId = 0
if inId>0:
    dprint('use input', inId)
    sys.stdin = open('input'+ str(inId) + '.txt', 'r') #标准输出重定向至文件
if outId>0:
    dprint('use output', outId)
    sys.stdout = open('stdout'+ str(outId) + '.txt', 'w') #标准输出重定向至文件
    atexit.register(lambda :sys.stdout.close())     #idle 中不会执行 atexit
    
T, = getIntList()
#print(N)

for _ in range(T):
    n,x,y,d = getIntList()
    y-=1
    x-=1
    f = abs(y-x)
    if f%d ==0:
        print(f//d)
        continue
    g1 = (x-1)//d + 1
    r1 = -1    
    if y%d==0:
        r1 = y//d + g1
    g2 = (n-1-x -1)//d + 1
    r2 = -1
    if (n-1 -y)%d==0:
        r2 = (n-1-y)//d + g2
    if r1>0 and r2 >0:
        print(min(r1,r2))
        continue
    print(max(r1,r2))


    




t = int(input())

for i in range(t):
    n, x, y, d = list(map(int, input().split()))
    w = []
    if x % d == y % d:
        w.append(abs(y - x) // d)
    if y % d == 1 % d:
        w.append(((x + d - 1) // d + y // d))
    if y % d == n % d:
        w.append((n - x + d - 1) // d + (n - y) // d)
    if w:
        print(min(w))
    else:
        print(-1)


def get(x, y, d):
	if abs(x - y) % d == 0:
		return abs(x - y) // d
	return -1

def moves(x, y, d):
	return (abs(x - y) + d - 1) // d

T = int(input())
for _ in range(T):
	(n, x, y, d) = list(map(int, input().split(' ')))
	ans = int(2e9)
	if abs(x - y) % d == 0:
		ans = abs(x - y) // d
	else:
		if get(1, y, d) != -1:
			ans = min(ans, moves(x, 1, d) + get(1, y, d))
		if get(n, y, d) != -1:
			ans = min(ans, moves(x, n, d) + get(n, y, d))
	print(ans if ans != int(2e9) else -1)

t = int(input())
for i in range(t):
	n, x, y, d = list(map(int, input().split()))
	x -= 1
	y -= 1
	n -= 1
	a = abs(x - y)
	#print('a', a)
	if a % d == 0:
		print(a // d)
		continue
	if (y % d != 0 and (n - y) % d != 0):
		print(-1)
		continue
	i1 = max(0, (n - x + d - 1) // d) + (n - y) // d
	if (n - y) % d != 0:
		i1 = 1e10
	i2 = max(0, ((x + d - 1) // d)) + y // d
	if y % d != 0:
		i2 = 1e10
	print(min(i1, i2))


t = int(input())
for i in range(t):
    n, x, y, d = list(map(int, input().split()))
    x -= 1
    y -= 1
    if x % d == y % d:
        print(abs(x - y) // d)
    elif y % d == 0:
        ans = x // d + 1 + y // d
        if (n - 1) % d == 0:
            ans = min(ans, (n - 1 - x) // d + 1 + (n - 1 - y) // d)
        print(ans)
    elif y % d == (n - 1) % d:
        print((n - 1 - x) // d + 1 + (n - 1 - y) // d)
    else:
        print(-1)


import math
n = int(input())

def f(x, y, d):
	t = abs(x - y)
	if y == 1:
		return math.ceil(t/d)
	if y == n:
		return math.ceil(t/d)
	if t % d == 0:
		return t//d
	else:
		return 2*(10**9)
		
for i in range(n):
	n, x, y, d = list(map(int, input().split()))
	ans = min([f(x, y, d), f(x, 1, d) + f(1, y, d), f(x, n, d) + f(n, y, d)])
	if ans == 2*(10**9):
		print("-1")
	else:
		print(ans)


t = int(input())
for i in range(t):
    n, x, y, d = map(int, input().split())
    if abs(y - x) % d == 0:
        print(abs(y - x) // d)
    else:
        flag = False
        num1, num2 = float("inf"), float("inf")
        if (n - y) % d == 0:
            flag = True
            num1 = (n - y) // d + (n - x) // d + int((n - x) % d != 0)
        if (y - 1) % d == 0:
            flag = True
            num2 = (y - 1) // d + (x - 1) // d + int((x - 1) % d != 0)

        if not flag:
            print(-1)
        else:
            print(min(num1, num2))

T = int(input())
for i in range(T):
    t = input().split(' ')
    n,x,y,d = int(t[0]), int(t[1]), int(t[2]), int(t[3])
    if (y-x)%d==0:
        print(abs(y-x)//d)
    else:
        a,b=-1,-1
        if (y-1)%d==0:
            a = (x-1)//d+1+(y-1)//d
        if (n-y)%d==0:
            b = (n-x)//d+1+(n-y)//d
        if a==-1 and b==-1:
            print(-1)
        elif a==-1:
            print(b)
        elif b==-1:
            print(a)
        else:
            print(min(a,b))


import math
n = int(input())
for i in range(n):
    c1, c2, c3 = 1e11, 1e11, 1e11
    n, x, y, d = list(map(int, input().split()))
    if (abs(y - x) % d == 0):
        c3 = abs((y - x))//d
    else:
        if (abs(y - 1) % d == 0):
            c1 = math.ceil((x - 1) / d) + ((y - 1) // d)
        if (abs(n - y) % d == 0):
            c2 = math.ceil((n - x) / d) + (abs((y - n)) // d)
    m = min(c1, c2, c3)
    if (m == 1e11):
        print(-1)
    else:
        print(m)
        


m = int(input())
for q in range(m):
    n, x, y, d = list(map(int, input().split()))
    answer = float('inf')
    if y % d == 1:
        answer = (x-2)//d+1+(y-1)//d
    if y % d == n % d:
        answer = min(answer, (n-x-1)//d+1+(n-y)//d)
    if y % d == x % d:
        answer = min(answer, abs(x-y)//d)
    if answer == float('inf'):
        print(-1)
    else:
        print(answer)


3.5
from math import ceil

T = int(input())
for i in range(0, T):
    n, x, y, d = [*list(map(int, input().split()))]
    if y == 1 or y == n:
        print(int(ceil(abs(x-y)/d)))
    else:
        if abs(x-y)%d == 0:
            print(abs(x-y)//d)
        else:
            if (y-1)%d == 0 and (n-y)%d == 0:
                ret1 = (y-1)//d + int(ceil(abs(x-1)/d))
                ret2 = (n-y)//d + int(ceil(abs(n-x)/d))

                print(min(ret1, ret2))

            elif (y-1)%d == 0:
                print((y-1)//d + int(ceil(abs(x-1)/d)))
            elif (n-y)%d == 0:
                print((n-y)//d + int(ceil(abs(n-x)/d)))
            else:
                print(-1)


for i in range(int(input())):
    n,x,y,d=map(int,input().split())
    if y==1 or y==n:
        print(abs(-abs(x-y)//d))
    elif abs(x-y)%d==0:
        print(abs(x-y)//d)
    elif abs(1-y)%d==0 and abs(n-y)%d==0:
        print(min(abs(-abs(x-1)//d)+(y-1)//d,abs(-abs(x-n)//d)+(n-y)//d))
    elif abs(1-y)%d==0:
        print(abs(-abs(x-1)//d)+(y-1)//d)
    elif abs(n-y)%d==0:
        print(abs(-abs(x-n)//d)+(n-y)//d)
    else:
        print(-1)