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$.

Source: codeforces | Difficulty: EASY
Tags: Mathematics

Implementation


Solutions:
class Scrl:

	def __init__(self, s, v, e):
		self.sp = s
		self.vp = v
		self.ep = e

	def spm(self, move):
		d1 = abs(self.vp - self.sp)
		d2 = abs(self.ep - self.vp)
		if d2 % move == 0:
			if d1 % move == 0:
				return (d1 + d2) // move
			else:
				return (d1 + d2) // move + 1
		else:
			return None
for t in range(int(input())):
	(n, x, y, d) = map(int, input().split())
	m = list()
	for o in (Scrl(x, x, y), Scrl(x, 1, y), Scrl(x, n, y)):
		m.append(o.spm(d))
		if m[-1] == None:
			m.pop()
	if m:
		print(min(m))
	else:
		print(-1)


t = int(input())
for i in range(t):
	(n, x, y, d) = map(int, input().split())
	if abs(x - y) % d == 0:
		print(abs(x - y) // d)
	else:
		num = 10 ** 10
		num2 = 10 ** 10
		if (y - 1) % d == 0:
			num = (x - 1) // d + (y - 1) // d
			if (x - 1) % d != 0:
				num += 1
		if (n - y) % d == 0:
			num2 = (n - x) // d + (n - y) // d
			if (n - x) % d != 0:
				num2 += 1
		if min(num, num2) == 10 ** 10:
			print(-1)
		else:
			print(min(num, num2))


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


t = int(input())

def func(n, x, y, d):
	if abs(y - x) % d == 0:
		return abs(y - x) // d
	else:
		isfirst = False
		islast = False
		if abs(y - 1) % d == 0:
			isfirst = True
		if abs(n - y) % d == 0:
			islast = True
		if isfirst and (not islast):
			a = abs(x - 1) // d + 1
			b = abs(y - 1) // d
			return a + b
		elif not isfirst and islast:
			a = abs(n - x - 1) // d + 1
			b = abs(n - y) // d
			return a + b
		elif isfirst and islast:
			a1 = abs(x - 1) // d + 1
			b1 = abs(y - 1) // d
			a2 = abs(n - x - 1) // d + 1
			b2 = abs(n - y) // d
			return min(a1 + b1, a2 + b2)
		else:
			return -1
for i in range(t):
	(n, x, y, d) = list(map(int, input().split()))
	print(func(n, x, y, d))


df = int(input())
for _ in range(df):
	(n, x, y, d) = map(int, input().split())
	d2 = abs(x - y)
	ans = 3000000000.0
	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 < 2000000000.0:
		print(ans)
	else:
		print(-1)


from math import *
q = int(input())
for i in range(q):
	(w, e, r, t) = map(int, input().split())
	d = list()
	if e == r:
		d.append(0)
	if (e - r) % t == 0:
		d.append(abs((e - r) // t))
	if (r - e) % t == 0:
		d.append(abs((r - e) / t))
	if (r - 1) % t == 0:
		d.append(abs((r - 1) // t + ceil((e - 1) / t)))
	if (w - r) % t == 0:
		d.append(abs((w - r) // t + ceil((w - e) / t)))
	if len(d) == 0:
		print(-1)
	else:
		print(min(d))


t = int(input())
while t > 0:
	t = t - 1
	(n, x, y, d) = map(int, input().split())
	f = 0
	(a, b, c) = (10 ** 9 + 1, 10 ** 9 + 1, 10 ** 9 + 1)
	if x % d == y % d:
		f = 1
		a = abs(y // d - x // d)
	if (y - 1) % d == 0:
		f = 1
		b = (x - 1 + d - 1) // d + (y - 1) // d
	if (n - y) % d == 0:
		f = 1
		c = (n - x + d - 1) // d + (n - y) // d
	if f == 1:
		print(min(a, b, c))
	else:
		print(-1)


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


t = int(input())
for i in range(t):
	(n, x, y, d) = 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)


import sys
import math
import bisect

def solve(n, x, y, d):
	min_val = 10 ** 18
	if abs(x - y) % d == 0:
		val = abs(x - y) // d
		min_val = min(min_val, val)
	if (y - 1) % d == 0:
		val = (x - 1 + d - 1) // d + (y - 1) // d
		min_val = min(min_val, val)
	if (n - y) % d == 0:
		val = (n - x + d - 1) // d + (n - y) // d
		min_val = min(min_val, val)
	if min_val == 10 ** 18:
		return -1
	return min_val

def main():
	for _ in range(int(input())):
		(n, x, y, d) = map(int, input().split())
		ans = solve(n, x, y, d)
		print(ans)
main()


def do():
	import math
	(n, x, y, d) = map(int, input().split())
	x -= 1
	y -= 1
	n -= 1
	res = 10 ** 18
	if x == y:
		print(0)
		return
	if (y - x) % d == 0:
		print(abs(y - x) // d)
		return
	cnt = 0
	cnt = math.ceil(x / d)
	if y % d == 0:
		cnt += abs(y // d)
		res = min(res, cnt)
	cnt = 0
	cnt = math.ceil((n - x) / d)
	if (n - y) % d == 0:
		cnt += abs((n - y) // d)
		res = min(res, cnt)
	if res == 10 ** 18:
		print(-1)
		return
	else:
		print(res)
		return
qq = int(input())
for _ in range(qq):
	do()


t = int(input())
for i in range(t):
	(n, x, y, d) = 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)


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


import sys
tc = int(sys.stdin.readline())
for _ in range(tc):
	(n, x, y, d) = map(int, sys.stdin.readline().split())
	ans = []
	if (y - x) % d == 0 and (y - x) // d >= 0:
		ans.append((y - x) // d)
	if (x - y) % d == 0 and (x - y) // d >= 0:
		ans.append((x - y) // d)
	if (y - 1) % d == 0 and (y - 1) // d >= 0:
		ans.append(int((x - 1) / d + 1) + (y - 1) // d)
	if (n - y) % d == 0 and (n - y) // d >= 0:
		ans.append(int((n - x) / d + 1) + (n - y) // d)
	if not ans:
		print(-1)
	else:
		print(min(ans))


import math

def main():
	buf = input()
	t = int(buf)
	n = []
	x = []
	y = []
	d = []
	for i in range(t):
		buf = input()
		buflist = buf.split()
		n.append(int(buflist[0]))
		x.append(int(buflist[1]))
		y.append(int(buflist[2]))
		d.append(int(buflist[3]))
	for i in range(t):
		min = int(1e+18) + 1
		if abs(x[i] - y[i]) % d[i] == 0:
			if abs((x[i] - y[i]) // d[i]) < min:
				min = abs((x[i] - y[i]) // d[i])
		if abs(y[i] - 1) % d[i] == 0:
			if abs(math.ceil((x[i] - 1) / d[i])) + abs((y[i] - 1) // d[i]) < min:
				min = abs(math.ceil((x[i] - 1) / d[i])) + abs((y[i] - 1) // d[i])
		if abs(y[i] - n[i]) % d[i] == 0:
			if abs(math.ceil((x[i] - n[i]) / d[i])) + abs((y[i] - n[i]) // d[i]) + 1 < min:
				min = abs(math.ceil((x[i] - n[i]) / d[i])) + abs((y[i] - n[i]) // d[i]) + 1
		if min > int(1e+18):
			print(-1)
		else:
			print(min)
main()


k = int(input())
for i in range(k):
	(n, x, y, d) = map(int, input().split())
	ans = []
	if y % d == x % d:
		ans.append(abs(x - y) // d)
	if y % d == n % d:
		tmp = 0
		if abs(x - n) % d != 0:
			tmp += abs(x - n) // d + 1
		else:
			tmp += abs(x - n) // d
		tmp += abs(n - y) // d
		ans.append(tmp)
	if y % d == 1:
		tmp = 0
		if abs(x - 1) % d != 0:
			tmp += abs(x - 1) // d + 1
		else:
			tmp += abs(x - 1) // d
		tmp += abs(1 - y) // d
		ans.append(tmp)
	ans.sort()
	f = False
	for i in range(len(ans)):
		if ans[i] >= 0:
			print(ans[i])
			f = True
			break
	if f == False:
		print(-1)


def mi():
	return list(map(int, input().split()))
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 = 1e+19
		if abs(n - y) % d == 0:
			ans2 += (n - y) // d
		else:
			ans2 = 1e+19
		if min(ans1, ans2) == 1e+19:
			print(-1)
		else:
			print(min(ans1, ans2))


t = int(input())
for i in range(t):
	(n, x, y, d) = map(int, input().split())
	if abs(x - y) % d == 0:
		print(abs(x - y) // d)
		continue
	dist1 = y - 1
	dist2 = n - y
	ans1 = int(1e+18)
	ans2 = int(1e+18)
	if dist1 % d == 0:
		ans1 = dist1 // d + (x + d - 2) // d
	if dist2 % d == 0:
		ans2 = dist2 // d + (n - x + d - 1) // d
	p = min(ans1, ans2)
	if p == int(1e+18):
		print(-1)
	else:
		print(p)


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


import math
t = int(input())
for i in range(t):
	ans = []
	(n, x, y, d) = [int(x) for x in input().split()]
	if abs(x - y) % d == 0:
		ans.append(abs(x - y) // d)
	if (y - 1) % d == 0:
		ans.append(math.ceil((x - 1) / d) + (y - 1) // d)
	if (n - y) % d == 0:
		ans.append(math.ceil((n - x) / d) + (n - y) // d)
	if len(ans) != 0:
		ans.sort()
		print(ans[0])
	else:
		print(-1)


t = int(input())
inf = int(1e+100)
for i in range(t):
	lis = input().split()
	(n, x, y, d) = (int(lis[0]), int(lis[1]), int(lis[2]), int(lis[3]))
	ans = inf
	if abs(x - y) % d == 0:
		ans = min(ans, abs(x - y) // d)
	if (y - 1) % d == 0:
		ans = min(ans, (x - 1) // d + int((x - 1) % d != 0) + (y - 1) // d)
	if (n - y) % d == 0:
		ans = min(ans, (n - x) // d + int((n - x) % d != 0) + (n - y) // d)
	if ans == inf:
		print(-1)
	else:
		print(ans)


from math import ceil
for _ in range(int(input())):
	(n, x, y, d) = map(int, input().split())
	f = 0
	dr = ot1 = ot2 = 100000000000.0
	if (y - 1) % d == 0:
		f = 1
		ot1 = (y - 1) // d + ceil((x - 1) / d)
	if abs(y - x) % d == 0:
		f = 1
		dr = abs(y - x) // d
	if (n - y) % d == 0:
		f = 1
		ot2 = (n - y) // d + ceil((n - x) / d)
	if f:
		print(min(dr, ot1, ot2))
	else:
		print(-1)


from math import ceil
for _ in range(int(input())):
	(n, x, y, d) = map(int, input().split())
	if abs(x - y) % d == 0:
		print(abs(x - y) // d)
	else:
		ans = 10 ** 18
		if (y - 1) % d == 0:
			ans = min(ans, (y - 1) // d + (x - 1 + d - 1) // d)
		if (n - y) % d == 0:
			ans = min(ans, (n - y) // d + (n - x + d - 1) // d)
		if ans == 10 ** 18:
			print(-1)
		else:
			print(ans)


from math import ceil

def solve():
	(n, x, y, d) = [int(x) for x in input().split()]
	INF = 1000000000.0
	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()


import math, sys, bisect, heapq
from collections import defaultdict, Counter, deque
from itertools import groupby, accumulate
int1 = lambda x: int(x) - 1
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
ilele = lambda : map(int, input().split())
alele = lambda : list(map(int, input().split()))
ilelec = lambda : map(int1, input().split())
alelec = lambda : list(map(int1, input().split()))

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def Y(c):
	print(['NO', 'YES'][c])

def y(c):
	print(['no', 'yes'][c])

def Yy(c):
	print(['No', 'Yes'][c])
for _ in range(int(input())):
	(n, x, y, d) = ilele()
	if x == y:
		print(0)
		continue
	(a, b, c) = (float('inf'), float('inf'), float('inf'))
	if abs(x - y) % d == 0:
		a = abs(x - y) // d
	if abs(y - 1) % d == 0:
		b = math.ceil(abs(1 - x) / d) + abs(y - 1) // d
	if abs(n - y) % d == 0:
		c = math.ceil(abs(n - x) / d) + abs(y - n) // d
	z = min(a, b, c)
	if z == float('inf'):
		print(-1)
	else:
		print(z)


import math

def solve(n, x, y, d):
	vals = []
	if (y - x) % d == 0:
		vals.append(abs(y - x) // d)
	if y % d == 1:
		moves = math.ceil((x - 1) / d)
		moves += (y - 1) // d
		vals.append(moves)
	if (n - y) % d == 0:
		moves = math.ceil((n - x) / d)
		moves += (n - y) // d
		vals.append(moves)
	if vals:
		return min(vals)
	else:
		return -1
t = int(input())
for _ in range(t):
	(n, x, y, d) = map(int, input().split())
	print(solve(n, x, y, d))


from math import ceil
for _ in range(int(input())):
	(n, x, y, d) = [int(i) for i in input().split()]
	if abs(y - x) % d == 0:
		print(abs(y - x) // d)
	elif y == 1 or y == n:
		print(ceil(abs(y - x) / d))
	else:
		a = []
		if (n - y) % d == 0:
			a.append(ceil((n - x) / d) + (n - y) // d)
		if (y - 1) % d == 0:
			a.append(ceil((x - 1) / d) + (y - 1) // d)
		if a == []:
			print(-1)
		else:
			print(min(a))


t = int(input())
import math
for z in range(t):
	(n, x, y, d) = list(map(int, input().split()))
	temp = abs(y - x)
	if temp % d == 0:
		print(temp // d)
		continue
	else:
		left = math.ceil((x - 1) / d)
		right = math.ceil((n - x) / d)
		dl = -1
		dr = -1
		if (y - 1) % d == 0:
			dl = (y - 1) // d
		if (n - y) % d == 0:
			dr = (n - y) // d
		if dl == -1 and dr == -1:
			print(-1)
		elif dl == -1 and dr != -1:
			print(right + dr)
		elif dr == -1 and dl != -1:
			print(left + dl)
		else:
			print(min(left + dl, right + dr))


for s in [*open(0)][1:]:
	(n, x, y, d) = map(int, s.split())
	(a, b, c) = (abs(x - y) // d, (x + d - 1) // d + (y - 1) // d, -((x - n) // d) + (n - y) // d)
	r = a if x % d == y % d else -1
	if r < 0 and y % d == 1:
		r = b
	if n % d == y % d:
		r = [min(r, c), c][r < 0]
	print(r)


import sys, math, queue
MOD = 998244353
sys.setrecursionlimit(1000000)
for _ in range(int(input())):
	(n, x, y, d) = map(int, input().split())
	a = []
	if abs(x - y) % d == 0:
		a.append(abs(x - y) // d)
	if (y - 1) % d == 0:
		a.append((x - 1 + d - 1) // d + (y - 1) // d)
	if (n - y) % d == 0:
		a.append((n - x + d - 1) // d + (n - y) // d)
	if len(a) == 0:
		print(-1)
	else:
		print(min(a))


from math import *
t = int(input())
for _ in range(t):
	ans = float('Inf')
	(n, x, y, d) = map(int, input().split())
	if abs(x - y) % d == 0:
		ans = abs(y - x) // d
	if y % d == 1:
		ans = min(ans, ceil((x - 1) / d) + y // d)
	if (n - y) % d == 0:
		ans = min(ans, ceil((n - x) / d) + (n - y) // d)
	print(ans if ans != float('Inf') else -1)


t = int(input())
from math import fabs
for i in range(t):
	(n, x, y, d) = map(int, input().split())
	if fabs(x - y) % d == 0:
		print(int(fabs(x - y)) // d)
		continue
	(res, shet) = (1000000000000, 0)
	if (y - 1) % d == 0:
		(div, mod) = ((x - 1) // d, (x - 1) % d)
		if mod > 0:
			div += 1
		res = div + (y - 1) // d
		shet = 1
	if (n - y) % d == 0:
		(div, mod) = ((n - x) // d, (n - x) % d)
		if mod > 0:
			div += 1
		res = min(res, div + (n - y) // d)
		shet = 1
	if shet == 0:
		res = -1
	print(res)


import math
t = int(input())
for i in range(t):
	(n, x, y, d) = [int(z) for z in input().split(' ')]
	a = []
	m = False
	direct = abs(x - y)
	if direct % d == 0:
		print(direct // d)
	else:
		if y % d == 1:
			a.append(math.ceil((x - 1) / d) + y // d)
			m = True
		if (n - y) % d == 0:
			a.append(math.ceil((n - x) / d) + (n - y) // d)
			m = True
		if m:
			print(min(a))
		else:
			print(-1)


from math import *
t = int(input())
for i in range(t):
	p = -1
	q = -1
	(n, x, y, d) = [int(s) for s in input().split()]
	if abs(y - x) % d == 0:
		print(ceil(abs(y - x) / d))
	else:
		if abs(y - 1) % d == 0:
			p = ceil(abs(x - 1) / d) + ceil(abs(y - 1) / d)
		if abs(n - y) % d == 0:
			q = ceil(abs(x - n) / d) + ceil(abs(n - y) / d)
		if p != -1 and q != -1:
			print(min(p, q))
		elif p == -1:
			print(q)
		elif q == -1:
			print(p)
		else:
			print(-1)


from math import ceil

def func(n, x, y, d):
	arr = []
	if (x - y) % d == 0:
		arr.append(abs(x - y) // d)
	if y % d == 1:
		arr.append(ceil(abs(x - 1) / d) + (y - 1) // d)
	if y % d == n % d:
		arr.append(ceil(abs(n - x) / d) + (n - y) // d)
	if arr == []:
		print(-1)
	else:
		print(min(arr))

def main():
	count = int(input())
	for x in range(count):
		arr = input().split()
		func(int(arr[0]), int(arr[1]), int(arr[2]), int(arr[3]))
main()


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))


from math import ceil
t = int(input())
for _ in range(t):
	(n, x, y, d) = [int(p) for p in input().split()]
	f = 0
	b = 0
	direct = abs(y - x) / d
	if direct.is_integer():
		print(int(direct))
	else:
		front = (y - 1) / d
		fshift = ceil((x - 1) / d) + front
		if front.is_integer():
			f += 1
		back = (n - y) / d
		bshift = ceil((n - x) / d) + back
		if back.is_integer():
			b += 1
		if f == 1 and b == 1:
			if fshift < bshift:
				print(int(fshift))
			else:
				print(int(bshift))
		elif f == 1:
			print(int(fshift))
		elif b == 1:
			print(int(bshift))
		else:
			print(-1)


def parse_input():
	T = int(input())
	for t in range(T):
		yield (int(v) for v in input().split())
for (n, x, y, d) in parse_input():
	if (x - y) % d == 0:
		print(abs(x - y) // d)
		continue
	if (y - 1) % d != 0 and (n - y) % d != 0:
		print(-1)
		continue
	ans = 2 * n
	if (y - 1) % d == 0:
		(dx, mx) = divmod(x - 1, d)
		ans_1 = dx + (1 if mx else 0) + (y - 1) // d
		ans = min(ans, ans_1)
	if (n - y) % d == 0:
		(dx, mx) = divmod(n - x, d)
		ans_n = dx + (1 if mx else 0) + (n - y) // d
		ans = min(ans, ans_n)
	print(ans)


for _ in range(int(input())):
	(n, x, y, d) = tuple(map(int, input().split()))
	start = x % d
	end = y % d
	init = 1
	last = n % d
	if start == end:
		print(abs(y - x) // d)
	elif init != end and last != end:
		print(-1)
	elif init == last:
		print(min((x + d - 2) // d + (y + d - 2) // d, (n - x + d - 1) // d + (n - y + d - 1) // d))
	elif init == end:
		print((x + d - 2) // d + (y + d - 2) // d)
	else:
		print((n - x + d - 1) // d + (n - y + d - 1) // d)


from math import ceil
k = int(input())
for i in range(k):
	(n, x, y, d) = map(int, input().split())
	if x > y:
		l = x - y
	else:
		l = y - x
	if l % d == 0:
		k1 = l // d
	else:
		k1 = 10 ** 9 + 1
	if (y - 1) % d == 0:
		k2 = (y - 1) // d + ceil((x - 1) / d)
	else:
		k2 = 10 ** 9 + 1
	if (n - y) % d == 0:
		k3 = (n - y) // d + ceil((n - x) / d)
	else:
		k3 = 10 ** 9 + 1
	ans = min(k1, k2, k3)
	if ans == 10 ** 9 + 1:
		print(-1)
	else:
		print(ans)


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


n = int(input())
c = []
for i in range(n):
	a = list(map(int, input().split()))
	l = a[0]
	l1 = a[1]
	l2 = a[2]
	l3 = a[3]
	s = 0
	s1 = 0
	fl = False
	if (l2 - l1) % l3 == 0 or (l1 - l2) % l3 == 0:
		if l2 >= l1:
			s1 = (l2 - l1) // l3
		else:
			s1 = (l1 - l2) // l3
		if s1 < s or not fl:
			s = s1
			fl = True
	if (l2 - 1) % l3 == 0:
		if (l1 - 1) % l3 == 0:
			s1 = (l1 - 1) // l3 + (l2 - 1) // l3
		else:
			s1 = (l1 - 1) // l3 + (l2 - 1) // l3 + 1
		if s1 < s or not fl:
			s = s1
			fl = True
	if (l - l2) % l3 == 0:
		if (l - l1) % l3 == 0:
			s1 = (l - l1) // l3 + (l - l2) // l3
		else:
			s1 = (l - l1) // l3 + (l - l2) // l3 + 1
		if s1 < s or not fl:
			s = s1
			fl = True
	if s == 0 and (not fl):
		c.append(-1)
	elif s == 0 and fl:
		c.append(0)
	else:
		c.append(s)
for i in c:
	print(i)


from math import ceil
q = int(input())
for _ in range(q):
	(n, x, y, d) = 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)


import sys
from array import array
import typing as Tp

def input():
	return sys.stdin.buffer.readline().decode('utf-8')

def main():
	inf = 10 ** 18
	for _ in range(int(input())):
		(n, x, y, d) = map(int, input().split())
		ans = min(abs(x - y) // d if abs(x - y) % d == 0 else inf, (x + d - 2) // d + (y - 1) // d if (y - 1) % d == 0 else inf, (n - x + d - 1) // d + (n - y) // d if (n - y) % d == 0 else inf)
		print(ans if ans < inf else -1)
main()


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


import math
t = int(input())
ans = []
for l in range(t):
	(n, x, y, d) = map(int, input().split())
	if abs(y - x) % d == 0:
		ans.append(abs(y - x) / d)
	elif abs(y - 1) % d != 0 and abs(n - y) % d != 0:
		ans.append(-1)
	else:
		maxs = 100000000000000000
		if abs(y - 1) % d == 0:
			maxs = min(maxs, math.ceil((x - 1) / d) + math.ceil((y - 1) / d))
		if abs(n - y) % d == 0:
			maxs = min(maxs, math.ceil((n - x) / d) + math.ceil((n - y) / d))
		ans.append(maxs)
for i in range(len(ans)):
	print(int(ans[i]))


t = int(input())
for _ in range(t):
	(n, x, y, d) = map(int, input().split())
	p = -1
	k = -1
	if abs(y - x) % d == 0:
		print(abs(y - x) // d)
	else:
		if (y - 1) % d == 0:
			k = (x - 1) // d + 1 + (y - 1) // d
		if (n - y) % d == 0:
			p = (n - x) // d + 1 + (n - y) // d
		r = min(p, k)
		if r == -1:
			print(max(p, k))
		else:
			print(min(p, k))


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


t = int(input())
for _ in range(t):
	(n, x, y, d) = 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)


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


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(2000000000.0)
	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(2000000000.0) else -1)


import collections, atexit, math, sys, bisect
sys.setrecursionlimit(1000000)

def getIntList():
	return list(map(int, input().split()))
try:
	import numpy

	def dprint(*args, **kwargs):
		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())
(T,) = getIntList()
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))


import math

def solve():
	(n, x, y, d) = [int(x) for x in input().split()]
	if x == y:
		print(0)
		return
	if d == 0:
		print(-1)
		return
	if abs(x - y) % d == 0:
		print(abs(x - y) // d)
		return
	(a1, a2) = [0, 0]
	a1 += math.ceil((x - 1) / d)
	a2 += math.ceil((n - x) / d)
	if (y - 1) % d == 0:
		a1 += (y - 1) // d
	else:
		a1 += int(1000000000000.0)
	if (n - y) % d == 0:
		a2 += (n - y) // d
	else:
		a2 += int(1000000000000.0)
	if min(a1, a2) >= int(1000000000000.0):
		print(-1)
	else:
		print(min(a1, a2))
for case in range(int(input())):
	solve()


m = int(input())
for i in range(0, m):
	l = 0
	(n, x, y, d) = map(int, input().split())
	a = 7289567892756268
	b = 11111111111111111
	c = 1000000000000000
	w = 0
	if x > y:
		w = x - y
	else:
		w = y - x
	if w % d == 0:
		a = w // d
		l = l + 1
	if (y - 1) % d == 0:
		if (x - 1) % d == 0:
			b = (y - 1) // d + (x - 1) // d
			l = l + 1
		else:
			b = (y - 1) // d + (x - 1) // d + 1
			l = l + 1
	if (n - y) % d == 0:
		if (n - x) % d == 0:
			c = (n - y) // d + (n - x) // d
			l = l + 1
		else:
			c = (n - y) // d + (n - x) // d + 1
			l = l + 1
	if l == 0:
		print(-1)
	else:
		print(min(a, b, c))


from math import *
t = int(input())
for i in range(0, t):
	(n, x, y, d) = map(int, input().split(' '))
	ans = inf
	if abs(y - x) % d == 0:
		ans = abs(y - x) / d
	if abs(1 - y) % d == 0:
		ans = min(ans, ceil(abs(1 - x) / d) + ceil(abs(1 - y) / d))
	if abs(n - y) % d == 0:
		ans = min(ans, ceil(abs(n - x) / d) + ceil(abs(n - y) / d))
	if ans == inf:
		print(-1)
	else:
		print(int(ans))


t = int(input())
while t > 0:
	(n, x, y, d) = map(int, input().split())
	di = abs(x - y)
	if di % d == 0:
		print(di // d)
	elif (y - 1) % d == 0:
		a = (x - 1) // d + (y - 1) // d
		if (x - 1) % d != 0:
			a += 1
		if (n - y) % d == 0:
			b = (n - x) // d + (n - y) // d
			if (n - x) % d != 0:
				b += 1
			print(min(a, b))
		else:
			print(a)
	elif (n - y) % d == 0:
		b = (n - x) // d + (n - y) // d
		if (n - x) % d != 0:
			b += 1
		print(b)
	else:
		print(-1)
	t -= 1


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)


t = int(input())
for i in range(t):
	s = [int(x) for x in input().split()]
	n = s[0]
	x = s[1] - 1
	y = s[2] - 1
	d = s[3]
	ans = 10000000000
	if abs(y - x) % d == 0:
		ans = min(ans, abs(y - x) // d)
	if y % d == 0:
		f = x % d != 0
		ans = min(ans, x // d + y // d + f)
	if (n - y - 1) % d == 0:
		f = (n - 1 - x) % d != 0
		ans = min(ans, (n - y - 1) // d + (n - x - 1) // d + f)
	if ans == 10000000000:
		print('-1')
	else:
		print(ans)


import sys, os
from io import BytesIO, IOBase
from math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log
from collections import defaultdict as dd, deque
from heapq import merge, heapify, heappop, heappush, nsmallest
from bisect import bisect_left as bl, bisect_right as br, bisect
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(stdin, stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
mod = pow(10, 9) + 7
mod2 = 998244353

def inp():
	return stdin.readline().strip()

def iinp():
	return int(inp())

def out(var, end='\n'):
	stdout.write(str(var) + '\n')

def outa(*var, end='\n'):
	stdout.write(' '.join(map(str, var)) + end)

def lmp():
	return list(mp())

def mp():
	return map(int, inp().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(m, val) for j in range(n)]

def ceil(a, b):
	return (a + b - 1) // b
S1 = 'abcdefghijklmnopqrstuvwxyz'
S2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

def isprime(x):
	if x <= 1:
		return False
	if x in (2, 3):
		return True
	if x % 2 == 0:
		return False
	for i in range(3, int(sqrt(x)) + 1, 2):
		if x % i == 0:
			return False
	return True
for _ in range(iinp()):
	(n, x, y, d) = mp()
	c = abs(x - y)
	mn = inf
	if x % d == y % d:
		mn = min(mn, c // d)
	if y % d == 1 % d:
		mn = min(mn, (y - 1) // d + ceil(x - 1, d))
	if y % d == n % d:
		mn = min(mn, (n - y) // d + ceil(n - x, d))
	print(mn if mn != inf else -1)


t = int(input())
for i in range(t):
	(n, x, y, d) = [int(j) for j in input().split()]
	m = 3 * n
	if x % d == y % d or y % d == 1 % d or y % d == n % d:
		if x % d == y % d:
			m = abs(x - y) // d
		if y % d == 1 % d:
			try:
				m = min(m, (y - 1) // d + (x - 1 + d - 1) // d)
			except:
				m = (y - 1) // d + (x - 1 + d - 1) // d
		if y % d == n % d:
			try:
				m = min(m, (n - x + d - 1) // d + (n - y) // d)
			except:
				m = (n - x + d - 1) // d + (n - y) // d
		print(m)
	else:
		print(-1)


import math
for _ in range(int(input())):
	(n, x, y, d) = [int(x) for x in input().split()]
	ans1 = ans2 = ans3 = 999999999999
	if abs(x - y) % d == 0:
		ans1 = abs(x - y) // d
	if abs(n - y) % d == 0:
		ans2 = (n - y) // d
		ans2 += math.ceil((n - x) / d)
	if abs(y - 1) % d == 0:
		ans3 = abs(y - 1) // d
		ans3 += math.ceil((x - 1) / d)
	mina = min(ans3, ans2, ans1)
	if mina == 999999999999:
		print(-1)
	else:
		print(mina)


import math

def clac(n: int, x: int, y: int, d: int):
	ans = []
	temp = abs(x - y)
	if temp % d == 0:
		ans.append(temp // d)
	temp = math.ceil(abs(1 - x) / d)
	if abs(1 - y) % d == 0:
		ans.append(abs(1 - y) // d + temp)
	temp = math.ceil(abs(n - x) / d)
	if abs(n - y) % d == 0:
		ans.append(abs(y - n) // d + temp)
	if ans:
		return min(ans)
	else:
		return -1
for tt in range(int(input())):
	(n, x, y, d) = map(int, input().split())
	print(clac(n, x, y, d))


import math
t = int(input())
topdown = -1
downtop = -1
for _ in range(t):
	(n, x, y, d) = list(map(int, input().split()))
	if abs(x - y) % d == 0:
		print(abs(x - y) // d)
	else:
		if (n - y) % d == 0:
			topdown = int(math.ceil((n - x) / d)) + int((n - y) / d)
		if (y - 1) % d == 0:
			downtop = int(math.ceil((x - 1) / d)) + int((y - 1) / d)
		if topdown == -1 and downtop == -1:
			print(-1)
		elif topdown < downtop and topdown != -1 or (topdown > downtop and downtop == -1):
			print(topdown)
		elif downtop < topdown and downtop != -1 or (downtop > topdown and topdown == -1):
			print(downtop)
		elif topdown == downtop:
			print(topdown)
	topdown = -1
	downtop = -1


import math
n = int(input())
for i in range(n):
	(c1, c2, c3) = (100000000000.0, 100000000000.0, 100000000000.0)
	(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 == 100000000000.0:
		print(-1)
	else:
		print(m)


NMAX = 2 * 10 ** 9
t = int(input())
for _ in range(t):
	(n, x, y, d) = list(map(int, input().split()))
	res = -1
	if (x - y) % d == 0:
		res = abs(x - y) // d
	else:
		if (y - 1) % d == 0:
			lres = (x + d - 2) // d + (y - 1) // d
		else:
			lres = NMAX
		if (n - y) % d == 0:
			rres = (n - x + d - 1) // d + (n - y) // d
		else:
			rres = NMAX
		res = min(rres, lres)
		if res == NMAX:
			res = -1
	print(res)


for _ in range(int(input())):
	(n, x, y, d) = map(int, input().split())
	if abs(x - y) % d == 0:
		print(abs(x - y) // d)
	else:
		(k1, k2) = (-1, -1)
		if (y - 1) % d == 0:
			p = (x - 1) // d
			if x - p * d != 1:
				p += 1
			k1 = p + (y - 1) // d
		k = (n - y) // d
		if n - k * d == y:
			k2 = k
			p = (n - x) // d
			if x + p * d != n:
				p += 1
			k2 += p
		if k1 == -1 and k2 == -1:
			print('-1')
		elif k1 == -1:
			print(k2)
		elif k2 == -1:
			print(k1)
		else:
			print(min(k1, k2))


import math as m
t = int(input())
for i in range(t):
	en = list(map(int, input().split()))
	n = en[0]
	x = en[1]
	y = en[2]
	d = en[3]
	if abs(x - y) % d == 0:
		print(m.ceil(abs(x - y) / d))
	else:
		right = m.ceil((n - x) / d)
		if (n - y) % d != 0:
			right = -1
		else:
			right += m.ceil((n - y) / d)
		left = m.ceil((x - 1) / d)
		if (y - 1) % d != 0:
			left = -1
		else:
			left += m.ceil((y - 1) / d)
		if right == -1 and left == -1:
			print(-1)
		elif right == -1 and left != -1:
			print(left)
		elif right != -1 and left == -1:
			print(right)
		elif left < right:
			print(left)
		else:
			print(right)


from math import ceil

def solve():
	results = []
	for _ in range(int(input())):
		(N, X, Y, D) = map(int, input().split())
		ans1 = 10 ** 10
		if abs(X - Y) % D == 0:
			ans1 = abs(X - Y) // D
		ans2 = 10 ** 10
		ans3 = 10 ** 10
		if (Y - 1) % D == 0:
			ans2 = ceil((X - 1) / D) + (Y - 1) // D
		if (N - Y) % D == 0:
			ans3 = ceil((N - X) / D) + (N - Y) // D
		ans = min(ans1, ans2, ans3)
		results.append(ans if ans < 10 ** 10 else -1)
	print('\n'.join((str(k) for k in results)))
solve()


from math import ceil
for _ in range(int(input())):
	(n, s, f, d) = list(map(int, input().split()))
	ans = 10 ** 10
	if abs(f - s) % d == 0:
		ans = min(ans, abs(f - s) // d)
	if abs(f - 1) % d == 0:
		ans = min(ans, abs(s + d - 2) // d + abs(f - 1) // d)
	if abs(n - f) % d == 0:
		ans = min(ans, abs(n - s + d - 1) // d + abs(n - f) // d)
	if ans == 10 ** 10:
		print(-1)
	else:
		print(ans)


T = int(input())
from math import ceil
for i in range(T):
	(n, in_, fin_, d) = map(int, input().split())
	r1 = in_ % d
	r2 = fin_ % d
	r = n % d
	z = float('inf')
	(a1, a2, a3) = (float('inf'), float('inf'), float('inf'))
	if r1 == r2:
		a1 = abs(fin_ - in_) // d
	if r2 == r:
		a2 = ceil((n - in_) / d) + abs((fin_ - n) // d)
	if r2 == 1:
		a3 = ceil(in_ / d) + abs(fin_ - 1) // d
	z = min(a1, a2, a3)
	if z == float('inf'):
		print(-1)
	else:
		print(z)


def run():
	t = int(input())
	while t > 0:
		(n, x, y, d) = map(int, input().split())
		if (y - x) % d == 0:
			print(abs(y - x) // d)
		else:
			(le, ri) = (False, False)
			if (y - 1) % d == 0:
				rem = (x - 1) % d
				left = abs(x - 1) // d
				if rem > 0:
					left += 1
				left += abs(y - 1) // d
				le = True
			if (n - y) % d == 0:
				rem2 = (n - x) % d
				right = abs(n - x) // d
				if rem2 > 0:
					right += 1
				right += abs(n - y) // d
				ri = True
			if not le and (not ri):
				print(-1)
			elif not le:
				print(abs(right))
			elif not ri:
				print(abs(left))
			else:
				print(abs(min(left, right)))
		t -= 1
run()


time = int(input())
for i in range(time):
	(n, x, y, d) = map(int, input().split())
	k = (1 - x) // d * -1 + (1 - y) // d * -1
	k2 = (x - n) // d * -1 + (y - n) // d * -1
	k1 = 10 ** 9 + 3
	q = False
	if not abs(y - x) % d:
		q = True
		k1 = abs(y - x) // d
	if not (y - 1) % d:
		q = True
		k1 = min(k1, k)
	if not (n - y) % d:
		q = True
		k1 = min(k2, k1)
	if q:
		print(k1)
	else:
		print(-1)


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


for _ in range(int(input())):
	(n, x, y, d) = map(int, input().split())
	count = []
	if abs(y - x) % d == 0:
		count.append(abs(y - x) // d)
	if (y - 1) % d == 0:
		count.append(abs(-x // d) + (y - 1) // d)
	if (n - y) % d == 0:
		count.append(abs((-n + x) // d) + (n - y) // d)
	if len(count) == 0:
		print(-1)
	else:
		print(min(count))


for _ in range(int(input())):
	(n, x, y, d) = map(int, input().split(' '))
	z = 1000000000000
	if abs(y - x) % d == 0:
		z = min(abs(y - x) // d, z)
	if abs(n - y) % d == 0:
		xtn = (n - x) // d + (0 if (n - x) % d == 0 else 1)
		nty = (n - y) // d
		z = min(xtn + nty, z)
	if (y - 1) % d == 0:
		xt1 = (x - 1) // d + (0 if (x - 1) % d == 0 else 1)
		ty = (y - 1) // d
		z = min(xt1 + ty, z)
	print(z if z != 1000000000000 else -1)


t = int(input())
(p3, p2, p1) = (0, 0, 0)
rez = []
for i in range(t):
	a = [int(s) for s in input().split()]
	n = a[0]
	x = a[1]
	y = a[2]
	d = a[3]
	if (y - x) % d == 0:
		p1 = abs(y - x) // d
		rez.append(p1)
	if (n - y) % d == 0:
		p2 = (n - x) // d + int((n - x) % d != 0)
		p2 += (n - y) // d
		rez.append(p2)
	z = 1
	if (y - z) % d == 0:
		p3 = (x - 1) // d + int((x - 1) % d != 0)
		p3 += (y - 1) // d
		rez.append(p3)
	if len(rez) != 0:
		print(min(rez))
	else:
		print(-1)
	rez = []


t = int(input())
for _ in range(t):
	(n, x, y, d) = map(int, input().strip().split())
	t = abs(x - y)
	if t % d != 0:
		ans = -1
		if (n - y) % d == 0:
			ans = (n - x) // d + 1 + (n - y) // d
		if (y - 1) % d == 0:
			c = int((x - 1) // d + 1 + (y - 1) / d)
			if ans > c or ans == -1:
				ans = c
		print(ans)
	else:
		print(t // d)


import os
import heapq
import sys
import math
import operator
from collections import defaultdict
from io import BytesIO, IOBase

def inpt():
	return [int(k) for k in input().split()]

def main():
	for _ in range(int(input())):
		(n, x, y, d) = map(int, input().split())
		if x == y:
			print(0)
		elif abs(x - y) % d == 0:
			print(abs(x - y) // d)
		else:
			(ans1, ans2) = (-1, -1)
			if (y - 1) % d == 0:
				temp = abs((x - 1) // d) + (1 if (x - 1) % d != 0 else 0)
				ans1 = temp + abs((y - 1) // d)
			if (n - y) % d == 0:
				temp = abs((n - x) // d) + (1 if (n - x) % d != 0 else 0)
				ans2 = temp + abs((n - y) // d)
			if ans1 == -1 and ans2 == -1:
				print(-1)
			elif ans1 == -1:
				print(ans2)
			elif ans2 == -1:
				print(ans1)
			else:
				print(min(ans1, ans2))
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
t = int(input())
while t > 0:
	(n, x, y, d) = [int(a) for a in input().split()]
	if abs(x - y) % d == 0:
		print(int(abs(x - y) / d))
	elif abs(1 - y) % d != 0 and abs(n - y) % d != 0:
		print(-1)
	elif abs(n - y) % d != 0:
		ans = int(abs(1 - x) / d) + int(abs(1 - y) / d)
		if abs(1 - x) % d != 0:
			ans += 1
		print(ans)
	elif abs(1 - y) % d != 0:
		ans = int(abs(n - x) / d) + int(abs(n - y) / d)
		if abs(n - x) % d != 0:
			ans += 1
		print(ans)
	else:
		ans1 = int(abs(1 - x) / d) + int(abs(1 - y) / d)
		if abs(1 - x) % d != 0:
			ans1 += 1
		ans2 = int(abs(n - x) / d) + int(abs(n - y) / d)
		if abs(n - x) % d != 0:
			ans2 += 1
		print(min(ans1, ans2))
	t -= 1


n = int(input())
for i in range(n):
	(n, x, y, d) = map(int, input().split())
	ans = 10 ** 18
	if x <= y:
		if x % d == y % d:
			ans = min(ans, (y - x) // d)
		if y % d == 1:
			ans = min(ans, (x - 1 + d - 1) // d + (y - 1) // d)
		if y % d == n % d:
			ans = min(ans, (n - x + d - 1) // d + (n - y) // d)
	else:
		if x % d == y % d:
			ans = min(ans, (x - y) // d)
		if y % d == 1:
			ans = min(ans, (x - 1 + d - 1) // d + (y - 1) // d)
		if y % d == n % d:
			ans = min(ans, (n - x + d - 1) // d + (n - y) // d)
	if ans != 10 ** 18:
		print(ans)
	else:
		print(-1)


t = int(input())
for tc in range(t):
	(n, x, y, d) = map(int, input().split())
	if (y - x) % d == 0:
		print(abs(y - x) // d)
	else:
		p = []
		a = (x - 2) // d + (y - 1) // d + 1
		if (y - 1) % d == 0:
			p.append(a)
		b = (n - x - 1) // d + (n - y) // d + 1
		if (n - y) % d == 0:
			p.append(b)
		print(min(p) if len(p) > 0 else -1)


def get_num(arr):
	num_pages = arr[0]
	start = arr[1]
	desired = arr[2]
	scroll = arr[3]
	s = abs(start - desired)
	if s % scroll == 0:
		print(int(s / scroll))
	else:
		if (num_pages - start) % scroll == 0:
			s1 = int((num_pages - start) / scroll)
		else:
			s1 = int((num_pages - start) / scroll + 1)
		if (start - 1) % scroll == 0:
			s2 = int((start - 1) / scroll)
		else:
			s2 = int((start - 1) / scroll + 1)
		if (num_pages - desired) % scroll == 0:
			num1 = int((num_pages - desired) / scroll + s1)
		else:
			num1 = 1000000000.0
		if (desired - 1) % scroll == 0:
			num2 = int((desired - 1) / scroll + s2)
		else:
			num2 = 1000000000.0
		num = min(num1, num2)
		if num == 1000000000.0:
			print(-1)
		else:
			print(num)
t = input()
l = []
for i in range(int(t)):
	l.append(input())
a = []
for i in l:
	b = i.split()
	for j in range(len(b)):
		b[j] = int(b[j])
	a.append(b)
for i in a:
	get_num(i)


t = int(input().strip())
INF = 99999999999
while t > 0:
	t -= 1
	(n, x, y, d) = [int(i) for i in input().strip().split(' ')]
	if x == y:
		print(0)
	elif x < y and (y - x) % d == 0:
		print((y - x) // d)
	elif x > y and (x - y) % d == 0:
		print((x - y) // d)
	elif (y - 1) % d == 0 or (n - y) % d == 0:
		left = (y - 1) // d + x // d + (1 if x % d > 0 else 0) if (y - 1) % d == 0 else INF
		right = (n - y) // d + (n - x) // d + (0 if (n - x) % d == 0 else 1) if (n - y) % d == 0 else INF
		print(min(left, right))
	else:
		print(-1)


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 _ in range(t):
	(n, x, y, d) = map(int, input().split())
	possible = []
	if abs(x - y) % d == 0:
		possible += [abs(x - y) // d]
	if (y - 1) % d == 0:
		possible += [(y - 1) // d + (x - 1 + d - 1) // d]
	if (n - y) % d == 0:
		possible += [(n - y) // d + (n - x + d - 1) // d]
	if len(possible) == 0:
		print(-1)
	else:
		print(min(possible))


import math
t = int(input())
for _ in range(t):
	(n, x, y, d) = map(int, str(input()).split())
	if abs(y - x) % d == 0:
		print(int(abs(y - x) / d))
		continue
	else:
		r1 = None
		r2 = None
		if abs(y - 1) % d == 0:
			r1 = int(math.ceil(abs(x - 1) / d) + abs(y - 1) / d)
		if abs(n - y) % d == 0:
			r2 = int(math.ceil(abs(n - x) / d) + abs(n - y) / d)
		if r1:
			if r2:
				print(min(r1, r2))
			else:
				print(r1)
		elif r2:
			print(r2)
		else:
			print(-1)
pass


n = int(input())
for x in range(n):
	ka = []
	(t, f, s, d) = list(map(int, input().split()))
	lam = abs(f - s)
	if lam % d == 0:
		print(lam // d)
	elif (t - f) % d == 0 and (f - 1) % d == 0:
		print(-1)
	else:
		if (t - s) % d == 0:
			o = (t - s) // d
			kaa = (t - f) / d
			saa = (t - f) // d
			if kaa == (t - f) // d:
				ka.append(o + kaa)
			else:
				ka.append(o + saa + 1)
		if (s - 1) % d == 0:
			o = (s - 1) // d
			kaa = (f - 1) / d
			saa = (f - 1) // d
			if kaa == (f - 1) // d:
				ka.append(o + saa)
			else:
				ka.append(o + saa + 1)
		if ka != list():
			print(min(ka))
		else:
			print(-1)


f = lambda x, y, d: (abs(y - x) + d - 1) // d
for _ in range(int(input())):
	(n, x, y, d) = map(int, input().split())
	res = float('INF')
	if y % d == x % d:
		res = f(x, y, d)
	if y % d == 1:
		res = min(res, f(x, 1, d) + f(1, y, d))
	if y % d == n % d:
		res = min(res, f(x, n, d) + f(n, y, d))
	if res == float('INF'):
		res = -1
	print(res)


for _ in range(int(input())):
	(n, x, y, d) = map(int, input().split())
	diff = abs(x - y) // d
	if x + diff * d == y or x - diff * d == y:
		ans = diff
	else:
		a = (x - 1) // d
		if 1 + a * d < x:
			a += 1
		b = (y - 1) // d
		if b * d + 1 == y:
			ans1 = a + b
		else:
			ans1 = -1
		a = (n - x) // d
		if x + a * d < n:
			a += 1
		b = (n - y) // d
		if y + b * d == n:
			ans2 = a + b
		else:
			ans2 = -1
		if ans1 != -1 and ans2 != -1:
			ans = min(ans1, ans2)
		elif ans2 != -1:
			ans = ans2
		elif ans1 != -1:
			ans = ans1
		else:
			ans = -1
	print(ans)


from math import *
t = int(input())
for i in range(t):
	(n, x, y, d) = map(int, input().split())
	min = 99999999999
	if abs(x - y) % d == 0:
		a = abs(x - y) / d
		if min > a:
			min = a
	if (y - 1) % d == 0:
		a = ceil((x - 1) / d) + (y - 1) / d
		if min > a:
			min = a
	if (n - y) % d == 0:
		a = ceil((n - x) / d) + (n - y) / d
		if min > a:
			min = a
	if min == 99999999999:
		min = -1
	print(int(min))


for _ in range(int(input())):
	(n, x, y, d) = map(int, input().split())
	if abs(y - x) % d == 0:
		print(abs(y - x) // d)
		continue
	(a, b) = (100000000000000, 100000000000000)
	if (n - y) % d == 0:
		a = (n - x + d - 1) // d + (n - y) // d
	if (y - 1) % d == 0:
		b = (x - 1 + d - 1) // d + (y - 1) // d
	if min(a, b) == 100000000000000:
		print(-1)
		continue
	print(min(a, b))


__author__ = 'tanunia'
from sys import stdin
t = int(stdin.readline())
for _ in range(t):
	(n, x, y, d) = [int(k) for k in stdin.readline().split()]
	ans = 1000000005000
	if x % d == y % d:
		ans = abs(x - y) // d
	if 1 == y % d:
		to_start = (x - 1) // d
		if (x - 1) % d > 0:
			to_start += 1
		ans = min(ans, abs(y - 1) // d + to_start)
	if n % d == y % d:
		to_end = (n - x) // d
		if (n - x) % d > 0:
			to_end += 1
		ans = min(ans, (n - y) // d + to_end)
	if ans == 1000000005000:
		ans = -1
	print(ans)


import array
t = int(input())
for i in range(1, t + 1):
	(n, x, y, d) = map(int, input().split())
	z1 = (n - x) // d + (0 if (n - x) % d == 0 else 1) + ((n - y) // d if (n - y) % d == 0 else 10000000000)
	z2 = (x - 1) // d + (0 if (x - 1) % d == 0 else 1) + ((y - 1) // d if (y - 1) % d == 0 else 10000000000)
	if (y - x) % d == 0:
		print(abs((y - x) // d))
	elif (n - y) % d == 0 and z1 <= z2:
		print(abs(z1))
	elif (y - 1) % d == 0:
		print(abs(z2))
	else:
		print(-1)


def fastio():
	import sys
	from io import StringIO
	from atexit import register
	global input
	sys.stdin = StringIO(sys.stdin.read())
	input = lambda : sys.stdin.readline().rstrip('\r\n')
	sys.stdout = StringIO()
	register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()
MOD = 10 ** 9 + 7
I = lambda : list(map(int, input().split()))
from math import *
(t,) = I()
while t:
	t -= 1
	(n, x, y, d) = I()
	if abs(x - y) % d and (y - 1) % d and (n - y) % d:
		print(-1)
		continue
	if abs(x - y) % d == 0:
		print(ceil(abs(x - y) / d))
		continue
	k = (y - 1) // d + ceil((x - 1) / d)
	s = (n - y) // d + ceil((n - x) / d)
	if (y - 1) % d:
		print(s)
	elif (n - y) % d:
		print(k)
	else:
		print(min(k, s))


T = int(input())
sol = list()
for _ in range(T):
	(n, x, y, d) = map(int, input().split())
	ans = 1e+18
	if abs(y - x) % d == 0:
		ans = min(ans, abs(y - x) // d)
	if (n - y) % d == 0:
		ans = min(ans, abs(n - y) // d + (n - x + d - 1) // d)
	if (y - 1) % d == 0:
		ans = min(ans, (y - 1) // d + (x + d - 1) // d)
	sol.append(ans)
for i in range(T):
	if sol[i] == 1e+18:
		print(-1)
	else:
		print(sol[i])


from sys import stdin, stdout
from math import sin, tan, cos, pi, atan2, sqrt, acos, atan, factorial
INF = float('inf')

def solution():
	if abs(x - y) % d == 0:
		return abs(x - y) // d
	if (y - 1) % d == 0:
		a1 = (x - 1 + d - 1) // d + (y - 1) // d
	else:
		a1 = INF
	if (n - y) % d == 0:
		a2 = (n - x + d - 1) // d + (n - y) // d
	else:
		a2 = INF
	if min(a1, a2) == INF:
		return -1
	return min(a1, a2)
t = int(stdin.readline())
for i in range(t):
	(n, x, y, d) = map(int, stdin.readline().split())
	stdout.write(str(solution()) + '\n')


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


import math
n = int(input())
s = []
for i in range(n):
	(a, b, c, d) = map(int, input().split())
	if abs(c - b) % d == 0:
		m = int(abs(b - c) / d)
	else:
		x = math.ceil(abs(b - 1) / d) + abs((c - 1) / d)
		y = math.ceil(abs(b - a) / d) + abs((a - c) / d)
		if x != int(x) and y != int(y):
			m = -1
		elif x != int(x) and y == int(y):
			m = int(y)
		elif x == int(x) and y != int(y):
			m = int(x)
		elif x == int(x) and y == int(y):
			m = int(min(x, y))
	s.append(m)
for i in s:
	print(i)


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


import math
n = int(input())
for i in range(n):
	(count, start, end, step) = map(int, input().split())
	start -= 1
	end -= 1
	d = abs(start - end)
	if d % step == 0:
		print(int(d / step))
	else:
		arr = []
		dLeft = start
		leftSteps = math.ceil(dLeft / step)
		leftSteps += int(end / step)
		if end % step == 0:
			arr.append(leftSteps)
		dRight = abs(count - start)
		rightSteps = math.ceil(dRight / step)
		rightSteps += int((count - end - 1) / step)
		if (count - end - 1) % step == 0:
			arr.append(rightSteps)
		if len(arr) == 0:
			print(-1)
		else:
			print(min(arr))


import sys
import math
t = int(sys.stdin.readline())

def MIN(a, b):
	if a == -1:
		return b
	elif b == -1:
		return a
	else:
		return min(a, b)
while t > 0:
	t = t - 1
	(n, x, y, d) = map(int, sys.stdin.readline().split())
	res = -1
	if x % d == y % d:
		res = abs(x - y) // d
	if y % d == n % d:
		res = MIN(res, (n - x + (d - 1)) // d + (n - y + (d - 1)) // d)
	if y % d == 1 % d:
		res = MIN(res, (x - 1 + (d - 1)) // d + (y - 1 + (d - 1)) // d)
	print(res)


import sys
t = int(input())
for i in range(0, t):
	ln = [int(j) for j in sys.stdin.readline().rstrip().split(' ')]
	n = ln[0]
	x = ln[1]
	y = ln[2]
	d = ln[3]
	num = -1
	if x % d == y % d:
		num = abs(x - y) // d
	elif y == 1:
		num = (x - 2) // d + 1
	else:
		num1 = (x - 2) // d + 1
		num2 = (n - x - 1) // d + 1
		mnum = 1000000000
		om = mnum
		if 1 % d == y % d:
			mnum = num1 + (y - 1) // d
		if n % d == y % d:
			mnum = min(mnum, num2 + (n - y) // d)
		if mnum != om:
			num = mnum
	print(num)


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))


n = int(input())
import math
for i in range(n):
	(n, x, y, d) = map(int, input().split())
	lis = [[y - 1, (y - 1) // d + math.ceil((x - 1) / d)], [n - y, (n - y) // d + math.ceil((n - x) / d)], [abs(x - y), abs(x - y) // d]]
	output = []
	for i in lis:
		if i[0] % d == 0:
			output.append(i)
	if output == []:
		print(-1)
	else:
		print(min((i[1] for i in output)))


t = int(input())
for i in range(t):
	(n, x, y, d) = map(int, input().split())
	x -= 1
	y -= 1
	n -= 1
	a = abs(x - y)
	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 = 10000000000.0
	i2 = max(0, (x + d - 1) // d) + y // d
	if y % d != 0:
		i2 = 10000000000.0
	print(min(i1, i2))


from math import ceil
t = int(input())
for _ in range(t):
	(n, x, y, d) = map(int, input().split())
	v = []
	if (x - y) % d == 0:
		v += [max(x - y, y - x) // d]
	if (y - 1) % d == 0:
		v += [(y - 1) // d + ceil((x - 1) / d)]
	if (n - y) % d == 0:
		v += [(n - y) // d + ceil((n - x) / d)]
	if len(v) == 0:
		print(-1)
	else:
		v.sort()
		print(v[0])


t = int(input())
while t > 0:
	t = t - 1
	(n, x, y, d) = map(int, input().split())
	ans = 10 ** 9 + 2
	if abs(y - x) % d == 0:
		ans = min(abs(y - x) // d, ans)
	if (y - 1) % d == 0:
		ans = min((y - 1) // d + (x - 1 + d - 1) // d, ans)
	if (n - y) % d == 0:
		ans = min((n - y) // d + (n - x + d - 1) // d, ans)
	if ans == 10 ** 9 + 2:
		ans = -1
	print(ans)


m = int(input())
for q in range(m):
	(n, x, y, d) = 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)


testcases = int(input())
for i in range(testcases):
	(n, x, y, d) = map(int, input().split())
	answer = 1000000000.0
	if abs(y - x) % d == 0:
		answer = int(abs(y - x) / d)
	if (y - 1) % d == 0:
		total = int((x - 1) / d)
		if (x - 1) % d != 0:
			total += 1
		total += (y - 1) / d
		answer = min(int(total), answer)
	if (n - y) % d == 0:
		total = int((n - x) / d)
		if (n - x) % d != 0:
			total += 1
		total += (n - y) / d
		answer = min(int(total), answer)
	print(-1) if answer == 1000000000.0 else print(int(answer))


import math

def solve():
	(n, x, y, d) = [int(s) for s in input().split(' ')]
	m = float('inf')
	if (x - y) % d == 0:
		m = abs(x - y) // d
	if (y - 1) % d == 0:
		m = min(m, math.ceil((x - 1) / d) + (y - 1) // d)
	if (n - y) % d == 0:
		m = min(m, math.ceil((n - x) / d) + (n - y) // d)
	if m == float('inf'):
		m = -1
	return m
t = int(input())
for case in range(t):
	print(solve())


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


pocSkusok = int(input())
for i in range(pocSkusok):
	(pocStran, strana, hladana, posun) = list(map(int, input().split(' ')))
	odpoved = 100000000000
	skuska1 = abs(hladana - strana)
	if skuska1 / posun == skuska1 // posun:
		odpoved = skuska1 / posun
	skuska2 = hladana - 1
	if skuska2 / posun == skuska2 // posun:
		odpoved = min(odpoved, skuska2 / posun + round((strana - 1) / posun + 0.5))
	skuska3 = pocStran - hladana
	if skuska3 / posun == skuska3 // posun:
		odpoved = min(odpoved, skuska3 / posun + round((pocStran - strana) / posun + 0.5))
	if odpoved == 100000000000:
		odpoved = -1
	print(int(odpoved))


import math

def calc(n: int, x: int, y: int, d: int):
	ans: list = []
	tmp = abs(x - y)
	if tmp % d == 0:
		ans.append(tmp // d)
	tmp = math.ceil(abs(1 - x) / d)
	if abs(1 - y) % d == 0:
		ans.append(abs(1 - y) // d + tmp)
	tmp = math.ceil(abs(n - x) / d)
	if abs(n - y) % d == 0:
		ans.append(abs(n - y) // d + tmp)
	if ans:
		return min(ans)
	else:
		return -1

def main():
	for _ in range(int(input())):
		(n, x, y, d) = map(int, input().split())
		print(calc(n, x, y, d))
main()


t = int(input())
while t > 0:
	(n, x, y, d) = map(int, input().split())
	if (y - x) % d == 0:
		print(int(abs(y - x) / d))
	else:
		ans1 = -1
		ans2 = -1
		if (y - 1) % d == 0:
			ans1 = int((y - 1) / d)
			kk = x % d
			ans1 += int(x / d)
			if kk == 0 or kk == 1:
				ans1 += 0
			else:
				ans1 += 1
		if (n - y) % d == 0:
			ans2 = int((n - y) / d)
			ans2 += int((n - x) / d)
			kk = (n - x) % d
			if kk != 0:
				ans2 += 1
		if ans1 >= 0 and ans2 >= 0:
			ans = min(ans1, ans2)
			print(ans)
		else:
			print(max(ans1, ans2))
	t -= 1


import math
n = int(input())
for i in range(n):
	a = input().split()
	ans = 10 ** 9 + 1
	if (int(a[2]) - int(a[1])) % int(a[3]) == 0:
		ans = abs((int(a[2]) - int(a[1])) / int(a[3]))
	else:
		if (int(a[2]) - 1) % int(a[3]) == 0:
			ans = math.ceil(int(a[1]) / int(a[3])) + (int(a[2]) - 1) / int(a[3])
		if (int(a[0]) - int(a[2])) % int(a[3]) == 0:
			ans = min(ans, math.ceil((int(a[0]) - int(a[1])) / int(a[3])) + (int(a[0]) - int(a[2])) / int(a[3]))
	if ans > 10 ** 9:
		ans = -1
	print(int(ans))


import math
n = int(input())
data = []
for i in range(0, n):
	data.append(list(map(int, input().rstrip().split())))

def flips(lis):
	uppages = 0
	downpages = 0
	if lis[1] % lis[3] == lis[2] % lis[3]:
		return abs(lis[1] - lis[2]) // lis[3]
	if lis[2] % lis[3] == 1:
		downpages = math.ceil((lis[1] - 1) / lis[3]) + (lis[2] - 1) / lis[3]
	if lis[2] % lis[3] == lis[0] % lis[3]:
		uppages = math.ceil((lis[0] - lis[1]) / lis[3]) + (lis[0] - lis[2]) / lis[3]
	if uppages == 0 and downpages == 0:
		return -1
	elif uppages == 0:
		return int(downpages)
	elif downpages == 0:
		return int(uppages)
	else:
		return int(min([uppages, downpages]))
for i in range(0, n):
	print(flips(data[i]))


cas = int(input())
from math import ceil
for case in range(cas):
	(a, b, c, d) = map(int, input().split())
	dist1 = abs(b - c)
	if dist1 % d == 0:
		print(dist1 // d)
		continue
	dist2 = abs(1 - c)
	dist3 = abs(a - c)
	aux2 = abs(b - 1)
	if aux2 % d != 0:
		aux2 = aux2 // d
		aux2 += 1
	else:
		aux2 = aux2 // d
	aux3 = abs(b - a)
	if aux3 % d != 0:
		aux3 = aux3 // d
		aux3 += 1
	else:
		aux3 = aux3 // d
	if dist2 % d == 0 and dist3 % d == 0:
		print(min(dist2 // d + aux2, dist3 // d + aux3))
		continue
	if dist2 % d == 0:
		print(dist2 // d + aux2)
		continue
	if dist3 % d == 0:
		print(dist3 // d + aux3)
		continue
	print(-1)


import sys, math
t = int(sys.stdin.readline())
for _ in range(t):
	(n, x, y, d) = map(int, sys.stdin.readline().split())
	if abs(x - y) % d == 0:
		print(abs(x - y) // d)
	elif abs(y - 1) % d != 0 and abs(n - y) % d != 0:
		print(-1)
	elif abs(y - 1) % d == 0 and abs(n - y) % d != 0:
		ans = 10 ** 10
		temp = math.ceil(abs(x - 1) / d)
		temp += abs(y - 1) // d
		ans = min(ans, temp)
		print(ans)
	elif abs(y - 1) % d != 0 and abs(n - y) % d == 0:
		ans = 10 ** 10
		temp = math.ceil(abs(n - x) / d)
		temp += abs(y - n) // d
		ans = min(ans, temp)
		print(ans)
	else:
		ans = 10 ** 10
		temp = math.ceil(abs(x - 1) / d)
		temp += abs(y - 1) // d
		ans = min(ans, temp)
		temp = math.ceil(abs(n - x) / d)
		temp += abs(y - n) // d
		ans = min(ans, temp)
		print(ans)


import math
T = int(input())
for q in range(T):
	(n, x, y, d) = map(int, input().strip().split())
	if abs(y - x) % d == 0:
		print(abs(y - x) // d)
	elif (y - 1) % d == 0 and (n - y) % d == 0:
		if math.ceil(x / d) + (y - 1) // d > math.ceil((n - x) / d) + (n - y) // d:
			print((n - y) // d + math.ceil((n - x) / d))
		else:
			print((y - 1) // d + math.ceil(x / d))
	elif (y - 1) % d == 0 or (n - y) % d == 0:
		if (y - 1) % d == 0:
			print((y - 1) // d + math.ceil(x / d))
		else:
			print((n - y) // d + math.ceil((n - x) / d))
	else:
		print(-1)


from sys import stdin, stdout
from math import *
from heapq import *
from collections import *

def main():
	t = int(stdin.readline())
	for test in range(t):
		(n, x, y, d) = [int(c) for c in stdin.readline().split()]
		res = 999999999999999999999999999999
		if abs(x - y) % d == 0:
			res = floor(abs(x - y) / d)
		else:
			if (y - 1) % d == 0:
				res = min(res, floor(abs(x - 1 + d - 1) / d) + floor(abs(y - 1) / d))
			if (n - y) % d == 0:
				res = min(res, floor(abs(n - x + d - 1) / d) + floor(abs(n - y) / d))
		if res == 999999999999999999999999999999:
			res = -1
		stdout.write(str(res) + '\n')
	return 0
main()


import math
for _ in range(int(input())):
	(n, x, y, m) = map(int, input().split())
	ans = y - x
	flag1 = flag2 = 0
	if abs(ans) % m == 0:
		print(abs(ans) // m)
	elif (y - 1) % m == 0 or (n - y) % m == 0:
		if (y - 1) % m == 0:
			xx = math.ceil((x - 1) / m)
			xx += (y - 1) // m
			flag1 = 1
		if (n - y) % m == 0:
			yy = math.ceil((n - x) / m)
			yy += (n - y) // m
			flag2 = 1
	else:
		print(-1)
	if flag1 == 1 and flag2 == 1:
		print(min(xx, yy))
	elif flag1 == 1:
		print(xx)
	elif flag2 == 1:
		print(yy)


t = int(input())
for _ in range(t):
	(n, x, y, d) = [int(x) for x in input().split(' ')]
	if y not in [1, n]:
		if abs(x - y) % d == 0:
			print(abs(x - y) // d)
		elif (y - 1) % d == 0 and (n - y) % d == 0:
			(p, q, r, s) = (1000000000, 1000000000, 1000000000, 1000000000)
			if (x - 1) % d == 0:
				p = (x - 1) // d + (y - 1) // d
			else:
				q = (x - 1) // d + 1 + (y - 1) // d
			if (n - x) % d == 0:
				r = (n - x) // d + (n - y) // d
			else:
				s = (n - x) // d + 1 + (n - y) // d
			print(min(p, q, r, s))
		elif (y - 1) % d == 0:
			if (x - 1) % d == 0:
				print((x - 1) // d + (y - 1) // d)
			else:
				print((x - 1) // d + 1 + (y - 1) // d)
		elif (n - y) % d == 0:
			if (n - x) % d == 0:
				print((n - x) // d + (n - y) // d)
			else:
				print((n - x) // d + 1 + (n - y) // d)
		else:
			print(-1)
	elif abs(x - y) % d == 0:
		print(abs(x - y) // d)
	else:
		print(abs(x - y) // d + 1)


t = int(input())
INF = 10000000000
for t_num in range(t):
	(n, start, end, d) = 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())

def left_edge():
	return (x + d - 1) // d + y // d

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


import sys, math
t = int(sys.stdin.readline())
INF = 10000000000.0
for i in range(t):
	[n, x, y, d] = map(int, sys.stdin.readline().split())
	if (y - x) % d == 0:
		print(abs(y - x) // d)
	else:
		[_1_to_y, n_to_y] = [(y - 1) // d if (y - 1) % d == 0 else INF, (n - y) // d if (n - y) % d == 0 else INF]
		if [_1_to_y, n_to_y] == [INF, INF]:
			print(-1)
		else:
			print(min(_1_to_y + math.ceil((x - 1) / d), n_to_y + math.ceil((n - x) / d)))


INF = 10 ** 20
for _ in range(int(input())):
	(n, x, y, d) = map(int, input().split())
	ans = [INF]
	if abs(x - y) % d == 0:
		ans.append(abs(x - y) // d)
	xback = (x - 1) // d + ((x - 1) % d != 0)
	if (y - 1) % d == 0:
		ans.append(xback + (y - 1) // d)
	xforward = (n - x) // d + ((n - x) % d != 0)
	if (n - y) % d == 0:
		ans.append(xforward + (n - y) // d)
	print(-1 if len(ans) == 1 else min(ans))


import math
for tt in range(int(input())):
	(n, x, y, d) = map(int, input().split())
	ans = []
	r = abs(x - y)
	if r % d == 0:
		ans.append(r // d)
	r2 = math.ceil((x - 1) / d)
	if abs(y - 1) % d == 0:
		ans.append(r2 + abs((y - 1) // d))
	r3 = math.ceil((n - x) / d)
	if (n - y) % d == 0:
		ans.append(r3 + (n - y) // d)
	if len(ans) == 0:
		print('-1')
	else:
		print(min(ans))


count = int(input())
for _ in range(count):
	(p_all, p_st, p_fin, p_count) = map(int, input().split())
	b1 = (p_all - p_fin) % p_count == 0
	b2 = (p_fin - 1) % p_count == 0
	b3 = (p_fin - p_st) % p_count == 0
	answ = 0
	if b3:
		answ = abs((p_fin - p_st) // p_count)
	else:
		a = (p_st + p_count - 2) // p_count + (p_fin - 1) // p_count
		b = (p_all - p_st + p_count - 1) // p_count + (p_all - p_fin) // p_count
		if b1 and b2:
			answ = abs(min(a, b))
		elif b1:
			answ = abs(b)
		elif b2:
			answ = abs(a)
		else:
			answ = -1
	print(answ)


t = int(input())
for i in range(t):
	(n, x, y, d) = map(int, input().split())
	res = float('inf')
	if y - x >= 0 and (y - x) % d == 0:
		res = min(res, (y - x) // d)
	if x - y >= 0 and (x - y) % d == 0:
		res = min(res, (x - y) // d)
	if y - 1 >= 0 and (y - 1) % d == 0:
		mv = (x - 1) // d
		if (x - 1) % d:
			mv += 1
		res = min(res, mv + (y - 1) // d)
	if (n - y) % d == 0:
		mv = (n - x) // d
		if (n - x) % d:
			mv += 1
		res = min(res, mv + (n - y) // d)
	print(res if res != float('inf') else -1)


m = int(input())
for i in range(m):
	(n, x, y, d) = map(int, input().split())
	ans = 10000000000
	if y % d == x % d:
		ans = abs(x - y) // d
	if y % d == 1:
		ans = min(ans, (x - 2) // d + 1 + (y - 1) // d)
	if n % d == y % d:
		ans = min(ans, (n - x - 1) // d + 1 + (n - y) // d)
	if ans == 10000000000:
		ans = -1
	print(ans)


from math import ceil
n = int(input())
for i in range(n):
	(n, x, y, d) = map(int, input().split())
	ans1 = ans2 = 10 ** 18
	dist = abs(x - y)
	if dist % d == 0:
		print(dist // d)
	elif (y - 1) % d and (n - y) % d:
		print(-1)
	else:
		if (y - 1) % d == 0:
			ans1 = (y - 1) // d + (x - 1) // d + (1 if (x - 1) % d else 0)
		if (n - y) % d == 0:
			ans2 = (n - y) // d + (n - x) // d + (1 if (n - x) % d else 0)
		print(min(ans1, ans2))


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


import math
t = int(input())
n = []
x = []
y = []
d = []
for i in range(t):
	(ntemp, xtemp, ytemp, dtemp) = map(int, input().split())
	n.append(ntemp)
	x.append(xtemp)
	y.append(ytemp)
	d.append(dtemp)
for j in range(t):
	if abs(y[j] - x[j]) % d[j] == 0:
		output = abs(y[j] - x[j]) // d[j]
		print(output)
	elif y[j] == 1 or y[j] == n[j]:
		output = math.ceil(abs(y[j] - x[j]) / d[j])
		print(output)
	elif (y[j] - 1) % d[j] != 0 and (n[j] - y[j]) % d[j] != 0:
		print(-1)
	elif (y[j] - 1) % d[j] != 0 and (n[j] - y[j]) % d[j] == 0:
		output = (n[j] - y[j]) // d[j] + math.ceil((n[j] - x[j]) / d[j])
		print(output)
	elif (y[j] - 1) % d[j] == 0 and (n[j] - y[j]) % d[j] != 0:
		output = (y[j] - 1) // d[j] + math.ceil((x[j] - 1) / d[j])
		print(output)
	elif (y[j] - 1) % d[j] == 0 and (n[j] - y[j]) % d[j] == 0:
		outputleft = (y[j] - 1) // d[j] + math.ceil((x[j] - 1) / d[j])
		outputright = (n[j] - y[j]) // d[j] + math.ceil((n[j] - x[j]) / d[j])
		output = min(outputleft, outputright)
		print(output)


def dist(x, y):
	return (abs(x - y) + d - 1) // d
for _ in range(int(input())):
	(n, x, y, d) = map(int, input().split())
	res = float('inf')
	if (x - y) % d == 0:
		res = min(res, abs(x - y) // d)
	if (y - 1) % d == 0:
		res = min(res, dist(x, 1) + (y - 1) // d)
	if (n - y) % d == 0:
		res = min(res, dist(x, n) + (n - y) // d)
	if res == float('inf'):
		res = -1
	print(res)


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)


for cas in range(int(input())):
	(n, x, y, d) = map(int, input().split())
	Min = 10 ** 15
	t = abs(x - y)
	tan = t // d
	if t % d == 0:
		Min = min(Min, tan)
	elif y == n or y == 1:
		tan += 1
		Min = min(Min, tan)
	t = abs(x - n)
	if t % d == 0:
		t = t // d
	else:
		t = t // d
		t += 1
	tt = abs(n - y)
	if tt % d == 0:
		t += tt // d
		Min = min(Min, t)
	t = abs(1 - x)
	if t % d == 0:
		t = t // d
	else:
		t = t // d
		t += 1
	tt = abs(1 - y)
	if tt % d == 0:
		t += tt // d
		Min = min(Min, t)
	if Min == 10 ** 15:
		print(-1)
	else:
		print(Min)


t = int(input())
for i in range(t):
	inp = input().split()
	n = int(inp[0])
	x = int(inp[1])
	y = int(inp[2])
	d = int(inp[3])
	check = [-1, -1, -1]
	if (y - x) % d == 0:
		check[0] = abs(int((y - x) / d))
	if (y - 1) % d == 0:
		if (x - 1) % d == 0:
			count = int((x - 1) / d)
		else:
			count = int((x - 1) / d)
			count += 1
		count += int((y - 1) / d)
		check[1] = count
	if (n - y) % d == 0:
		if (n - x) % d == 0:
			count = int((n - x) / d)
		else:
			count = int((n - x) / d)
			count += 1
		count += int((n - y) / d)
		check[2] = count
	min_moves = -1
	for j in check:
		if j >= 0:
			if min_moves == -1:
				min_moves = j
			elif j < min_moves:
				min_moves = j
	print(min_moves)


import io
import os
import math
t = int(input())
for tc in range(t):
	(n, x, y, d) = map(int, input().split())
	ans = []
	if abs(x - y) % d == 0:
		ans.append(abs(x - y) // d)
	if (y - 1) % d == 0:
		goback = math.ceil((x - 1) / d)
		goforward = (y - 1) // d
		ans.append(goback + goforward)
	if (-1 * y + n) % d == 0:
		goforward = math.ceil((n - x) / d)
		goback = (n - y) // d
		ans.append(goback + goforward)
	if len(ans) == 0:
		print(-1)
		continue
	print(min(ans))


from sys import stdin, stdout
import cProfile, math
from collections import Counter
from bisect import bisect_left, bisect, bisect_right
import itertools
from copy import deepcopy
from fractions import Fraction
import sys, threading
sys.setrecursionlimit(10 ** 6)
threading.stack_size(2 ** 27)
printHeap = str()
memory_constrained = False
P = 10 ** 9 + 7
import sys
sys.setrecursionlimit(10000000)

class Operation:

	def __init__(self, name, function, function_on_equal, neutral_value=0):
		self.name = name
		self.f = function
		self.f_on_equal = function_on_equal

def add_multiple(x, count):
	return x * count

def min_multiple(x, count):
	return x

def max_multiple(x, count):
	return x
sum_operation = Operation('sum', sum, add_multiple, 0)
min_operation = Operation('min', min, min_multiple, 1000000000.0)
max_operation = Operation('max', max, max_multiple, -1000000000.0)

class SegmentTree:

	def __init__(self, array, operations=[sum_operation, min_operation, max_operation]):
		self.array = array
		if type(operations) != list:
			raise TypeError('operations must be a list')
		self.operations = {}
		for op in operations:
			self.operations[op.name] = op
		self.root = SegmentTreeNode(0, len(array) - 1, self)

	def query(self, start, end, operation_name):
		if self.operations.get(operation_name) == None:
			raise Exception('This operation is not available')
		return self.root._query(start, end, self.operations[operation_name])

	def summary(self):
		return self.root.values

	def update(self, position, value):
		self.root._update(position, value)

	def update_range(self, start, end, value):
		self.root._update_range(start, end, value)

	def __repr__(self):
		return self.root.__repr__()

class SegmentTreeNode:

	def __init__(self, start, end, segment_tree):
		self.range = (start, end)
		self.parent_tree = segment_tree
		self.range_value = None
		self.values = {}
		self.left = None
		self.right = None
		if start == end:
			self._sync()
			return
		self.left = SegmentTreeNode(start, start + (end - start) // 2, segment_tree)
		self.right = SegmentTreeNode(start + (end - start) // 2 + 1, end, segment_tree)
		self._sync()

	def _query(self, start, end, operation):
		if end < self.range[0] or start > self.range[1]:
			return None
		if start <= self.range[0] and self.range[1] <= end:
			return self.values[operation.name]
		self._push()
		left_res = self.left._query(start, end, operation) if self.left else None
		right_res = self.right._query(start, end, operation) if self.right else None
		if left_res is None:
			return right_res
		if right_res is None:
			return left_res
		return operation.f([left_res, right_res])

	def _update(self, position, value):
		if position < self.range[0] or position > self.range[1]:
			return
		if position == self.range[0] and self.range[1] == position:
			self.parent_tree.array[position] = value
			self._sync()
			return
		self._push()
		self.left._update(position, value)
		self.right._update(position, value)
		self._sync()

	def _update_range(self, start, end, value):
		if end < self.range[0] or start > self.range[1]:
			return
		if start <= self.range[0] and self.range[1] <= end:
			self.range_value = value
			self._sync()
			return
		self._push()
		self.left._update_range(start, end, value)
		self.right._update_range(start, end, value)
		self._sync()

	def _sync(self):
		if self.range[0] == self.range[1]:
			for op in self.parent_tree.operations.values():
				current_value = self.parent_tree.array[self.range[0]]
				if self.range_value is not None:
					current_value = self.range_value
				self.values[op.name] = op.f([current_value])
		else:
			for op in self.parent_tree.operations.values():
				result = op.f([self.left.values[op.name], self.right.values[op.name]])
				if self.range_value is not None:
					bound_length = self.range[1] - self.range[0] + 1
					result = op.f_on_equal(self.range_value, bound_length)
				self.values[op.name] = result

	def _push(self):
		if self.range_value is None:
			return
		if self.left:
			self.left.range_value = self.range_value
			self.right.range_value = self.range_value
			self.left._sync()
			self.right._sync()
			self.range_value = None

	def __repr__(self):
		ans = '({}, {}): {}\n'.format(self.range[0], self.range[1], self.values)
		if self.left:
			ans += self.left.__repr__()
		if self.right:
			ans += self.right.__repr__()
		return ans

def display(string_to_print):
	stdout.write(str(string_to_print) + '\n')

def primeFactors(n):
	factors = dict()
	for i in range(2, math.ceil(math.sqrt(n)) + 1):
		while n % i == 0:
			if i in factors:
				factors[i] += 1
			else:
				factors[i] = 1
			n = n // i
	if n > 2:
		factors[n] = 1
	return factors

def isprime(n):
	if n < 4:
		return True
	if n % 2 == 0:
		return False
	if n % 3 == 0:
		return False
	i = 5
	w = 2
	while i * i <= n:
		if n % i == 0:
			return False
		i += w
		w = 6 - w
	return True

def test_print(*args):
	if testingMode:
		print(args)

def display_list(list1, sep=' '):
	stdout.write(sep.join(map(str, list1)) + '\n')

def get_int():
	return int(stdin.readline().strip())

def get_tuple():
	return map(int, stdin.readline().split())

def get_list():
	return list(map(int, stdin.readline().split()))
memory = dict()

def clear_cache():
	global memory
	memory = dict()

def cached_fn(fn, *args):
	global memory
	if args in memory:
		return memory[args]
	else:
		result = fn(*args)
		memory[args] = result
		return result
TestCases = True
testingMode = False
optimiseForReccursion = False

def main():
	(n, x, y, d) = get_tuple()
	(a1, a2, a3) = (10 ** 12, 10 ** 12, 10 ** 12)
	if abs(x - y) % d == 0:
		a1 = abs(x - y) // d
	if abs(y - 1) % d == 0:
		a2 = math.ceil((x - 1) / d) + math.ceil((y - 1) / d)
	if abs(n - y) % d == 0:
		a3 = math.ceil((n - x) / d) + math.ceil((n - y) / d)
	print(min(a1, a2, a3)) if min(a1, a2, a3) < 10 ** 12 else print(-1)
if TestCases:
	for _ in range(get_int()):
		cProfile.run('main()') if testingMode else main()
else:
	(cProfile.run('main()') if testingMode else main()) if not optimiseForReccursion else threading.Thread(target=main).start()


for _ in range(int(input())):
	(n, x, y, d) = map(int, input().split())
	if abs(x - y) % d == 0:
		print(abs(x - y) // d)
	else:
		mini = float('inf')
		if (y - 1) % d == 0:
			mini = min(mini, (y - 1) // d + -(-(x - 1) // d))
		if (n - y) % d == 0:
			mini = min(mini, (n - y) // d + -(-(n - x) // d))
		if mini == float('inf'):
			print(-1)
		else:
			print(mini)


def nbCoups(n, x, y, d):
	if (x - y) % d == 0:
		return abs(x - y) // d
	if (y - 1) % d != 0 and (n - y) % d != 0:
		return -1
	if (y - 1) % d == 0:
		coups_gauche = (x - 2) // d + 1 + (y - 1) // d
		if (n - y) % d != 0:
			return coups_gauche
		coups_droit = (n - y) // d + (n - x - 1) // d + 1
		return min(coups_gauche, coups_droit)
	else:
		return (n - y) // d + (n - x - 1) // d + 1
t = int(input())
for i in range(t):
	(n, x, y, d) = [int(x) for x in input().split(' ')]
	print(nbCoups(n, x, y, d))


import math, sys
from collections import Counter, defaultdict, deque
from sys import stdin, stdout
input = stdin.readline
li = lambda : list(map(int, input().split()))

def case():
	(n, x, y, d) = li()
	if abs(x - y) % d == 0:
		print(abs(x - y) // d)
	else:
		(p, k) = (-1, -1)
		if (y - 1) % d == 0:
			k = int(math.ceil((x - 1) / d)) + int(math.ceil((y - 1) / d))
		if (n - y) % d == 0:
			p = int(math.ceil((n - x) / d)) + int(math.ceil((n - y) / d))
		if p != -1 and k != -1:
			print(min(p, k))
		elif p != -1 or k != -1:
			print(max(p, k))
		else:
			print(-1)
for _ in range(int(input())):
	case()


def max(a, b):
	a = int(a)
	b = int(b)
	if a < b:
		return b
	return a

def min(a, b):
	a = int(a)
	b = int(b)
	if a < b:
		return a
	return b

def abs(a):
	a = int(a)
	if a < 0:
		return -a
	return a

def sufit(a, b):
	a = int(a)
	b = int(b)
	if a % b == 0:
		return a / b
	return a / b + 1
T = int(input())
while T:
	T -= 1
	(n, x, y, d) = input().split()
	n = int(n)
	x = int(x)
	y = int(y)
	d = int(d)
	roz1 = abs(x - y)
	if roz1 % d == 0:
		print(int(roz1 / d))
		continue
	wynik = 10000000000000
	if (y - 1) % d == 0:
		wynik = min(wynik, (y - 1) / d + sufit(x - 1, d))
	if (n - y) % d == 0:
		wynik = min(wynik, sufit(n - x, d) + (n - y) / d)
	if wynik == 10000000000000:
		print(-1)
		continue
	print(wynik)


import math
t = int(input())
for i in range(t):
	a = 10 ** 90
	b = 10 ** 90
	(n, x, y, d) = [int(x) for x in input().split()]
	if abs(y - x) % d == 0:
		print(abs(x - y) // d)
	else:
		if (y - 1) % d == 0:
			a = math.ceil((x - 1) / d) + math.ceil((y - 1) // d)
		if (n - y) % d == 0:
			b = math.ceil((n - y) / d) + math.ceil((n - x) / d)
		if a != 10 ** 90 or b != 10 ** 90:
			print(min(a, b))
		else:
			print(-1)


for _ in range(int(input())):
	(n, x, y, d) = map(int, input().split())
	if abs(y - x) % d == 0 or y == 1 or y == n:
		print((abs(x - y) + d - 1) // d)
	elif 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)


import sys
t = int(input())
for i in range(t):
	[n, x, y, d] = [int(m) for m in input().split()]
	if (y - x) % d == 0:
		print(abs(y - x) // d)
	elif (y - 1) % d == 0:
		count1 = (y - 1) // d + ((x - 1) // d if (x - 1) % d == 0 else (x - 1) // d + 1)
		count2 = ((n - y) // d if (n - y) % d == 0 else sys.maxsize) + ((n - x) // d if (n - x) % d == 0 else (n - x) // d + 1)
		print(min(count1, count2))
	elif (n - y) % d == 0:
		print(((n - y) // d if (n - y) % d == 0 else sys.maxsize) + ((n - x) // d if (n - x) % d == 0 else (n - x) // d + 1))
	else:
		print(-1)


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


for _ in range(int(input())):
	(n, x, y, d) = map(int, input().split())
	A = []
	if not (x - y) % d:
		A.append(abs(x - y) // d)
	if not (y - 1) % d:
		A.append((x + d - 2) // d + (y - 1) // d)
	if not (n - y) % d:
		A.append((n - x + d - 1) // d + (n - y) // d)
	print(min(A, default=-1))


for i in range(int(input())):
	(n, x, y, d) = map(int, input().split())
	dist = abs(x - y)
	if dist % 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(dist // d)


t = int(input())
for _ in range(t):
	(n, x, y, d) = map(int, input().split())
	if abs(x - y) % d == 0:
		print(abs(x - y) // d)
	else:
		if (y - 1) % d == 0:
			a = (x - 1 + d - 1) // d + (y - 1) // d
		else:
			a = 10 ** 18
		if (n - y) % d == 0:
			b = (n - x + d - 1) // d + (n - y) // d
		else:
			b = 10 ** 18
		ans = min(a, b)
		if ans == 10 ** 18:
			print(-1)
		else:
			print(ans)


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


from math import ceil

def solve(n, x, y, d):
	if (y - x) % d == 0:
		return abs(y - x) // d
	else:
		(l1, l2) = (-1, -1)
		if (y - 1) % d == 0:
			l1 = ceil((x - 1) / d) + (y - 1) // d
		if (n - y) % d == 0:
			l2 = ceil((n - x) / d) + (n - y) // d
		if l1 == -1:
			if l2 == -1:
				return -1
			else:
				return l2
		elif l2 == -1:
			return l1
		else:
			return min(l1, l2)
for _ in range(int(input())):
	(n, x, y, d) = map(int, input().split())
	print(solve(n, x, y, d))


def solve(n, x, y, d):
	if y > n or y < 1:
		print('-1')
	if abs(x - y) % d == 0:
		if y >= x:
			print(str(int((y - x) / d)))
		else:
			print(str(int((x - y) / d)))
		return
	else:
		temp1 = int((y - 1) / d) + int((x - 1) / d) + 1
		temp2 = int((n - y) / d) + int((n - x) / d) + 1
		if (y - 1) % d == 0 and (n - y) % d == 0:
			print(str(int(min(temp1, temp2))))
			return
		elif (y - 1) % d == 0:
			print(str(temp1))
		elif (n - y) % d == 0:
			print(str(temp2))
		else:
			print('-1')
		return
t = input()
temp = []
for i in range(int(t)):
	temp.append(input())
for case in temp:
	tmp = case.split(' ')
	n = int(tmp[0])
	x = int(tmp[1])
	y = int(tmp[2])
	d = int(tmp[3])
	solve(n, x, y, d)


from math import inf
for _ in range(int(input())):
	(n, x, y, d) = map(int, input().split())
	res = -1
	if not abs(y - x) % d:
		res = abs(y - x) // d
	else:
		temp = inf
		if not (n - y) % d:
			temp = (n - x) // d + 1 + (n - y) // d
		if not (y - 1) % d:
			temp = min(temp, (x - 1) // d + 1 + (y - 1) // d)
		if temp != inf:
			res = temp
	print(res)


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


t = int(input())
answer = []
for u in range(t):
	(n, x, y, d) = [int(i) for i in input().split()]
	(a1, b1) = divmod(abs(y - x), d)
	if b1 != 0:
		a1 = -1

	def get(x, y, d):
		(a2, b2) = divmod(y - 1, d)
		if b2 == 0:
			(p, q) = divmod(x - 1, d)
			if q == 0:
				a2 += p
			else:
				a2 += p + 1
		else:
			a2 = -1
		return a2
	a2 = get(x, y, d)
	(x, y) = (n - x + 1, n - y + 1)
	a3 = get(x, y, d)
	for i in sorted([a1, a2, a3]):
		if i != -1:
			answer.append(i)
			break
	else:
		answer.append(-1)
for k in answer:
	print(k)


import sys
r = lambda : sys.stdin.readline()
fir = int(input())
for ref in range(fir):
	(n, x, y, d) = map(int, r().split())
	ans = []
	if (y - x) % d == 0:
		ans += [int(abs(y - x) / d)]
	up_stair = int((n - x) / d)
	if (n - x) % d != 0:
		up_stair += 1
	if (y - n) % d == 0:
		ans += [int(abs(y - n) / d) + up_stair]
	down_stair = int(x / d)
	if x % d != 0:
		down_stair += 1
	if (y - 1) % d == 0:
		ans += [int(abs(y - 1) / d) + down_stair]
	if ans == []:
		print(-1)
	else:
		print(min(ans))


import math
t = int(input())
while t > 0:
	(n, x, y, d) = tuple(map(int, input().split()))
	ans = 11000000000000000
	ans1 = 1100000000000000
	ans2 = 1000000000000000
	f = False
	if y > x:
		if (y - x) % d == 0:
			ans = (y - x) // d
			f = True
	elif y <= x:
		if (x - y) % d == 0:
			ans = (x - y) // d
			f = True
	if (n - y) % d == 0:
		ans1 = math.ceil((n - x) / d) + math.ceil((n - y) // d)
		f = True
	if (y - 1) % d == 0:
		ans2 = math.ceil((x - 1) / d) + math.ceil((y - 1) / d)
		f = True
	if not f:
		print(-1)
		t = t - 1
		continue
	print(min(ans1, ans2, ans))
	t = t - 1


from sys import stdin, stdout
import math
T = int(input())
for i in range(T):
	(n, x, y, d) = [int(x) for x in stdin.readline().split()]
	if x >= y and (x - y) % d == 0:
		print((x - y) // d)
		continue
	elif y >= x and (y - x) % d == 0:
		print((y - x) // d)
		continue
	res = -1
	b = (x - 1) // d + 1 * ((x - 1) % d != 0)
	if (y - 1) % d == 0:
		r1 = (y - 1) // d + b
		res = r1
	f = (n - x) // d + 1 * ((n - x) % d != 0)
	if (n - y) % d == 0:
		r2 = (n - y) // d + f
		if res == -1:
			res = r2
		else:
			res = min(res, r2)
	if res == -1:
		print(-1)
	else:
		print(res)


from math import ceil

def solve(n, x, y, d):
	if x == y:
		return 0
	if abs(y - x) % d == 0:
		return abs(y - x) // d
	zl = zr = 1000000000000.0
	if (n - y) % d == 0:
		zr = ceil((n - x) / d) + ceil((n - y) / d)
	if (y - 1) % d == 0:
		zl = ceil((x - 1) / d) + ceil((y - 1) / d)
	if zl == 1000000000000.0 and zr == 1000000000000.0:
		return -1
	return min(zl, zr)
t = int(input())
for _ in range(t):
	(n, x, y, d) = map(int, input().split())
	print(solve(n, x, y, d))


import math
t = int(input())

def fn():
	(n, x, y, d) = map(int, input().split())
	z = abs(int(x - y))
	if z % d == 0:
		print(z // d)
		return
	front = math.ceil((n - x) / d) + math.ceil((n - y) / d)
	end = math.ceil((x - 1) / d) + math.ceil((y - 1) / d)
	c1 = (n - y) % d
	c2 = (y - 1) % d
	if c1 != 0 and c2 != 0:
		print(-1)
	elif c1 == 0 and c2 != 0:
		print(front)
	elif c1 != 0 and c2 == 0:
		print(end)
	else:
		print(min(front, end))
while t:
	t -= 1
	fn()


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)


t = int(input())
for _ in range(t):
	(n, x, y, d) = map(int, input().split())
	xmy = abs(x - y)
	xpy = x + y
	if xmy % d == 0:
		print(xmy // d)
	elif y % d == 1:
		if n % d == 1:
			print(min((xpy + d - 3) // d, (n * 2 + d - 1 - xpy) // d))
		else:
			print((xpy + d - 3) // d)
	elif (n - y) % d == 0:
		print((n * 2 + d - 1 - xpy) // d)
	else:
		print('-1')


import math
t = int(input())
for i in range(t):
	(n, x, y, d) = map(int, input().split())
	if abs(x - y) % d == 0:
		print(abs(x - y) // d)
		continue
	if (y - 1) % d == 0:
		left = True
	else:
		left = False
	if (n - y) % d == 0:
		right = True
	else:
		right = False
	if left:
		if right:
			print(min((math.ceil((x - 1) / d) + (y - 1) // d, math.ceil((n - x) / d) + (n - y) // d)))
		else:
			print(math.ceil((x - 1) / d) + (y - 1) // d)
		continue
	if right:
		print(math.ceil((n - x) / d) + (n - y) // d)
		continue
	print(-1)


t = int(input())
while t > 0:
	t = t - 1
	(n, x, y, d) = map(int, input().split())
	ans = 1e+18
	if abs(y - x) % d == 0:
		ans = min(abs(y - x) // d, ans)
	if (y - 1) % d == 0:
		ans = min((y - 1) // d + (x - 1 + d - 1) // d, ans)
	if (n - y) % d == 0:
		ans = min((n - y) // d + (n - x + d - 1) // d, ans)
	if ans == 1e+18:
		print(-1)
	else:
		print(ans)


import math
t = int(input())
for i in range(t):
	(n, x, y, d) = map(int, input().strip().split())
	diff = abs(x - y)
	if diff % d == 0:
		print(diff // d)
	else:
		l = abs(x - 1)
		r = abs(n - x)
		l_steps = l / d
		r_steps = r / d
		if l_steps != int(l_steps):
			l_steps = int(l_steps) + 1
		if r_steps != int(r_steps):
			r_steps = int(r_steps) + 1
		diff1 = abs(y - 1)
		diff2 = abs(n - y)
		if diff1 % d == 0 and diff2 % d == 0:
			print(min(int(l_steps) + diff1 // d, int(r_steps) + diff2 // d))
		elif diff1 % d == 0 and diff2 % d != 0:
			print(int(l_steps) + diff1 // d)
		elif diff1 % d != 0 and diff2 % d == 0:
			print(int(r_steps) + diff2 // d)
		else:
			print(-1)


count = int(input())

def goto(n, x, y, d):
	z = -1
	if abs(x - y) % d == 0:
		return int(abs(x - y) / d)
	if (n - y) % d == 0:
		if (n - x) % d == 0:
			z = int((n - x) / d)
		else:
			z = int((n - x) / d) + 1
		z = z + int((n - y) / d)
	if y % d == 1:
		if (x - 1) % d == 0:
			z1 = int((x - 1) / d)
		else:
			z1 = int((x - 1) / d) + 1
		z1 = z1 + int((y - 1) / d)
		if z != -1 and z < z1:
			return z
		return z1
	return z
result = []
for _ in range(count):
	(n, x, y, d) = map(int, input().split())
	result.append(goto(n, x, y, d))
for i in result:
	print(i)


for _ in ' ' * int(input()):
	(n, x, y, d) = map(int, input().split())
	if abs(x - y) % d == 0:
		print(abs(x - y) // d)
	elif (y - 1) % d == (n - y) % d == 0:
		z = 0 if (x - 1) % d == 0 else 1
		z1 = 0 if (n - x) % d == 0 else 1
		print(min(z + (x - 1) // d + (y - 1) // d, z1 + (n - x) // d + (n - y) // d))
	elif (y - 1) % d == 0:
		z = 0 if (x - 1) % d == 0 else 1
		print(z + (x - 1) // d + (y - 1) // d)
	elif (n - y) % d == 0:
		z = 0 if (n - x) % d == 0 else 1
		print(z + (n - x) // d + (n - y) // d)
	else:
		print(-1)


from math import ceil
for _ in range(int(input())):
	(n, x, y, d) = map(int, input().split())
	f = 0
	cand = []
	if (y - x) % d == 0:
		cand.append((y - x) // d)
		f = 1
	if (y - 1) % d == 0:
		cand.append(ceil((x - 1) / d) + ceil((y - 1) / d))
		f = 1
	if (n - y) % d == 0:
		cand.append(ceil((n - x) / d) + ceil((n - y) / d))
		f = 1
	if f == 0:
		print(-1)
	else:
		cand = [abs(i) for i in cand]
		print(min(cand))


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


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


n = int(input())
for _ in range(n):
	(lastpage, frompage, topage, skip) = (int(i) for i in input().split())
	if (topage - frompage) % skip == 0:
		print(abs(topage - frompage) // skip)
	else:
		presses = []
		if (topage - 1) % skip == 0:
			presses.append((frompage - 2 + skip) // skip + (topage - 1) // skip)
		if (lastpage - topage) % skip == 0:
			presses.append((lastpage - 1 - frompage + skip) // skip + (lastpage - topage) // skip)
		if presses:
			print(min(presses))
		else:
			print(-1)


from math import ceil
ncases = int(input())
for i in range(ncases):
	(page_num, start, desire, d) = list(map(int, input().split()))
	if page_num % d != desire % d and desire % d != 1 and (start % d != desire % d):
		print(-1)
	elif start % d == desire % d:
		print(abs(start - desire) // d)
	elif desire % d == 1 and page_num % d != desire % d:
		print(ceil((start - 1) / d) + (desire - 1) // d)
	elif desire % d != 1 and page_num % d == desire % d:
		print(ceil((page_num - start) / d) + (page_num - desire) // d)
	elif desire % d == 1 and page_num % d == desire % d:
		print(min(ceil((page_num - start) / d) + (page_num - desire) // d, ceil((start - 1) / d) + (desire - 1) // d))


import math
for N in range(int(input())):
	entry = input().split(' ')
	n = int(entry[0])
	x = int(entry[1])
	y = int(entry[2])
	d = int(entry[3])
	if y == x:
		print(0)
	elif y < x:
		if (x - y) % d == 0:
			print(int((x - y) / d))
		else:
			a = -1
			b = -1
			if (y - 1) % d == 0:
				a = int((y - 1) / d + math.ceil((x - 1) / d))
			if (n - y) % d == 0:
				b = int((n - y) / d + math.ceil((n - x) / d))
			if a != -1 and b != -1:
				if a < b:
					print(a)
				else:
					print(b)
			elif a != -1:
				print(a)
			elif b != -1:
				print(b)
			else:
				print(-1)
	elif (y - x) % d == 0:
		print(int((y - x) / d))
	else:
		a = -1
		b = -1
		if (y - 1) % d == 0:
			a = int((y - 1) / d + math.ceil((x - 1) / d))
		if (n - y) % d == 0:
			b = int((n - y) / d + math.ceil((n - x) / d))
		if a != -1 and b != -1:
			if a < b:
				print(a)
			else:
				print(b)
		elif a != -1:
			print(a)
		elif b != -1:
			print(b)
		else:
			print(-1)


def ceil(a, b):
	return -(-a // b)
t = int(input())
for i in range(t):
	(n, x, y, d) = map(int, input().split())
	if abs(x - y) % d == 0:
		print(abs(x - y) // d)
	elif abs(y - 1) % d and abs(y - n) % d:
		print(-1)
	else:
		val1 = val2 = 1 << 30
		if abs(y - 1) % d == 0:
			val1 = ceil(abs(x - 1), d) + abs(1 - y) // d
		if abs(y - n) % d == 0:
			val2 = ceil(abs(x - n), d) + abs(y - n) // d
		print(min(val1, val2))


t = int(input())
from math import ceil, floor
while t > 0:
	t -= 1
	(n, a, b, d) = eval(input().replace(' ', ','))
	options = []
	delta = max(a, b) - min(a, b)
	if delta % d == 0:
		options.append(delta // d)
	to_first = ceil((a - 1) / d)
	to_last = ceil((n - a) / d)
	if (b - 1) % d == 0:
		options.append((b - 1) // d + to_first)
	if (n - b) % d == 0:
		options.append((n - b) // d + to_last)
	if len(options) > 0:
		print(min(options))
	else:
		print(-1)


import sys
sys.setrecursionlimit(2000)
from collections import Counter
from math import ceil

def solution(n, x, y, d):
	diff = abs(x - y)
	if diff % d == 0:
		print(distance(x, y, d))
	else:
		diff1 = abs(1 - y)
		diff2 = abs(n - y)
		if diff1 % d != 0 and diff2 % d != 0:
			print(-1)
		elif diff1 % d == 0 and diff2 % d != 0:
			print(distance(x, 1, d) + distance(1, y, d))
		elif diff1 % d != 0 and diff2 % d == 0:
			print(distance(x, n, d) + distance(n, y, d))
		else:
			dist1 = distance(x, 1, d) + distance(1, y, d)
			dist2 = distance(x, n, d) + distance(n, y, d)
			print(min(dist1, dist2))

def distance(x, y, d):
	return int(ceil(abs(x - y) / float(d)))
q = [int(val) for val in input().split()][0]
for x in range(q):
	(n, x, y, d) = [int(val) for val in input().split()]
	solution(n, x, y, d)


INF = 10 ** 10

def solve():
	(n, x, y, d) = map(int, input().split())
	ans = INF
	if abs(y - x) % d == 0:
		ans = min(ans, abs(y - x) // d)
	if (n - y) % d == 0:
		t = (n - x + d - 1) // d
		t += (n - y) // d
		ans = min(ans, t)
	if (y - 1) % d == 0:
		t = (x - 1 + d - 1) // d
		t += (y - 1) // d
		ans = min(ans, t)
	if ans == INF:
		ans = -1
	return ans

def main():
	t = int(input())
	for i in range(t):
		print(solve())
main()


import math

def dist(x, y):
	return (abs(x - y) + (d - 1)) // d
t = int(input())
for _ in range(t):
	(n, x, y, d) = map(int, input().split())
	len = abs(x - y)
	res = math.inf
	if len % d == 0:
		res = min(res, dist(x, y))
	len = y - 1
	if len % d == 0:
		res = min(res, dist(x, 1) + dist(1, y))
	len = n - y
	if len % d == 0:
		res = min(res, dist(x, n) + dist(n, y))
	if res == math.inf:
		res = -1
	print(res)


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


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


t = int(input())
for i in range(t):
	(n, x, y, d) = (int(i) for i in input().split())
	razn = abs(x - y)
	if y == 1 or y == n:
		print(razn // d + (razn % d != 0))
	elif razn % d == 0:
		print(razn // d)
	else:
		tostart = x - 1
		toend = n - x
		tostartsteps = tostart // d + (tostart % d != 0)
		toendsteps = toend // d + (toend % d != 0)
		startrazn = y - 1
		endrazn = n - y
		startsteps = -1
		endsteps = -1
		if startrazn % d == 0:
			startsteps = startrazn // d
		if endrazn % d == 0:
			endsteps = endrazn // d
		if startsteps != -1:
			if endsteps != -1:
				print(min(startsteps + tostartsteps, endsteps + toendsteps))
			else:
				print(startsteps + tostartsteps)
		elif endsteps != -1:
			print(endsteps + toendsteps)
		else:
			print(-1)


from math import ceil
for _ in range(int(input())):
	(n, x, y, d) = map(int, input().split())
	ok = False
	ans = 10000000000.0
	if (y - x) % d == 0:
		ans = min(ans, int(ceil(abs(y - x)) / d))
		ok = True
	if (y - 1) % d == 0:
		ok = True
		ans = min(ans, int((y - 1) / d + ceil((x - 1) / d)))
	if (n - y) % d == 0:
		ok = True
		ans = min(ans, int((n - y) / d + ceil((n - x) / d)))
	if not ok:
		print(-1)
	else:
		print(ans)


def solve(n, start, end, step):
	if start % step != end % step and 1 != end % step and (n % step != end % step):
		return -1
	ans = 900000000000
	if start % step == end % step:
		ans = min(ans, abs(start - end) // step)
	if 1 == end % step:
		ans = min(ans, (start + step - 2) // step + abs(end - 1) // step)
	if n % step == end % step:
		ans = min(ans, (n - start + step - 1) // step + abs(n - end) // step)
	return ans
t = int(input())
for i in range(t):
	(n, start, end, step) = list(map(int, input().split()))
	print(solve(n, start, end, step))


from sys import stdin

def input():
	return stdin.readline()
for _ in range(int(input())):
	(n, x, y, d) = map(int, input().split())
	if (max(x, y) - min(x, y)) % d == 0:
		print((max(x, y) - min(x, y)) // d)
	else:
		diff1 = y - 1
		diff2 = n - y
		if x % d > 1:
			take = 1
		else:
			take = 0
		ans1 = take + x // d
		if x % d > 1:
			take = 1
		else:
			take = 0
		if (n - x) % d > 0:
			take = 1
		else:
			take = 0
		ans2 = (n - x) // d + take
		if diff1 % d == 0 and diff2 % d == 0:
			print(min(ans1 + diff1 // d, ans2 + diff2 // d))
		elif diff1 % d == 0:
			print(ans1 + diff1 // d)
		elif diff2 % d == 0:
			print(ans2 + diff2 // d)
		else:
			print(-1)


import math
t = int(input())
while t:
	t -= 1
	(n, x, y, d) = map(int, input().split())
	if y >= 1 and y <= n and (x >= 1) and (x <= n):
		ans = -5
		if abs(x - y) % d == 0:
			ans = abs(x - y) // d
		elif (y - 1) % d != 0 and (n - y) % d != 0:
			ans = -1
		else:
			if (y - 1) % d == 0:
				ans = (y - 1) // d + math.ceil((x - 1) / d)
			if (n - y) % d == 0:
				a2 = (n - y) // d + math.ceil((n - x) / d)
				if ans != -5:
					ans = min(ans, a2)
				else:
					ans = a2
		print(int(ans))
	else:
		print(-1)


tcase = int(input().strip())
for case in range(tcase):
	(n, x, y, d) = map(int, input().strip().split())
	if (y - x) % d == 0:
		print(abs(y - x) // d)
	elif (y - 1) % d == 0 and (n - y) % d == 0:
		if (x - 1) % d == 0:
			a = 0
		else:
			a = 1
		if (n - x) % d == 0:
			b = 0
		else:
			b = 1
		print(min((y - 1) // d + a + (x - 1) // d, (n - y) // d + b + (n - x) // d))
	elif (y - 1) % d == 0:
		if (x - 1) % d == 0:
			a = 0
		else:
			a = 1
		print((y - 1) // d + a + (x - 1) // d)
	elif (n - y) % d == 0:
		if (n - x) % d == 0:
			b = 0
		else:
			b = 1
		print((n - y) // d + b + (n - x) // d)
	else:
		print('-1')


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


from math import ceil
t = int(input())
for _ in range(t):
	(n, x, y, d) = map(int, input().split())
	if x % d == y % d:
		print(abs(x - y) // d)
		continue
	v1 = None
	if 1 == y % d:
		v1 = ceil((x - 1) / d) + (y - 1) // d
	v2 = None
	if n % d == y % d:
		v2 = ceil((n - x) / d) + (n - y) // d
	if v1 is not None and v2 is not None:
		print(min(v1, v2))
	elif v1 is not None:
		print(v1)
	elif v2 is not None:
		print(v2)
	else:
		print(-1)


t = int(input())
ans = []
for _ in range(t):
	(n, x, y, d) = map(int, input().split(' '))
	a = abs(y - x)
	m1 = a % d
	temp_ans = -1
	if m1 == 0:
		temp_ans = a // d
	else:
		abx = x - 1
		afx = n - x
		aby = y - 1
		afy = n - y
		mby = aby % d
		mfy = afy % d
		if mby > 0 and mfy > 0:
			temp_ans = -1
		elif mby == 0 and mfy > 0:
			temp_ans = aby // d + abx // d + 1
		elif mby > 0 and mfy == 0:
			temp_ans = afy // d + afx // d + 1
		elif mby == 0 and mfy == 0:
			temp_ans = min(aby // d + abx // d + 1, afy // d + afx // d + 1)
	ans.append(temp_ans)
print(*ans, sep='\n')


def div(a, b):
	if a % b:
		return a // b + 1
	return a // b
for _ in range(int(input())):
	(n, x, y, d) = [int(i) for i in input().split()]
	if not (x - y) % d:
		print(abs((x - y) // d))
	elif not (y - 1) % d and (not (n - y) % d):
		print(min(abs(div(x - 1, d)) + abs((y - 1) // d), abs(div(n - x, d)) + abs((n - y) // d)))
	elif not (y - 1) % d:
		print(abs((y - 1) // d) + abs(div(x - 1, d)))
	elif not (n - y) % d:
		print(abs((n - y) // d) + abs(div(n - x, d)))
	else:
		print(-1)


from math import ceil
T = int(input())
for _ in range(T):
	(n, x, y, d) = map(int, input().split())
	d1 = d2 = d3 = 10000000000.0
	if abs(y - x) % d == 0:
		d1 = abs(y - x) // d
	if (y - 1) % d == 0:
		d2 = (y - 1) // d
		d2 += ceil((x - 1) / d)
	if (n - y) % d == 0:
		d3 = (n - y) // d
		d3 += ceil((n - x) / d)
	l = [d1, d2, d3]
	if all((x == 10000000000.0 for x in l)):
		print(-1)
	else:
		print(min(l))


def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(mi())
INF = 10 ** 18
for t in range(ii()):
	(n, x, y, d) = mi()
	ans = INF
	if x % d == y % d:
		ans = min(ans, abs(y - x) // d)
	if 1 % d == y % d:
		ans = min(ans, (x - 1 + d - 1) // d + (y - 1) // d)
	if n % d == y % d:
		ans = min(ans, (n - x + d - 1) // d + (n - y) // d)
	print(-1 if ans == INF else ans)


import sys

def mp():
	return list(map(int, input().split()))

def main():
	INF = int(1e+18)
	q = int(input())
	for i in range(q):
		(n, x, y, d) = mp()
		if abs(y - x) % d == 0:
			print(abs(y - x) // d)
		else:
			ans1 = INF
			if (n - y) % d == 0:
				ans1 = (n - y) // d + (n - x - 1) // d + 1
			if (y - 1) % d == 0:
				ans1 = min(ans1, (y - 1) // d + (x - 2) // d + 1)
			if ans1 == INF:
				print(-1)
			else:
				print(ans1)
koala = 0
if koala:
	file = open('input.txt', 'r')
	input = file.readline
else:
	input = sys.stdin.readline
main()
if koala:
	file.close()


from math import ceil
T = int(input())
for i in range(0, T):
	(n, x, y, d) = [*map(int, input().split())]
	if y == 1 or y == n:
		print(int(ceil(abs(x - y) / d)))
	elif abs(x - y) % d == 0:
		print(abs(x - y) // d)
	elif (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)


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


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


for _ in range(int(input())):
	(n, x, y, d) = [int(x) for x in input().split()]
	v1 = 0
	v2 = 0
	flag = 0
	if (x - y) % d == 0:
		flag = 1
	if (y - 1) % d == 0:
		if x % d == 0:
			v1 = (y - 1) // d + x // d
		else:
			v1 = (y - 1) // d + x // d + 1
	if (n - y) % d == 0:
		if (n - x) % d == 0:
			v2 = (n - x) // d + (n - y) // d
		else:
			v2 = (n - x) // d + 1 + (n - y) // d
	if flag == 1:
		print(abs(x - y) // d)
	elif v1 == v2 == 0:
		print(-1)
	elif v1 == 0:
		print(v2)
	elif v2 == 0:
		print(v1)
	else:
		print(min(v1, v2))


t = int(input())
for _ in range(t):
	(n, x, y, d) = [int(x) for x in input().split()]
	if abs(y - x) % d == 0:
		print(int(abs(y - x) / d))
	else:
		ans = -1
		if (y - 1) % d == 0:
			ans = (y - 1) / d + int((x - 1) / d) + 1
		if (n - y) % d == 0:
			sub_ans = (n - y) / d + int((n - x) / d) + 1
			if ans == -1 or sub_ans < ans:
				ans = sub_ans
		print(int(ans))


import math

def find_min_dist(n, x, y, d):
	min_dist = 10 ** 10
	if (y - 1) % d == 0:
		curr_dist = math.ceil((x - 1) / d) + (y - 1) // d
		min_dist = curr_dist
	if (n - y) % d == 0:
		curr_dist = math.ceil((n - x) / d) + (n - y) // d
		min_dist = min(min_dist, curr_dist)
	if abs(y - x) % d == 0:
		curr_dist = abs(y - x) // d
		min_dist = min(min_dist, curr_dist)
	if min_dist == 10 ** 10:
		return -1
	else:
		return min_dist
n = int(input())
for i in range(0, n):
	(n, x, y, d) = list(map(int, input().split()))
	min_dist = find_min_dist(n, x, y, d)
	print(min_dist)


from bisect import bisect_right as br
from bisect import bisect_left as bl
from collections import defaultdict
from itertools import combinations
import sys
import math
MAX = sys.maxsize
MAXN = 10 ** 5 + 10
MOD = 10 ** 9 + 7

def isprime(n):
	n = abs(int(n))
	if n < 2:
		return False
	if n == 2:
		return True
	if not n & 1:
		return False
	for x in range(3, int(n ** 0.5) + 1, 2):
		if n % x == 0:
			return False
	return True

def mhd(a, b, x, y):
	return abs(a - x) + abs(b - y)

def numIN():
	return map(int, sys.stdin.readline().strip().split())

def charIN():
	return sys.stdin.readline().strip().split()

def solve(a, b, d, n):
	x = (b - a) / d + 1
	if x == int(x):
		return int(x) - 1
	else:
		p = (b - 1) / abs(d) + 1
		q = (b - n) / -abs(d) + 1
		ans = MAX
		if p == int(p):
			z = (1 - a) / -abs(d) + 1
			z = int(z)
			ans = min(ans, z + p - 1)
		if q == int(q):
			z = (n - a) / abs(d) + 1
			z = int(z)
			ans = min(ans, z + q - 1)
		if ans == MAX:
			return -1
		else:
			return int(ans)
for _ in range(int(input())):
	(n, x, y, d) = numIN()
	if x == y:
		print(0)
	elif y > x:
		print(solve(x, y, d, n))
	else:
		print(solve(x, y, -d, n))


from sys import stdin
from math import ceil

def solve(n, x, y, d):
	delta = abs(x - y)
	if delta % d == 0:
		return delta // d
	res = -1
	if (y - 1) % d == 0:
		e = ceil((x - 1) * 1.0 / d)
		res = e + (y - 1) / d
	if (n - y) % d == 0:
		e = ceil((n - x) * 1.0 / d) + (n - y) / d
		if res == -1:
			res = e
		else:
			res = min(res, e)
	return int(res)

def main():
	T = int(stdin.readline())
	for _ in range(T):
		(n, x, y, d) = stdin.readline().strip().split()
		res = solve(int(n), int(x), int(y), int(d))
		print(res)
main()


import sys
from math import ceil
for tc in range(int(input())):
	(n, x, y, d) = map(int, input().split())
	if abs(x - y) % d == 0:
		print(abs(x - y) // d, sep=' ', end='\n', file=sys.stdout, flush=False)
	else:
		p = float('inf')
		q = float('inf')
		if (y - 1) % d == 0:
			p = ceil((x - 1) / d)
			p += (y - 1) // d
		if (n - y) % d == 0:
			q = ceil((n - x) / d)
			q += (n - y) // d
		if p != float('inf') or q != float('inf'):
			print(min(p, q))
		else:
			print(-1)


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


t = int(input())
r = []
for i in range(t):
	(n, x, y, d) = map(int, input().split())
	if abs(y - x) % d == 0:
		temp = abs(y - x) // d
		r.append(str(temp))
	elif (n - y) % d == 0 and (y - 1) % d == 0:
		temp1 = (y - 1) // d
		tempn = (n - y) // d
		if (x - 1) % d == 0:
			temp1 += (x - 1) // d
		else:
			temp1 += (x - 1) // d + 1
		if (n - x) % d == 0:
			tempn += (n - x) // d
		else:
			tempn += (n - x) // d + 1
		temp = min(temp1, tempn)
		r.append(str(temp))
	elif (y - 1) % d == 0:
		temp = (y - 1) // d
		if (x - 1) % d == 0:
			temp += (x - 1) // d
		else:
			temp += (x - 1) // d + 1
		r.append(str(temp))
	elif (n - y) % d == 0:
		temp = (n - y) // d
		if (n - x) % d == 0:
			temp += (n - x) // d
		else:
			temp += (n - x) // d + 1
		r.append(str(temp))
	else:
		r.append(str(-1))
print('\n'.join(r))


for t in range(int(input())):
	(n, s, f, d) = map(int, input().split())
	(x1, x2, x3) = (-1, -1, -1)
	if abs(f - s) % d == 0:
		x1 = abs(f - s) // d
	if (f - 1) % d == 0:
		x2 = (s - 2) // d + 1 + (f - 1) // d
	if (n - f) % d == 0:
		x3 = (n - s - 1) // d + 1 + (n - f) // d
	A = []
	if x1 != -1:
		A += [x1]
	if x2 != -1:
		A += [x2]
	if x3 != -1:
		A += [x3]
	if A == []:
		print(-1)
	else:
		print(min(A))


import math
t = int(input())
for _ in range(t):
	(n, x, y, d) = map(int, input().split(' '))
	if x == y:
		print(0)
	elif abs(x - y) % d == 0:
		print(int(abs(x - y) / d))
	elif y == n:
		print(math.ceil((n - x) / d))
	elif y == 1:
		print(math.ceil((x - 1) / d))
	else:
		if (y - 1) % d == 0:
			res1 = math.ceil((x - 1) / d) + int((y - 1) / d)
		else:
			res1 = -1
		if (n - y) % d == 0:
			res2 = math.ceil((n - x) / d) + int((n - y) / d)
		else:
			res2 = -1
		if res1 > 0 and res2 > 0:
			print(min(res1, res2))
		elif res1 < 0:
			print(res2)
		elif res2 < 0:
			print(res1)


n = int(input())
for i in range(n):
	k = 10 ** 10
	l = input().split()
	n = int(l[0])
	x = int(l[1])
	y = int(l[2])
	d = int(l[3])
	if y % d == x % d:
		k = abs(x - y) // d
	if y % d == 1:
		k = min(k, (x - 2) // d + 1 + (y - 1) // d)
	if n % d == y % d:
		k = min(k, (n - x - 1) // d + 1 + (n - y) // d)
	if k == 10 ** 10:
		k = -1
	print(k)


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


test = int(input())
for itest in range(test):
	(n, x, y, d) = map(int, input().split())
	res = int(1000000000.0)
	if (x - y) % d == 0:
		res = min(res, int(abs(x - y) / d))
	if (y - 1) % d == 0:
		res = min(res, int((x - 2) / d + 1 + (y - 1) / d))
	if (n - y) % d == 0:
		res = min(res, int((n - x - 1) / d + 1 + (n - y) / d))
	if res == 1000000000.0:
		print(-1)
	else:
		print(res)


t = int(input())
while t != 0:
	t -= 1
	(n, x, y, d) = input().split()
	(n, x, y, d) = (int(n), int(x), int(y), int(d))
	if abs(x - y) % d == 0:
		print(abs(x - y) // d)
	elif (y - 1) % d != 0 and (n - y) % d != 0:
		print(-1)
	elif (y - 1) % d == 0 and (n - y) % d != 0:
		ans = x // d + min(1, x % d) + (y - 1) // d
		print(ans)
	elif (y - 1) % d != 0 and (n - y) % d == 0:
		ans = (n - x) // d + min(1, (n - x) % d) + (n - y) // d
		print(ans)
	else:
		ans = min(x // d + min(1, x % d) + (y - 1) // d, (n - x) // d + min(1, (n - x) % d) + (n - y) // d)
		print(ans)


q = int(input())
for i in range(q):
	ans = []
	q = True
	(n, x, y, d) = map(int, input().split())
	if abs(x - y) % d == 0:
		ans.append(abs(x - y) // d)
		q = False
	if (y - 1) % d == 0:
		ans.append((y - 1) // d + int((x - 1) / d + 0.99999999999999))
		q = False
	if (n - y) % d == 0:
		ans.append((n - y) // d + int((n - x) / d + 0.99999999999999))
		q = False
	if q:
		ans.append(-1)
	print(min(ans))


def main():
	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:
			a = (x - 1 + d - 1) // d
			b = (n - x + d - 1) // d
			(ans1, ans2) = (12341241341414123412341, 12341241341414123412341)
			if (y - 1) % d == 0:
				ans1 = a + (y - 1) // d
			if (n - y) % d == 0:
				ans2 = b + (n - y) // d
			ans = min(ans1, ans2)
			if ans == 12341241341414123412341:
				print(-1)
			else:
				print(ans)
main()


t = int(input())
ls = []
for j in range(t):
	(n, x, y, d) = [int(i) for i in input().split()]
	if (x - 1) % d == 0:
		p = (x - 1) // d
	else:
		p = (x - 1) // d + 1
	if (n - x) % d == 0:
		q = (n - x) // d
	else:
		q = (n - x) // d + 1
	if max(x - y, y - x) % d == 0:
		ls.append(max(x - y, y - x) // d)
	elif (y - 1) % d == 0 and (n - y) % d == 0:
		ls.append(min(p + (y - 1) // d, q + (n - y) // d))
	elif (y - 1) % d == 0:
		ls.append(p + (y - 1) // d)
	elif (n - y) % d == 0:
		ls.append(q + (n - y) // d)
	else:
		ls.append(-1)
for w in range(t):
	print(ls[w])


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


from math import ceil, sqrt, gcd

def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().strip().split(' '))

def li():
	return list(mi())
t = ii()
while t:
	t -= 1
	(n, x, y, d) = mi()
	if abs(x - y) % d == 0:
		print(abs(x - y) // d)
	elif (y - 1) % d == 0 or (n - y) % d == 0:
		f1 = 0
		f2 = 0
		if (y - 1) % d == 0:
			c1 = (y - 1) // d
			c1 += ceil((x - 1) / d)
			f1 = 1
		if (n - y) % d == 0:
			c2 = (n - y) // d
			c2 += ceil((n - x) / d)
			f2 = 1
		if f1 and f2:
			print(min(c1, c2))
		elif f1:
			print(c1)
		else:
			print(c2)
	else:
		print('-1')


def first(x, y, d):
	if not abs(x - y) % d:
		return abs(x - y) // d
	return 10000000000.0

def second(x, y, d):
	return abs(x - y) // d + int(abs(x - y) % d > 0)
amount = int(input())
for i in range(amount):
	min_ = 10000000000.0
	(n, x, y, d) = [int(s) for s in input().split()]
	min_ = min(min_, first(x, y, d))
	min_ = min(min_, second(x, 1, d) + first(1, y, d))
	min_ = min(min_, second(x, n, d) + first(n, y, d))
	if min_ == 10000000000.0:
		print(-1)
	else:
		print(min_)


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


def inint():
	return int(input())

def inlist():
	return list(map(int, input().split()))

def main():
	for _ in range(inint()):
		(n, x, y, d) = inlist()
		ans = []
		from math import ceil
		if abs(x - y) % d == 0:
			ans.append(abs(x - y) // d)
		if abs(1 - y) % d == 0:
			ans.append(ceil(abs(x - 1) / d) + (y - 1) // d)
		if abs(n - y) % d == 0:
			ans.append(ceil(abs(x - n) / d) + (n - y) // d)
		if len(ans) == 0:
			print(-1)
		else:
			print(min(ans))
main()


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


t = int(input())
for i in range(t):
	bl = False
	(n, x, y, d) = map(int, input().split())
	a = 1000000000
	b = 1000000000
	c = 1000000000
	if y % d == x % d:
		bl = True
		a = abs(x - y) // d
	if y % d == 1:
		bl = True
		b = x // d
		if x % d > 1:
			b += 1
		b += (y - 1) // d
	if y % d == n % d:
		bl = True
		c = (n - x) // d
		if (n - x) % d > 0:
			c += 1
		c += (n - y) // d
	if bl:
		print(min(a, b, c))
	else:
		print(-1)


from math import ceil
n = int(input())
for i in range(n):
	(n, x, y, d) = list(map(int, input().split()))
	max_num = 10 ** 10
	if abs(y - x) % d == 0:
		max_num = abs(y - x) // d
	if y % d == 1:
		a = ceil(x / d) + (y - 1) // d
		if a < max_num:
			max_num = a
	if y % d == n % d:
		a = ceil((n - x) / d) + (n - y + 1) // d
		if a < max_num:
			max_num = a
	if max_num == 10 ** 10:
		print(-1)
	else:
		print(max_num)


import math
t = int(input())
for i in range(t):
	(n, x, y, d) = map(int, input().split())
	mn = 10 ** 9
	if y >= x:
		if (y - x) % d == 0 or y == n or (y - 1) % d == 0 or ((n - y) % d == 0):
			if (y - x) % d == 0 or y == n:
				tp = math.ceil((y - x) / d)
				if tp < mn:
					mn = tp
			if (n - y) % d == 0:
				tp = (n - y) // d + math.ceil((n - x) / d)
				mn = min(tp, mn)
			if (y - 1) % d == 0:
				tp = (y - 1) // d + math.ceil((x - 1) / d)
				mn = min(tp, mn)
			print(mn)
		else:
			print(-1)
	elif (x - y) % d == 0 or y == 1 or (n - y) % d == 0 or ((y - 1) % d == 0):
		if (x - y) % d == 0 or y == 1:
			tp = math.ceil((x - y) / d)
			if tp < mn:
				mn = tp
		if (n - y) % d == 0:
			tp = (n - y) // d + math.ceil((n - x) / d)
			mn = min(tp, mn)
		if (y - 1) % d == 0:
			tp = (y - 1) // d + math.ceil((x - 1) / d)
			mn = min(tp, mn)
		print(mn)
	else:
		print(-1)


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


from math import ceil
for i in range(int(input())):
	(n, x, y, d) = [int(i) for i in input().split()]
	ans = 100000000000.0
	if abs(y - x) % d == 0:
		ans = abs(y - x) // d
	if (y - 1) % d == 0:
		ans = min(ans, (y - 1) // d + ceil((x - 1) / d))
	if (n - y) % d == 0:
		ans = min(ans, (n - y) // d + ceil((n - x) / d))
	if ans == 100000000000.0:
		ans = -1
	print(ans)


__author__ = 'Esfandiar'
from math import ceil
for i in range(int(input())):
	(n, x, y, d) = map(int, input().split())
	if abs(x - y) % d == 0:
		print(abs(x - y) // d)
	else:
		yy = ceil((x - 1) / d)
		f = 0
		if (y - 1) % d == 0:
			f = 1
		yyy = ceil((n - x) / d)
		ff = 0
		if (n - y) % d == 0:
			ff = 1
		if f == 0 and ff == 0:
			print(-1)
		elif f == 1 and ff == 0:
			print(yy + (y - 1) // d)
		elif f == 0 and ff == 1:
			print(yyy + (n - y) // d)
		else:
			print(min(yy + (y - 1) // d, yyy + (n - y) // d))


import sys
t = int(sys.stdin.readline().strip())
for i in range(0, t):
	line = sys.stdin.readline().split()
	n = int(line[0])
	x = int(line[1])
	y = int(line[2])
	d = int(line[3])
	z = -1
	if (x - y) % d == 0:
		z = abs((x - y) // d)
	if (1 - y) % d == 0:
		z2 = (y - 1) // d + (x - 1) // d + 1
		if z != -1:
			z = min(z, z2)
		else:
			z = z2
	if (n - y) % d == 0:
		z3 = (n - y) // d + (n - x) // d + 1
		if z != -1:
			z = min(z, z3)
		else:
			z = z3
	print(z)


t = int(input())
while t:
	t -= 1
	(a, b, c, d) = map(int, input().split())
	if abs(c - b) % d == 0:
		print(int(abs(c - b) / d))
		continue
	else:
		(s, s1) = (999989999899998999989999899998, 999989999899998999989999899998)
		if (c - 1) % d == 0:
			s = int((b - 1) // d + 1 + (c - 1) / d)
		if (a - c) % d == 0:
			s1 = int((a - b) // d + 1 + (a - c) / d)
	if s != 999989999899998999989999899998 or s1 != 999989999899998999989999899998:
		print(min(s, s1))
	else:
		print(-1)


t = int(input())
for i in range(t):
	(n, x, y, d) = map(int, input().split())
	diff = abs(y - x)
	INF = 100000000000000000000000000000000000000000
	answer = INF
	if diff % d == 0:
		answer = int(diff / d)
	if (y - 1) % d == 0:
		total = int((x - 1) / d)
		if (x - 1) % d != 0:
			total += 1
		total += (y - 1) / d
		answer = min(int(total), answer)
	if (n - y) % d == 0:
		total = int((n - x) / d)
		if (n - x) % d != 0:
			total += 1
		total += (n - y) / d
		answer = min(int(total), answer)
	if answer == INF:
		answer = -1
	print(int(answer))


count = int(input())
cases = []
for i in range(count):
	lst = [int(x) for x in input().split()]
	cases.append(lst)
import math

def count_steps(n, x, y, d):
	if not (y - x) % d:
		return dis(x, y, d)
	if (y - 1) % d and (n - y) % d:
		return -1
	(left, right) = (float('inf'), float('inf'))
	if not (y - 1) % d:
		left = dis(x, 1, d) + dis(y, 1, d)
	if not (n - y) % d:
		right = dis(n, x, d) + dis(n, y, d)
	return min(left, right)

def dis(x, y, d):
	return math.ceil(abs(x - y) / d)
for (n, x, y, d) in cases:
	print(count_steps(n, x, y, d))


import math
t = int(input())
for i in range(t):
	(n, x, y, d) = map(int, input().split())
	(r1, r2, r3) = [math.inf] * 3
	if (x - y) % d == 0:
		r1 = abs(x - y) // d
	if y % d == 1:
		r2 = math.ceil(x / d) + (y - 1) // d
	if y % d == n % d:
		r3 = math.ceil((n - x) / d) + (n - y) // d
	r = min(r1, r2, r3)
	if r == math.inf:
		print(-1)
	else:
		print(r)


case = int(input())
for _ in range(case):
	(n, x, y, d) = map(int, input().split())
	if x % d == y % d:
		print(abs(x - y) // d)
	else:
		ans = n
		if y % d == 1:
			ans = min(ans, (y - 1) // d + (x - 2) // d + 1)
		if y % d == n % d:
			ans = min(ans, (n - y) // d + (n - x - 1) // d + 1)
		if ans == n:
			print(-1)
		else:
			print(ans)


import sys
from math import ceil, floor
input = sys.stdin.readline
t = int(input())
for zzz in range(t):
	ans = 0
	(n, x, y, d) = map(int, input().split())
	ans1 = 999999999999
	ans2 = 999999999999
	ans3 = 999999999999
	if (y - x) % d == 0:
		ans1 = abs((y - x) / d)
	if (y - 1) % d == 0:
		ans2 = ceil((x - 1) / d) + (y - 1) / d
	if (n - y) % d == 0:
		ans3 = ceil((n - x) / d) + (n - y) / d
	if ans1 == 999999999999 and ans2 == 999999999999 and (ans3 == 999999999999):
		print(-1)
	else:
		print(int(min(ans1, ans2, ans3)))


import math

def solve(n, x, y, d):
	solutions = []
	if x % d == y % d:
		solutions.append(abs(x - y) // d)
	if y % d == n % d:
		solutions.append(math.ceil((n - x) / d) + (n - y) // d)
	if y % d == 1:
		solutions.append(math.ceil((x - 1) / d) + (y - 1) // d)
	if not solutions:
		return -1
	else:
		return min(solutions)
n = int(input())
for _ in range(n):
	(n, x, y, d) = map(int, input().split())
	print(solve(n, x, y, d))