// Arup Guha
// 10/14/2013
// Solution to 2009 South East Regional Problem F: The Ninja Way
import java.util.*;

class pair implements Comparable<pair> {

	int value;
	int index;

	public pair(int number, int rank) {
		value = number;
		index = rank;
	}

	public int compareTo(pair other) {
		return this.value - other.value;
	}
}

public class f {

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);

		int n = stdin.nextInt();
		int d = stdin.nextInt();

		while (n != 0) {

			// Read the numbers and sort in order they must be traversed.
			pair[] numbers = new pair[n];
			int[] save = new int[n];
			for (int i=0; i<n; i++) {
				int tmp = stdin.nextInt();
				numbers[i] = new pair(tmp, i);
				save[i] = tmp;
			}
			Arrays.sort(numbers);

			int[] pos = new int[n];

			// Anchor with the smaller tree to the left of the larger tree.
			if (numbers[0].index > numbers[n-1].index) {
				for (int i=0; i<n/2; i++) {
					pair temp = numbers[i];
					numbers[i] = numbers[n-1-i];
					numbers[n-1-i] = temp;
				}
			}

			// We will index the smallest tree at x = 0.
			pos[numbers[0].index] = 0;
			int anchor = numbers[0].index;

			// Just space everything out by 1 for right now.
			for (int i=anchor-1; i>=0; i--)
				pos[i] = i-anchor;
			for (int i=anchor+1; i<pos.length; i++)
				pos[i] = pos[i-1] + 1;

			int ans = 0;

			// Check if this basic arrangement works - it must if any will.
			if (!canDo(pos, numbers, d))
				ans = -1;

			// Use binary search to iteratively get the best answer for each successive tree.
			else {
				for (int i=anchor+1; i<=numbers[n-1].index; i++)
					pos[i] = binSearch(pos, numbers, i, d);
				ans = pos[numbers[n-1].index];
			}

			// Here's our answer!
			System.out.println(ans);
			n = stdin.nextInt();
			d = stdin.nextInt();
		}

	}

	public static int binSearch(int[] pos, pair[] numbers, int curIndex, int d) {

		// low and high bounds for this position, jumping by 1 or d...
		int low = pos[curIndex-1] + 1;
		int high = pos[curIndex-1] + d;

		// Typical binary search.
		while (low < high-1) {

			int mid = (low+high)/2;

			// Fill in mid in this spot, and just add 1 to the rest for now.
			pos[curIndex] = mid;
			for (int i=curIndex+1; i<pos.length; i++)
				pos[i] = pos[i-1] + 1;

			// See if we can do this or not.
			if (canDo(pos, numbers, d))
				low = mid;
			else
				high = mid-1;
		}

		// Try high one last time.
		pos[curIndex] = high;
		for (int i=curIndex+1; i<pos.length; i++)
			pos[i] = pos[i-1] + 1;
		if (canDo(pos, numbers, d))
			return high;
		return low;
	}

	// Just check if consecutive trees are close enough or not.
	public static boolean canDo(int[] pos, pair[] numbers, int d) {

		for (int i=0; i<numbers.length-1; i++)
			if (Math.abs(pos[numbers[i].index] - pos[numbers[i+1].index]) > d)
				return false;

		return true;
	}
}