#include <stdio.h>
#include <stdlib.h>
int main(void)
{
	int testcase;
	scanf("%d", &testcase);
	long a[1001][5];
	for (int i = 1; i <= testcase; i++)
		for (int j = 1; j <= 4; j++)
			scanf("%d", &a[i][j]);

	for (int i = 1; i <= testcase; i++)
	{
		//a[i][1]num of pages
		//a[i][2]stating pages
		//a[i][3]desired pages
		//a[i][4]scrolled once

		int dirstep = (a[i][3] - a[i][2]) / a[i][4];
		if (dirstep < 0)
			dirstep = -dirstep;

		if ((a[i][2] - a[i][3]) % a[i][4] == 0)
		{
			printf("%ld\n", dirstep);
		}
		else
		{
			int goleft1 = 0, goright1 = 0;
			if ((a[i][2] - 1) % a[i][4] == 0)
				goleft1 = (a[i][2] - 1) / a[i][4];
			else
				goleft1 = (a[i][2] - 1) / a[i][4] + 1;
			if ((a[i][1] - a[i][2]) % a[i][4] == 0)
				goright1 = (a[i][1] - a[i][2]) / a[i][4];
			else
				goright1 = (a[i][1] - a[i][2]) / a[i][4] + 1;

			int goleft2 = 0, goright2 = 0;
			if ((a[i][3] - 1) % a[i][4] == 0)
				goleft2 = (a[i][3] - 1) / a[i][4];
			if ((a[i][1] - a[i][3]) % a[i][4] == 0)
				goright2 = (a[i][1] - a[i][3]) / a[i][4];

			if (goright2 == 0 && goleft2 == 0 && (a[i][1] - a[i][3]) != 0 && a[i][3] != 1)
				printf("-1\n");
			else
			{
				if ((goright1+goright2) > (goleft1+goleft2))
					printf("%ld\n", goleft2 + goleft1);
				else
					printf("%ld\n", goright2 + goright1);
			}
		}
	}
	//system("pause");
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
int main(void)
{
	int testcase;
	scanf("%d", &testcase);
	long a[1001][5];
	for (int i = 1; i <= testcase; i++)
		for (int j = 1; j <= 4; j++)
			scanf("%d", &a[i][j]);

	for (int i = 1; i <= testcase; i++)
	{
		//a[i][1]num of pages
		//a[i][2]stating pages
		//a[i][3]desired pages
		//a[i][4]scrolled once

		int dirstep = (a[i][3] - a[i][2]) / a[i][4];
		if (dirstep < 0)
			dirstep = -dirstep;

		if ((a[i][2] - a[i][3]) % a[i][4] == 0)
		{
			printf("%ld\n", dirstep);
		}
		else
		{
			int goleft1 = 0, goright1 = 0;
			if ((a[i][2] - 1) % a[i][4] == 0)
				goleft1 = (a[i][2] - 1) / a[i][4];
			else
				goleft1 = (a[i][2] - 1) / a[i][4] + 1;
			if ((a[i][1] - a[i][2]) % a[i][4] == 0)
				goright1 = (a[i][1] - a[i][2]) / a[i][4];
			else
				goright1 = (a[i][1] - a[i][2]) / a[i][4] + 1;

			int goleft2 = 0, goright2 = 0;
			if ((a[i][3] - 1) % a[i][4] == 0)
				goleft2 = (a[i][3] - 1) / a[i][4];
			if ((a[i][1] - a[i][3]) % a[i][4] == 0)
				goright2 = (a[i][1] - a[i][3]) / a[i][4];

			if (goright2 == 0 && goleft2 == 0 && (a[i][1] - a[i][3]) != 0 && a[i][3] != 1)
				printf("-1\n");
			else
			{
				if (goright2 > goleft2&&goleft2 != 0)
					printf("%ld\n", goleft2 + goleft1);
				if (goleft2 > goright2&&goright2 != 0)
					printf("%ld\n", goright2 + goright1);
				if (goleft2 == 0)
					printf("%ld\n", goright2 + goright1);
				else
					if (goright2 == 0)
					printf("%ld\n", goleft2 + goleft1);
			}
		}
	}
	system("pause");
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
int main(void)
{
	int testcase;
	scanf("%d", &testcase);
	long a[1001][5];
	for (int i = 1; i <= testcase; i++)
		for (int j = 1; j <= 4; j++)
			scanf("%d", &a[i][j]);

	for (int i = 1; i <= testcase; i++)
	{
		//a[i][1]num of pages
		//a[i][2]stating pages
		//a[i][3]desired pages
		//a[i][4]scrolled once

		int dirstep = (a[i][3] - a[i][2]) / a[i][4];
		if (dirstep < 0)
			dirstep = -dirstep;

		if ((a[i][2] - a[i][3]) % a[i][4] == 0)
		{
			printf("%ld\n", dirstep);
		}
		else
		{
			int goleft1 = 0, goright1 = 0;
			if ((a[i][2] - 1) % a[i][4] == 0)
				goleft1 = (a[i][2] - 1) % a[i][4];
			else
				goleft1 = (a[i][2] - 1) % a[i][4] + 1;
			if ((a[i][1] - a[i][2]) % a[i][4] == 0)
				goright1 = (a[i][1] - a[i][2]) % a[i][4];
			else
				goright1 = (a[i][1] - a[i][2]) % a[i][4] + 1;

			int goleft2 = 0, goright2 = 0;
			if ((a[i][3] - 1) % a[i][4] == 0)
				goleft2 = (a[i][3] - 1) / a[i][4];
			if ((a[i][1] - a[i][3]) % a[i][4] == 0)
				goright2 = (a[i][1] - a[i][3]) / a[i][4];

			if (goright2 == 0 && goleft2 == 0)
				printf("-1\n");
			else
			{
				if (goright2 > goleft2&&goleft2 != 0)
					printf("%ld\n", goleft2 + goleft1);
				if (goleft2 > goright2&&goright2 != 0)
					printf("%ld\n", goright2 + goright1);
				if (goleft2 == 0)
					printf("%ld\n", goright2 + goright1);
				if (goright2 == 0)
					printf("%ld\n", goleft2 + goleft1);
			}
		}
	}
	//system("pause");
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
int main(void)
{
	int testcase;
	scanf("%d", &testcase);
	long long a[1001][5];
	long long goleft1 = 0, goright1 = 0;
	long long goleft2 = 0, goright2 = 0;
	long long dirstep;
	int i,j;
	for (i = 1; i <= testcase; i++)
		for (j = 1; j <= 4; j++)
			scanf("%d", &a[i][j]);

	for (i = 1; i <= testcase; i++)
	{
		//a[i][1]num of pages
		//a[i][2]stating pages
		//a[i][3]desired pages
		//a[i][4]scrolled once

		dirstep = (a[i][3] - a[i][2]) / a[i][4];
		if (dirstep < 0)
			dirstep = -dirstep;

		if ((a[i][2] - a[i][3]) % a[i][4] == 0)
		{
			printf("%f\n", dirstep);
		}
		else
		{
			goleft1 = 0, goright1 = 0;
			if ((a[i][2] - 1) % a[i][4] == 0)
				goleft1 = (a[i][2] - 1) % a[i][4];
			else
				goleft1 = (a[i][2] - 1) % a[i][4]+1;
			if ((a[i][1] - a[i][2]) % a[i][4] == 0)
				goright1 = (a[i][1] - a[i][2]) % a[i][4];
			else
				goright1 = (a[i][1] - a[i][2]) % a[i][4]+1;

			goleft2 = 0, goright2 = 0;
			if ((a[i][3] - 1) % a[i][4] == 0)
				goleft2 = (a[i][3] - 1) / a[i][4];
			if ((a[i][1] - a[i][3]) % a[i][4] == 0)
				goright2 = (a[i][1] - a[i][3]) / a[i][4];

			if (goright2 == 0 && goleft2 == 0)
				printf("-1\n");
			else
			{
				if (goright2 > goleft2&&goleft2 != 0)
					printf("%f\n", goleft2 + goleft1);
				if (goleft2 > goright2&&goright2 != 0)
					printf("%f\n", goright2 + goright1);
				if (goleft2 == 0)
					printf("%f\n", goright2 + goright1);
				if (goright2 == 0)
					printf("%f\n", goleft2 + goleft1);
			}
		}
	}
	system("pause");
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main(void)
{
	int testcase;
	scanf("%d", &testcase);
	int a[10][5];
	int goleft1 = 0, goright1 = 0;
	int dirstep;
	int i,j;
	for (i = 1; i <= testcase; i++)
		for (j = 1; j <= 4; j++)
			scanf("%d", &a[i][j]);

	for (i = 1; i <= testcase; i++)
	{
		//a[i][1]num of pages
		//a[i][2]stating pages
		//a[i][3]desired pages
		//a[i][4]scrolled once

		dirstep = (a[i][3] - a[i][2]) / a[i][4];
		if (dirstep < 0)
			dirstep = -dirstep;

		if ((a[i][2] - a[i][3]) % a[i][4] == 0)
		{
			printf("%d\n", dirstep);
		}
		else
		{
			goleft1 = 0, goright1 = 0;
			if ((a[i][2] - 1) % a[i][4] == 0)
				goleft1 = (a[i][2] - 1) % a[i][4];
			else
				goleft1 = (a[i][2] - 1) % a[i][4]+1;
			if ((a[i][1] - a[i][2]) % a[i][4] == 0)
				goright1 = (a[i][1] - a[i][2]) % a[i][4];
			else
				goright1 = (a[i][1] - a[i][2]) % a[i][4]+1;

			int goleft2 = 0, goright2 = 0;
			if ((a[i][3] - 1) % a[i][4] == 0)
				goleft2 = (a[i][3] - 1) / a[i][4];
			if ((a[i][1] - a[i][3]) % a[i][4] == 0)
				goright2 = (a[i][1] - a[i][3]) / a[i][4];

			if (goright2 == 0 && goleft2 == 0)
				printf("-1\n");
			else
			{
				if (goright2 > goleft2&&goleft2 != 0)
					printf("%d\n", goleft2 + goleft1);
				if (goleft2 > goright2&&goright2 != 0)
					printf("%d\n", goright2 + goright1);
				if (goleft2 == 0)
					printf("%d\n", goright2 + goright1);
				if (goright2 == 0)
					printf("%d\n", goleft2 + goleft1);
			}
		}
	}
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
int main(void)
{
	int testcase;
	scanf("%d", &testcase);
	int a[1001][5];
	for (int i = 1; i <= testcase; i++)
		for (int j = 1; j <= 4; j++)
			scanf("%d", &a[i][j]);

	for (int i = 1; i <= testcase; i++)
	{
		//a[i][1]num of pages
		//a[i][2]stating pages
		//a[i][3]desired pages
		//a[i][4]scrolled once

		int dirstep = (a[i][3] - a[i][2]) / a[i][4];
		if (dirstep < 0)
			dirstep = -dirstep;

		if ((a[i][2] - a[i][3]) % a[i][4] == 0)
		{
			printf("%d\n", dirstep);
		}
		else
		{
			int goleft1 = 0, goright1 = 0;
			if ((a[i][2] - 1) % a[i][4] == 0)
				goleft1 = (a[i][2] - 1) % a[i][4];
			else
				goleft1 = (a[i][2] - 1) % a[i][4] + 1;
			if ((a[i][1] - a[i][2]) % a[i][4] == 0)
				goright1 = (a[i][1] - a[i][2]) % a[i][4];
			else
				goright1 = (a[i][1] - a[i][2]) % a[i][4] + 1;

			int goleft2 = 0, goright2 = 0;
			if ((a[i][3] - 1) % a[i][4] == 0)
				goleft2 = (a[i][3] - 1) / a[i][4];
			if ((a[i][1] - a[i][3]) % a[i][4] == 0)
				goright2 = (a[i][1] - a[i][3]) / a[i][4];

			if (goright2 == 0 && goleft2 == 0)
				printf("-1\n");
			else
			{
				if (goright2 > goleft2&&goleft2 != 0)
					printf("%d\n", goleft2 + goleft1);
				if (goleft2 > goright2&&goright2 != 0)
					printf("%d\n", goright2 + goright1);
				if (goleft2 == 0)
					printf("%d\n", goright2 + goright1);
				if (goright2 == 0)
					printf("%d\n", goleft2 + goleft1);
			}
		}
	}
	//system("pause");
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main(void)
{
	int testcase;
	scanf("%d", &testcase);
	long long a[1001][5];
	int goleft1 = 0, goright1 = 0;
	int dirstep;
	int i,j;
	for (i = 1; i <= testcase; i++)
		for (j = 1; j <= 4; j++)
			scanf("%d", &a[i][j]);

	for (i = 1; i <= testcase; i++)
	{
		//a[i][1]num of pages
		//a[i][2]stating pages
		//a[i][3]desired pages
		//a[i][4]scrolled once

		dirstep = (a[i][3] - a[i][2]) / a[i][4];
		if (dirstep < 0)
			dirstep = -dirstep;

		if ((a[i][2] - a[i][3]) % a[i][4] == 0)
		{
			printf("%d\n", dirstep);
		}
		else
		{
			goleft1 = 0, goright1 = 0;
			if ((a[i][2] - 1) % a[i][4] == 0)
				goleft1 = (a[i][2] - 1) % a[i][4];
			else
				goleft1 = (a[i][2] - 1) % a[i][4]+1;
			if ((a[i][1] - a[i][2]) % a[i][4] == 0)
				goright1 = (a[i][1] - a[i][2]) % a[i][4];
			else
				goright1 = (a[i][1] - a[i][2]) % a[i][4]+1;

			int goleft2 = 0, goright2 = 0;
			if ((a[i][3] - 1) % a[i][4] == 0)
				goleft2 = (a[i][3] - 1) / a[i][4];
			if ((a[i][1] - a[i][3]) % a[i][4] == 0)
				goright2 = (a[i][1] - a[i][3]) / a[i][4];

			if (goright2 == 0 && goleft2 == 0)
				printf("-1\n");
			else
			{
				if (goright2 > goleft2&&goleft2 != 0)
					printf("%d\n", goleft2 + goleft1);
				if (goleft2 > goright2&&goright2 != 0)
					printf("%d\n", goright2 + goright1);
				if (goleft2 == 0)
					printf("%d\n", goright2 + goright1);
				if (goright2 == 0)
					printf("%d\n", goleft2 + goleft1);
			}
		}
	}
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
int main(void)
{
	int testcase;
	scanf("%d", &testcase);
	long a[1001][5];
	for (int i = 1; i <= testcase; i++)
		for (int j = 1; j <= 4; j++)
			scanf("%d", &a[i][j]);

	for (int i = 1; i <= testcase; i++)
	{
		//a[i][1]num of pages
		//a[i][2]stating pages
		//a[i][3]desired pages
		//a[i][4]scrolled once

		int dirstep = (a[i][3] - a[i][2]) / a[i][4];
		if (dirstep < 0)
			dirstep = -dirstep;

		if ((a[i][2] - a[i][3]) % a[i][4] == 0)
		{
			printf("%ld\n", dirstep);
		}
		else
		{
			int goleft1 = 0, goright1 = 0;
			if ((a[i][2] - 1) % a[i][4] == 0)
				goleft1 = (a[i][2] - 1) / a[i][4];
			else
				goleft1 = (a[i][2] - 1) / a[i][4] + 1;
			if ((a[i][1] - a[i][2]) % a[i][4] == 0)
				goright1 = (a[i][1] - a[i][2]) / a[i][4];
			else
				goright1 = (a[i][1] - a[i][2]) / a[i][4] + 1;

			int goleft2 = 0, goright2 = 0;
			if ((a[i][3] - 1) % a[i][4] == 0)
				goleft2 = (a[i][3] - 1) / a[i][4];
			else
				goleft2 = -1;
			if ((a[i][1] - a[i][3]) % a[i][4] == 0)
				goright2 = (a[i][1] - a[i][3]) / a[i][4];
			else
				goright2 = -1;

			if (goright2 < 0 && goleft2 < 0)
				printf("-1\n");
			else
			{
				if (goright2 < 0) printf("%ld\n", goleft1 + goleft2);
				else
					if (goleft2 < 0) printf("%ld\n", goright1 + goright2);
				else
					if ((goright1 + goright2) > (goleft1 + goleft2))
					printf("%ld\n", goleft1 + goleft2);
				else
					printf("%ld\n", goright1 + goright2);
			}
		}
	}
	//system("pause");
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
int main(void)
{
	int testcase;
	scanf("%d", &testcase);
	long long a[1001][5];
	long long goleft1 = 0, goright1 = 0;
	long long goleft2 = 0, goright2 = 0;
	long long dirstep;
	int i,j;
	for (i = 1; i <= testcase; i++)
		for (j = 1; j <= 4; j++)
			scanf("%d", &a[i][j]);

	for (i = 1; i <= testcase; i++)
	{
		//a[i][1]num of pages
		//a[i][2]stating pages
		//a[i][3]desired pages
		//a[i][4]scrolled once

		dirstep = (a[i][3] - a[i][2]) / a[i][4];
		if (dirstep < 0)
			dirstep = -dirstep;

		if ((a[i][2] - a[i][3]) % a[i][4] == 0)
		{
			printf("%f\n", dirstep);
		}
		else
		{
			goleft1 = 0, goright1 = 0;
			if ((a[i][2] - 1) % a[i][4] == 0)
				goleft1 = (a[i][2] - 1) % a[i][4];
			else
				goleft1 = (a[i][2] - 1) % a[i][4]+1;
			if ((a[i][1] - a[i][2]) % a[i][4] == 0)
				goright1 = (a[i][1] - a[i][2]) % a[i][4];
			else
				goright1 = (a[i][1] - a[i][2]) % a[i][4]+1;

			goleft2 = 0, goright2 = 0;
			if ((a[i][3] - 1) % a[i][4] == 0)
				goleft2 = (a[i][3] - 1) / a[i][4];
			if ((a[i][1] - a[i][3]) % a[i][4] == 0)
				goright2 = (a[i][1] - a[i][3]) / a[i][4];

			if (goright2 == 0 && goleft2 == 0)
				printf("-1\n");
			else
			{
				if (goright2 > goleft2&&goleft2 != 0)
					printf("%f\n", goleft2 + goleft1);
				if (goleft2 > goright2&&goright2 != 0)
					printf("%f\n", goright2 + goright1);
				if (goleft2 == 0)
					printf("%f\n", goright2 + goright1);
				if (goright2 == 0)
					printf("%f\n", goleft2 + goleft1);
			}
		}
	}
	//system("pause");
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main(void)
{
	int testcase;
	scanf("%d", &testcase);
	int a[1010][5];
	int goleft1 = 0, goright1 = 0;
	int dirstep;
	int i,j;
	for (i = 1; i <= testcase; i++)
		for (j = 1; j <= 4; j++)
			scanf("%d", &a[i][j]);

	for (i = 1; i <= testcase; i++)
	{
		//a[i][1]num of pages
		//a[i][2]stating pages
		//a[i][3]desired pages
		//a[i][4]scrolled once

		dirstep = (a[i][3] - a[i][2]) / a[i][4];
		if (dirstep < 0)
			dirstep = -dirstep;

		if ((a[i][2] - a[i][3]) % a[i][4] == 0)
		{
			printf("%d\n", dirstep);
		}
		else
		{
			goleft1 = 0, goright1 = 0;
			if ((a[i][2] - 1) % a[i][4] == 0)
				goleft1 = (a[i][2] - 1) % a[i][4];
			else
				goleft1 = (a[i][2] - 1) % a[i][4]+1;
			if ((a[i][1] - a[i][2]) % a[i][4] == 0)
				goright1 = (a[i][1] - a[i][2]) % a[i][4];
			else
				goright1 = (a[i][1] - a[i][2]) % a[i][4]+1;

			int goleft2 = 0, goright2 = 0;
			if ((a[i][3] - 1) % a[i][4] == 0)
				goleft2 = (a[i][3] - 1) / a[i][4];
			if ((a[i][1] - a[i][3]) % a[i][4] == 0)
				goright2 = (a[i][1] - a[i][3]) / a[i][4];

			if (goright2 == 0 && goleft2 == 0)
				printf("-1\n");
			else
			{
				if (goright2 > goleft2&&goleft2 != 0)
					printf("%d\n", goleft2 + goleft1);
				if (goleft2 > goright2&&goright2 != 0)
					printf("%d\n", goright2 + goright1);
				if (goleft2 == 0)
					printf("%d\n", goright2 + goright1);
				if (goright2 == 0)
					printf("%d\n", goleft2 + goleft1);
			}
		}
	}
	system("pause");
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
int main(void)
{
	int testcase;
	scanf("%d", &testcase);
	long long a[1001][5];
	long long goleft1 = 0, goright1 = 0;
	long long goleft2 = 0, goright2 = 0;
	long long dirstep;
	int i,j;
	for (i = 1; i <= testcase; i++)
		for (j = 1; j <= 4; j++)
			scanf("%d", &a[i][j]);

	for (i = 1; i <= testcase; i++)
	{
		//a[i][1]num of pages
		//a[i][2]stating pages
		//a[i][3]desired pages
		//a[i][4]scrolled once

		dirstep = (a[i][3] - a[i][2]) / a[i][4];
		if (dirstep < 0)
			dirstep = -dirstep;

		if ((a[i][2] - a[i][3]) % a[i][4] == 0)
		{
			printf("%d\n", dirstep);
		}
		else
		{
			goleft1 = 0, goright1 = 0;
			if ((a[i][2] - 1) % a[i][4] == 0)
				goleft1 = (a[i][2] - 1) % a[i][4];
			else
				goleft1 = (a[i][2] - 1) % a[i][4]+1;
			if ((a[i][1] - a[i][2]) % a[i][4] == 0)
				goright1 = (a[i][1] - a[i][2]) % a[i][4];
			else
				goright1 = (a[i][1] - a[i][2]) % a[i][4]+1;

			goleft2 = 0, goright2 = 0;
			if ((a[i][3] - 1) % a[i][4] == 0)
				goleft2 = (a[i][3] - 1) / a[i][4];
			if ((a[i][1] - a[i][3]) % a[i][4] == 0)
				goright2 = (a[i][1] - a[i][3]) / a[i][4];

			if (goright2 == 0 && goleft2 == 0)
				printf("-1\n");
			else
			{
				if (goright2 > goleft2&&goleft2 != 0)
					printf("%d\n", goleft2 + goleft1);
				if (goleft2 > goright2&&goright2 != 0)
					printf("%d\n", goright2 + goright1);
				if (goleft2 == 0)
					printf("%d\n", goright2 + goright1);
				if (goright2 == 0)
					printf("%d\n", goleft2 + goleft1);
			}
		}
	}
	system("pause");
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
int main(void)
{
	int testcase;
	scanf("%d", &testcase);
	long a[1001][5];
	for (int i = 1; i <= testcase; i++)
		for (int j = 1; j <= 4; j++)
			scanf("%d", &a[i][j]);

	for (int i = 1; i <= testcase; i++)
	{
		//a[i][1]num of pages
		//a[i][2]stating pages
		//a[i][3]desired pages
		//a[i][4]scrolled once

		int dirstep = (a[i][3] - a[i][2]) / a[i][4];
		if (dirstep < 0)
			dirstep = -dirstep;

		if ((a[i][2] - a[i][3]) % a[i][4] == 0)
		{
			printf("%ld\n", dirstep);
		}
		else
		{
			int goleft1 = 0, goright1 = 0;
			if ((a[i][2] - 1) % a[i][4] == 0)
				goleft1 = (a[i][2] - 1) / a[i][4];
			else
				goleft1 = (a[i][2] - 1) / a[i][4] + 1;
			if ((a[i][1] - a[i][2]) % a[i][4] == 0)
				goright1 = (a[i][1] - a[i][2]) / a[i][4];
			else
				goright1 = (a[i][1] - a[i][2]) / a[i][4] + 1;

			int goleft2 = 0, goright2 = 0;
			if ((a[i][3] - 1) % a[i][4] == 0)
				goleft2 = (a[i][3] - 1) / a[i][4];
			else
				goleft2 = -1;
			if ((a[i][1] - a[i][3]) % a[i][4] == 0)
				goright2 = (a[i][1] - a[i][3]) / a[i][4];
			else
				goright2 = -1;

			if (goright2 < 0 && goleft2 < 0)
				printf("-1\n");
			else
			{
				if (goright2 < 0) printf("%ld\n", goleft1 + goleft2);
				else
					if (goleft2 < 0) printf("%ld\n", goright1 + goright2);
				else
					if ((goright1 + goright2) > (goleft1 + goleft2))
					printf("%ld\n", goleft1 + goleft2);
				else
					printf("%ld\n", goright1 + goright2);
			}
		}
	}
	system("pause");
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
int main(void)
{
	int testcase;
	scanf("%d", &testcase);
	long a[1001][5];
	for (int i = 1; i <= testcase; i++)
		for (int j = 1; j <= 4; j++)
			scanf("%d", &a[i][j]);

	for (int i = 1; i <= testcase; i++)
	{
		//a[i][1]num of pages
		//a[i][2]stating pages
		//a[i][3]desired pages
		//a[i][4]scrolled once

		int dirstep = (a[i][3] - a[i][2]) / a[i][4];
		if (dirstep < 0)
			dirstep = -dirstep;

		if ((a[i][2] - a[i][3]) % a[i][4] == 0)
		{
			printf("%ld\n", dirstep);
		}
		else
		{
			int goleft1 = 0, goright1 = 0;
			if ((a[i][2] - 1) % a[i][4] == 0)
				goleft1 = (a[i][2] - 1) / a[i][4];
			else
				goleft1 = (a[i][2] - 1) / a[i][4] + 1;
			if ((a[i][1] - a[i][2]) % a[i][4] == 0)
				goright1 = (a[i][1] - a[i][2]) / a[i][4];
			else
				goright1 = (a[i][1] - a[i][2]) / a[i][4] + 1;

			int goleft2 = 0, goright2 = 0;
			if ((a[i][3] - 1) % a[i][4] == 0)
				goleft2 = (a[i][3] - 1) / a[i][4];
			if ((a[i][1] - a[i][3]) % a[i][4] == 0)
				goright2 = (a[i][1] - a[i][3]) / a[i][4];

			if (goright2 == 0 && goleft2 == 0)
				printf("-1\n");
			else
			{
				if (goright2 > goleft2&&goleft2 != 0)
					printf("%ld\n", goleft2 + goleft1);
				if (goleft2 > goright2&&goright2 != 0)
					printf("%ld\n", goright2 + goright1);
				if (goleft2 == 0)
					printf("%ld\n", goright2 + goright1);
				if (goright2 == 0)
					printf("%ld\n", goleft2 + goleft1);
			}
		}
	}
	//system("pause");
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
int main(void)
{
	int testcase;
	scanf("%d", &testcase);
	long long a[1001][5];
	int goleft1 = 0, goright1 = 0;
	int goleft2 = 0, goright2 = 0;
	int dirstep;
	int i,j;
	for (i = 1; i <= testcase; i++)
		for (j = 1; j <= 4; j++)
			scanf("%d", &a[i][j]);

	for (i = 1; i <= testcase; i++)
	{
		//a[i][1]num of pages
		//a[i][2]stating pages
		//a[i][3]desired pages
		//a[i][4]scrolled once

		dirstep = (a[i][3] - a[i][2]) / a[i][4];
		if (dirstep < 0)
			dirstep = -dirstep;

		if ((a[i][2] - a[i][3]) % a[i][4] == 0)
		{
			printf("%d\n", dirstep);
		}
		else
		{
			goleft1 = 0, goright1 = 0;
			if ((a[i][2] - 1) % a[i][4] == 0)
				goleft1 = (a[i][2] - 1) % a[i][4];
			else
				goleft1 = (a[i][2] - 1) % a[i][4]+1;
			if ((a[i][1] - a[i][2]) % a[i][4] == 0)
				goright1 = (a[i][1] - a[i][2]) % a[i][4];
			else
				goright1 = (a[i][1] - a[i][2]) % a[i][4]+1;

			goleft2 = 0, goright2 = 0;
			if ((a[i][3] - 1) % a[i][4] == 0)
				goleft2 = (a[i][3] - 1) / a[i][4];
			if ((a[i][1] - a[i][3]) % a[i][4] == 0)
				goright2 = (a[i][1] - a[i][3]) / a[i][4];

			if (goright2 == 0 && goleft2 == 0)
				printf("-1\n");
			else
			{
				if (goright2 > goleft2&&goleft2 != 0)
					printf("%d\n", goleft2 + goleft1);
				if (goleft2 > goright2&&goright2 != 0)
					printf("%d\n", goright2 + goright1);
				if (goleft2 == 0)
					printf("%d\n", goright2 + goright1);
				if (goright2 == 0)
					printf("%d\n", goleft2 + goleft1);
			}
		}
	}
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main(void)
{
	int testcase;
	scanf("%d", &testcase);
	int a[10][5];
	int goleft1 = 0, goright1 = 0;
	int dirstep;
	int i,j;
	for (i = 1; i <= testcase; i++)
		for (j = 1; j <= 4; j++)
			scanf("%d", &a[i][j]);

	for (i = 1; i <= testcase; i++)
	{
		//a[i][1]num of pages
		//a[i][2]stating pages
		//a[i][3]desired pages
		//a[i][4]scrolled once

		dirstep = (a[i][3] - a[i][2]) / a[i][4];
		if (dirstep < 0)
			dirstep = -dirstep;

		if ((a[i][2] - a[i][3]) % a[i][4] == 0)
		{
			printf("%d\n", dirstep);
		}
		else
		{
			goleft1 = 0, goright1 = 0;
			if ((a[i][2] - 1) % a[i][4] == 0)
				goleft1 = (a[i][2] - 1) % a[i][4];
			else
				goleft1 = (a[i][2] - 1) % a[i][4]+1;
			if ((a[i][1] - a[i][2]) % a[i][4] == 0)
				goright1 = (a[i][1] - a[i][2]) % a[i][4];
			else
				goright1 = (a[i][1] - a[i][2]) % a[i][4]+1;

			int goleft2 = 0, goright2 = 0;
			if ((a[i][3] - 1) % a[i][4] == 0)
				goleft2 = (a[i][3] - 1) / a[i][4];
			if ((a[i][1] - a[i][3]) % a[i][4] == 0)
				goright2 = (a[i][1] - a[i][3]) / a[i][4];

			if (goright2 == 0 && goleft2 == 0)
				printf("-1\n");
			else
			{
				if (goright2 > goleft2&&goleft2 != 0)
					printf("%d\n", goleft2 + goleft1);
				if (goleft2 > goright2&&goright2 != 0)
					printf("%d\n", goright2 + goright1);
				if (goleft2 == 0)
					printf("%d\n", goright2 + goright1);
				if (goright2 == 0)
					printf("%d\n", goleft2 + goleft1);
			}
		}
	}
	system("pause");
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main(void)
{
	int testcase;
	scanf("%d", &testcase);
	int a[1001][5];
	int goleft1 = 0, goright1 = 0;
	int dirstep;
	int i,j;
	for (i = 1; i <= testcase; i++)
		for (j = 1; j <= 4; j++)
			scanf("%d", &a[i][j]);

	for (i = 1; i <= testcase; i++)
	{
		//a[i][1]num of pages
		//a[i][2]stating pages
		//a[i][3]desired pages
		//a[i][4]scrolled once

		dirstep = (a[i][3] - a[i][2]) / a[i][4];
		if (dirstep < 0)
			dirstep = -dirstep;

		if ((a[i][2] - a[i][3]) % a[i][4] == 0)
		{
			printf("%d\n", dirstep);
		}
		else
		{
			goleft1 = 0, goright1 = 0;
			if ((a[i][2] - 1) % a[i][4] == 0)
				goleft1 = (a[i][2] - 1) % a[i][4];
			else
				goleft1 = (a[i][2] - 1) % a[i][4]+1;
			if ((a[i][1] - a[i][2]) % a[i][4] == 0)
				goright1 = (a[i][1] - a[i][2]) % a[i][4];
			else
				goright1 = (a[i][1] - a[i][2]) % a[i][4]+1;

			int goleft2 = 0, goright2 = 0;
			if ((a[i][3] - 1) % a[i][4] == 0)
				goleft2 = (a[i][3] - 1) / a[i][4];
			if ((a[i][1] - a[i][3]) % a[i][4] == 0)
				goright2 = (a[i][1] - a[i][3]) / a[i][4];

			if (goright2 == 0 && goleft2 == 0)
				printf("-1\n");
			else
			{
				if (goright2 > goleft2&&goleft2 != 0)
					printf("%d\n", goleft2 + goleft1);
				if (goleft2 > goright2&&goright2 != 0)
					printf("%d\n", goright2 + goright1);
				if (goleft2 == 0)
					printf("%d\n", goright2 + goright1);
				if (goright2 == 0)
					printf("%d\n", goleft2 + goleft1);
			}
		}
	}
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
int main(void)
{
	int testcase;
	scanf("%d", &testcase);
	long a[1001][5];
	for (int i = 1; i <= testcase; i++)
		for (int j = 1; j <= 4; j++)
			scanf("%d", &a[i][j]);

	for (int i = 1; i <= testcase; i++)
	{
		//a[i][1]num of pages
		//a[i][2]stating pages
		//a[i][3]desired pages
		//a[i][4]scrolled once

		int dirstep = (a[i][3] - a[i][2]) / a[i][4];
		if (dirstep < 0)
			dirstep = -dirstep;

		if ((a[i][2] - a[i][3]) % a[i][4] == 0)
		{
			printf("%ld\n", dirstep);
		}
		else
		{
			int goleft1 = 0, goright1 = 0;
			if ((a[i][2] - 1) % a[i][4] == 0)
				goleft1 = (a[i][2] - 1) / a[i][4];
			else
				goleft1 = (a[i][2] - 1) / a[i][4] + 1;
			if ((a[i][1] - a[i][2]) % a[i][4] == 0)
				goright1 = (a[i][1] - a[i][2]) / a[i][4];
			else
				goright1 = (a[i][1] - a[i][2]) / a[i][4] + 1;

			int goleft2 = 0, goright2 = 0;
			if ((a[i][3] - 1) % a[i][4] == 0)
				goleft2 = (a[i][3] - 1) / a[i][4];
			if ((a[i][1] - a[i][3]) % a[i][4] == 0)
				goright2 = (a[i][1] - a[i][3]) / a[i][4];

			if (goright2 == 0 && goleft2 == 0 && (a[i][1] - a[i][3]) != 0 && a[i][3] != 1)
				printf("-1\n");
			else
			{
				if (goright2 > goleft2&&goleft2 != 0)
					printf("%ld\n", goleft2 + goleft1);
				if (goleft2 > goright2&&goright2 != 0)
					printf("%ld\n", goright2 + goright1);
				if (goleft2 == 0)
					printf("%ld\n", goright2 + goright1);
				else
					if (goright2 == 0)
					printf("%ld\n", goleft2 + goleft1);
			}
		}
	}
	//system("pause");
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
int main(void)
{
	int testcase;
	scanf("%d", &testcase);
	long long a[1001][5];
	int goleft1 = 0, goright1 = 0;
	int goleft2 = 0, goright2 = 0;
	int dirstep;
	int i,j;
	for (i = 1; i <= testcase; i++)
		for (j = 1; j <= 4; j++)
			scanf("%d", &a[i][j]);

	for (i = 1; i <= testcase; i++)
	{
		//a[i][1]num of pages
		//a[i][2]stating pages
		//a[i][3]desired pages
		//a[i][4]scrolled once

		dirstep = (a[i][3] - a[i][2]) / a[i][4];
		if (dirstep < 0)
			dirstep = -dirstep;

		if ((a[i][2] - a[i][3]) % a[i][4] == 0)
		{
			printf("%f\n", dirstep);
		}
		else
		{
			goleft1 = 0, goright1 = 0;
			if ((a[i][2] - 1) % a[i][4] == 0)
				goleft1 = (a[i][2] - 1) % a[i][4];
			else
				goleft1 = (a[i][2] - 1) % a[i][4]+1;
			if ((a[i][1] - a[i][2]) % a[i][4] == 0)
				goright1 = (a[i][1] - a[i][2]) % a[i][4];
			else
				goright1 = (a[i][1] - a[i][2]) % a[i][4]+1;

			goleft2 = 0, goright2 = 0;
			if ((a[i][3] - 1) % a[i][4] == 0)
				goleft2 = (a[i][3] - 1) / a[i][4];
			if ((a[i][1] - a[i][3]) % a[i][4] == 0)
				goright2 = (a[i][1] - a[i][3]) / a[i][4];

			if (goright2 == 0 && goleft2 == 0)
				printf("-1\n");
			else
			{
				if (goright2 > goleft2&&goleft2 != 0)
					printf("%f\n", goleft2 + goleft1);
				if (goleft2 > goright2&&goright2 != 0)
					printf("%f\n", goright2 + goright1);
				if (goleft2 == 0)
					printf("%f\n", goright2 + goright1);
				if (goright2 == 0)
					printf("%f\n", goleft2 + goleft1);
			}
		}
	}
	system("pause");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int inf = -10;

int main()
{
    int t,n,x,y,d,ans;
    scanf("%d",&t);
    while(t--)
    {
        ans = inf;
        scanf("%d%d%d%d",&n,&x,&y,&d);
        if(!(abs(x - y) % d)) ans = abs(x - y) / d;
        else
        {
            if(!((y - 1) % d)) ans = min(ans,(x - 1) / d + 1 + (y - 1) / d);
            if(!((n - y) % d)) ans = min(ans,(n - x) / d + 1 + (n - y) / d);
        }
        if(ans != inf) cout << ans << endl;
        else cout << "-1" << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define NIL 1 << 21

int main()
{
    int t,n,x,y,d,ans;
    scanf("%d",&t);
    while(t--)
    {
        ans = NIL;
        scanf("%d%d%d%d",&n,&x,&y,&d);
        if(!(abs(x - y) % d)) ans = abs(x - y) / d;
        else
        {
            if(!((n - y) % d)) ans = min(ans,(n - x) / d + 1 + (n - y) / d);
            else if(!((y - 1) % d)) ans = min(ans,(x - 1) / d + 1 + (y - 1) / d);
        }
        if(ans != NIL) cout << ans << endl;
        else cout << "-1" << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int inf = (1 << 21);

int main()
{
    int t,n,x,y,d,ans;
    scanf("%d",&t);
    while(t--)
    {
        ans = inf;
        scanf("%d%d%d%d",&n,&x,&y,&d);
        if(!(abs(x - y) % d)) ans = abs(x - y) / d;
        else
        {
            if(!((y - 1) % d)) ans = min(ans,(x - 1) / d + 1 + (y - 1) / d);
            if(!((n - y) % d)) ans = min(ans,(n - x) / d + 1 + (n - y) / d);
        }
        if(ans != inf) cout << ans << endl;
        else cout << "-1" << endl;
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
#define MAX 100000005

int n,x,y,d;
int vis[MAX];
struct Node
{
    int xx;
    int step;
};

int bfs()
{
    memset(vis,0,sizeof(vis));
    Node node1,node2;
    queue<Node> q;
    node1.xx = x;
    node1.step = 0;
    q.push(node1);
    vis[x] = 1;
    while(!q.empty())
    {
        node1 = q.front();
        q.pop();
        if(node1.xx == y) return node1.step;
        node2.xx = node1.xx + d;
        node2.step = node1.step + 1;
        if(node2.xx >= n) node2.xx = n;
        if(!vis[node2.xx])
        {
            q.push(node2);
            vis[node2.xx] = 1;
        }
        node2.xx = node1.xx - d;
        node2.step = node1.step + 1;
        if(node2.xx <= 1) node2.xx = 1;
        if(!vis[node2.xx])
        {
            q.push(node2);
            vis[node2.xx] = 1;
        }
    }
    return -1;
}

int main()
{
    int t,num;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d%d",&n,&x,&y,&d);
        num = bfs();
        cout << num << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
    int t,n,x,y,d,sum1,sum2,sum;
    scanf("%d",&t);
    while(t--)
    {
        sum = sum1 = sum2 = 0;
        scanf("%d%d%d%d",&n,&x,&y,&d);
        if(abs(x - y) % d == 0)
        {
            cout << (abs(x - y) / d) << endl;
            continue;
        }
        else
        {
            if((y - 1) % d == 0) sum1 = (y - 1) / d;
            if((n - y) % d == 0) sum2 = (n - y) / d;
        }
        if(!sum1 && !sum2)
        {
            cout << "-1" << endl;
            continue;
        }
        else if(sum1 == 0 && sum2 != 0)
        {
            if((n - x) % d == 0) sum = sum2 + (n - x) / d;
            else sum = sum2 + (n - x) / d + 1;
        }
        else if(sum1 != 0 && sum2 == 0)
        {
            if((x - 1) % d == 0) sum = sum1 + (x - 1) / d;
            else sum = sum1 + (x - 1) / d + 1;
        }
        else
        {
            if((x - 1) % d == 0) sum1 = sum1 + (x - 1) / d;
            else sum1 = sum1 + (x - 1) / d + 1;
            if((n - x) % d == 0) sum2 = sum2 + (n - x) / d;
            else sum2 = sum2 + (n - x) / d + 1;
            sum = min(sum1,sum2);
        }
        cout << sum << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int inf = -100;

int main()
{
    int t,n,x,y,d,ans;
    scanf("%d",&t);
    while(t--)
    {
        ans = inf;
        scanf("%d%d%d%d",&n,&x,&y,&d);
        if(!(abs(x - y) % d)) ans = abs(x - y) / d;
        else
        {
            if(!((y - 1) % d)) ans = min(ans,(x - 1) / d + 1 + (y - 1) / d);
            if(!((n - y) % d)) ans = min(ans,(n - x) / d + 1 + (n - y) / d);
        }
        if(ans != inf) cout << ans << endl;
        else cout << "-1" << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;

int main()
{
    int t,n,x,y,d,ans;
    scanf("%d",&t);
    while(t--)
    {
        ans = inf;
        scanf("%d%d%d%d",&n,&x,&y,&d);
        if(!(abs(x - y) % d)) ans = abs(x - y) / d;
        else
        {
            if(!((y - 1) % d)) ans = min(ans,(x - 1) / d + 1 + (y - 1) / d);
            if(!((n - y) % d)) ans = min(ans,(n - x) / d + 1 + (n - y) / d);
        }
        if(ans != inf) cout << ans << endl;
        else cout << "-1" << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define NIL 0x3f3f3f3f

int main()
{
    int t,n,x,y,d,ans;
    scanf("%d",&t);
    while(t--)
    {
        ans = NIL;
        scanf("%d%d%d%d",&n,&x,&y,&d);
        if(!(abs(x - y) % d)) ans = abs(x - y) / d;
        else
        {
            if(!((y - 1) % d)) ans = min(ans,(x - 1) / d + 1 + (y - 1) / d);
            else if(!((n - y) % d)) ans = min(ans,(n - x) / d+ 1 + (n - y) / d);
        }
        if(ans != NIL) cout << ans << endl;
        else cout << "-1" << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define NIL 1 << 21

int main()
{
    int t,n,x,y,d,ans;
    scanf("%d",&t);
    while(t--)
    {
        ans = NIL;
        scanf("%d%d%d%d",&n,&x,&y,&d);
        if(!(abs(x - y) % d)) ans = abs(x - y) / d;
        else
        {
            if(!((y - 1) % d)) ans = min(ans,(x - 1) / d + 1 + (y - 1) / d);
            else if(!((n - y) % d)) ans = min(ans,(n - x) / d + 1 + (n - y) / d);
        }
        if(ans != NIL) cout << ans << endl;
        else cout << "-1" << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
    int t,n,x,y,d,sum1,sum2,sum;
    scanf("%d",&t);
    while(t--)
    {
        sum = sum1 = sum2 = 0;
        scanf("%d%d%d%d",&n,&x,&y,&d);
        if(abs(x - y) % d == 0)
        {
            cout << (abs(x - y) / d) << endl;
            continue;
        }
        else
        {
            if((y - 1) % d == 0) sum1 = (y - 1) / d;
            if((n - y) % d == 0) sum2 = (n - y) / d;
        }
        if(!sum1 && !sum2 || y == n || y == 1)
        {
            cout << "-1" << endl;
            continue;
        }
        else if(sum1 == 0 && sum2 != 0)
        {
            if((n - x) % d == 0) sum = sum2 + (n - x) / d;
            else sum = sum2 + (n - x) / d + 1;
        }
        else if(sum1 != 0 && sum2 == 0)
        {
            if((x - 1) % d == 0) sum = sum1 + (x - 1) / d;
            else sum = sum1 + (x - 1) / d + 1;
        }
        else
        {
            if((x - 1) % d == 0) sum1 = sum1 + (x - 1) / d;
            else sum1 = sum1 + (x - 1) / d + 1;
            if((n - x) % d == 0) sum2 = sum2 + (n - x) / d;
            else sum2 = sum2 + (n - x) / d + 1;
            sum = min(sum1,sum2);
        }
        cout << sum << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
    int t,n,x,y,d,sum1,sum2,sum;
    scanf("%d",&t);
    while(t--)
    {
        sum = sum1 = sum2 = 0;
        scanf("%d%d%d%d",&n,&x,&y,&d);
        if(abs(x - y) % d == 0)
        {
            cout << (abs(x - y) / d) << endl;
            continue;
        }
        else
        {
            if((y - 1) % d == 0) sum1 = (y - 1) / d;
            if((n - y) % d == 0) sum2 = (n - y) / d;
        }
        if(!sum1 && !sum2 && y != n && y != 1)
        {
            cout << "-1" << endl;
            continue;
        }
        else if(sum1 == 0 && sum2 != 0)
        {
            if((n - x) % d == 0) sum = sum2 + (n - x) / d;
            else sum = sum2 + (n - x) / d + 1;
        }
        else if(sum1 != 0 && sum2 == 0)
        {
            if((x - 1) % d == 0) sum = sum1 + (x - 1) / d;
            else sum = sum1 + (x - 1) / d + 1;
        }
        else
        {
            if((x - 1) % d == 0) sum1 = sum1 + (x - 1) / d;
            else sum1 = sum1 + (x - 1) / d + 1;
            if((n - x) % d == 0) sum2 = sum2 + (n - x) / d;
            else sum2 = sum2 + (n - x) / d + 1;
            sum = min(sum1,sum2);
        }
        cout << sum << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int inf=0x3f3f3f3f;
int T,n,x,y,d,ans;
int main(){
	cin>>T;
	while(T--){
		cin>>n>>x>>y>>d;
		ans=inf;
		if(abs(y-x)%d==0)ans=abs(y-x)/d;
		else{
			if((y-1)%d==0)ans=min(ans,(x-1)/d+1+(y-1)/d);
			if((n-y)%d==0)ans=min(ans,(n-x)/d+1+(n-y)/d);
		}
		if(ans==inf)ans=-1;
		cout<<ans<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
    int t,n,x,y,d;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d%d",&n,&x,&y,&d);
        if(abs(x - y) % d == 0) cout << (abs(x - y) / d) << endl;
        else
        {
            if((y - 1) % d == 0) cout << (x - 1) / d + 1 + (y - 1) / d << endl;
            else if((n - y) % d == 0) cout << (n - x) / d + 1 + (n - y) / d << endl;
            else cout << "-1" << endl;
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int inf = 1000000000;

int main()
{
    int t,n,x,y,d,ans;
    scanf("%d",&t);
    while(t--)
    {
        ans = inf;
        scanf("%d%d%d%d",&n,&x,&y,&d);
        if(!(abs(x - y) % d)) ans = abs(x - y) / d;
        else
        {
            if(!((y - 1) % d)) ans = min(ans,(x - 1) / d + 1 + (y - 1) / d);
            if(!((n - y) % d)) ans = min(ans,(n - x) / d + 1 + (n - y) / d);
        }
        if(ans != inf) cout << ans << endl;
        else cout << "-1" << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 1000000;
const int INF = 10000000;
const int MOD = 10000000;
vector<int>a;
int vis[maxn];

int main()
{
    int n;
    cin >> n;
    string s;
    cin >> s;
    int now = 0, tmp = 0;
    for(int i = 0; i < n; i++)
    {
        if(s[i] == 'G')now++;
        else if(now)
        {
            a.push_back(now), now = 0, vis[i] = ++tmp;
        }
        else vis[i] = tmp;
    }
    if(now)a.push_back(now);
    int m = a.size();
    if(m == 0)
    {
        cout<<"0\n";
    }
    else if(m == 1)
    {
        cout<<a[0]<<endl;
    }
    else if(m == 2)
    {
        bool flag = 0;
        for(int i = 1; i < n - 1; i++)
        {
            if(s[i] == 'S' && s[i - 1] == 'G' && s[i + 1] == 'G')
            {
                flag = 1;
                break;
            }
        }
        if(flag)cout<<a[0] + a[1]<<endl;
        else cout<<max(a[0], a[1]) + 1<<endl;
    }
    else
    {
        int ans = 0;
        for(int i = 0; i < m; i++)ans = max(ans, a[i] + 1);
        for(int i = 1; i < n - 1; i++)
        {
            if(s[i] == 'S' && s[i - 1] == 'G' && s[i + 1] == 'G')
            {
                ans = max(ans, a[vis[i] - 1] + a[vis[i]] + 1);
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int n, p, x, y, d;

int main() {
    cin >> n;
    while(n--) {
        int ans = -1, now;
        cin >> p >> x >> y >> d;
        if(abs(y-x)%d == 0) {
            ans = abs(y-x)/d;
        }
        if((y-1)%d == 0) {
            now = (x-1)/d + (y-1)/d;
            if((x-1)/d == 0) now++;
            if(ans > 0) ans = min(ans, now);
            else ans = now;
        }
        if((p-y)%d == 0) {
            now = (p-x)/d + (p-y)/d;
            if((p-x)/d == 0) now++;
            if(ans > 0) ans = min(ans, now);
            else ans = now;
        }
        cout << ans << '\n';
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int n, p, x, y, d;

int main() {
    cin >> n;
    while(n--) {
        int ans = -1, now;
        cin >> p >> x >> y >> d;
        if(abs(y-x)%d == 0) {
            ans = abs(y-x)/d;
        }
        if((y-1)%d == 0) {
            now = (x-1)/d + (y-1)/d;
            if((x-1)/d) now++;
            if(ans > 0) ans = min(ans, now);
            else ans = now;
        }
        if((p-y)%d == 0) {
            now = (p-x)/d + (p-y)/d;
            if((p-x)/d) now++;
            if(ans > 0) ans = min(ans, now);
            else ans = now;
        }
        cout << ans << '\n';
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int n, p, x, y, d;

int main() {
    cin >> n;
    while(n--) {
        int ans = -1;
        cin >> p >> x >> y >> d;
        if(abs(y-x)%d == 0) {
            ans = abs(y-x)/d;
        }
        if((y-1)%d == 0) {
            if(ans > 0) {
                ans = min(ans, x/d + (y-1)/d);
            } else {
                ans = x/d + (y-1)/d;
            }
        }
        if((p-y)%d == 0) {
            int now = (p-x)/d + (p-y)/d;
            if(ans > 0) ans = min(ans, now);
            else ans = now;
        }
        cout << ans << '\n';
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int n, p, x, y, d;

int main() {
    cin >> n;
    while(n--) {
        int ans = -1, now;
        cin >> p >> x >> y >> d;
        if(abs(y-x)%d == 0) {
            ans = abs(y-x)/d;
        }
        if((y-1)%d == 0) {
            now = x/d + (y-1)/d;
            if((y-1)/d == 0) now++;
            if(ans > 0) ans = min(ans, now);
            else ans = now;
        }
        if((p-y)%d == 0) {
            now = (p-x)/d + (p-y)/d;
            if((p-y)/d == 0) now++;
            if(ans > 0) ans = min(ans, now);
            else ans = now;
        }
        cout << ans << '\n';
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

long long n, p, x, y, d;

int main() {
    cin >> n;
    while(n--) {
        long long ans = -1, now;
        cin >> p >> x >> y >> d;
        if(abs(y-x)%d == 0) {
            ans = abs(y-x)/d;
        }
        if((y-1)%d == 0) {
            now = (x-1)/d + (y-1)/d;
            if((x-1)/d) now++;
            if(ans > -1) ans = min(ans, now);
            else ans = now;
        }
        if((p-y)%d == 0) {
            now = (p-x)/d + (p-y)/d;
            if((p-x)/d) now++;
            if(ans > -1) ans = min(ans, now);
            else ans = now;
        }
        cout << ans << '\n';
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int n, p, x, y, d;

int main() {
    cin >> n;
    while(n--) {
        cin >> p >> x >> y >> d;
        if(abs(y-x)%d == 0) {
            cout << abs(y-x)/d;
        } else {
            if((y-1)%d == 0 || (p-y)%d == 0) {
                int ch1 = x/d + (y-1)/d;
                int ch2 = (p-x)/d + (p-y)/d;
                cout << min(ch1, ch2);
            } else {
                cout << -1;
            }
        }
        cout << '\n';
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

long long n, p, x, y, d;

int main() {
    cin >> n;
    while(n--) {
        long long ans = 1000*1000*1000, now;
        cin >> p >> x >> y >> d;
        if(abs(y-x)%d == 0) {
            ans = abs(y-x)/d;
        }
        else {
            if((y-1)%d == 0) {
                now = (x-1)/d + (y-1)/d;
                if((x-1)%d) now++;
                ans = min(ans, now);
            }
            if((p-y)%d == 0) {
                now = (p-x)/d + (p-y)/d;
                if((p-x)%d) now++;
                ans = min(ans, now);
            }
        }
        if(ans == 1000*1000*1000) cout << -1;
        else cout << ans;
        cout << '\n';
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

long long n, p, x, y, d;

int main() {
    cin >> n;
    while(n--) {
        long long ans = -1, now;
        cin >> p >> x >> y >> d;
        if(abs(y-x)%d == 0) {
            ans = abs(y-x)/d;
        }
        if((y-1)%d == 0) {
            now = (x-1)/d + (y-1)/d;
            if((x-1)/d) now++;
            if(ans > 0) ans = min(ans, now);
            else ans = now;
        }
        if((p-y)%d == 0) {
            now = (p-x)/d + (p-y)/d;
            if((p-x)/d) now++;
            if(ans > 0) ans = min(ans, now);
            else ans = now;
        }
        cout << ans << '\n';
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

long long n, p, x, y, d;

int main() {
    cin >> n;
    while(n--) {
        long long ans = 1000*1000*1000, now;
        cin >> p >> x >> y >> d;
        if(abs(y-x)%d == 0) {
            ans = abs(y-x)/d;
        }
        else {
            if((y-1)%d == 0) {
                now = (x-1)/d + (y-1)/d;
                if((x-1)/d) now++;
                ans = min(ans, now);
            }
            if((p-y)%d == 0) {
                now = (p-x)/d + (p-y)/d;
                if((p-x)/d) now++;
                ans = min(ans, now);
            }
        }
        if(ans == 1000*1000*1000) cout << -1;
        else cout << ans;
        cout << '\n';
    }
    return 0;
}


#include <bits/stdc++.h>

#pragma GCC optimize("O3")
#pragma GCC diagnostic error "-std=c++11"
//#pragma comment(linker, "/STACK:102400000,102400000")
#define REP(i, a, b) for(int i = a; i < b; ++i)
#define debug(x) { cerr << #x << '=' << x << "\n"; }
#define Arr(a, l, r) { cerr << #a << " = {"; REP(_, l, r) cerr << ' ' << a[_]; cerr << " }\n"; }
#define mset(a,b) memset(a,b,sizeof(a))
#define mkpr make_pair
#define ll long long

using namespace std;
const int maxn = 1e9+6;
int n,s,t,d,m,u,dis;
bool vis[maxn],flag;
queue<pair<int,int> > Q;
void clear(queue<pair<int,int> >& q) {
    queue<pair<int,int> > empty;
    swap(empty, q);
}

int main(){
	ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
	cin>>m;
	while(m--){
		cin>>n>>s>>t>>d;
		clear(Q);
		mset(vis,0);
		Q.push(mkpr(s,0));
		vis[s] = true;
		flag = false;
		while(!Q.empty()){
  		u = Q.front().first;dis = Q.front().second;Q.pop();//debug(u);
  		if(u==t){
  			cout<<dis<<"\n";
  			flag = true;
  			break;
  		}
  		if(!vis[u+d]){
  			Q.push(mkpr(u+d<=n?u+d:n,dis+1));
  			vis[u+d] = true;
  		}
  		if(!vis[u-d]){
  			Q.push(mkpr(u-d>=1?u-d:1,dis+1));
  			vis[u-d] = true;
  		}
  	}
  	if(!flag)cout<<-1<<"\n";
  }
  return 0;
}


#include <bits/stdc++.h>

#pragma GCC optimize("O3")
#pragma GCC diagnostic error "-std=c++11"
//#pragma comment(linker, "/STACK:102400000,102400000")
#define REP(i, a, b) for(int i = a; i < b; ++i)
#define debug(x) { cerr << #x << '=' << x << "\n"; }
#define Arr(a, l, r) { cerr << #a << " = {"; REP(_, l, r) cerr << ' ' << a[_]; cerr << " }\n"; }
#define mset(a,b) memset(a,b,sizeof(a))
#define mkpr make_pair
#define ll long long

using namespace std;
const int maxn = 1e9+6;
int n,s,t,d,m,u,dis,pre,nextv,rec;
bool flag;
struct node{int u,dis,pre;};
queue<node> Q;
void clear(queue<node>& q) {
	queue<node> empty;
	swap(empty, q);
}

int main(){
	ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
	cin>>m;
	while(m--){
		cin>>n>>s>>t>>d;
		clear(Q);
		Q.push((node){s,0,0});
		flag = false;rec = 0;
		while(!Q.empty()){
			pre = Q.front().pre;u = Q.front().u;dis = Q.front().dis;
			Q.pop();rec++;//debug(u);
			if(u==t){
				cout<<dis<<"\n";
				flag = true;
				break;
			}
			if(rec>n)break;
			nextv = u+d<n?u+d:n;
			if(nextv!=pre)Q.push((node){nextv,dis+1,u});
			nextv = u-d>1?u-d:1;
			if(nextv!=pre)Q.push((node){nextv,dis+1,u});
		}
		if(!flag)cout<<-1<<"\n";
	}
	return 0;
}


#include <bits/stdc++.h>

#pragma GCC optimize("O3")
#pragma GCC diagnostic error "-std=c++11"
//#pragma comment(linker, "/STACK:102400000,102400000")
#define REP(i, a, b) for(int i = a; i < b; ++i)
#define debug(x) { cerr << #x << '=' << x << "\n"; }
#define Arr(a, l, r) { cerr << #a << " = {"; REP(_, l, r) cerr << ' ' << a[_]; cerr << " }\n"; }
#define mset(a,b) memset(a,b,sizeof(a))
#define mkpr make_pair
#define ll long long

using namespace std;
int n,s,t,d,m,ans;
bool flag;
struct node{int u,dis,pre;};
queue<node> Q;

int main(){
	ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
	cin>>m;
	while(m--){
		cin>>n>>s>>t>>d;
		ans = -1;
		int dis = abs(s-t);
		if(dis%d==0)ans = dis/d;
		else{
			if((t-1)%d==0)ans = (s-1)/d+(t-1)/d+((s-1)%d?1:0);
			if((n-t)%d==0){
				int tmp = (n-s)/d+(n-t)/d+((n-s)%d?1:0);
				ans = tmp<ans&&ans!=-1?tmp:ans;
			}
		}
		cout<<ans<<"\n";
	}
	return 0;
}


#include <bits/stdc++.h>

#pragma GCC optimize("O3")
#pragma GCC diagnostic error "-std=c++11"
//#pragma comment(linker, "/STACK:102400000,102400000")
#define REP(i, a, b) for(int i = a; i < b; ++i)
#define debug(x) { cerr << #x << '=' << x << "\n"; }
#define Arr(a, l, r) { cerr << #a << " = {"; REP(_, l, r) cerr << ' ' << a[_]; cerr << " }\n"; }
#define mset(a,b) memset(a,b,sizeof(a))
#define mkpr make_pair
#define ll long long

using namespace std;
const int maxn = 1e9+6;
int n,s,t,d,m,u,dis;
bool vis[maxn],flag;

int main(){
	ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
	cin>>m;
	while(m--){
		cin>>n>>s>>t>>d;
		queue<pair<int,int> > Q;
		mset(vis,0);
		Q.push(mkpr(s,0));
		vis[s] = true;
		flag = false;
		while(!Q.empty()){
  		u = Q.front().first;dis = Q.front().second;Q.pop();//debug(u);
  		if(u==t){
  			cout<<dis<<"\n";
  			flag = true;
  			break;
  		}
  		if(!vis[u+d]){
  			Q.push(mkpr(u+d<=n?u+d:n,dis+1));
  			vis[u+d] = true;
  		}
  		if(!vis[u-d]){
  			Q.push(mkpr(u-d>=1?u-d:1,dis+1));
  			vis[u-d] = true;
  		}
  	}
  	if(!flag)cout<<-1<<"\n";
  }
  return 0;
}


#include <bits/stdc++.h>

#pragma GCC optimize("O3")
#pragma GCC diagnostic error "-std=c++11"
//#pragma comment(linker, "/STACK:102400000,102400000")
#define REP(i, a, b) for(int i = a; i < b; ++i)
#define debug(x) { cerr << #x << '=' << x << "\n"; }
#define Arr(a, l, r) { cerr << #a << " = {"; REP(_, l, r) cerr << ' ' << a[_]; cerr << " }\n"; }
#define mset(a,b) memset(a,b,sizeof(a))
#define mkpr make_pair
#define ll long long

using namespace std;
int n,s,t,d,m,u,dis,pre,nextv,rec;
bool flag;
struct node{int u,dis,pre;};
queue<node> Q;

int main(){
	ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
	cin>>m;
	while(m--){
		cin>>n>>s>>t>>d;
		while(!Q.empty())Q.pop();
		Q.push((node){s,0,0});
		flag = false;rec = 0;
		while(!Q.empty()){
			pre = Q.front().pre;u = Q.front().u;dis = Q.front().dis;
			Q.pop();rec++;//debug(u);
			if(u==t){
				cout<<dis<<"\n";
				flag = true;
				break;
			}
			if(rec>n)break;
			nextv = u+d<n?u+d:n;
			if(nextv!=pre)Q.push((node){nextv,dis+1,u});
			nextv = u-d>1?u-d:1;
			if(nextv!=pre)Q.push((node){nextv,dis+1,u});
		}
		if(!flag)cout<<-1<<"\n";
	}
	return 0;
}


#include <bits/stdc++.h>

#pragma GCC optimize("O3")
#pragma GCC diagnostic error "-std=c++11"
//#pragma comment(linker, "/STACK:102400000,102400000")
#define REP(i, a, b) for(int i = a; i < b; ++i)
#define debug(x) { cerr << #x << '=' << x << "\n"; }
#define Arr(a, l, r) { cerr << #a << " = {"; REP(_, l, r) cerr << ' ' << a[_]; cerr << " }\n"; }
#define mset(a,b) memset(a,b,sizeof(a))
#define mkpr make_pair
#define ll long long

using namespace std;
const int maxn = 1e9+6;
int n,s,t,d,m,u,dis,pre,next,rec;
bool flag;
struct node{int u,dis,pre;};
queue<node> Q;

int main(){
	ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
	cin>>m;
	while(m--){
		cin>>n>>s>>t>>d;
		while(!Q.empty())Q.pop();
		Q.push((node){s,0,0});
		flag = false;rec = 0;
		while(!Q.empty()){
			pre = Q.front().pre;u = Q.front().u;dis = Q.front().dis;
			Q.pop();rec++;//debug(u);
			if(u==t){
				cout<<dis<<"\n";
				flag = true;
				break;
			}
			if(rec>n)break;
			next = u+d<n?u+d:n;
			if(next!=pre)Q.push((node){next,dis+1,u});
			next = u-d>1?u-d:1;
			if(next!=pre)Q.push((node){next,dis+1,u});
		}
		if(!flag)cout<<-1<<"\n";
	}
	return 0;
}


#include <bits/stdc++.h>

#pragma GCC optimize("O3")
#pragma GCC diagnostic error "-std=c++11"
//#pragma comment(linker, "/STACK:102400000,102400000")
#define REP(i, a, b) for(int i = a; i < b; ++i)
#define debug(x) { cerr << #x << '=' << x << "\n"; }
#define Arr(a, l, r) { cerr << #a << " = {"; REP(_, l, r) cerr << ' ' << a[_]; cerr << " }\n"; }
#define mset(a,b) memset(a,b,sizeof(a))
#define mkpr make_pair
#define ll long long

using namespace std;
int n,s,t,d,m,ans;
bool flag;
struct node{int u,dis,pre;};
queue<node> Q;

int main(){
	ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
	cin>>m;
	while(m--){
		cin>>n>>s>>t>>d;
		ans = -1;
		int dis = abs(s-t);
		if(dis%d==0)ans = dis/d;
		else{
			if((t-1)%d==0)ans = (s-1)/d+(t-1)/d+((s-1)%d?1:0);
			if((n-t)%d==0){
				int tmp = (n-s)/d+(n-t)/d+((n-s)%d?1:0);
				ans = tmp<ans?tmp:ans;
			}
		}
		cout<<ans<<"\n";
	}
	return 0;
}


#include <bits/stdc++.h>

#pragma GCC optimize("O3")
#pragma GCC diagnostic error "-std=c++11"
//#pragma comment(linker, "/STACK:102400000,102400000")
#define REP(i, a, b) for(int i = a; i < b; ++i)
#define debug(x) { cerr << #x << '=' << x << "\n"; }
#define Arr(a, l, r) { cerr << #a << " = {"; REP(_, l, r) cerr << ' ' << a[_]; cerr << " }\n"; }
#define mset(a,b) memset(a,b,sizeof(a))
#define mkpr make_pair
#define ll long long

using namespace std;
int n,s,t,d,m,ans;
bool flag;
struct node{int u,dis,pre;};
queue<node> Q;

int main(){
	ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
	cin>>m;
	while(m--){
		cin>>n>>s>>t>>d;
		ans = -1;
		int dis = abs(s-t);
		if(dis%d==0)ans = dis/d;
		else{
			if((t-1)%d==0)ans = (s-1)/d+(t-1)/d+((s-1)%d?1:0);
			if((n-t)%d==0){
				int tmp = (n-s)/d+(n-t)/d+((n-s)%d?1:0);
				if(ans==-1)ans = tmp;
				else ans = tmp<ans?tmp:ans;
			}
		}
		cout<<ans<<"\n";
	}
	return 0;
}


#include <bits/stdc++.h>

#pragma GCC optimize("O3")
#pragma GCC diagnostic error "-std=c++11"
//#pragma comment(linker, "/STACK:102400000,102400000")
#define REP(i, a, b) for(int i = a; i < b; ++i)
#define debug(x) { cerr << #x << '=' << x << "\n"; }
#define Arr(a, l, r) { cerr << #a << " = {"; REP(_, l, r) cerr << ' ' << a[_]; cerr << " }\n"; }
#define mset(a,b) memset(a,b,sizeof(a))
#define mkpr make_pair
#define ll long long

using namespace std;
const int maxn = 1e9+6;
int n,s,t,d,m,u,dis,pre,next,rec;
bool flag;
struct node{int u,dis,pre;};
queue<node> Q;
void clear(queue<node>& q) {
	queue<node> empty;
	swap(empty, q);
}

int main(){
	ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
	cin>>m;
	while(m--){
		cin>>n>>s>>t>>d;
		clear(Q);
		Q.push((node){s,0,0});
		flag = false;rec = 0;
		while(!Q.empty()){
			pre = Q.front().pre;u = Q.front().u;dis = Q.front().dis;
			Q.pop();rec++;//debug(u);
			if(u==t){
				cout<<dis<<"\n";
				flag = true;
				break;
			}
			if(rec>n)break;
			next = u+d<n?u+d:n;
			if(next!=pre)Q.push((node){next,dis+1,u});
			next = u-d>1?u-d:1;
			if(next!=pre)Q.push((node){next,dis+1,u});
		}
		if(!flag)cout<<-1<<"\n";
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int main(){
	int t;
	cin >> t;
	while(t--){
		int n, x, y, d;
		int ans = 0x3f3f3f3f;
		cin >> n >> x >> y >> d;
		if(abs(y - x) % d == 0)ans = (y - x) / d;
		if((y - 1) % d == 0)ans = min(ans, (y - 1) / d + (x - 1) / d + 1);
		if((n - y) % d == 0)ans = min(ans, (n - y) / d + (n - x) / d + 1);
		printf("%d\n", ans == 0x3f3f3f3f ? -1 : ans);
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int main(){
	int t;
	cin >> t;
	while(t--){
		int n, x, y, d;
		int ans = 2147483647;
		cin >> n >> x >> y >> d;
		if(abs(y - x) % d == 0)ans = abs(y - x) / d;
		if((y - 1) % d == 0)ans = min(ans, (y - 1) / d + (x - 1) / d + 1);
		if((n - y) % d == 0)ans = min(ans, (n - y) / d + (n - x) / d + 1);
		printf("%d\n", ans == 2147483647 ? -1 : ans);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int T,n,x,y,d;
int main()
{
	cin>>T;
	while(T--) {
		scanf("%d%d%d%d",&n,&x,&y,&d);
			if(x<=y){
				if((y-x)%d==0) {printf("%d\n",(y-x)/d);continue;}
			}else {
				if((x-y)%d==0) {printf("%d\n",(x-y)/d);continue;}
			}

			long long g=(long long)ceil(1.0*(n-x)/d);
			long long ans=20000000000000;
			if((n-y)%d==0) {ans=min(ans,g+(n-y)/d);}
			long long gg=(long long)ceil(1.0*(x-1)/d);
			if((y-1)%d==0) {ans=min(ans,gg+(y-1)/d);}
			if(ans==20000000000000) printf("-1\n");
			else printf("%lld\n",ans);
	}
	return 0;
}


# include <bits/stdc++.h>

int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        int n, x, y, d;
        scanf("%d%d%d%d", &n, &x, &y, &d);
        int ans = 0x7f7f7f7f;
        if(abs(x - y) % d == 0)
            ans = abs(x - y) / d;
        if((y - 1) % d == 0)
            ans = std::min(ans, (x - 1) / d + bool((x - 1) % d) + (y - 1) / d);
        if ((n - y) % d == 0)
            ans = std::min(ans, (n - x) / d + bool((n - x) % d) + (n - y) / d);
        if(ans == 0x7f7f7f7f)
        {
            printf("-1\n");
            continue;
        }
        printf("%d\n", ans);
    }
    return 0;
}


# include <cstdio>
# include <cstdlib>
# include <iostream>
# include <algorithm>
# include <cmath>
using namespace std;

inline void gi(int &x)
{
    x=0;int t=1,k=getchar();
    for(;k<'0'||k>'9';k=getchar())if(k=='-')t=-1;
    for(;k>='0'&&k<='9';k=getchar())x=(x<<1)+(x<<3)+(k^48);x*=t;
}

int n, x, y, d, ans;
inline int fmin(const int&a, const int&b) { return a<b?a:b; }
inline int fabs(const int&a) { return a>0?a:-a; }

int main(void)
{
  int T; gi(T);
  while(T--)
  {
    gi(n), gi(x), gi(y), gi(d);
    ans=1e9;
    if(x==y)
    {
      puts("0");
      continue;
    }
    if(fabs(y-x)%d == 0) ans=fabs(y-x)/d;
    if((n-y)%d == 0) ans=fmin(ans, (n-y)/d+(n-x+d-1)/d);
    if((y-1)%d == 0) ans=fmin(ans, (y-1)/d+(x+d-2)/d);
    if(ans == 1e9) ans=-1;
    printf("%d\n", ans);
  }
  return 0;
}


#include<iostream>
#include<queue>
#include<string.h>
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
typedef long long int LL;
const int N = 100005;
const int inf = 0x7fffffff;

LL ans = 0;
LL min(LL a, LL b) {
	if(a <= b)
		return a;
	else
		return b;
}
int main()
{
	int n, x, y,d;
	int t;
	cin >> t;
	while (t--) {
		ans = 0;
		cin >> n >> x >> y >> d;
		int orix = x;
		int oriy = y;
		if (x > y) //让y在x后面
	      swap(x, y);
			if ((y - x) % d == 0){ //直接翻
			cout << (y - x) / d << endl;
		}
		else {
			int ansa=inf;
			int ansb =inf;
			if ((oriy-1)%d==0&&(oriy-1)/d!=0) {//经过1到达y,保证从1可以到达y
				if (((orix - 1) % d) != 0) {
					ansa = (orix - 1) / d + 1;
					ansa += (oriy - 1) / d;
				}
				else {
					ansa = (orix - 1) / d;
					ansa += (oriy - 1) / d;
				}

			}
			if ((n-oriy)%d==0&&(n-oriy)/d!=0) {//经过n到达y
				if ((n - orix)%d != 0) {
					ansb = (n - orix)/d + 1;
					ansb += (n - oriy) / d;
				}
				else {
					ansb = (n - orix) / d;
					ansb += (n - oriy) / d;
				}
			}
			LL ans = min(ansa, ansb);
			if (ans == inf)
				cout << -1 << endl;
				else
			cout <<ans<< endl;
		}

	}

	return 0;

}


# include <cstdio>
# include <cstdlib>
# include <iostream>
# include <algorithm>
using namespace std;

inline void gi(int &x)
{
    x=0;int t=1,k=getchar();
    for(;k<'0'||k>'9';k=getchar())if(k=='-')t=-1;
    for(;k>='0'&&k<='9';k=getchar())x=(x<<1)+(x<<3)+(k^48);x*=t;
}

int n, x, y, d;

int main(void)
{
  int T; gi(T);
  while(T--)
  {
    gi(n), gi(x), gi(y), gi(d);
    x=x<y?x:y; y=y>x?y:x;
    if(x+d == y) puts("1");
    else if((y-x)%d == 0) printf("%d\n", (y-x)/d);
    else if((n-y)%d == 0) printf("%d\n", (n-x)/d+( (n-x) %d != 0)+(n-y)/d);
    else if((y-1)%d == 0) printf("%d\n", (x-1)/d+( (x-1) %d != 0)+(y-1)/d);
    else puts("-1");
  }
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int t;
    cin>>t;
    int n,x,y,d;
    while(t--){
    cin>>n>>x>>y>>d;
    int flag1,flag2;
    flag1=flag2=0;
    if((x+y-1)%d==0) flag1=(x+y-1)/d;
    if(abs(x-y)%d==0) flag2=abs(x-y)/d;

    if((flag1+flag2)==0){
        cout<<-1<<endl;
    }
    else if(flag1==0&&flag2!=0) cout<<flag2<<endl;
        else if(flag1!=0&&flag2==0) cout<<flag1<<endl;
        else cout<<min(flag1,flag2)<<endl;
    }
        return 0;
}


# include <cstdio>
# include <cstdlib>
# include <iostream>
# include <algorithm>
# include <cmath>
using namespace std;

inline void gi(int &x)
{
    x=0;int t=1,k=getchar();
    for(;k<'0'||k>'9';k=getchar())if(k=='-')t=-1;
    for(;k>='0'&&k<='9';k=getchar())x=(x<<1)+(x<<3)+(k^48);x*=t;
}

int n, x, y, d, ans;
inline int fmin(const int&a, const int&b) { return a<b?a:b; }
inline int fabs(const int&a) { return a>0?a:-a; }

int main(void)
{
  int T; gi(T);
  while(T--)
  {
    gi(n), gi(x), gi(y), gi(d);
    ans=1e9;
    if(x+d == y || x-d == y) puts("1");
    else if(fabs(y-x)%d == 0) ans=fabs(y-x)/d;
    else if((n-y)%d == 0) ans=fmin(ans, (n-y)/d+(n-x+d-1)/d);
    else if((y-1)%d == 0) ans=fmin(ans, (y-1)/d+(x+d-2) / d);
    else puts("-1");

    if(ans != 1e9) printf("%d\n", ans);
  }
  return 0;
}


#include<bits/stdc++.h>
#define inf 2e9+10
using namespace std;
int main()
{
    int t;
    cin>>t;
    int n,x,y,d;

    while(t--){
             int ans=inf;
        cin>>n>>x>>y>>d;

        if(abs(x-y)%d==0) ans=min(ans,abs(x-y)/d);

        if((y-1)%d==0){
            int ans1=(x-1)/d+1;
            ans1+=(y-1)/d;
            ans=min(ans1,ans);
        }
        if((n-y)%d==0){
            int ans1=(n-x)/d+1;
            ans1+=(n-y)/d;
            ans=min(ans,ans1);
        }
        if(ans==inf) cout<<-1<<endl;
        else cout<<ans<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define LL long long
using namespace std;

int main()
{
    int t;
    LL n,x,y,d;
    cin>>t;
    while(t--)
    {   cin>>n>>x>>y>>d;
        LL sum=0;
        if(abs(y-x)%d==0)
        {   sum=(y-x)/d;
        }
        else
        {   LL tcnt1=-1,tcnt2=-1;
            if((y-1)%d==0)
            {   tcnt1=(y-1)/d;
                if((x-1)%d==0)
                    tcnt1=(x-1)/d;
                else
                    tcnt1=(x-1)/d+1;
            }
            if((n-y)%d==0)
            {   tcnt2=(n-y)/d;
                if((n-x)%d==0)
                  tcnt2=(n-x)/d;
                else
                    tcnt2=(n-x)/d+1;
            }
            if(tcnt1!=-1&&tcnt2!=-1)
            {   sum=min(tcnt1,tcnt2);
            }
            else if(tcnt1!=-1&&tcnt2==-1)
                sum=tcnt1;
            else if(tcnt1==-1&&tcnt2!=-1)
                sum=tcnt2;
            else
                sum=-1;
        }
        cout<<sum<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define LL long long
using namespace std;

int main()
{
    int t;
    LL n,x,y,d;
    cin>>t;
    while(t--)
    {   cin>>n>>x>>y>>d;
        LL sum=0;
        if(abs(y-x)%d==0)
        {   sum=(y-x)/d;
        }
        else
        {   LL tcnt1=-1,tcnt2=-1;
            if((y-1)%d==0)
            {   tcnt1=(y-1)/d;
                if((x-1)%d==0)
                    tcnt1+=(x-1)/d;
                else
                    tcnt1+=(x-1)/d+1;
            }
            if((n-y)%d==0)
            {   tcnt2=(n-y)/d;
                if((n-x)%d==0)
                  tcnt2+=(n-x)/d;
                else
                    tcnt2+=(n-x)/d+1;
            }
            if(tcnt1!=-1&&tcnt2!=-1)
            {   sum=min(tcnt1,tcnt2);
            }
            else if(tcnt1!=-1&&tcnt2==-1)
                sum=tcnt1;
            else if(tcnt1==-1&&tcnt2!=-1)
                sum=tcnt2;
            else
                sum=-1;
        }
        cout<<sum<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define LL long long
using namespace std;

int main()
{
    int t;
    LL n,x,y,d;
    cin>>t;
    while(t--)
    {   cin>>n>>x>>y>>d;
        LL sum=0;
        if(abs(y-x)%d==0)
        {   sum=(y-x)/d;
        }
        else
        {   LL tcnt1=-1,tcnt2=-1;
            if((x-1)%d==0)
            {   tcnt1=(x-1)/d;
            }
            else
                tcnt1=(x-1)/d+1;
            if((y-1)%d==0)
            {   tcnt1+=(y-1)/d;
            }
            else
            {   tcnt1=-1;
            }
            if((n-x)%d==0)
            {   tcnt2=(n-x)/d;
            }
            else
                tcnt2=(n-x)/d+1;
            if((n-y)%d==0)
            {   tcnt2+=(n-y)/d;
            }
            else
            {   tcnt2=-1;
            }
            if(tcnt1!=-1&&tcnt2!=-1)
            {   sum=min(tcnt1,tcnt2);
            }
            else if(tcnt1!=-1&&tcnt2==-1)
                sum=tcnt1;
            else if(tcnt1==-1&&tcnt2!=-1)
                sum=tcnt2;
            else
                sum=-1;
        }
        cout<<sum<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;

int main()
{
    int t;
    LL n,x,y,d;
    cin>>t;
    while(t--)
    {   cin>>n>>x>>y>>d;
        LL sum=INF;
        if(abs(y-x)%d==0)
        {   sum=(y-x)/d;
        }
        else
        {   LL tcnt1=INF,tcnt2=INF;
            if((y-1)%d==0)
            {   tcnt1=(y-1)/d;

                    tcnt1+=(x-1)/d+1;
            }
            if((n-y)%d==0)
            {   tcnt2=(n-y)/d;

                    tcnt2+=(n-x)/d+1;
            }
            sum=min(sum,min(tcnt1,tcnt2));
        }
        if(sum==INF)
            cout<<-1<<endl;
        else
        cout<<sum<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define LL long long
#define INF 2e10 q
using namespace std;

int main()
{
    int t;
    LL n,x,y,d;
    cin>>t;
    while(t--)
    {   cin>>n>>x>>y>>d;
        LL sum=INF;
        if(abs(y-x)%d==0)
        {   sum=(y-x)/d;
        }
        else
        {   LL tcnt1=INF,tcnt2=INF;
            if((y-1)%d==0)
            {   tcnt1=(y-1)/d;

                    tcnt1+=(x-1)/d+1;
            }
            if((n-y)%d==0)
            {   tcnt2=(n-y)/d;

                    tcnt2+=(n-x)/d+1;
            }
            sum=min(sum,min(tcnt1,tcnt2));
        }
        if(sum==INF)
            cout<<-1<<endl;
        else
        cout<<sum<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define LL long long
#define INF
using namespace std;

int main()
{
    int t;
    LL n,x,y,d;
    cin>>t;
    while(t--)
    {   cin>>n>>x>>y>>d;
        LL sum=INF;
        if(abs(y-x)%d==0)
        {   sum=abs(y-x)/d;
        }
        else
        {   LL tcnt1=INF,tcnt2=INF;
            if((y-1)%d==0)
            {   tcnt1=(y-1)/d;
                if((x-1)%d==0)
                    tcnt1+=(x-1)/d;
                else
                    tcnt1+=(x-1)/d+1;
            }
            if((n-y)%d==0)
            {   tcnt2=(n-y)/d;
                if((n-x)%d==0)
                  tcnt2+=(n-x)/d;
                else
                    tcnt2+=(n-x)/d+1;
            }
            sum=min(sum,min(tcnt1,tcnt2));
        }
        if(sum==INF)
            cout<<-1<<endl;
        else
        cout<<sum<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define MAX 101000
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;

int main()
{   ios::sync_with_stdio(false);cin.tie(0);
    int i,j,t;
    LL n,x,y,d;
    cin>>t;
    while(t--)
    {   cin>>n>>x>>y>>d;
        LL sum=0;
        if(x>y)
            swap(x,y);
        if((y-x)%d==0)
        {   sum=(y-x)/d;
        }
        else
        {   LL cnt1,cnt2;
            if((x-1)%d==0)
            {   cnt1=(x-1)/d;
            }
            else
                cnt1=(x-1)/d+1;
            if((y-1)%d==0)
            {   cnt1+=(y-1)/d;
            }
            else
            {   cnt1=-1;
            }
            if((n-x)%d==0)
            {   cnt2=(n-x)/d;
            }
            else
                cnt2=(n-x)/d+1;
            if((n-y)%d==0)
            {   cnt2+=(n-y)/d;
            }
            else
            {   cnt2=-1;
            }
            if(cnt1!=-1&&cnt2!=-1)
            {   sum=min(cnt1,cnt2);
            }
            else if(cnt1!=-1&&cnt2==-1)
                sum=cnt1;
            else if(cnt1==-1&&cnt2!=-1)
                sum=cnt2;
            else
                sum=-1;

        }
        cout<<sum<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;

int main()
{
    int t;
    LL n,x,y,d;
    cin>>t;
    while(t--)
    {   cin>>n>>x>>y>>d;
        LL sum=INF;
        if(abs(y-x)%d==0)
        {   sum=(y-x)/d;
        }
        else
        {   LL tcnt1=INF,tcnt2=INF;
            if((y-1)%d==0)
            {   tcnt1=(y-1)/d;
                if((x-1)%d==0)
                    tcnt1+=(x-1)/d;
                else
                    tcnt1+=(x-1)/d+1;
            }
            if((n-y)%d==0)
            {   tcnt2=(n-y)/d;
                if((n-x)%d==0)
                  tcnt2+=(n-x)/d;
                else
                    tcnt2+=(n-x)/d+1;
            }
            sum=min(sum,min(tcnt1,tcnt2));
        }
        if(sum==INF)
            cout<<-1<<endl;
        else
        cout<<sum<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define LL long long
#define INF 2e10
using namespace std;

int main()
{
    int t;
    LL n,x,y,d;
    cin>>t;
    while(t--)
    {   cin>>n>>x>>y>>d;
        LL sum=INF;
        if(abs(y-x)%d==0)
        {   sum=(y-x)/d;
        }
        else
        {   LL tcnt1=INF,tcnt2=INF;
            if((y-1)%d==0)
            {   tcnt1=(y-1)/d+(x-1)/d+1;
            }
            if((n-y)%d==0)
            {   tcnt2=(n-y)/d+(n-x)/d+1;
            }
            sum=min(sum,min(tcnt1,tcnt2));
        }
        if(sum==INF)
            cout<<-1<<endl;
        else
        cout<<sum<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define MAX 101000
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;

int main()
{   ios::sync_with_stdio(false);cin.tie(0);
    int i,j,t;
    LL n,x,y,d;
    cin>>t;
    while(t--)
    {   cin>>n>>x>>y>>d;
        LL sum=-1;
        if(x>y)
            swap(x,y);
        if((y-x)%d==0)
        {   sum=(y-x)/d;
        }
        else
        {   LL cnt1=-1,cnt2=-1;
            if((x-1)%d==0)
            {   cnt1=(x-1)/d;
            }
            else
                cnt1=(x-1)/d+1;
            if((y-1)%d==0)
            {   cnt1+=(y-1)/d;
            }
            else
            {   cnt1=-1;
            }
            if((n-x)%d==0)
            {   cnt2=(n-x)/d;
            }
            else
                cnt2=(n-x)/d+1;
            if((n-y)%d==0)
            {   cnt2+=(n-y)/d;
            }
            else
            {   cnt2=-1;
            }
            if(cnt1!=-1&&cnt2!=-1)
            {   sum=min(cnt1,cnt2);
            }
            else if(cnt1!=-1&&cnt2==-1)
                sum=cnt1;
            else if(cnt1==-1&&cnt2!=-1)
                sum=cnt2;
            else
                sum=-1;

        }
        cout<<sum<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define LL long long
#define INF 2e10
using namespace std;

int main()
{
    int t;
    LL n,x,y,d;
    cin>>t;
    while(t--)
    {   cin>>n>>x>>y>>d;
        LL sum=INF;
        if(abs(y-x)%d==0)
        {   sum=(y-x)/d;
        }
          LL tcnt1=INF,tcnt2=INF;
            if((y-1)%d==0)
            {   tcnt1=(y-1)/d+(x-1)/d+1;
            }
            if((n-y)%d==0)
            {   tcnt2=(n-y)/d+(n-x)/d+1;
            }
            sum=min(sum,min(tcnt1,tcnt2));

        if(sum==INF)
            cout<<-1<<endl;
        else
        cout<<sum<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;

int main()
{
    int t;
    LL n,x,y,d;
    cin>>t;
    while(t--)
    {   cin>>n>>x>>y>>d;
        LL sum=INF;
        if(x>y)
            swap(x,y);
        if(y-x%d==0)
        {   sum=(y-x)/d;
        }
        else
        {   LL tcnt1=INF,tcnt2=INF;
            if((y-1)%d==0)
            {   tcnt1=(y-1)/d;
                if((x-1)%d==0)
                    tcnt1+=(x-1)/d;
                else
                    tcnt1+=(x-1)/d+1;
            }
            if((n-y)%d==0)
            {   tcnt2=(n-y)/d;
                if((n-x)%d==0)
                  tcnt2+=(n-x)/d;
                else
                    tcnt2+=(n-x)/d+1;
            }
            sum=min(sum,min(tcnt1,tcnt2));
        }
        if(sum==INF)
            cout<<-1<<endl;
        else
        cout<<sum<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;

int main()
{
    int t;
    LL n,x,y,d;
    cin>>t;
    while(t--)
    {   cin>>n>>x>>y>>d;
        LL sum=INF;
        if(x>y)
            swap(x,y);
        if((y-x)%d==0)
        {   sum=(y-x)/d;
        }
        else
        {   LL tcnt1=INF,tcnt2=INF;
            if((y-1)%d==0)
            {   tcnt1=(y-1)/d;
                if((x-1)%d==0)
                    tcnt1+=(x-1)/d;
                else
                    tcnt1+=(x-1)/d+1;
            }
            if((n-y)%d==0)
            {   tcnt2=(n-y)/d;
                if((n-x)%d==0)
                  tcnt2+=(n-x)/d;
                else
                    tcnt2+=(n-x)/d+1;
            }
            sum=min(sum,min(tcnt1,tcnt2));
        }
        if(sum==INF)
            cout<<-1<<endl;
        else
        cout<<sum<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define MAX 101000
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;

int main()
{   ios::sync_with_stdio(false);cin.tie(0);
    int i,j,t;
    LL n,x,y,d;
    cin>>t;
    while(t--)
    {   cin>>n>>x>>y>>d;
        LL sum=0;
        if(x>y)
            swap(x,y);
        if((y-x)%d==0)
        {   sum=(y-x)/d;
        }
        else
        {   LL cnt1,cnt2;
            if((x-1)%d==0)
            {   cnt1=(x-1)/d;
            }
            else
                cnt1=(x-1)/d+1;
            if((y-1)%d==0)
            {   cnt1+=(y-1)/d;
            }
            else
            {   cnt1=-1;
            }
            if((n-x)%d==0)
            {   cnt2=(n-x)/d;
            }
            else
                cnt2=(n-x)/d+1;
            if((n-y)%d==0)
            {   cnt2+=(n-y)/d;
            }
            else
            {   cnt2=-1;
            }
            sum=max(cnt1,cnt2);
        }
        cout<<sum<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;

int main()
{
    int t;
    LL n,x,y,d;
    cin>>t;
    while(t--)
    {   cin>>n>>x>>y>>d;
        LL sum=INF;
        if(abs(y-x)%d==0)
        {   sum=abs(y-x)/d;
        }
        else
        {   LL tcnt1=INF,tcnt2=INF;
            if((y-1)%d==0)
            {   tcnt1=(y-1)/d;
                if((x-1)%d==0)
                    tcnt1+=(x-1)/d;
                else
                    tcnt1+=(x-1)/d+1;
            }
            if((n-y)%d==0)
            {   tcnt2=(n-y)/d;
                if((n-x)%d==0)
                  tcnt2+=(n-x)/d;
                else
                    tcnt2+=(n-x)/d+1;
            }
            sum=min(sum,min(tcnt1,tcnt2));
        }
        if(sum==INF)
            cout<<-1<<endl;
        else
        cout<<sum<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define LL long long
#define INF 2e10
using namespace std;

int main()
{
    int t;
    LL n,x,y,d;
    cin>>t;
    while(t--)
    {   cin>>n>>x>>y>>d;
        LL sum=INF;
        if(abs(y-x)%d==0)
        {   sum=(y-x)/d;
        }
        else
        {   LL tcnt1=INF,tcnt2=INF;
            if((y-1)%d==0)
            {   tcnt1=(y-1)/d;

                    tcnt1+=(x-1)/d+1;
            }
            if((n-y)%d==0)
            {   tcnt2=(n-y)/d;

                    tcnt2+=(n-x)/d+1;
            }
            sum=min(sum,min(tcnt1,tcnt2));
        }
        if(sum==INF)
            cout<<-1<<endl;
        else
        cout<<sum<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
int main()
{
    int t;
    scanf("%d",&t);
    while (t--)
        {
            int n,x,y,d,u,l,ans=0;
            scanf("%d%d%d%d",&n,&x,&y,&d);
            if ((x%d)==(y%d))
                {
                    u=abs(x-y);
                    printf("%d\n",u/d);
                }
            if ((x%d)!=(y%d))
                {
                    if ((y%d)!=1 && (y%d)!=(n%d))
                        {
                            printf("-1 \n");
                            continue;
                        }
                    if (y==1)
                        {
                            l=x-1;
                             if ((l%d)!=0)
                                ans=l/d+1;
                            else ans+=l/d;
                            printf("%d\n",ans);
                            continue;
                        }
                    if (y==n)
                        {
                            l=n-x;
                             if ((l%d)!=0)
                                ans=l/d+1;
                            else ans+=l/d;
                            printf("%d\n",ans);
                            continue;
                        }
                    if ((y%d)==1 && (n%d)!=1)
                        {
                            l=x-1;
                            if ((l%d)!=0)
                                ans=l/d+1;
                            else ans+=l/d;
                            ans+=(y-1)/d;
                        }
                    if ((y%d)!=1 && (n%d)==(y%d))
                        {
                            l=n-x;
                            if ((l%d)!=0)
                                ans=l/d+1;
                            else ans+=l/d;
                            ans+=(n-y)/d;
                        }
                    if ((y%d)==1&&(n%d)==1)
                    {
                        int a=x-1,b=y-1,c=0,q=0,s1=0;
                        int e=n-x,f=n-y,g=0,h=0,s2=0;
                        if (a%d==0)
                             c+=a/d;
                        else c+=a/d+1;
                        if (b%d==0)
                            q+=b/d;
                        else q+=b/d+1;
                        if (e%d==0)
                            g+=e/d;
                        else g+=e/d+1;
                        if (f%d==0)
                             h+=f/d;
                        else h+=f/d+1;
                        s1=c+q;
                        s2=g+h;
                        ans=min(s1,s2);
                        printf("%d\n",ans);
                    }
                }
        }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
int main()
{
    int t;
    scanf("%d",&t);
    while (t--)
        {
            int n,x,y,d,u,l,ans=0;
            scanf("%d%d%d%d",&n,&x,&y,&d);
            if ((x%d)==(y%d))
                {
                    u=abs(x-y);
                    printf("%d\n",u/d);
                }
            if ((x%d)!=(y%d))
                {
                    if ((y%d)!=1 && (y%d)!=(n%d))
                        {
                            printf("-1 \n");
                            continue;
                        }
                    if (y==1)
                        {
                            l=x-1;
                             if ((l%d)!=0)
                                ans=l/d+1;
                            else ans+=l/d;
                            printf("%d\n",ans);
                            continue;
                        }
                    if (y==n)
                        {
                            l=n-x;
                             if ((l%d)!=0)
                                ans=l/d+1;
                            else ans+=l/d;
                            printf("%d\n",ans);
                            continue;
                        }
                    if ((y%d)==1 && (n%d)!=1)
                        {
                            l=x-1;
                            if ((l%d)!=0)
                                ans=l/d+1;
                            else ans+=l/d;
                            ans+=(y-1)/d;
                        }
                    if ((y%d)!=1 && (n%d)==(y%d))
                        {
                            l=n-x;
                            if ((l%d)!=0)
                                ans=l/d+1;
                            else ans+=l/d;
                            ans+=(n-y)/d;
                        }
                    if ((y%d)==1&&(n%d)==1)
                    {
                        int a=x-1,b=y-1,c=0,q=0,s1=0;
                        int e=n-x,f=n-y,g=0,h=0,s2=0;
                        if (a%d==0)
                             c+=a/d;
                        else c+=a/d+1;
                        if (b%d==0)
                            q+=b/d;
                        else q+=b/d+1;
                        if (e%d==0)
                            g+=e/d;
                        else g+=e/d+1;
                        if (f%d==0)
                             h+=f/d;
                        else h+=f/d+1;
                        s1=c+q;
                        s2=g+h;
                        ans=min(s1,s2);
                    }
                    printf("%d\n",ans);
                }
        }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
int main()
{
    int t,n,x,y,d,u,l,ans=0;
    scanf("%d",&t);
    while (t--)
        {
            scanf("%d%d%d%d",&n,&x,&y,&d);
            if ((x%d)==(y%d))
                {
                    u=abs(x-y);
                    printf("%d\n",u/d);
                }
            if ((x%d)!=(y%d))
                {
                    if ((y%d)!=1 && (y%d)!=(n%d))
                        {
                            printf("-1 \n");
                            continue;
                        }
                    if (y==1 || y==n)
                        {
                            l=abs(x-y);
                            if (l%d==0)
                                ans+=l/d;
                            else
                                ans+=l/d+1;
                            printf("%d\n",ans);
                            continue;
                        }
                    if ((y%d)==1 && (n%d)!=1)
                        {
                            l=x-1;
                            if ((l%d)!=0)
                                ans=l/d+1;
                            else ans+=l/d;
                            ans+=(y-1)/d;
                        }
                    if ((y%d)!=1 && (n%d)==(y%d))
                        {
                            l=n-x;
                            if ((l%d)!=0)
                                ans=l/d+1;
                            else ans+=l/d;
                            ans+=(n-y)/d;
                        }
                    if ((y%d)==1&&(n%d)==1)
                        {
                            l=min(n-x,x-1);
                            if ((l%d)!=0)
                                ans=l/d+1;
                            else ans+=l/d;
                            if (l==x-1)
                                ans+=(y-1)/d;
                            else
                                ans+=(n-y)/d;
                        }
                    printf("%d\n",ans);
                }
        }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
int main()
{
    int t,n,x,y,d,u,l,ans=0;
    scanf("%d",&t);
    while (t--)
        {
            scanf("%d%d%d%d",&n,&x,&y,&d);
            if ((x%d)==(y%d))
                {
                    u=abs(x-y);
                    printf("%d\n",u/d);
                }
            if ((x%d)!=(y%d))
                {
                    if ((y%d)!=1 && (y%d)!=(n%d))
                        {
                            printf("-1 \n");
                            continue;
                        }
                    if ((y%d)==1 && (n%d)!=1)
                        {
                            l=x-1;
                            if ((l%d)!=0)
                                ans=l/d+1;
                            else ans+=l/d;
                            ans+=(y-1)/d;
                        }
                    if ((y%d)!=1 && (n%d)==(y%d))
                        {
                            l=n-x;
                            if ((l%d)!=0)
                                ans=l/d+1;
                            else ans+=l/d;
                            ans+=(n-y)/d;
                        }
                    if ((y%d)==1&&(n%d)==1)
                        {
                            l=min(n-x,x-1);
                            if ((l%d)!=0)
                                ans=l/d+1;
                            else ans+=l/d;
                            if (l==x-1)
                                ans+=(y-1)/d;
                            else
                                ans+=(n-y)/d;
                        }
                    printf("%d\n",ans);
                }
        }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
int main()
{
    int t;
    scanf("%d",&t);
    while (t--)
        {
            int n,x,y,d,u,l,ans=0;
            scanf("%d%d%d%d",&n,&x,&y,&d);
            if ((x%d)==(y%d))
                {
                    u=abs(x-y);
                    printf("%d\n",u/d);
                }
            if ((x%d)!=(y%d))
                {
                    if ((y%d)!=1 && (y%d)!=(n%d))
                        {
                            printf("-1 \n");
                            continue;
                        }
                    int a=x-1,b=y-1,c=0,q=0,s1=0;
                    int e=n-x,f=n-y,g=0,h=0,s2=0;
                    if (a%d==0)
                        c+=a/d;
                    else c+=a/d+1;
                    if (b%d==0)
                        q+=b/d;
                    else q+=b/d+1;
                    if (e%d==0)
                        g+=e/d;
                    else g+=e/d+1;
                    if (f%d==0)
                        h+=f/d;
                    else h+=f/d+1;
                    s1=c+q;
                    s2=g+h;
                    ans=min(s1,s2);
                    printf("%d\n",ans);
                }
        }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
int main()
{
    int t;
    scanf("%d",&t);
    while (t--)
        {
            int n,x,y,d,u,l,ans=0;
            scanf("%d%d%d%d",&n,&x,&y,&d);
            if ((x%d)==(y%d))
                {
                    u=abs(x-y);
                    printf("%d\n",u/d);
                }
            if ((x%d)!=(y%d))
                {
                    if ((y%d)!=1 && (y%d)!=(n%d))
                        {
                            printf("-1 \n");
                            continue;
                        }
                    if (y==1)
                        {
                            l=x-1;
                             if ((l%d)!=0)
                                ans=l/d+1;
                            else ans+=l/d;
                            printf("%d\n",ans);
                            continue;
                        }
                    if (y==n)
                        {
                            l=n-x;
                             if ((l%d)!=0)
                                ans=l/d+1;
                            else ans+=l/d;
                            printf("%d\n",ans);
                            continue;
                        }
                    if ((y%d)==1 && (n%d)!=1)
                        {
                            l=x-1;
                            if ((l%d)!=0)
                                ans=l/d+1;
                            else ans+=l/d;
                            ans+=(y-1)/d;
                        }
                    if ((y%d)!=1 && (n%d)==(y%d))
                        {
                            l=n-x;
                            if ((l%d)!=0)
                                ans=l/d+1;
                            else ans+=l/d;
                            ans+=(n-y)/d;
                        }
                    if ((y%d)==1&&(n%d)==1)
                        {
                            l=min(n-x,x-1);
                            if ((l%d)!=0)
                                ans=l/d+1;
                            else ans+=l/d;
                            if (l==x-1)
                                ans+=(y-1)/d;
                            else
                                ans+=(n-y)/d;
                        }
                    printf("%d\n",ans);
                }
        }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
int main()
{
    int t;
    scanf("%d",&t);
    while (t--)
        {
            int n,x,y,d,u,l,ans=0;
            scanf("%d%d%d%d",&n,&x,&y,&d);
            if ((x%d)==(y%d))
                {
                    u=abs(x-y);
                    printf("%d\n",u/d);
                }
            if ((x%d)!=(y%d))
                {
                    if ((y%d)!=1 && (y%d)!=(n%d))
                        {
                            printf("-1 \n");
                            continue;
                        }
                    if ((y==1 || y==n))
                        {
                            l=abs(x-y);
                            if (l%d==0)
                                ans+=l/d;
                            else
                                ans+=l/d+1;
                            printf("%d\n",ans);
                            continue;
                        }
                    if ((y%d)==1 && (n%d)!=1)
                        {
                            l=x-1;
                            if ((l%d)!=0)
                                ans=l/d+1;
                            else ans+=l/d;
                            ans+=(y-1)/d;
                        }
                    if ((y%d)!=1 && (n%d)==(y%d))
                        {
                            l=n-x;
                            if ((l%d)!=0)
                                ans=l/d+1;
                            else ans+=l/d;
                            ans+=(n-y)/d;
                        }
                    if ((y%d)==1&&(n%d)==1)
                        {
                            l=min(n-x,x-1);
                            if ((l%d)!=0)
                                ans=l/d+1;
                            else ans+=l/d;
                            if (l==x-1)
                                ans+=(y-1)/d;
                            else
                                ans+=(n-y)/d;
                        }
                    printf("%d\n",ans);
                }
        }
}


#include<iostream>
#include<stdio.h>
#include<cmath>
using  namespace std;
const int MAX=0x7fffffff;
int t,n,x,y,d,step,step1,step2;
bool arrive(int x,int y,int d){
	int dis=abs(x-y);
	//double num=abs(x-y)/d;//以后不用这个 用int判断;
	if(/*num==int(num)*/dis%d==0){
		step=/*int(num)*/dis/d;
		//cout<<"在arrive函数中的step: "<<step<<endl;
		return true;
	}
	return false;
}
int main(){
////	freopen("D:/C++/in.txt","r",stdin);
	cin>>t;
	for(int i=0;i<t;i++){
		cin>>n>>x>>y>>d;
		step=0;
		step1=MAX;
		step2=MAX;
		if(arrive(x,y,d)){
			cout<<step<<endl;
			continue;
		}
		if(arrive(1,y,d)){
			step1=step+abs(x-1+d-1)/d;
		//	cout<<"step1:　"<<step1<<endl;
		}
		if(arrive(n,y,d)){
			step2=step+abs(n-x+d-1)/d;
		//	cout<<"step2：　"<<step2<<endl;
		}
		step=step1>step2?step2:step1;
		if(step==MAX){
			step=-1;
		}
		cout<<step<<endl;
	}
	return 0;
}


#include<iostream>
#include<stdio.h>
#include<cmath>
using  namespace std;
const int MAX=0Xfffffff;
int t,n,x,y,d,step,step1,step2;
bool arrive(int x,int y,int d){
	int dis=abs(x-y);
//	double num=abs(x-y)/d;
	if(/*num==int(num)*/dis%d==0){
		step=/*int(num)*/dis/d;
		//cout<<"在arrive函数中的step: "<<step<<endl;
		return true;
	}
	return false;
}
int main(){
//	freopen("D:/C++/in.txt","r",stdin);
	cin>>t;
	for(int i=0;i<t;i++){
		cin>>n>>x>>y>>d;
		step=0;
		step1=MAX;
		step2=MAX;
		if(arrive(x,y,d)){
			cout<<step<<endl;
			continue;
		}
		if(arrive(1,y,d)){
			step1=step+abs(x-1+d-1)/d;
		//	cout<<"step1:　"<<step1<<endl;
		}
		if(arrive(n,y,d)){
			step2=step+abs(n-x+d-1)/d;
		//	cout<<"step2：　"<<step2<<endl;
		}
		step=step1>step2?step2:step1;
		if(step==MAX){
			step=-1;
		}
		cout<<step<<endl;
	}
	return 0;
}


#include<iostream>
#include<stdio.h>
#include<cmath>
using  namespace std;
const int MAX=0Xfffffff;
int t,n,x,y,d,step,step1,step2;
bool arrive(int x,int y,int d){
	double num=abs(x-y)/d;
	if(num==int(num)){
		step=int(num);
		return true;
	}
	return false;
}
int main(){
	//freopen("D:/C++/in.txt","r",stdin);
//	cout<<MAX<<endl;
	cin>>t;
	for(int i=0;i<t;i++){
		cin>>n>>x>>y>>d;
		step=0;
		step1=MAX;
		step2=MAX;
		if(arrive(x,y,d)){
			cout<<step<<endl;
			continue;
		}
		if(arrive(1,y,d)){
			step1=step+(x-1+d-1)/d;
		}
		if(arrive(n,y,d)){
			step2=step+(n-x+d-1)/d;
		}
		step=step1>step2?step2:step1;
		if(step==MAX){
			step=-1;
		}
		cout<<step<<endl;
	}
	return 0;
}


#include<iostream>
#include<stdio.h>
#include<cmath>
using  namespace std;
const int MAX=0x7fffffff;
int t,n,x,y,d,step,step1,step2;
bool arrive(int x,int y,int d){
	int dis=abs(x-y);
	//double num=abs(x-y)/d;//以后不用这个 用int判断;
	if(/*num==int(num)*/dis%d==0){
		step=/*int(num)*/dis/d;
		//cout<<"在arrive函数中的step: "<<step<<endl;
		return true;
	}
	return false;
}
int main(){
//	freopen("D:/C++/in.txt","r",stdin);
	cin>>t;
	for(int i=0;i<t;i++){
		cin>>n>>x>>y>>d;
		step=0;
		step1=MAX;
		step2=MAX;
		if(arrive(x,y,d)){
			cout<<step<<endl;
			continue;
		}
		if(arrive(1,y,d)){
			step1=step+abs(x-1+d-1)/d;
		//	cout<<"step1:　"<<step1<<endl;
		}
		if(arrive(n,y,d)){
			step2=step+abs(n-x+d-1)/d;
		//	cout<<"step2：　"<<step2<<endl;
		}
		step=step1>step2?step2:step1;
		if(step==MAX){
			step=-1;
		}
		cout<<step<<endl;
	}
	return 0;
}


#include<iostream>
#include<stdio.h>
#include<cmath>
using  namespace std;
const int MAX=0x7fffffff;
int t,n,x,y,d,step,step1,step2;
bool arrive(int x,int y,int d){
/*	int dis=abs(x-y);*/
	double num=abs(x-y)/d;
	if(num==int(num)/*dis%d==0*/){
		step=int(num)/*dis/d*/;
		//cout<<"在arrive函数中的step: "<<step<<endl;
		return true;
	}
	return false;
}
int main(){
//	freopen("D:/C++/in.txt","r",stdin);
	cin>>t;
	for(int i=0;i<t;i++){
		cin>>n>>x>>y>>d;
		step=0;
		step1=MAX;
		step2=MAX;
		if(arrive(x,y,d)){
			cout<<step<<endl;
			continue;
		}
		if(arrive(1,y,d)){
			step1=step+abs(x-1+d-1)/d;
		//	cout<<"step1:　"<<step1<<endl;
		}
		if(arrive(n,y,d)){
			step2=step+abs(n-x+d-1)/d;
		//	cout<<"step2：　"<<step2<<endl;
		}
		step=step1>step2?step2:step1;
		if(step==MAX){
			step=-1;
		}
		cout<<step<<endl;
	}
	return 0;
}


#include<iostream>
#include<stdio.h>
#include<cmath>
using  namespace std;
const int MAX=0Xfffffff;
int t,n,x,y,d,step,step1,step2;
bool arrive(int x,int y,int d){
	double num=abs(x-y)/d;
	if(num==int(num)){
		step=int(num);
		return true;
	}
	return false;
}
int main(){
	//freopen("D:/C++/in.txt","r",stdin);
//	cout<<MAX<<endl;
	cin>>t;
	for(int i=0;i<t;i++){
		cin>>n>>x>>y>>d;
		step=0;
		step1=MAX;
		step2=MAX;
		if(arrive(x,y,d)){
			cout<<step<<endl;
			continue;
		}
		if(arrive(1,y,d)){
			step1=step+abs(x-1+d-1)/d;
		}
		if(arrive(n,y,d)){
			step2=step+abs(n-x+d-1)/d;
		}
		step=step1>step2?step2:step1;
		if(step==MAX){
			step=-1;
		}
		cout<<step<<endl;
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#define all(i) i.begin(), i.end()
using namespace std;
typedef long long ll;
main() {
	int n, m, cnt = 0;
	cin >> n >> m;
	vector<vector<int>> v(m - 1);
	vector<vector<ll>> cost(m - 1, vector<ll>(n, 0));
	for (int i = 0; i < n; ++i) {
		int x, y;
		cin >> x >> y;
		if (x == 1) ++cnt;
		else v[x - 2].push_back(y);
	}
	for (int i = 0; i < m - 1; ++i) {
		sort(all(v[i]));
		for (int j : v[i]) cost[i][0] += j;
		for (int j = 1; j < n; ++j)
			cost[i][j] = cost[i][j - 1] - (j > v[i].size() ? 0 : v[i][v[i].size() - j]);
	}
	ll ans = 1ll << 60;
	for (int self = cnt; self <= n; ++self) {
		int tmp = cnt;
		ll sum = 0;
		priority_queue<int, vector<int>, greater<int>> q;
		for (int i = 0; i < m - 1; ++i) {
			if (v[i].size() >= self) sum += cost[i][self - 1], tmp += v[i].size() - self + 1;
			for (int j = v[i].size() - 1; j >= max(0, (int) v[i].size() - self + 1); --j)
				q.push(v[i][j]);
		}
		while (tmp < self)
			++tmp, sum += q.top(), q.pop();
		ans = min(ans, sum);
	}
	cout << ans;
}


#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#define all(i) i.begin(), i.end()
using namespace std;
typedef long long ll;
main() {
	int n, m, cnt = 0;
	cin >> n >> m;
	vector<vector<ll>> v(m - 1), cost(m - 1, vector<ll>(n / 2 + 1));
	for (int i = 0; i < n; ++i) {
		int x, y;
		cin >> x >> y;
		if (x == 1) ++cnt;
		else v[x - 2].push_back(y);
	}
	for (int i = 0; i < m - 1; ++i) {
		sort(all(v[i]));
		cost[i][0] = accumulate(all(v[i]), 0ll);
		for (int j = 1; j < n / 2 + 1; ++j)
			cost[i][j] = cost[i][j - 1] - (j > v[i].size() ? 0 : v[i][v[i].size() - j]);
	}
	ll ans = 1ll << 60;
	for (int self = cnt; self <= n; ++self) {
		int tmp = cnt;
		ll sum = 0;
		priority_queue<int, vector<int>, greater<int>> q;
		for (int i = 0; i < m - 1; ++i) {
			if (v[i].size() >= self) sum += cost[i][self - 1], tmp += v[i].size() - self + 1;
			for (int j = v[i].size() - 1; j >= max(0, (int) v[i].size() - self + 1); --j)
				q.push(v[i][j]);
		}
		while (tmp < self)
			++tmp, sum += q.top(), q.pop();
		ans = min(ans, sum);
	}
	cout << ans;
}


/*************************************************************************
    > File Name: A.cpp
    > Author: Roundgod
    > Mail: wcysai@foxmail.com
    > Created Time: 2018-08-11 21:36:48
 ************************************************************************/

#pragma GCC optimize(3)
#include<bits/stdc++.h>
#define MAXN 3005
#define INF 1000000000000000000LL
#define MOD 1000000007
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
ll n,m,p[MAXN],c[MAXN],cc[MAXN];
multiset<ll> s[MAXN];
multiset<ll> tot;
int main()
{
    scanf("%lld%lld",&n,&m);
    memset(cc,0,sizeof(cc));
    bool f=false;
    for(ll i=1;i<=n;i++)
    {
        scanf("%lld%lld",&p[i],&c[i]);
        cc[p[i]]++;
        s[p[i]].insert(c[i]);
        if(p[i]!=1) tot.insert(c[i]);
    }
    for(ll i=2;i<=m;i++) if(cc[i]>=cc[1]) f=true;
    if(!f) {puts("0"); return 0;}
    ll ans=INF;
    ll res=0,cnt=cc[1];
    for(ll i=n/2+1;i>=max(cc[1],1LL);i--)
    {
        for(ll j=2;j<=m;j++)
        {
            while((ll)s[j].size()>=i)
            {
                res+=*s[j].begin();
                tot.erase(tot.find(*s[j].begin()));
                s[j].erase(s[j].begin());
                cnt++;
            }
        }
        vector<ll> save;
        save.clear();
        while(cnt<i)
        {
            cnt++;
            res+=*tot.begin();
            save.push_back(*tot.begin());
            tot.erase(tot.begin());
        }
        ans=min(ans,res);
        for(ll j=0;j<(int)save.size();j++)
        {
            cnt--;
            tot.insert(save[j]);
            res-=save[j];
        }
    }
    printf("%lld\n",ans);
    return 0;
}


/*************************************************************************
    > File Name: A.cpp
    > Author: Roundgod
    > Mail: wcysai@foxmail.com
    > Created Time: 2018-08-11 21:36:48
 ************************************************************************/

#pragma GCC optimize(3)
#include<bits/stdc++.h>
#define MAXN 100005
#define INF 1000000000000000000LL
#define MOD 1000000007
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
ll n,m,p[MAXN],c[MAXN],cc[MAXN];
multiset<ll> s[MAXN];
multiset<ll> tot;
int main()
{
    scanf("%lld%lld",&n,&m);
    memset(cc,0,sizeof(cc));
    ll MX=-1;
    for(ll i=1;i<=n;i++)
    {
        scanf("%lld%lld",&p[i],&c[i]);
        cc[p[i]]++;
        MX=max(MX,cc[p[i]]);
    }
    ll id=-1;
    for(int i=1500;i>=0;i--)
    {
        int res=0;
        for(int j=2;j<=m;j++)
            if(cc[j]>i) res+=cc[j]-i;
        if(res+cc[1]>i) {id=i; break;}
    }
    ll ans=INF;
    for(ll i=id;i<=MX;i++)
    {
        ll res=0;
        tot.clear();
        for(ll j=1;j<=m;j++) s[j].clear();
        for(ll j=1;j<=n;j++)
        {
            if(p[j]!=1) tot.insert(c[j]);
            s[p[j]].insert(c[j]);
        }
        ll cnt=0,mx=-INF;
        for(ll j=2;j<=m;j++)
        {
            while((int)s[j].size()>i)
            {
                auto it=s[j].begin();
                s[j].erase(s[j].begin());
                res+=*it;
                tot.erase(*it);
                cnt++;
            }
            mx=max(mx,(ll)s[j].size());
        }
        while((int)s[1].size()+cnt<=mx)
        {
            cnt++;
            res+=*tot.begin();
            tot.erase(tot.begin());
        }
        ans=min(ans,res);
    }
    printf("%lld\n",ans);
    return 0;
}