You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).

To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.

For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:

choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;

choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;

choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.

Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.


-----Input-----

The first line contains one integer $t$ ($1 \le t \le 500$) — the number of test cases.

Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) — the number of elements in the sequence and the initial value of $x$.

The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).

The sum of values of $n$ over all test cases in the input does not exceed $500$.


-----Output-----

For each test case, print one integer — the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.


-----Examples-----

Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3


-----Note-----

None

Difficulty: MEDIUM_HARD
Tags: Dynamic programming

Sorting

Greedy algorithms
Skill types: Dynamic programming

Sorting

Greedy algorithms

Starter code:

Solutions:
for i in range(int(input())):
	(n, x) = map(int, input().split())
	arr = list(map(int, input().split()))
	count = 0
	for i in range(n):
		if sorted(arr) == arr:
			break
		if arr[i] > x:
			(x, arr[i]) = (arr[i], x)
			count += 1
	print(count) if sorted(arr) == arr else print(-1)


t = int(input())

def is_sorted(a, i, j):
	if i == j or i + 1 == j:
		return True
	if a[i] > a[i + 1]:
		return False
	for ii in range(i + 1, j):
		if a[ii - 1] > a[ii]:
			return False
	return True
while t:
	(n, x) = list(map(int, input().split()))
	arr = list(map(int, input().split()))
	if is_sorted(arr, 0, len(arr)):
		print(0)
	else:
		ans = 0
		for i in range(len(arr)):
			if arr[i] > x:
				tmp = arr[i]
				arr[i] = x
				x = tmp
				ans += 1
			if is_sorted(arr, i, len(arr)):
				break
		if is_sorted(arr, 0, len(arr)):
			print(ans)
		else:
			print(-1)
	t -= 1


tc = int(input())
while tc:
	(n, x) = map(int, input().split())
	li = list(map(int, input().split()))
	pre = [li[0]]
	m = 0

	def not_sorted(li):
		for i in range(1, n):
			if li[i] < li[i - 1]:
				return 1
		return 0
	m = 0
	flag = True
	while not_sorted(li):
		m += 1
		i = 0
		while i < n and li[i] <= x:
			i += 1
		if i == n:
			flag = False
			break
		(li[i], x) = (x, li[i])
	if flag:
		print(m)
	else:
		print(-1)
	tc -= 1


import sys
import math
from collections import defaultdict as dd
mod = 1000000007
T = 1
T = int(sys.stdin.readline())
for _ in range(T):
	(n, x) = map(int, input().split())
	l = list(map(int, input().split()))
	ff = 1
	for i in range(n - 1):
		if l[i] > l[i + 1]:
			ff = 0
			break
	if ff:
		print('0')
		continue
	ans = 0
	flg = 1
	for i in range(n - 1):
		if l[i] > x:
			(x, l[i]) = (l[i], x)
			ans += 1
		if l[i] > l[i + 1]:
			flg = 0
			break
		t = 1
		for j in range(i, n - 1):
			if l[j] > l[j + 1]:
				t = 0
				break
		if t:
			break
	if flg:
		print(ans)
	else:
		print(-1)


import collections
from collections import defaultdict
import math
from collections import Counter

def fun(a):
	flag = True
	for i in range(n - 1):
		if a[i] > a[i + 1]:
			flag = False
			break
	return flag
for _ in range(int(input())):
	(n, x) = [int(x) for x in input().split()]
	a = [int(x) for x in input().split()]
	ans = 0
	flag = fun(a)
	if flag:
		print(0)
		continue
	while not fun(a):
		flag = False
		for i in range(n):
			if a[i] > x:
				(a[i], x) = (x, a[i])
				ans += 1
				flag = True
				break
		if not flag:
			ans = -1
			break
	print(ans)


t = int(input())
for i in range(t):
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	c = sorted(b)
	flag = False
	if b == c:
		print(0)
	else:
		count = 0
		for j in range(a[0]):
			if sorted(b) != b:
				if b[j] > a[1]:
					count += 1
					(b[j], a[1]) = (a[1], b[j])
					if sorted(b) == b:
						print(count)
						flag = True
						break
		if flag == False:
			print(-1)


t = int(input())
for _ in range(t):
	(n, x) = map(int, input().split())
	s = 0
	a = list(map(int, input().split()))
	for i in range(n - 1):
		if a[i] > a[i + 1]:
			s = 1
	if s == 0:
		print(0)
	else:
		c = 0
		for i in range(n - 1):
			if a[i] > x:
				(a[i], x) = (x, a[i])
				c += 1
				jj = 1
				for j in range(i, n - 1):
					if a[j] > a[j + 1]:
						jj = 0
				if jj == 1:
					break
		r = 0
		for i in range(n - 1):
			if a[i] > a[i + 1]:
				r = 1
		if r == 1:
			print(-1)
		else:
			print(c)


t = int(input())
while t != 0:
	(n, x) = map(int, input().split())
	list1 = list(map(int, input().split()))
	ans = 0
	for i in range(n):
		if sorted(list1) == list1:
			break
		if x < list1[i]:
			(list1[i], x) = (x, list1[i])
			ans += 1
	if sorted(list1) == list1:
		print(ans)
	else:
		print(-1)
	t -= 1


import math
for _ in range(int(input())):
	(n, x) = map(int, input().split())
	A = list(map(int, input().split()))
	B = list()
	mm = math.inf
	flag = True
	for i in range(n - 1, -1, -1):
		if not flag:
			B.append(flag)
		elif A[i] <= mm:
			mm = A[i]
			B.append(flag)
		else:
			flag = False
			B.append(flag)
	B.reverse()
	(mx, c) = (0, 0)
	for (i, v) in enumerate(A):
		if B[i] and mx <= A[i]:
			continue
		if v > x:
			(mx, c, x) = (max(mx, x), c + 1, v)
			continue
		if v >= mx:
			mx = v
			continue
		break
	else:
		print(c)
		continue
	print(-1)


import sys
import bisect as bi
import math
from collections import defaultdict as dd
import queue
import heapq
import itertools
input = sys.stdin.readline
import random
mo = 10 ** 9 + 7

def cin():
	return map(int, sin().split())

def ain():
	return list(map(int, sin().split()))

def sin():
	return input().strip()

def inin():
	return int(input())
for _ in range(inin()):
	(n, x) = cin()
	ar = ain()
	mo = 0
	while 1:
		if ar == sorted(ar):
			print(mo)
			break
		if max(ar) <= x:
			print(-1)
			break
		for i in range(n):
			if ar[i] > x:
				(ar[i], x) = (x, ar[i])
				mo += 1
				break


from __future__ import division, print_function
import os, sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

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

def si():
	return input()

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

def msi():
	return map(str, input().strip().split(' '))

def li():
	return list(mi())

def dmain():
	sys.setrecursionlimit(1000000)
	threading.stack_size(1024000)
	thread = threading.Thread(target=main)
	thread.start()
import sys
input = sys.stdin.readline
scanner = lambda : int(input())
string = lambda : input().rstrip()
get_list = lambda : list(read())
read = lambda : map(int, input().split())
get_float = lambda : map(float, input().split())

def ceil(x):
	if x != int(x):
		x = int(x) + 1
	return x

def factorial(x, m):
	val = 1
	while x > 0:
		val = val * x % m
		x -= 1
	return val

def fact(x):
	val = 1
	while x > 0:
		val *= x
		x -= 1
	return val

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def lcm(a, b):
	return a * b // math.gcd(a, b)

def is_integer(n):
	return math.ceil(n) == math.floor(n)

def nCr(n, k):
	if k > n:
		return 0
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return int(res)

def primefs(n):
	primes = {}
	while n % 2 == 0 and n > 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0 and n > 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	if y == 0:
		return 1
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	(prime[0], prime[1]) = (False, False)
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime

def phi(n):
	result = n
	p = 2
	while p * p <= n:
		if n % p == 0:
			while n % p == 0:
				n = n // p
			result = result * (1.0 - 1.0 / float(p))
		p = p + 1
	if n > 1:
		result = result * (1.0 - 1.0 / float(n))
	return int(result)

def is_prime(n):
	if n == 0:
		return False
	if n == 1:
		return True
	for i in range(2, int(n ** (1 / 2)) + 1):
		if not n % i:
			return False
	return True

def next_prime(n, primes):
	while primes[n] != True:
		n += 1
	return n
MAXN = int(100000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i
spf = [0 for i in range(MAXN)]

def factoriazation(x):
	res = []
	for i in range(2, int(x ** 0.5) + 1):
		while x % i == 0:
			res.append(i)
			x //= i
	if x != 1:
		res.append(x)
	return res

def factors(n):
	res = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			res.append(i)
			res.append(n // i)
	return list(set(res))

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

def float_array():
	return list(map(float, input().strip().split()))

def str_array():
	return input().strip().split()

def binary_search(low, high, w, h, n):
	while low < high:
		mid = low + (high - low) // 2
		if check(mid, w, h, n):
			low = mid + 1
		else:
			high = mid
	return low

def check(moves, n):
	val = (moves + 1) // 2
	rem = moves - val
	sol = (val + 1) * (rem + 1)
	return sol < n

def sortSecond(val):
	return val[1]

def derangements(n):
	if n == 0:
		return 1
	return n * derangements(n - 1) + (-1) ** n
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
alphs = 'abcdefghijklmnopqrstuvwxyz'
from itertools import permutations
import math
import bisect as bis
import random
import sys
import collections as collect
import functools as fnt
from decimal import Decimal

def solve():
	(n, x) = read()
	a = get_list()
	c = 0
	for i in range(n):
		if sorted(a) == a:
			break
		if a[i] > x:
			(a[i], x) = (x, a[i])
			c += 1
	if sorted(a) == a:
		print(c)
	else:
		print(-1)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

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

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

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

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

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
t = scanner()
for i in range(t):
	solve()


def testcase():
	(n, x) = list(map(int, input().split()))
	arr = list(map(int, input().split()))
	ans = 0
	for i in range(n - 1):
		if arr[i] > arr[i + 1]:
			for j in range(i):
				if arr[j] > x:
					(x, arr[j]) = (arr[j], x)
					ans += 1
			(arr[i], x) = (x, arr[i])
			ans += 1
	for i in range(n - 1):
		if arr[i] > arr[i + 1]:
			print(-1)
			return
	print(ans)
	return
import sys, os
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
sys.setrecursionlimit(10 ** 5)
t = int(input())
for _ in range(t):
	testcase()


for _ in range(int(input())):
	(n, x) = map(int, input().split())
	a = [int(i) for i in input().split()]
	ans = 0
	temp = 0
	x1 = x
	for i in range(n - 1):
		if a[i] > a[i + 1]:
			if a[i] <= x:
				ans = -1
				break
			elif x > a[i + 1]:
				ans = -1
				break
			else:
				ans += 1 + temp
				x = a[i]
			temp = 0
		elif x < a[i] and i == 0:
			temp += 1
			x = a[i]
		elif i != 0:
			if x < a[i] and a[i] != a[i - 1]:
				temp += 1
				x = a[i]
	print(ans)


def swap(arr, x):
	count = 0
	for i in range(1, len(arr)):
		if arr[i] < x and arr[i] < arr[i - 1]:
			return -1
		if arr[i] < arr[i - 1]:
			for j in range(0, i):
				if arr[j] > x:
					scratch = x
					x = arr[j]
					arr[j] = scratch
					count += 1
			if arr[i] < x and arr[i] < arr[i - 1]:
				return -1
			if arr[i] < arr[i - 1]:
				scratch = x
				x = arr[i]
				arr[i] = scratch
				count += 1
	return count
t = int(input())
for i in range(0, t):
	nx = list(map(int, input().strip().split()))
	arr = list(map(int, input().strip().split()))
	print(swap(arr, nx[1]))


inn = lambda : int(input())
inm = lambda : map(int, input().split())
ins = lambda : str(input())
ina = lambda : list(map(int, input().split()))

def solve():
	(n, x) = inm()
	a = ina()
	if sorted(a) == a:
		print(0)
		return
	moves = 0
	pref = [1] * n
	for i in range(n - 2, -1, -1):
		if a[i] <= a[i + 1]:
			pref[i] = 1 & pref[i + 1]
		else:
			pref[i] = 0
	for (i, v) in enumerate(a):
		if v > x:
			(a[i], x) = (x, a[i])
			moves += 1
		if pref[i]:
			break
	b = a[:]
	if sorted(b) == a:
		print(moves)
	else:
		print(-1)

def main():
	t = 1
	t = int(input())
	for _ in range(t):
		solve()
main()


for i in range(int(input())):
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	n = a[0]
	x = a[1]
	if n == 1:
		print(0)
		continue
	i = 0
	count = 0
	f = 0
	flag = 0
	while i < n:
		ans = 0
		for j in range(i, n):
			if j == 0:
				continue
			if b[j] < b[j - 1]:
				flag = 1
				break
		for z in range(i, j + 1):
			if z == j and b[z] < b[z - 1] and (x < b[z]) and (flag == 1):
				ans += 1
				q = b[z]
				b[z] = x
				x = q
				break
			elif b[z] > x and flag == 1:
				q = b[z]
				b[z] = x
				x = q
				ans += 1
		i = j + 1
		flag = 0
		for g in range(1, j + 1):
			if b[g] < b[g - 1]:
				f = 1
		if f == 1:
			count = -1
			break
		else:
			count += ans
	if f == 1:
		print(-1)
	else:
		print(count)


import math
for _ in range(int(input())):
	(n, x) = map(int, input().split())
	a = list(map(int, input().split()))
	ans = 0
	ok = True
	while a != sorted(a):
		ans += 1
		i = 0
		while i < n and a[i] <= x:
			i += 1
		if i == n:
			ok = False
			break
		(a[i], x) = (x, a[i])
	if ok:
		print(ans)
	else:
		print(-1)


T = int(input())
for _ in range(T):
	(N, X) = map(int, input().split())
	L1 = list(map(int, input().split()))
	D = 0
	for i in range(N):
		if L1 == sorted(L1):
			break
		if L1[i] > X:
			(X, L1[i]) = (L1[i], X)
			D += 1
	if L1 == sorted(L1):
		print(D)
	else:
		print(-1)


from sys import stdin
nii = lambda : map(int, stdin.readline().split())
lnii = lambda : list(map(int, stdin.readline().split()))
t = int(input())
for tt in range(t):
	(n, x) = nii()
	a = lnii()
	if a == sorted(a):
		print(0)
		continue
	ans = 0
	for i in range(n):
		if a[i] > x:
			ans += 1
			(a[i], x) = (x, a[i])
		if a == sorted(a):
			print(ans)
			break
	else:
		print(-1)


import sys, math
from collections import deque, defaultdict
import operator as op
from functools import reduce
I = sys.stdin.readline

def ii():
	return int(I().strip())

def li():
	return list(map(int, I().strip().split()))

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

def ncr(n, r):
	r = min(r, n - r)
	numer = reduce(op.mul, range(n, n - r, -1), 1)
	denom = reduce(op.mul, range(1, r + 1), 1)
	return numer // denom

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def valid(row, col, rows, cols, rcross, lcross):
	return rows[row] == 0 and cols[col] == 0 and (rcross[col + row] == 0) and (lcross[col - row] == 0)

def div(n):
	tmp = []
	for i in range(2, int(n ** 0.5) + 1):
		if n % i == 0:
			cnt = 0
			while n % i == 0:
				n = n // i
				cnt += 1
			tmp.append((i, cnt))
	return tmp

def main():
	ans = ''
	for _ in range(ii()):
		(n, x) = mi()
		arr = li()
		cnt = 0
		flag = 0
		while sorted(arr) != arr:
			cnt += 1
			for i in range(n):
				if arr[i] > x:
					(arr[i], x) = (x, arr[i])
					break
			else:
				flag = 1
				break
		if flag:
			ans += '-1\n'
		else:
			ans += str(cnt) + '\n'
	print(ans)
main()


for _ in range(int(input())):
	(n, x) = map(int, input().split())
	a = list(map(int, input().split()))
	if a == sorted(a):
		print(0)
		continue
	ans = 0
	j = 0
	b = [0]
	while j < n - 1:
		if a[j] > a[j + 1]:
			b[j] += 1
		b += (b[j],)
		j += 1
	j = 0
	while j < n:
		if a[j] > x:
			(a[j], x) = (x, a[j])
			ans += 1
			if b[j] == b[-1]:
				break
		j += 1
	if a == sorted(a):
		print(ans)
	else:
		print(-1)


for i in range(int(input())):
	(n, x) = [int(_) for _ in input().split()]
	arr = [int(_) for _ in input().split()]
	c = 0
	for i in range(n):
		if sorted(arr) == arr:
			break
		if arr[i] > x:
			(x, arr[i]) = (arr[i], x)
			c += 1
	if sorted(arr) == arr:
		print(c)
	else:
		print(-1)


import sys
import math
mod = 10 ** 9 + 7
LI = lambda : [int(k) for k in input().split()]
input = lambda : sys.stdin.readline().rstrip()
IN = lambda : int(input())
S = lambda : input()
r = range
for t in r(IN()):
	(n, k) = LI()
	a = LI()
	flag = False
	cnt = 0
	ans = 0
	for i in range(n - 1):
		if a[i] > a[i + 1]:
			if a[i] > k and a[i + 1] >= k:
				(a[i], k) = (k, a[i])
				ans += cnt + 1
				cnt = 0
			else:
				print(-1)
				break
		elif a[i] > k:
			k = a[i]
			cnt += 1
	else:
		print(ans)


for _ in range(int(input())):
	(n, x) = map(int, input().split())
	l = list(map(int, input().split()))
	f = 1
	ans = 0
	for i in range(1, n):
		if l[i] < l[i - 1]:
			idx = i - 1
			val = i - 1
			for j in range(i - 2, -1, -1):
				val = j
				if x >= l[j]:
					break
			idx = val
			if idx == 0 and l[0] > x:
				idx -= 1
			for k in range(idx + 1, i - 1):
				if x == l[k]:
					continue
				temp = l[k]
				l[k] = x
				x = temp
				ans += 1
			cv = l[i - 1]
			if l[i - 1] > x and l[i] >= x:
				temp = cv
				l[i - 1] = x
				x = temp
				ans += 1
			else:
				f = 0
	if f:
		print(ans)
	else:
		print(-1)


import math
from math import log2, sqrt
import sys
from sys import stdin, stdout
import os
sys.setrecursionlimit(pow(10, 6))
import collections
from collections import defaultdict
from statistics import median
inf = float('inf')

def fun(index, previous, x):
	if index == n:
		return 0
	if l[index] < previous:
		return inf
	if l[index] <= x or x < previous:
		no_change = fun(index + 1, l[index], x)
		return no_change
	if True:
		no_change = fun(index + 1, l[index], x)
		change = 1 + fun(index + 1, x, l[index])
		return min(no_change, change)
t = int(input())
for i in range(t):
	(n, x) = map(int, input().split())
	l = list(map(int, input().split()))
	ans = fun(0, -1, x)
	if ans == inf:
		print(-1)
		continue
	print(ans)


def solve(l, x):
	count = 0
	for i in range(len(l)):
		if sorted(l) == l:
			break
		if l[i] > x:
			(l[i], x) = (x, l[i])
			count += 1
	if sorted(l) == l:
		return count
	else:
		return -1
for _ in range(int(input())):
	(n, x) = map(int, input().split())
	l = list(map(int, input().split()))
	print(solve(l, x))


def check(ll):
	for i in range(len(l) - 1):
		if l[i] <= l[i + 1]:
			continue
		else:
			return False
	return True
for i in range(int(input())):
	(n, k) = map(int, input().split())
	l = list(map(int, input().split()))
	c = 0
	if len(l) == 1:
		print(0)
		continue
	if check(l):
		print(0)
		continue
	for i in range(len(l)):
		if l[i] > k:
			t = l[i]
			l[i] = k
			k = t
			c += 1
			if not check(l):
				continue
			else:
				break
	if not check(l):
		c = -1
	print(c)


def sorted_list(mylist):
	n = len(mylist)
	if n == 1:
		return 1
	for i in range(1, n):
		if mylist[i - 1] > mylist[i]:
			return -1
		elif i == n - 1:
			return 1
cases = int(input())
for z in range(cases):
	(n, x) = input().split()
	n = int(n)
	x = int(x)
	mylist = [int(i) for i in input().split()]
	flag = 0
	count = 0
	for i in range(0, n):
		if sorted_list(mylist) == 1:
			flag = 1
			break
		if mylist[i] > x:
			temp = x
			count += 1
			x = mylist[i]
			mylist[i] = temp
	if flag == 1:
		print(count)
	else:
		print(-1)


def sortultra():
	(z, x) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	if a == sorted(a):
		return 0
	else:
		steps = 0
		while a != sorted(a):
			for i in range(len(a)):
				if x < a[i]:
					steps += 1
					temp = x
					x = a[i]
					a[i] = temp
					break
				if i == len(a) - 1:
					return -1
		return steps
for _ in range(int(input())):
	print(sortultra())


import sys
import math
input = sys.stdin.readline

def get(l, j, n, x):
	pos = -1
	for i in range(j, n):
		if l[i] > x:
			pos = i
			break
	return pos

def issorted(l):
	res = []
	for i in range(len(l)):
		res.append(l[i])
	res.sort()
	return res == l
for _ in range(int(input())):
	(n, x) = map(int, input().split())
	l = list(map(int, input().split()))
	if issorted(l) == True:
		print(0)
	else:
		count = 0
		f = 0
		for i in range(0, n - 1):
			if issorted(l) == True:
				break
			for j in range(i, n):
				idx = get(l, j, n, x)
				if idx != -1:
					(x, l[idx]) = (l[idx], x)
					count += 1
					break
				else:
					f = 1
					break
			if f:
				break
			if issorted(l) == True:
				break
		if issorted(l) == False:
			print(-1)
		else:
			print(count)


import sys

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

def get_int():
	return list(map(int, sys.stdin.readline().strip().split()))[0]

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

def get_string():
	return sys.stdin.readline().strip()
N = 0
a = []

def solve():
	global N
	(N, X) = get_ints()
	a = get_list()
	lastSortedPosition = N - 1
	notSorted = False
	for i in reversed(range(1, N)):
		if a[i] < a[i - 1]:
			lastSortedPosition = i
			notSorted = True
			break
	if not notSorted:
		print(0)
		return
	bad = False
	answer = 0
	for i in range(lastSortedPosition):
		if a[i] > X:
			(a[i], X) = (X, a[i])
			answer = answer + 1
			if i != N - 1 and a[i] > a[i + 1]:
				bad = True
				break
		elif i == lastSortedPosition - 1 and a[i] > a[i + 1]:
			bad = True
			break
		elif i != 0 and a[i] < a[i - 1]:
			bad = True
			break
	if bad:
		print(-1)
	else:
		print(answer)
test_cases = get_int()
for _ in range(test_cases):
	solve()


import sys
input = sys.stdin.readline
(T,) = map(int, input().split())
for _ in range(T):
	(N, x) = map(int, input().split())
	A = list(map(int, input().split()))
	f = 0
	for i in range(N - 1):
		if A[i] > A[i + 1]:
			f = 1
			break
	if not f:
		print(0)
		continue
	for K in range(1, N):
		for j in range(N):
			if A[j] > x:
				(A[j], x) = (x, A[j])
				break
		f = 0
		for i in range(N - 1):
			if A[i] > A[i + 1]:
				f = 1
				break
		if not f:
			break
	if f:
		print(-1)
	else:
		print(K)


import os, sys
from io import BytesIO, IOBase

def main():
	for _ in range(int(input())):
		(n, x) = map(int, input().split())
		a = list(map(int, input().split()))
		if a == sorted(a):
			print(0)
			continue
		(i, op) = (0, 0)
		while i != n:
			y = a[i:]
			bb = sorted(y) != y
			if not bb:
				break
			if a[i] > x and bb:
				op += 1
				(a[i], x) = (x, a[i])
			i += 1
		if a == sorted(a):
			print(op)
		else:
			print(-1)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

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

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

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

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

class IOWrapper(IOBase):

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


T = int(input())
r = 1
while r <= T:
	(n, x) = map(int, input().split())
	arr = list(map(int, input().split()))
	ans = 0
	stack = [0]
	temp = [x]
	for i in range(len(arr)):
		a = arr[i]
		if stack[-1] <= a:
			stack.append(a)
			continue
		if stack[-1] <= temp[-1]:
			ans = -1
			break
		while stack[-1] > temp[0]:
			temp.append(stack.pop())
		temp = [temp[0]] + temp[1:][::-1]
		for ele in temp[:-1]:
			stack.append(ele)
			if ele != arr[len(stack) - 2]:
				ans += 1
		temp = [temp[-1]]
		if stack[-1] <= a:
			stack.append(a)
		else:
			ans = -1
			break
	print(ans)
	r += 1


import math

def check(l):
	return l == sorted(l)
for tt in range(int(input())):
	(n, x) = map(int, input().split())
	l = list(map(int, input().split()))
	ma = -1
	cnt = 0
	while not check(l):
		ind = -1
		for i in range(n):
			if l[i] > x:
				ind = i
				(l[i], x) = (x, l[i])
				cnt += 1
				break
		if check(l):
			break
		if ind == -1:
			break
	if check(l):
		print(cnt)
	else:
		print(-1)


import sys
readline = sys.stdin.readline
rdw = lambda : readline().rstrip()
rdws = lambda : readline().split()
rdwl = lambda : list(readline().split())
rdi = lambda : int(readline())
rdis = lambda : map(int, readline().split())
rdil = lambda : list(map(int, readline().split()))
rdilrows = lambda cnt: [rdil() for _ in range(cnt)]

def solve():
	(n, x) = rdis()
	a = rdil()
	res = 0
	for i in range(n - 1):
		if a[i] > a[i + 1]:
			if a[i] > x and x <= a[i + 1]:
				j = i - 1
				while j >= 0 and a[j] > x:
					if a[j] > a[i + 1]:
						print(-1)
						return
					if a[j] != a[j + 1]:
						res += 1
					j -= 1
				x = a[i]
				res += 1
			else:
				res = -1
				break
	print(res)
tests = 1
tests = rdi()
for testnum in range(tests):
	solve()


import math
t = int(input())
for _ in range(t):
	(n, x) = map(int, input().split())
	l = list(map(int, input().split()))
	cnt = 0
	a = l[::-1]
	a.sort()
	i = -1
	for j in range(n - 1, -1, -1):
		if a[j] == l[j]:
			continue
		else:
			i = j
			break
	if i == -1:
		print(0)
	else:
		a[i] = x
		a.sort()
		cnt = 0
		for j in range(n):
			re = True
			for q in range(n):
				if x == a[q] and l[q] > x:
					(l[q], x) = (x, l[q])
					cnt += 1
					re = False
					break
			if re:
				break
		if l == a:
			print(cnt)
		else:
			print(-1)


t = int(input())
for i in range(t):
	(n, x) = map(int, input().split())
	A = list(map(int, input().split()))
	last = -1
	for i in range(n - 1):
		if A[i] > A[i + 1]:
			last = i
	ans = 0
	for i in range(last + 1):
		if A[i] > x:
			(A[i], x) = (x, A[i])
			ans += 1
	if A == sorted(A):
		print(ans)
	else:
		print(-1)


from sys import stdin
stdin.readline

def mp():
	return list(map(int, stdin.readline().strip().split()))

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

def not_sorted(l):
	for i in range(1, len(l)):
		if l[i] < l[i - 1]:
			return 1
	return 0
for _ in range(it()):
	(n, x) = mp()
	l = mp()
	(count, flag, i) = (0, 0, 0)
	while not_sorted(l):
		count += 1
		while i < n and l[i] <= x:
			i += 1
		if i == n:
			flag = 1
			break
		(l[i], x) = (x, l[i])
	if flag:
		print(-1)
	else:
		print(count)


I = lambda : map(int, input().split())
s = sorted
(T,) = I()
for _ in range(T):
	(n, k) = I()
	l = [*I()]
	a = 0
	for i in range(n):
		if s(l) == l:
			break
		if l[i] > k:
			(l[i], k) = (k, l[i])
			a += 1
	print([a, -1][s(l) < l])


import sys
input = sys.stdin.readline
import math

def inp():
	return int(input())

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

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())
t = inp()
for _ in range(t):
	(n, x) = invr()
	arr = inlt()
	ans = 0
	z = 0
	for j in range(0, n - 1):
		if x < arr[j] and arr[j] > arr[j + 1]:
			temp = arr[j]
			arr[j] = x
			x = temp
			ans += z + 1
			z = 0
		elif x < arr[j]:
			z += 1
			temp = arr[j]
			arr[j] = x
			x = temp
		if arr[j] > arr[j + 1]:
			ans = -1
			break
	if arr[n - 2] > arr[n - 1]:
		ans = -1
	print(ans)


n = int(input())
while n > 0:
	(a, x) = map(int, input().split())
	arr = list(map(int, input().split()))
	i = 0
	count = 0
	while i < len(arr):
		if sorted(arr) == arr:
			break
		if arr[i] > x:
			(x, arr[i]) = (arr[i], x)
			count += 1
		i += 1
	if sorted(arr) == arr:
		print(count)
	else:
		print(-1)
	n -= 1


def sol():
	(n, x) = list(map(int, input().split()))
	arr = list(map(int, input().split()))
	a = 0
	solution = 0
	if arr == sorted(arr):
		return 0
	for i in range(n):
		if arr[i] > x:
			solution += 1
			(arr[i], x) = (x, arr[i])
			if arr == sorted(arr):
				return solution
		if i == len(arr) - 1:
			return -1
	return solution
for _ in range(int(input())):
	print(sol())


t = int(input())
for i in range(t):
	(n, x) = map(int, input().split())
	lst = list(map(int, input().split()))
	c = 0
	lst1 = lst.copy()
	lst1.sort()
	ind = 0
	while True:
		if lst1 == lst or ind > n - 1:
			break
		elif lst[ind] > x:
			(lst[ind], x) = (x, lst[ind])
			c += 1
			lst1 = lst.copy()
			lst1.sort()
			ind += 1
		else:
			ind += 1
	lst1 = lst.copy()
	lst1.sort()
	if lst1 == lst:
		print(c)
	else:
		print(-1)


from sys import stdin, stdout
from math import gcd, sqrt, factorial, inf
from collections import deque, defaultdict
input = stdin.readline
R = lambda : map(int, input().split())
I = lambda : int(input())
S = lambda : input().rstrip('\n')
L = lambda : list(R())
P = lambda x: stdout.write(x)
lcm = lambda x, y: x * y // gcd(x, y)
hg = lambda x, y: (y + x - 1) // x * x
pw = lambda x: 0 if x == 1 else 1 + pw(x // 2)
chk = lambda x: chk(x // 2) if not x % 2 else True if x == 1 else False
sm = lambda x: (x ** 2 + x) // 2
N = 10 ** 9 + 7

def is_sort(a):
	p = a[0]
	for i in a[1:]:
		if i < p:
			return False
		p = i
	return True
for _ in range(I()):
	(n, k) = R()
	(*a,) = R()
	ans = 0
	for i in range(n):
		if is_sort(a):
			break
		if a[i] > k:
			(a[i], k) = (k, a[i])
			ans += 1
	if is_sort(a):
		print(ans)
	else:
		print(-1)


t = int(input())

def getfirst(a: list, x: int):
	for i in range(len(a)):
		if a[i] > x:
			return i
	return -1
for _ in range(t):
	(n, x) = map(int, input().split())
	a = list(map(int, input().split()))
	ans = 0
	while a != sorted(a):
		e = getfirst(a, x)
		if e < 0:
			ans = -1
			break
		else:
			(a[e], x) = (x, a[e])
			ans += 1
	print(ans)


for w in range(int(input())):
	(n, x) = tuple(map(int, input().split()))
	a = list(map(int, input().split()))
	c = 0
	ans = 0
	t = -1
	i = 0
	if a == sorted(a):
		print(0)
	elif x > max(a):
		print(-1)
	else:
		for i in range(n):
			if a[i] > x:
				(a[i], x) = (x, a[i])
				ans += 1
				if a == sorted(a):
					break
		if a == sorted(a):
			print(ans)
		else:
			print(-1)


import sys

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

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

def get_list_string():
	return list(map(str, sys.stdin.readline().strip().split()))

def get_string():
	return sys.stdin.readline().strip()

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

def get_print_int(x):
	sys.stdout.write(str(x) + '\n')

def get_print(x):
	sys.stdout.write(x + '\n')
from sys import maxsize

def arrsort(arr, n):
	for i in range(1, n):
		if arr[i] < arr[i - 1]:
			return False
	return True

def solve():
	for _ in range(get_int()):
		(n, x) = get_ints()
		arr = get_list()
		if arrsort(arr, n):
			get_print_int(0)
		else:
			ans = 0
			f = 0
			while True:
				ans += 1
				i = 0
				while i < n and arr[i] <= x:
					i += 1
				if i == n:
					ans = -1
					break
				(arr[i], x) = (x, arr[i])
				if arrsort(arr, n):
					break
			get_print_int(ans)
solve()


for _ in range(0, int(input())):
	(n, x) = map(int, input().split())
	a = list(map(int, input().split()))
	t = 0
	for i in range(0, n):
		if a == sorted(a):
			break
		elif a[i] > x:
			m = x
			x = a[i]
			a[i] = m
			t += 1
		else:
			continue
	if a == sorted(a):
		print(t)
	else:
		print(-1)


from collections import defaultdict, deque, Counter, OrderedDict
import sys
from bisect import bisect_right

def main():
	t = int(input())
	while t:
		t -= 1
		(n, x) = [int(i) for i in input().split()]
		arr = [int(i) for i in input().split()]
		(dips, sdips) = ([], set())
		for i in range(1, n):
			if arr[i] < arr[i - 1]:
				dips.append(i)
				sdips.add(i)
		if len(dips) == 0:
			print(0)
			continue
		(count, fl) = (0, False)
		res = deque(dips)
		while len(sdips) and len(res):
			initial = bisect_right(arr, x, 0, res[0])
			if initial == -1 or initial == n or initial in sdips:
				print(-1)
				fl = True
				break
			(x, arr[initial]) = (arr[initial], x)
			count += 1
			if initial + 1 < n and initial + 1 in sdips:
				if arr[initial + 1] < arr[initial]:
					print(-1)
					fl = True
					break
				sdips.remove(initial + 1)
				res.popleft()
		if not fl:
			print(count)
main()


import sys
input = sys.stdin.readline
for nt in range(int(input())):
	(n, x) = map(int, input().split())
	a = list(map(int, input().split()))
	found = 0
	ans = 0
	for i in range(n):
		if sorted(a) == a:
			break
		if a[i] > x:
			(a[i], x) = (x, a[i])
			ans += 1
	if sorted(a) == a:
		print(ans)
	else:
		print(-1)


def ans(arr, x):
	if len(arr) == 1:
		return 0
	elif arr == sorted(arr):
		return 0
	else:
		cnt = 0
		for i in range(len(arr)):
			if arr[i] > x:
				(arr[i], x) = (x, arr[i])
				cnt += 1
			if arr == sorted(arr):
				return cnt
				break
	return -1
for _ in range(int(input())):
	(n, x) = map(int, input().split())
	arr = list(map(int, input().split()))
	print(ans(arr, x))


for q11 in range(int(input())):
	(n, x) = map(int, input().split())
	a = list(map(int, input().split()))
	max1 = max2 = -float('inf')
	for q in range(n):
		if a[q] > max1:
			(max2, max1) = (max1, a[q])
		elif a[q] > max2:
			max2 = a[q]
		if a[q] < max2:
			print(-1)
			break
	else:
		seg = []
		for q in range(1, n):
			if a[q - 1] > a[q]:
				seg.append(q - 1)
		if len(seg) == 0:
			print(0)
		elif a[seg[0] + 1] < x:
			print(-1)
		else:
			(q1, q2, plus) = (seg[0] - 1, 0, 1)
			while q1 > -1 and a[q1] > x:
				plus += q1 == 0 or a[q1] != a[q1 - 1]
				q1 -= 1
			for q in range(seg[0] + 1, seg[-1] + 1):
				if q == seg[q2 + 1]:
					plus += 1
					q2 += 1
				else:
					plus += a[q] > a[seg[q2]] and a[q] != a[q - 1]
			print(plus)


for i in range(int(input())):
	(n, x) = map(int, input().split())
	j = list(map(int, input().split()))
	k = 0
	m = 0
	if j == sorted(j):
		m = 1
		print(k)
	elif j != sorted(j):
		for z in range(len(j)):
			if j[z] > x:
				t = j[z]
				j[z] = x
				x = t
				k += 1
				if j == sorted(j):
					m = 1
					print(k)
					break
	if m == 0:
		print(-1)


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
import copy
import time
starttime = time.time()
mod = int(pow(10, 9) + 7)
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

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

def L():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

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

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
try:
	sys.stdin = open('input.txt', 'r')
except:
	pass
for u in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	ans = 0
	for i in range(n):
		if sorted(a) == a:
			break
		if a[i] > k:
			(a[i], k) = (k, a[i])
			ans += 1
	if sorted(a) == a:
		print(ans)
	else:
		print(-1)
endtime = time.time()


from math import *
from sys import *
from bisect import *
t = int(stdin.readline())
for _ in range(t):
	(n, k) = map(int, stdin.readline().split())
	a = list(map(int, stdin.readline().split()))
	ans = 0
	for i in range(n - 1):
		if list(sorted(a)) == a:
			break
		if a[i] > min(a[i:]) or a[i] > k:
			if a[i] <= k:
				break
			(a[i], k) = (k, a[i])
			ans += 1
	if list(sorted(a)) == a:
		print(ans)
	else:
		print(-1)


t = int(input())
for i in range(t):
	(n, x) = [int(c) for c in input().split()]
	a = [int(c) for c in input().split()]
	a = [-1] + a + [1000]
	check1 = check2 = swaps = indx = 0
	for j in range(n, 1, -1):
		if a[j - 1] > a[j]:
			indx = j - 1
			check1 = 1
			break
	for j in range(1, indx + 2):
		if x < a[j]:
			swaps += 1
			(a[j], x) = (x, a[j])
		if a[j - 1] > a[j]:
			check2 = 1
			break
	if not check1:
		print(0)
	elif not check2:
		print(swaps)
	else:
		print(-1)


def swaps(A, x):
	n = len(A)
	count = 0
	if A == sorted(A):
		return 0
	for i in range(n):
		if A == sorted(A):
			return count
		if A[i] > x:
			(A[i], x) = (x, A[i])
			count += 1
	if A == sorted(A):
		return count
	return -1
t = int(input())
for i in range(t):
	(n, x) = input().split()
	n = int(n)
	x = int(x)
	a = [int(j) for j in input().split()][:n]
	print(swaps(a, x))


import time, math as mt, bisect as bs, sys
from sys import stdin, stdout
from collections import deque
from fractions import Fraction
from collections import Counter
from collections import OrderedDict
pi = 3.141592653589793

def II():
	return int(stdin.readline())

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

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

def P(x):
	return stdout.write(str(x) + '\n')

def PI(x, y):
	return stdout.write(str(x) + ' ' + str(y) + '\n')

def lcm(a, b):
	return a * b // gcd(a, b)

def gcd(a, b):
	if a == 0:
		return b
	elif b == 0:
		return a
	if a > b:
		return gcd(a % b, b)
	else:
		return gcd(a, b % a)

def bfs(adj, v):
	visited = [False] * (v + 1)
	q = deque()
	while q:
		pass

def setBit(n):
	count = 0
	while n != 0:
		n = n & n - 1
		count += 1
	return count
mx = 10 ** 7
spf = [mx] * (mx + 1)

def readTree(n, e):
	adj = [set() for i in range(n + 1)]
	for i in range(e):
		(u1, u2) = IP()
		adj[u1].add(u2)
	return adj

def sieve():
	li = [True] * (10 ** 3 + 5)
	(li[0], li[1]) = (False, False)
	for i in range(2, len(li), 1):
		if li[i] == True:
			for j in range(i * i, len(li), i):
				li[j] = False
	(prime, cur) = ([0] * 200, 0)
	for i in range(10 ** 3 + 5):
		if li[i] == True:
			prime[cur] = i
			cur += 1
	return prime

def SPF():
	mx = 10 ** 6 + 1
	spf[1] = 1
	for i in range(2, mx):
		if spf[i] == 1000000000.0:
			spf[i] = i
			for j in range(i * i, mx, i):
				if i < spf[j]:
					spf[j] = i
	return

def prime(n, d):
	prm = set()
	while n != 1:
		prm.add(spf[n])
		n = n // spf[n]
	for ele in prm:
		d[ele] = d.get(ele, 0) + 1
	return
mod = 998244353

def solve():
	(n, x) = IP()
	a = L()
	if n == 1:
		print(0)
		return
	(per_mov, tem_mov) = (0, 0)
	for i in range(n):
		if i == 0:
			if a[0] <= a[1]:
				if x < a[0]:
					tem_mov += 1
					(x, a[0]) = (a[0], x)
			elif x >= a[0]:
				print(-1)
				return
			else:
				per_mov += 1
				(x, a[0]) = (a[0], x)
		elif i > 0 and i < n - 1:
			if a[i] >= a[i - 1] and a[i] <= a[i + 1]:
				if x < a[i]:
					tem_mov += 1
					(x, a[i]) = (a[i], x)
			elif a[i] < a[i - 1]:
				print(-1)
				return
			elif a[i] > a[i + 1]:
				per_mov += tem_mov
				tem_mov = 0
				per_mov += 1
				(x, a[i]) = (a[i], x)
		elif i == n - 1:
			if a[n - 1] < a[n - 2]:
				print(-1)
				return
	b = list(a)
	b.sort()
	for i in range(n):
		if a[i] != b[i]:
			print(-1)
			return
	print(per_mov)
	return
t = II()
for i in range(t):
	solve()


def swap(l, r, x):
	temp = arr[r]
	for o in range(r, l, -1):
		arr[o] = arr[o - 1]
	arr[l] = x
	return temp
t = int(input())
for z in range(t):
	(n, x) = map(int, input().split())
	arr = list(map(int, input().split()))
	count = 0
	ok = 0
	for i in range(n - 1):
		if arr[i] > arr[i + 1]:
			if x <= arr[i + 1]:
				j = i - 1
				while j >= 0:
					if x < arr[j]:
						j = j - 1
					else:
						break
				count = count + len(set(arr[j + 1:i + 1]))
				x = swap(j + 1, i, x)
				if arr[i] > arr[i + 1]:
					ok = 1
			else:
				ok = 1
	if ok == 1:
		print('-1')
	else:
		print(count)


def not_sorted(n, a):
	for i in range(1, n):
		if a[i] < a[i - 1]:
			return -1
	return 0

def solve(n, x, a):
	cnt = 0
	while not_sorted(n, a):
		cnt += 1
		i = 0
		while i < n and a[i] <= x:
			i += 1
		if i == n:
			return -1
		(a[i], x) = (x, a[i])
	return cnt
t = int(input())
while t:
	(n, x) = map(int, input().split())
	a = list(map(int, input().split()))
	print(solve(n, x, a))
	t -= 1


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(n, x) = map(int, input().split())
	l = [*map(int, input().split())]
	if l == sorted(l):
		print(0)
		continue
	cunt = 0
	for i in range(n):
		if l == sorted(l):
			break
		if l[i] > x:
			(x, l[i]) = (l[i], x)
			cunt += 1
	if l == sorted(l):
		print(cunt)
		continue
	print(-1)


import sys
sys.setrecursionlimit(10 ** 5)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep='\n')

def II():
	return int(sys.stdin.buffer.readline())

def MI():
	return map(int, sys.stdin.buffer.readline().split())

def LI():
	return list(map(int, sys.stdin.buffer.readline().split()))

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def BI():
	return sys.stdin.buffer.readline().rstrip()

def SI():
	return sys.stdin.buffer.readline().rstrip().decode()
inf = 10 ** 16
md = 10 ** 9 + 7

def isinc(aa, n):
	for i in range(n - 1):
		if aa[i] > aa[i + 1]:
			return False
	return True

def solve(n, x, aa):
	if isinc(aa, n):
		return 0
	aa += [-inf]
	res = 0
	for i in range(n):
		if aa[i] > x and aa[i - 1] <= x:
			(aa[i], x) = (x, aa[i])
			res += 1
		if aa[i - 1] > aa[i]:
			return -1
		if isinc(aa, n):
			return res
	return res
for _ in range(II()):
	(n, x) = MI()
	aa = LI()
	print(solve(n, x, aa))


import bisect
import collections
import copy
import functools
import heapq
import itertools
import math
import random
import re
import sys
import time
import string
from typing import List
sys.setrecursionlimit(99999)

def II():
	return int(sys.stdin.readline().strip())

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

def SI():
	return sys.stdin.readline().strip()
for _ in [0] * II():
	(n, x) = IIs()
	arr = IIs()
	cnt = 0
	if sorted(arr) == arr:
		print(0)
	else:
		for i in range(n):
			if arr[i] > x:
				(arr[i], x) = (x, arr[i])
				cnt += 1
				if sorted(arr) == arr:
					print(cnt)
					break
		else:
			print(-1)


import sys
from math import gcd, sqrt, ceil, log2
from collections import defaultdict, Counter, deque
from bisect import bisect_left, bisect_right
import math
sys.setrecursionlimit(2 * 10 ** 5 + 10)
import heapq
from itertools import permutations
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
aa = 'abcdefghijklmnopqrstuvwxyz'

class FastIO(IOBase):
	newlines = 0

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

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

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

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

class IOWrapper(IOBase):

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

def get_sum(bit, i):
	s = 0
	i += 1
	while i > 0:
		s += bit[i]
		i -= i & -i
	return s

def update(bit, n, i, v):
	i += 1
	while i <= n:
		bit[i] += v
		i += i & -i

def modInverse(b, m):
	g = math.gcd(b, m)
	if g != 1:
		return -1
	else:
		return pow(b, m - 2, m)

def primeFactors(n):
	sa = []
	while n % 2 == 0:
		sa.append(2)
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			sa.append(i)
			n = n // i
	if n > 2:
		sa.append(n)
	return sa

def seive(n):
	pri = [True] * (n + 1)
	p = 2
	while p * p <= n:
		if pri[p] == True:
			for i in range(p * p, n + 1, p):
				pri[i] = False
		p += 1
	return pri

def check_prim(n):
	if n < 0:
		return False
	for i in range(2, int(sqrt(n)) + 1):
		if n % i == 0:
			return False
	return True

def getZarr(string, z):
	n = len(string)
	(l, r, k) = (0, 0, 0)
	for i in range(1, n):
		if i > r:
			(l, r) = (i, i)
			while r < n and string[r - l] == string[r]:
				r += 1
			z[i] = r - l
			r -= 1
		else:
			k = i - l
			if z[k] < r - i + 1:
				z[i] = z[k]
			else:
				l = i
				while r < n and string[r - l] == string[r]:
					r += 1
				z[i] = r - l
				r -= 1

def search(text, pattern):
	concat = pattern + '$' + text
	l = len(concat)
	z = [0] * l
	getZarr(concat, z)
	ha = []
	for i in range(l):
		if z[i] == len(pattern):
			ha.append(i - len(pattern) - 1)
	return ha

def mergeIntervals(arr):
	arr.sort(key=lambda x: x[0])
	m = []
	s = -10000
	max = -100000
	for i in range(len(arr)):
		a = arr[i]
		if a[0] > max:
			if i != 0:
				m.append([s, max])
			max = a[1]
			s = a[0]
		elif a[1] >= max:
			max = a[1]
	if max != -100000 and [s, max] not in m:
		m.append([s, max])
	return m

class SortedList:

	def __init__(self, iterable=[], _load=200):
		values = sorted(iterable)
		self._len = _len = len(values)
		self._load = _load
		self._lists = _lists = [values[i:i + _load] for i in range(0, _len, _load)]
		self._list_lens = [len(_list) for _list in _lists]
		self._mins = [_list[0] for _list in _lists]
		self._fen_tree = []
		self._rebuild = True

	def _fen_build(self):
		self._fen_tree[:] = self._list_lens
		_fen_tree = self._fen_tree
		for i in range(len(_fen_tree)):
			if i | i + 1 < len(_fen_tree):
				_fen_tree[i | i + 1] += _fen_tree[i]
		self._rebuild = False

	def _fen_update(self, index, value):
		if not self._rebuild:
			_fen_tree = self._fen_tree
			while index < len(_fen_tree):
				_fen_tree[index] += value
				index |= index + 1

	def _fen_query(self, end):
		if self._rebuild:
			self._fen_build()
		_fen_tree = self._fen_tree
		x = 0
		while end:
			x += _fen_tree[end - 1]
			end &= end - 1
		return x

	def _fen_findkth(self, k):
		_list_lens = self._list_lens
		if k < _list_lens[0]:
			return (0, k)
		if k >= self._len - _list_lens[-1]:
			return (len(_list_lens) - 1, k + _list_lens[-1] - self._len)
		if self._rebuild:
			self._fen_build()
		_fen_tree = self._fen_tree
		idx = -1
		for d in reversed(range(len(_fen_tree).bit_length())):
			right_idx = idx + (1 << d)
			if right_idx < len(_fen_tree) and k >= _fen_tree[right_idx]:
				idx = right_idx
				k -= _fen_tree[idx]
		return (idx + 1, k)

	def _delete(self, pos, idx):
		_lists = self._lists
		_mins = self._mins
		_list_lens = self._list_lens
		self._len -= 1
		self._fen_update(pos, -1)
		del _lists[pos][idx]
		_list_lens[pos] -= 1
		if _list_lens[pos]:
			_mins[pos] = _lists[pos][0]
		else:
			del _lists[pos]
			del _list_lens[pos]
			del _mins[pos]
			self._rebuild = True

	def _loc_left(self, value):
		if not self._len:
			return (0, 0)
		_lists = self._lists
		_mins = self._mins
		(lo, pos) = (-1, len(_lists) - 1)
		while lo + 1 < pos:
			mi = lo + pos >> 1
			if value <= _mins[mi]:
				pos = mi
			else:
				lo = mi
		if pos and value <= _lists[pos - 1][-1]:
			pos -= 1
		_list = _lists[pos]
		(lo, idx) = (-1, len(_list))
		while lo + 1 < idx:
			mi = lo + idx >> 1
			if value <= _list[mi]:
				idx = mi
			else:
				lo = mi
		return (pos, idx)

	def _loc_right(self, value):
		if not self._len:
			return (0, 0)
		_lists = self._lists
		_mins = self._mins
		(pos, hi) = (0, len(_lists))
		while pos + 1 < hi:
			mi = pos + hi >> 1
			if value < _mins[mi]:
				hi = mi
			else:
				pos = mi
		_list = _lists[pos]
		(lo, idx) = (-1, len(_list))
		while lo + 1 < idx:
			mi = lo + idx >> 1
			if value < _list[mi]:
				idx = mi
			else:
				lo = mi
		return (pos, idx)

	def add(self, value):
		_load = self._load
		_lists = self._lists
		_mins = self._mins
		_list_lens = self._list_lens
		self._len += 1
		if _lists:
			(pos, idx) = self._loc_right(value)
			self._fen_update(pos, 1)
			_list = _lists[pos]
			_list.insert(idx, value)
			_list_lens[pos] += 1
			_mins[pos] = _list[0]
			if _load + _load < len(_list):
				_lists.insert(pos + 1, _list[_load:])
				_list_lens.insert(pos + 1, len(_list) - _load)
				_mins.insert(pos + 1, _list[_load])
				_list_lens[pos] = _load
				del _list[_load:]
				self._rebuild = True
		else:
			_lists.append([value])
			_mins.append(value)
			_list_lens.append(1)
			self._rebuild = True

	def discard(self, value):
		_lists = self._lists
		if _lists:
			(pos, idx) = self._loc_right(value)
			if idx and _lists[pos][idx - 1] == value:
				self._delete(pos, idx - 1)

	def remove(self, value):
		_len = self._len
		self.discard(value)
		if _len == self._len:
			raise ValueError('{0!r} not in list'.format(value))

	def pop(self, index=-1):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		value = self._lists[pos][idx]
		self._delete(pos, idx)
		return value

	def bisect_left(self, value):
		(pos, idx) = self._loc_left(value)
		return self._fen_query(pos) + idx

	def bisect_right(self, value):
		(pos, idx) = self._loc_right(value)
		return self._fen_query(pos) + idx

	def count(self, value):
		return self.bisect_right(value) - self.bisect_left(value)

	def __len__(self):
		return self._len

	def __getitem__(self, index):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		return self._lists[pos][idx]

	def __delitem__(self, index):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		self._delete(pos, idx)

	def __contains__(self, value):
		_lists = self._lists
		if _lists:
			(pos, idx) = self._loc_left(value)
			return idx < len(_lists[pos]) and _lists[pos][idx] == value
		return False

	def __iter__(self):
		return (value for _list in self._lists for value in _list)

	def __reversed__(self):
		return (value for _list in reversed(self._lists) for value in reversed(_list))

	def __repr__(self):
		return 'SortedList({0})'.format(list(self))

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p

def sol(n):
	seti = set()
	for i in range(1, int(sqrt(n)) + 1):
		if n % i == 0:
			seti.add(n // i)
			seti.add(i)
	return seti

def lcm(a, b):
	return a * b // gcd(a, b)

def moore_voting(l):
	count1 = 0
	count2 = 0
	first = 10 ** 18
	second = 10 ** 18
	n = len(l)
	for i in range(n):
		if l[i] == first:
			count1 += 1
		elif l[i] == second:
			count2 += 1
		elif count1 == 0:
			count1 += 1
			first = l[i]
		elif count2 == 0:
			count2 += 1
			second = l[i]
		else:
			count1 -= 1
			count2 -= 1
	for i in range(n):
		if l[i] == first:
			count1 += 1
		elif l[i] == second:
			count2 += 1
	if count1 > n // 3:
		return first
	if count2 > n // 3:
		return second
	return -1

def find_parent(u, parent):
	if u != parent[u]:
		parent[u] = find_parent(parent[u], parent)
	return parent[u]

def dis_union(n, e):
	par = [i for i in range(n + 1)]
	rank = [1] * (n + 1)
	for (a, b) in e:
		(z1, z2) = (find_parent(a, par), find_parent(b, par))
		if rank[z1] > rank[z2]:
			(z1, z2) = (z2, z1)
		if z1 != z2:
			par[z1] = z2
			rank[z2] += rank[z1]
		else:
			return (a, b)

def dijkstra(n, tot, hash):
	hea = [[0, n]]
	dis = [10 ** 18] * (tot + 1)
	dis[n] = 0
	boo = defaultdict(bool)
	check = defaultdict(int)
	while hea:
		(a, b) = heapq.heappop(hea)
		if boo[b]:
			continue
		boo[b] = True
		for (i, w) in hash[b]:
			if b == 1:
				c = 0
				if (1, i, w) in nodes:
					c = nodes[1, i, w]
					del nodes[1, i, w]
				if dis[b] + w < dis[i]:
					dis[i] = dis[b] + w
					check[i] = c
				elif dis[b] + w == dis[i] and c == 0:
					dis[i] = dis[b] + w
					check[i] = c
			elif dis[b] + w <= dis[i]:
				dis[i] = dis[b] + w
				check[i] = check[b]
			heapq.heappush(hea, [dis[i], i])
	return check

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res *= x
		x = x * x
		y = y >> 1
	return res
import sys
from math import ceil, log2
INT_MAX = sys.maxsize

def minVal(x, y):
	return x if x < y else y

def getMid(s, e):
	return s + (e - s) // 2

def RMQUtil(st, ss, se, qs, qe, index):
	if qs <= ss and qe >= se:
		return st[index]
	if se < qs or ss > qe:
		return INT_MAX
	mid = getMid(ss, se)
	return minVal(RMQUtil(st, ss, mid, qs, qe, 2 * index + 1), RMQUtil(st, mid + 1, se, qs, qe, 2 * index + 2))

def RMQ(st, n, qs, qe):
	if qs < 0 or qe > n - 1 or qs > qe:
		print('Invalid Input')
		return -1
	return RMQUtil(st, 0, n - 1, qs, qe, 0)

def constructSTUtil(arr, ss, se, st, si):
	if ss == se:
		st[si] = arr[ss]
		return arr[ss]
	mid = getMid(ss, se)
	st[si] = minVal(constructSTUtil(arr, ss, mid, st, si * 2 + 1), constructSTUtil(arr, mid + 1, se, st, si * 2 + 2))
	return st[si]

def constructST(arr, n):
	x = int(ceil(log2(n)))
	max_size = 2 * int(2 ** x) - 1
	st = [0] * max_size
	constructSTUtil(arr, 0, n - 1, st, 0)
	return st
t = int(input())
for _ in range(t):
	(n, x) = map(int, input().split())
	l = list(map(int, input().split()))
	if sorted(l) == l:
		print(0)
		continue
	ans = []
	i = 0
	final = 0
	while i < n:
		if sorted(ans + l[i:]) == ans + l[i:]:
			break
		if l[i] <= x:
			ans.append(l[i])
			i += 1
		else:
			ans.append(x)
			x = l[i]
			i += 1
			final += 1
	if sorted(ans) == ans:
		print(final)
	else:
		print(-1)


for _ in range(int(input())):
	(n, x) = map(int, input().split())
	l = list(map(int, input().split()))
	ans = 0
	f = 0
	for i in range(1, n):
		if l[i] < l[i - 1]:
			f = 1
			break
	if f == 1:
		for i in range(n):
			ff = 0
			if l[i] > x:
				temp = l[i]
				l[i] = x
				x = temp
				ans += 1
			for i in range(1, n):
				if l[i] < l[i - 1]:
					ff = 1
					break
			if ff == 0:
				break
		if ff == 1:
			print(-1)
		else:
			print(ans)
	else:
		print(0)


t = int(input())
all = []
x_arr = []
for i in range(t):
	k = input()
	k = [int(m) for m in k.split()]
	(l, x) = k
	x_arr.append(x)
	h = input()
	a = [int(z) for z in h.split()]
	all.append(a)
for i in range(t):
	x = x_arr[i]
	arr = all[i]
	sorted_ = sorted(arr)
	if sorted_ == arr:
		print(0)
	else:
		step = 0
		for idx in range(len(arr)):
			if x < arr[idx]:
				swa = x
				x = arr[idx]
				arr[idx] = swa
				step += 1
			s = sorted(arr)
			if s == arr:
				break
		if sorted(arr) == arr:
			print(step)
		else:
			print(-1)


import copy
t = int(input())
for _ in range(t):
	(n, x) = (int(number) for number in input().split(' '))
	number_list = [int(number) for number in input().split(' ')]
	temp = copy.copy(number_list)
	temp.append(x)
	all_numbers_sorted = sorted(temp)
	if number_list == sorted(number_list):
		print(0)
	elif all_numbers_sorted[n] == x:
		print(-1)
	else:
		number_of_correct_at_end = 0
		number_of_possibly_correct = 0
		for i in range(n - 1, -1, -1):
			if number_list[i] == all_numbers_sorted[i + 1]:
				number_of_correct_at_end += 1
			else:
				break
		operations = 0
		busted = False
		for i in range(0, n - number_of_correct_at_end):
			if number_list[i] == all_numbers_sorted[i]:
				continue
			elif x == all_numbers_sorted[i]:
				x = number_list[i]
				operations += 1
			else:
				busted = True
				break
		if busted:
			print(-1)
		else:
			print(operations)


t = int(input())
for _ in range(t):
	(n, x) = tuple(map(int, input().split(' ')))
	ans = -1
	a = list(map(int, input().split(' ')))
	for i in range(0, n):
		if a[i] > x and sorted(a) != a:
			(a[i], x) = (x, a[i])
			ans = ans + 1
	if sorted(a) != a:
		ans = -1
		print(ans)
	else:
		print(ans + 1)


from collections import Counter
from math import *
from os import rename
import sys
mod = 1000000007

def is_sorted(arr):
	for i in range(1, len(arr)):
		if arr[i - 1] > arr[i]:
			return 0
	return 1

def first(arr, x):
	for i in range(len(arr)):
		if arr[i] > x:
			return i
	return -1

def pro(arr, x):
	ans = 0
	while not is_sorted(arr):
		ind = first(arr, x)
		if ind == -1:
			break
		ans += 1
		(arr[ind], x) = (x, arr[ind])
	print(ans if is_sorted(arr) else -1)
t = int(input())
for i in range(t):
	(n, k) = list(map(int, input().split()))
	arr = list(map(int, input().split()))
	pro(arr, k)


for i in range(int(input())):
	(n, x) = map(int, input().split(' '))
	l = list(map(int, input().split(' ')))
	if n == 1:
		print(0)
	else:
		c = 1
		count = 0
		for i in range(1, n):
			if l[i] >= l[i - 1]:
				c += 1
		if c == n:
			print(0)
		else:
			end = False
			for i in range(0, n):
				if end:
					i = n
					continue
				if l[i] > x:
					(x, l[i]) = (l[i], x)
					count += 1
					c = 1
					for i in range(1, n):
						if l[i] >= l[i - 1]:
							c += 1
					if not c == n:
						continue
					else:
						print(count)
						end = True
			if not end:
				c = 1
				for i in range(1, n):
					if l[i] >= l[i - 1]:
						c += 1
				if c == n:
					print(count)
				else:
					print(-1)


def issorted(l):
	flag = 0
	if all((l[i] <= l[i + 1] for i in range(len(l) - 1))):
		return True
	return False
t = int(input())
for _ in range(t):
	(n, x) = map(int, input().split())
	l = list(map(int, input().split()))
	if issorted(l):
		print(0)
	else:
		c = 0
		for i in range(n):
			while not issorted(l):
				if x < l[i]:
					(x, l[i]) = (l[i], x)
					c += 1
					break
				else:
					break
		if issorted(l):
			print(c)
		else:
			print(-1)


t = int(input())

def solve():
	(n, x) = map(int, input().split())
	a = list(map(int, input().split()))
	xwas = x
	sor = list(sorted(a))
	g = 1
	for i in range(n):
		g = g and sor[i] == a[i]
	if g:
		return 0
	sor = list(sorted(a + [x]))
	k = 0
	b = 1
	for i in range(n):
		if sor[i] != a[i]:
			k += 1
			if a[i] <= x:
				return -1
			else:
				(a[i], x) = (x, a[i])
				sor2 = list(sorted(a[i:]))
				if sor2 == a[i:]:
					return k
	return k
for _ in range(t):
	print(solve())


def answer():
	global x
	opp = 0
	if a == sorted(a):
		return opp
	i = 0
	while i < n:
		while i < n and a[i] <= x:
			i += 1
		if i < n:
			(x, a[i]) = (a[i], x)
			opp += 1
			if a == sorted(a):
				return opp
		i += 1
	return -1
for T in range(int(input())):
	(n, x) = map(int, input().split())
	a = list(map(int, input().split()))
	print(answer())


for _ in range(int(input())):
	(n, x) = map(int, input().split())
	a = list(map(int, input().split()))
	cnt = 0
	for i in range(n):
		if a == sorted(a):
			break
		if a[i] > x:
			(x, a[i]) = (a[i], x)
			cnt += 1
	if sorted(a) == a:
		print(cnt)
	else:
		print(-1)


Inp = input
for _ in range(int(Inp())):
	(n, k) = map(int, Inp().split())
	l = [*map(int, Inp().split())]
	b = 0
	for i in range(n):
		if sorted(l) == l:
			break
		if l[i] > k:
			(l[i], k) = (k, l[i])
			b += 1
	print([-1, b][sorted(l) == l])


from sys import stdin
input = stdin.readline
from heapq import heapify, heappush, heappop
from collections import defaultdict as dd, deque as dq, Counter as C
from math import factorial as f, ceil, gcd, sqrt, log
from itertools import permutations as p
from bisect import bisect_left as bl, bisect_right as br

def fibo_n(n):
	return ((1 + sqrt(5)) / 2) ** n / sqrt(5)
n = int(input())
for i in range(n):
	(a, b) = map(int, input().split())
	li = list(map(int, input().split()))
	index = None
	for j in range(a - 1):
		if li[j] > li[j + 1]:
			index = j
	if index == None:
		print(0)
	else:
		out = 0
		x = b
		for k in range(index + 1):
			if li[k] > x:
				(li[k], x) = (x, li[k])
				out += 1
		if li == sorted(li):
			print(out)
		else:
			print(-1)


def sS():
	(n, x) = map(int, input().split())
	arr = list(map(int, input().split()))
	if check(arr) == True:
		return 0
	c = 0
	for i in range(n):
		if arr[i] > x:
			(arr[i], x) = (x, arr[i])
			c += 1
		if check(arr) == True:
			return c
	if check(arr) == False:
		return -1

def check(arr):
	m = 0
	for i in range(len(arr) - 1):
		if arr[i] > arr[i + 1]:
			m += 1
	if m > 0:
		return False
	return True
t = int(input())
while t > 0:
	print(sS())
	t -= 1


def sol():
	for _ in range(int(input())):
		(n, x) = map(int, input().split())
		arr = list(map(int, input().split()))
		if arr == sorted(arr):
			print(0)
		else:
			count = 0
			flag = False
			for i in range(n):
				if arr[i] > x:
					(arr[i], x) = (x, arr[i])
					count += 1
					if arr == sorted(arr):
						flag = True
						break
			if flag == False:
				print(-1)
			else:
				print(count)
sol()


import sys

def solve():
	(n, x) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	o = 0
	if a == sorted(a):
		print(o)
		return
	for i in range(len(a)):
		if a == sorted(a):
			print(o)
			return
		if a[i] > x:
			temp = a[i]
			a[i] = x
			x = temp
			o += 1
	if a != sorted(a):
		print(-1)
	else:
		print(o)
num = int(input())
for n in range(num):
	solve()


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	in1 = list(map(int, input().split()))
	ans1 = 0
	in1_copy1 = in1
	for i in range(n):
		if sorted(in1) == in1_copy1:
			break
		elif in1[i] > k:
			(in1[i], k) = (k, in1[i])
			ans1 += 1
	print([-1, ans1][sorted(in1) == in1_copy1])


for _ in range(int(input())):
	(n, x) = map(int, input().split())
	l1 = list(map(int, input().split()))
	l2 = l1[:]
	l2.sort()
	if l1 == l2:
		print(0)
	else:
		co = 0
		for i in range(n):
			f1 = 0
			if l1[i] <= x:
				pass
			else:
				(l1[i], x) = (x, l1[i])
				co += 1
				for j in range(i + 1, n - 1, 1):
					if l1[j] > l1[j + 1]:
						f1 = 1
				if f1 == 0:
					break
		l3 = l1[:]
		l3.sort()
		if l3 == l1:
			print(co)
		else:
			print(-1)


def check_sort(list1):
	ans = 1
	for i in range(n - 1):
		if list1[i + 1] < list1[i]:
			ans = 0
			break
	return ans
t = int(input())
for _ in range(t):
	(n, x) = list(map(int, input().strip().split()))
	list1 = list(map(int, input().strip().split()))
	count = 0
	possible = 1
	for i in range(n):
		if check_sort(list1) == 1:
			break
		if list1[i] <= x:
			if i != 0 and list1[i] < list1[i - 1]:
				possible = 0
				break
		else:
			temp = list1[i]
			list1[i] = x
			x = temp
			count += 1
	if possible == 0:
		print(-1)
	else:
		print(count)


def read():
	return [int(x) for x in input().split()]
(ca,) = read()
for _ in range(ca):
	(n, x) = read()
	ds = read()
	i = 0
	r = 0
	for j in range(1, len(ds)):
		if ds[j] < ds[j - 1]:
			while i < j:
				if x < ds[i]:
					r += 1
					ds[i] ^= x
					x ^= ds[i]
					ds[i] ^= x
				i += 1
		if ds[j] < ds[j - 1]:
			r = -1
			break
	print(r)


test = int(input())
for t in range(0, test):
	(n, x) = input().split()
	n = int(n)
	x = int(x)
	a = [int(i) for i in input().split(' ')]
	ans = 0
	while 1:
		if a == sorted(a):
			print(ans)
			break
		f = 0
		for i in range(0, len(a)):
			if a[i] > x:
				temp = a[i]
				a[i] = x
				x = temp
				ans += 1
				f = 1
				break
		if a == sorted(a):
			print(ans)
			break
		if f == 0:
			print(-1)
			break


t = int(input())
ans = ''

def is_sorted(lst, key=lambda x: x):
	for (i, el) in enumerate(lst[1:]):
		if key(el) < key(lst[i]):
			return False
	return True
for j in range(t):
	(n, x) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	cnt = 0
	if is_sorted(a) == False:
		for i in range(n):
			if x < a[i]:
				(x, a[i]) = (a[i], x)
				cnt += 1
			if is_sorted(a):
				break
	if is_sorted(a) == False:
		cnt = -1
	ans += str(cnt) + '\n'
print(ans)


import sys
input = sys.stdin.readline

def solve():
	(n, x) = map(int, input().split())
	arr = list(map(int, input().split()))
	B = sorted(arr)
	if arr == B:
		return 0
	b = n - 1
	while b >= 0 and arr[b] == B[b]:
		b -= 1
	A = arr + [x]
	A.sort()
	cnt = 0
	prev = x
	for i in range(b + 1):
		if arr[i] != A[i]:
			if A[i] != prev:
				return -1
			prev = arr[i]
			cnt += 1
	return cnt
for _ in range(int(input())):
	print(solve())


for _ in range(int(input())):
	(n, x) = map(int, input().split())
	a = list(map(int, input().split()))
	res = 0
	i = 0
	backward = False
	while i < n - 1:
		if not backward:
			if a[i] > a[i + 1]:
				if a[i] <= x or x > a[i + 1]:
					res = -1
					break
				elif i > 0:
					if a[i - 1] <= x:
						buffer = a[i]
						a[i] = x
						x = buffer
						res += 1
						i += 1
					else:
						backward = True
						i -= 1
				else:
					buffer = a[i]
					a[i] = x
					x = buffer
					res += 1
					i += 1
			else:
				i += 1
		elif i > 0:
			if a[i - 1] > x:
				i -= 1
			else:
				backward = False
				buffer = a[i]
				a[i] = x
				x = buffer
				res += 1
				i += 1
		else:
			backward = False
			buffer = a[i]
			a[i] = x
			x = buffer
			res += 1
			i += 1
	print(res)


import sys
r = sys.stdin.readline
for _ in range(int(r())):
	(N, x) = map(int, r().split())
	L = list(map(int, r().split()))
	answer = 0
	for i in range(N):
		if L[i] > x:
			if L[i:] == sorted(L[i:]):
				break
			(L[i], x) = (x, L[i])
			answer += 1
	if L == sorted(L):
		print(answer)
	else:
		print(-1)


import math
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	ans = 0
	prob = 0
	rang = 0
	for i in range(1, n):
		if a[i] < a[i - 1]:
			prob = 1
			rang = i
	for i in range(rang):
		if a[i] > k:
			(a[i], k) = (k, a[i])
			ans += 1
	probb = 0
	for i in range(1, n):
		if a[i] < a[i - 1]:
			probb = 1
			break
	if prob == 0:
		print('0')
	elif probb == 1:
		print('-1')
	else:
		print(ans)


for T in range(int(input())):
	(n, x) = [int(x) for x in input().split()]
	arr = [int(x) for x in input().split()]
	ans = 0
	if arr == sorted(arr):
		print(ans)
		continue
	done = 0
	for i in range(n):
		if arr == sorted(arr):
			print(ans)
			done = 1
			break
		if arr[i] > x:
			(arr[i], x) = (x, arr[i])
			ans += 1
	if done:
		continue
	elif arr == sorted(arr):
		print(ans)
	else:
		print(-1)


from sys import stdin, stdout
import collections, heapq, bisect, math
input = stdin.readline

def rint():
	return int(input())

def rstr():
	return input().strip()

def rlstr():
	return list(input().strip().split())

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

def main():
	(n, x) = rlint()
	arr = rlint()
	if all((arr[i] <= arr[i + 1] for i in range(n - 1))):
		return 0
	res = 0
	for i in range(n):
		if x < arr[i]:
			(x, arr[i]) = (arr[i], x)
			res += 1
			flag = True
			for j in range(n - 1):
				if arr[j] > arr[j + 1]:
					flag = False
			if flag:
				return res
	for i in range(n - 1):
		if arr[i] > arr[i + 1]:
			return -1
	return res
for _ in range(rint()):
	print(main())


def is_sorted(a):
	n = len(a)
	for i in range(n - 1):
		if a[i + 1] < a[i]:
			return False
	return True
t = int(input())
for case in range(t):
	(n, x) = [int(s) for s in input().split(' ')]
	a = [int(s) for s in input().split(' ')]
	ans = 0
	i = 0
	while i < n:
		if is_sorted(a):
			break
		if a[i] > x:
			(a[i], x) = (x, a[i])
			ans += 1
		i += 1
	if not is_sorted(a):
		ans = -1
	print(ans)


from bisect import *
from collections import *
from math import gcd, ceil, sqrt, floor, inf
from heapq import *
from itertools import *
from operator import add, mul, sub, xor, truediv, floordiv
from functools import *
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

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

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

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

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

class IOWrapper(IOBase):

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

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def RLL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def N():
	return int(input())
from types import GeneratorType

def bootstrap(f, stack=[]):

	def wrappedfunc(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		else:
			to = f(*args, **kwargs)
			while True:
				if type(to) is GeneratorType:
					stack.append(to)
					to = next(to)
				else:
					stack.pop()
					if not stack:
						break
					to = stack[-1].send(to)
			return to
	return wrappedfunc
farr = [1]
ifa = []

def fact(x, mod=0):
	if mod:
		while x >= len(farr):
			farr.append(farr[-1] * len(farr) % mod)
	else:
		while x >= len(farr):
			farr.append(farr[-1] * len(farr))
	return farr[x]

def ifact(x, mod):
	global ifa
	ifa.append(pow(farr[-1], mod - 2, mod))
	for i in range(x, 0, -1):
		ifa.append(ifa[-1] * i % mod)
	ifa = ifa[::-1]

def per(i, j, mod=0):
	if i < j:
		return 0
	if not mod:
		return fact(i) // fact(i - j)
	return farr[i] * ifa[i - j] % mod

def com(i, j, mod=0):
	if i < j:
		return 0
	if not mod:
		return per(i, j) // fact(j)
	return per(i, j, mod) * ifa[j] % mod

def catalan(n):
	return com(2 * n, n) // (n + 1)

def isprime(n):
	for i in range(2, int(n ** 0.5) + 1):
		if n % i == 0:
			return False
	return True

def lowbit(n):
	return n & -n

def inverse(a, m):
	a %= m
	if a <= 1:
		return a
	return (1 - inverse(m, a) * m) // a % m

class BIT:

	def __init__(self, arr):
		self.arr = arr
		self.n = len(arr) - 1

	def update(self, x, v):
		while x <= self.n:
			self.arr[x] += v
			x += x & -x

	def query(self, x):
		ans = 0
		while x:
			ans += self.arr[x]
			x &= x - 1
		return ans

class DSU:

	def __init__(self, n):
		self.c = [-1] * n

	def same(self, x, y):
		return self.find(x) == self.find(y)

	def find(self, x):
		if self.c[x] < 0:
			return x
		self.c[x] = self.find(self.c[x])
		return self.c[x]

	def union(self, u, v):
		(u, v) = (self.find(u), self.find(v))
		if u == v:
			return False
		if self.c[u] < self.c[v]:
			(u, v) = (v, u)
		self.c[u] += self.c[v]
		self.c[v] = u
		return True

	def size(self, x):
		return -self.c[self.find(x)]

class UFS:

	def __init__(self, n):
		self.parent = [i for i in range(n)]
		self.ranks = [0] * n

	def find(self, x):
		if x != self.parent[x]:
			self.parent[x] = self.find(self.parent[x])
		return self.parent[x]

	def union(self, u, v):
		(pu, pv) = (self.find(u), self.find(v))
		if pu == pv:
			return False
		if self.ranks[pu] >= self.ranks[pv]:
			self.parent[pv] = pu
			if self.ranks[pv] == self.ranks[pu]:
				self.ranks[pu] += 1
		else:
			self.parent[pu] = pv

def Prime(n):
	c = 0
	prime = []
	flag = [0] * (n + 1)
	for i in range(2, n + 1):
		if not flag[i]:
			prime.append(i)
			c += 1
		for j in range(c):
			if i * prime[j] > n:
				break
			flag[i * prime[j]] = prime[j]
			if i % prime[j] == 0:
				break
	return flag

def dij(s, graph):
	d = {}
	d[s] = 0
	heap = [(0, s)]
	seen = set()
	while heap:
		(dis, u) = heappop(heap)
		if u in seen:
			continue
		for v in graph[u]:
			if v not in d or d[v] > d[u] + graph[u][v]:
				d[v] = d[u] + graph[u][v]
				heappush(heap, (d[v], v))
	return d

def GP(it):
	return [[ch, len(list(g))] for (ch, g) in groupby(it)]

class DLN:

	def __init__(self, val):
		self.val = val
		self.pre = None
		self.next = None

def nb(i, j):
	for (ni, nj) in [[i + 1, j], [i - 1, j], [i, j - 1], [i, j + 1]]:
		if 0 <= ni < n and 0 <= nj < m:
			yield (ni, nj)

@bootstrap
def gdfs(r, p):
	if len(g[r]) == 1 and p != -1:
		yield None
	for ch in g[r]:
		if ch != p:
			yield gdfs(ch, r)
	yield None
t = N()
for i in range(t):
	(n, x) = RL()
	a = RLL()
	if a == sorted(a):
		ans = 0
	else:
		ans = 0
		f = 0
		for i in range(n):
			if a[i] > x:
				(a[i], x) = (x, a[i])
				ans += 1
				if a == sorted(a):
					f = 1
					break
			else:
				continue
		if a == sorted(a):
			f = 1
		if not f:
			ans = -1
	print(ans)


for _ in range(int(input())):
	(n, x) = map(int, input().split())
	A = list(map(int, input().split()))
	if sorted(A) == A:
		print(0)
	else:
		i = 1
		ans = 0
		flag = 0
		while i < n:
			if A[i] < A[i - 1]:
				if x >= A[i - 1] and A[i] > x:
					(A[i], x) = (x, A[i])
					ans += 1
				else:
					j = 0
					for j in range(i):
						if A[j] > x:
							break
					else:
						flag = 1
						break
					for k in range(j, i):
						if x >= A[i - 1]:
							break
						if A[k] > x:
							(A[k], x) = (x, A[k])
							ans += 1
						elif A[k] == x:
							continue
					if A[i] >= A[i - 1]:
						continue
					if A[i] < A[i - 1] and A[i] > x:
						(A[i], x) = (x, A[i])
						ans += 1
					else:
						flag = 1
						break
			i += 1
		if flag:
			print(-1)
		else:
			print(ans)


from sys import stdin
from math import inf
input = lambda : stdin.readline().strip()
wtf = int(input())
for _ in range(wtf):
	(n, x) = map(int, input().split())
	a = list(map(int, input().split()))
	f = True
	mini = 0
	ind = 0
	can_e = 1
	for i in range(n - 1):
		if a[i] > a[i + 1]:
			ind = i
			while ind >= 0:
				if x < a[ind]:
					ind -= 1
				else:
					break
			if ind == i:
				f = False
				break
			ind += 1
			for j in range(ind, i + 1):
				if x < a[j]:
					(x, a[j]) = (a[j], x)
					mini += 1
			if a[i] > a[i + 1]:
				f = False
				break
	if f:
		print(mini)
	else:
		print(-1)


def check(l, n):
	for i in range(n - 1):
		if l[i] > l[i + 1]:
			return False
	return True

def main(n, x, l):
	c = 0
	for i in range(n):
		if check(l, n) is True:
			return c
		if l[i] > x:
			(x, l[i]) = (l[i], x)
			c += 1
	if check(l, n) is True:
		return c
	return -1
k = int(input())
for _ in range(k):
	(n, x) = map(int, input().strip().split())
	l = list(map(int, input().strip().split()))
	n = len(l)
	print(main(n, x, l))


t = int(input())
for _ in range(t):
	(n, x) = map(int, input().split())
	a = list(map(int, input().split()))
	count = 0
	i = 0
	while i < len(a):
		if a == sorted(a):
			break
		if a[i] > x:
			(a[i], x) = (x, a[i])
			count += 1
		else:
			i += 1
	if a == sorted(a):
		print(count)
	else:
		print(-1)


import sys
from bisect import bisect_left
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7

def resolve():
	t = int(input())
	for _ in range(t):
		(n, x) = map(int, input().split())
		A = [-f_inf] + list(map(int, input().split())) + [f_inf]
		res = 0
		while True:
			for i in range(n + 1):
				if A[i] > A[i + 1]:
					break
			else:
				break
			for i in range(n + 1):
				if A[i] > x:
					tmp = A[i]
					A[i] = x
					x = tmp
					res += 1
					break
			else:
				res = -1
				break
		print(res)
resolve()


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(n, x) = list(map(int, input().split()))
	arr = list(map(int, input().split()))
	ans = 0
	for i in range(1, len(arr)):
		if arr[i] < arr[i - 1]:
			index = i - 1
			while index >= 0 and arr[index] > x:
				index -= 1
				ans += 1
			index += 1
			for j in range(index, i):
				temp = arr[j]
				if arr[j] == x:
					ans -= 1
				arr[j] = x
				x = temp
	for i in range(1, len(arr)):
		if arr[i] < arr[i - 1]:
			ans = -1
	print(ans)


def func(x, l1):
	num = 0
	x0 = 0
	repeat = 0
	for i in range(len(l1) - 1):
		if l1[i] <= x:
			x0 = i + 1
		if x0 < i:
			if l1[i] == l1[i - 1]:
				repeat += 1
		if l1[i] > l1[i + 1]:
			if x > l1[i + 1] or (i > 0 and l1[i - 1] > l1[i + 1]):
				return -1
			num += i + 1 - x0 - repeat
			repeat = 0
			xx = x
			x = l1[i]
			l1[i] = xx
	return num
for test_num in range(int(input())):
	x = int(input().split(' ')[1])
	l = input().split(' ')
	l1 = []
	for i in l:
		l1.append(int(i))
	print(func(x, l1))


import operator
import os, sys
from collections import defaultdict, Counter, deque
from io import BytesIO, IOBase
from itertools import accumulate
from math import gcd, ceil, inf, sqrt, floor

def lcm(a, b):
	return a * b // gcd(a, b)
MAXN = 100001
spf = [0 for i in range(MAXN)]

def getFactorization(x):
	ret = list()
	while x != 1:
		ret.append(spf[x])
		x = x // spf[x]
	return ret

def printDivisors(n):
	i = 2
	z = []
	while i <= sqrt(n):
		if n % i == 0:
			if n / i == i:
				z.append(i)
			else:
				z.append(i)
				z.append(n // i)
		i = i + 1
	return z

def cheaker(a):
	j = 1
	p = False
	for i in a:
		if i != j:
			break
		j += 1
	q = len(a)
	for i in a[::-1]:
		if i != q:
			break
		q -= 1
	for i in range(j - 1, q):
		if a[i] == i + 1:
			return False
	return True

def create(n, x, f):
	pq = len(bin(n)[2:])
	if f == 0:
		tt = min
	else:
		tt = max
	dp = [[inf] * n for _ in range(pq)]
	dp[0] = x
	for i in range(1, pq):
		for j in range(n - (1 << i) + 1):
			dp[i][j] = tt(dp[i - 1][j], dp[i - 1][j + (1 << i - 1)])
	return dp

def enquiry(l, r, dp, f):
	if l > r:
		return inf if not f else -inf
	if f == 1:
		tt = max
	else:
		tt = min
	pq1 = len(bin(r - l + 1)[2:]) - 1
	return tt(dp[pq1][l], dp[pq1][r - (1 << pq1) + 1])

def main():
	for _ in range(int(input())):
		(n, x) = map(int, input().split())
		a = [int(x) for x in input().split()]
		an = 1
		p = n
		if a == sorted(a):
			print(0)
		else:
			for i in range(n):
				if a[i] > x:
					p = i
					break
				elif i > 0:
					if a[i - 1] > a[i]:
						an = -1
						break
			q = [False] * n
			q[-1] = True
			for i in range(n - 2, -1, -1):
				if a[i] <= a[i + 1]:
					q[i] = True
				else:
					break
			if an == 1:
				c = 0
				for i in range(p, n):
					if x < a[i]:
						(x, a[i]) = (a[i], x)
						c += 1
						if i != n - 1:
							if a[i + 1] >= a[i] and q[i + 1]:
								break
				if a == sorted(a):
					an = 1
				else:
					an = -1
			if an == 1:
				print(c)
			else:
				print(-1)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

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

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

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

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

class IOWrapper(IOBase):

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


def notsorted(l, n):
	for i in range(0, n - 1):
		if l[i] > l[i + 1]:
			return True
	return False
for _ in range(int(input())):
	(n, x) = map(int, input().split())
	l = list(map(int, input().split()))
	ans = 0
	i = 0
	flag = True
	while notsorted(l, n):
		ans += 1
		while i < n and l[i] <= x:
			i += 1
		if i == n:
			print(-1)
			flag = False
			break
		else:
			(x, l[i]) = (l[i], x)
	if flag:
		print(ans)


import math
t = int(input())
for _ in range(t):
	(n, x) = map(int, input().split())
	a = list(map(int, input().split()))
	cnt = 0
	for i in range(1, n):
		if a[i] >= a[i - 1]:
			cnt += 1
	if cnt == n - 1:
		print(0)
		continue
	ans = -1
	for i in range(n):
		b = []
		for j in range(n):
			if j != i:
				b.append(a[j])
			else:
				b.append(x)
		b.sort()
		c = []
		for j in range(n):
			if a[j] != b[j]:
				c.append((a[j], j))
		c.sort(key=lambda k: k[0])
		curr = 0
		xx = x
		for (aj, j) in c:
			if b[j] != xx or aj <= xx:
				curr = -1
				break
			else:
				xx = aj
		if xx != a[i]:
			continue
		if curr != -1:
			curr = len(c)
			if ans == -1:
				ans = curr
			else:
				ans = min(curr, ans)
	print(ans)


a = int(input())
for i in range(a):
	(n, k) = map(int, input().split())
	x = list(map(int, input().split()))
	l = 0
	for j in range(n):
		if sorted(x) == x:
			break
		elif x[j] > k:
			(x[j], k) = (k, x[j])
			l = l + 1
	if sorted(x) == x:
		print(l)
	else:
		print(-1)


from collections import defaultdict
import sys
import bisect
input = sys.stdin.readline
t = int(input())
for ii in range(t):
	(n, x) = map(int, input().split())
	a = [int(i) for i in input().split() if i != '\n']
	ans = 0
	if a == sorted(a):
		print(0)
		continue
	for i in range(n):
		if a[i] > x:
			(a[i], x) = (x, a[i])
			ans += 1
			if a == sorted(a):
				break
	else:
		ans = -1
	print(ans)


from collections import defaultdict as dd
from sys import stdin, stdout
from bisect import bisect_left, insort
from math import log, sqrt
for t in range(int(input())):
	(n, x) = map(int, input().split())
	a = list(map(int, input().split()))
	b = a.copy()
	b.sort()
	if a == b:
		print(0)
		continue
	c = 0
	flag = True
	for i in range(n):
		if a[i] > x:
			(a[i], x) = (x, a[i])
			c += 1
		b = a.copy()
		b.sort()
		if a == b:
			print(c)
			flag = False
			break
	if flag:
		print(-1)


for _ in range(int(input())):
	(n, x) = map(int, input().split())
	A = [int(x) for x in input().split()]
	count = 0
	for i in range(n):
		if A == sorted(A):
			break
		if A[i] > x:
			k = A[i]
			A[i] = x
			x = k
			count += 1
	if A == sorted(A):
		print(count)
	else:
		print(-1)


for u in range(int(input())):
	(n, x) = map(int, input().split())
	a = list(map(int, input().split()))
	i = 0
	ans = 0
	while i < n:
		k = list(a)
		k.sort()
		if k == a:
			break
		if a[i] > x:
			(a[i], x) = (x, a[i])
			ans += 1
		if x > max(a):
			break
		i += 1
	k = list(a)
	k.sort()
	if k == a:
		print(ans)
	else:
		print(-1)


for i in range(int(input())):
	(n, x) = map(int, input().split())
	l = list(map(int, input().split()))
	c = 0
	m = 0
	for i in range(n):
		if sorted(l) == l:
			break
		if l[i] > x:
			c = max(l[i], x)
			l[i] = x
			x = c
			m += 1
	k = sorted(l)
	if k == l:
		print(m)
	else:
		print(-1)


def sort1(l):
	n = len(l)
	for i in range(1, n):
		if l[i - 1] > l[i]:
			return False
	return True
for _ in range(int(input())):
	(n, x) = list(map(int, input().split()))
	l = list(map(int, input().split()))
	ans = 0
	f = 0
	while not sort1(l):
		ans += 1
		i = 0
		while i < n and l[i] <= x:
			i += 1
		if i == n:
			f = 1
			break
		(x, l[i]) = (l[i], x)
	if f == 1:
		print(-1)
	else:
		print(ans)


def check(a):
	if sorted(a) == a:
		return 1
	return 0
t = int(input())
for _ in range(t):
	kt = 0
	cnt = 0
	(n, x) = map(int, input().split())
	a = list(map(int, input().split()))
	if check(a):
		print(0)
		continue
	for i in range(n):
		if x < a[i]:
			a[i] = a[i] ^ x
			x = a[i] ^ x
			a[i] = a[i] ^ x
			cnt = cnt + 1
		if check(a):
			kt = 1
			break
	if kt:
		print(cnt)
	else:
		print(-1)


t_count = int(input())
for _ in range(t_count):
	(num_count, x) = map(int, input().split())
	nums = list(map(int, input().split()))
	operation_count = 0
	sorted_until = 0
	place_of_x = 0
	last_was_swap = False
	while sorted_until != num_count - 1:
		if nums[sorted_until + 1] >= nums[sorted_until]:
			if nums[sorted_until] <= x:
				place_of_x += 1
			sorted_until += 1
			last_was_swap = False
		else:
			operation_count += 1
			(nums[place_of_x], x) = (x, nums[place_of_x])
			if last_was_swap:
				operation_count = -1
				break
			last_was_swap = True
			sorted_until = place_of_x
	print(operation_count)


from itertools import accumulate
import os
import sys
from io import BytesIO, IOBase
import math
import bisect
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

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

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

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

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

class IOWrapper(IOBase):

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

def input():
	return sys.stdin.readline().rstrip('\r\n')
for _ in range(int(input())):
	(n, x) = map(int, input().split())
	f = 0
	a = list(map(int, input().split()))
	ans = 0
	while True:
		if a == sorted(a):
			break
		else:
			for i in range(0, len(a)):
				if a[i] > x:
					ans += 1
					(a[i], x) = (x, a[i])
				if a == sorted(a):
					f = 1
					break
		if f == 1:
			break
		else:
			ans = -1
			break
	print(ans)


def how_many_operations_need_to_make_sorted(a_seq, x):
	a_seq = list(a_seq)
	operations_n = 0
	while not __is_sorted(a_seq):
		for i in range(len(a_seq)):
			if a_seq[i] > x:
				(a_seq[i], x) = (x, a_seq[i])
				operations_n += 1
				break
		else:
			raise ValueError
	return operations_n

def __is_sorted(seq):
	for i in range(len(seq) - 1):
		if not seq[i] <= seq[i + 1]:
			return False
	return True

def __main():
	(t,) = __read_ints()
	for i_t in range(t):
		(n, x) = __read_ints()
		a_seq = tuple(__read_ints())
		try:
			result = how_many_operations_need_to_make_sorted(a_seq, x)
		except ValueError:
			result = -1
		print(result)

def __read_ints():
	return map(int, input().split())
__main()


def ans(A, x):
	if A == sorted(A):
		return 0
	i = 0
	ret = 0
	while i < len(A) and A != sorted(A):
		if x < A[i]:
			ret += 1
			(x, A[i]) = (A[i], x)
			i += 1
		else:
			i += 1
			continue
	if A == sorted(A):
		return ret
	return -1
T = int(input())
for _ in range(T):
	(_, x) = input().split()
	x = int(x)
	A = list(map(int, input().split()))
	print(ans(A, x))


def probeg(n, a):
	index = 0
	LN = 0
	V = 1
	while index < n and V:
		if a[index] < LN:
			V = 0
		LN = a[index]
		index += 1
	if V:
		return 0
	else:
		return 1
t = int(input())
for _ in range(t):
	(n, x) = map(int, input().split())
	a = list(map(int, input().split()))
	V = probeg(n, a)
	answer = 0
	S = 1
	while V:
		answer += 1
		index = 0
		valid = 1
		while index < n and valid:
			if a[index] > x:
				t = a[index]
				a[index] = x
				x = t
				valid = 0
			if index == n - 1:
				if a[index] <= x:
					answer = -1
					S = 0
			index += 1
		if S:
			V = probeg(n, a)
		else:
			V = 0
	print(answer)


t = int(input())
for i in range(t):
	(n, x) = map(int, input().split())
	arr = list(map(int, input().split()))
	ans = 0
	while not all((arr[j] <= arr[j + 1] for j in range(len(arr) - 1))):
		ans += 1
		for j in range(len(arr)):
			if arr[j] > x:
				(arr[j], x) = (x, arr[j])
				break
		else:
			ans = -1
			break
	print(ans)


def s(a, n):
	for i in range(1, n):
		if arr[i] < arr[i - 1]:
			return False
	return True
for _ in range(int(input())):
	(n, k) = map(int, input().split(' '))
	arr = [int(num) for num in input().split(' ')]
	a = arr.copy()
	a.sort()
	ans = 0
	while s(arr, n) == False:
		f = 0
		for i in range(n):
			if arr[i] > k:
				temp = arr[i]
				arr[i] = k
				k = temp
				ans = ans + 1
				f = 1
				break
		if f == 0:
			break
	if s(arr, n) == True:
		print(ans)
	else:
		print(-1)


for u in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	ans = 0
	for i in range(n):
		if sorted(a) == a:
			break
		if a[i] > k:
			(a[i], k) = (k, a[i])
			ans += 1
	if sorted(a) == a:
		print(ans)
	else:
		print(-1)


t = int(input())
for i in range(t):
	(n, x) = map(int, input().split())
	array = [int(num) for num in input().split()]
	ans = 0
	j = 0
	found = False
	if array == sorted(array):
		print(0)
		continue
	for i in range(n):
		if array[i] > x:
			temp = array[i]
			array[i] = x
			x = temp
			ans += 1
			if array == sorted(array):
				print(ans)
				found = True
				break
	if found == False:
		print(-1)


def calc_swaps(nums, x):
	if nums == sorted(nums):
		return 0
	swaps = 0
	for ind in range(len(nums)):
		if nums[ind] > x:
			(x, nums[ind]) = (nums[ind], x)
			swaps += 1
		if nums == sorted(nums):
			return swaps
	return -1
data_count = int(input())
ans = []
for _ in range(data_count):
	(cnt, x) = list(map(int, input().split()))
	nums = list(map(int, input().split()))
	ans.append(calc_swaps(nums, x))
for res in ans:
	print(res)


t = int(input())

def swap(a, b):
	return (b, a)
for _ in range(t):
	(n, x) = map(int, input().split())
	l = [*map(int, input().split())]
	a = 0
	for i in range(n):
		if sorted(l) == l:
			break
		if l[i] > x:
			(l[i], x) = swap(l[i], x)
			a += 1
	print([-1, a][sorted(l) == l])


def f(l):
	last = l[0]
	bool = True
	for i in l[1:]:
		if last > i:
			bool = False
			break
		last = i
	return bool
for _ in range(int(input())):
	(n, x) = map(int, input().split())
	l = list(map(int, input().split()))
	bool = False
	ans = 0
	if f(l):
		print(0)
		continue
	while True:
		ans += 1
		i = 0
		while i < n and l[i] <= x:
			i += 1
		if i == n:
			ans = -1
			break
		(l[i], x) = (x, l[i])
		if f(l):
			break
	print(ans)


import sys

def minp():
	return sys.stdin.readline().strip()

def mint():
	return int(minp())

def mints():
	return map(int, minp().split())

def solve():
	(n, x) = mints()
	a = list(mints())
	b = a.copy()
	b.append(x)
	b.sort()
	res = int(1000000000.0)
	for i in range(n + 1):
		ok = True
		if b[i] >= x:
			k = 0
			xx = x
			cnt = 0
			for j in range(n):
				if k == i:
					k += 1
				if b[k] != a[j]:
					if xx != b[k] or a[j] < xx:
						ok = False
						break
					xx = a[j]
					cnt += 1
				k += 1
			if ok:
				res = min(res, cnt)
	if res >= int(1000000000.0):
		print(-1)
	else:
		print(res)
for t in range(mint()):
	solve()


import sys, os, getpass
import heapq as hq
import math, random, functools, itertools
from collections import Counter, defaultdict, deque
input = sys.stdin.readline
OFFLINE_TEST = getpass.getuser() == 'hkmac'

def log(*args):
	if OFFLINE_TEST:
		print('\x1b[36m', *args, '\x1b[0m', file=sys.stderr)

def solve_(lst, x):
	if lst == sorted(lst):
		return 0
	count = 0
	for (i, a) in enumerate([x for x in lst]):
		if a > x:
			(x, lst[i]) = (a, x)
			count += 1
			if lst == sorted(lst):
				return count
	return -1

def solve(*args):
	if OFFLINE_TEST:
		log('----- solving ------')
		log(*args)
		log('----- ------- ------')
	return solve_(*args)

def read_matrix(rows):
	return [list(map(int, input().split())) for _ in range(rows)]

def read_strings(rows):
	return [input().strip() for _ in range(rows)]
for case_num in range(int(input())):
	(_, x) = list(map(int, input().split()))
	lst = list(map(int, input().split()))
	res = solve(lst, x)
	print(res)


def find(a, si):
	x = a[si]
	for i in range(si + 1, len(a)):
		if x > a[i]:
			return False
		x = a[i]
	return True

def solve(a, x):
	p = 0
	c = 0
	while p < len(a):
		if find(a, 0):
			return c
		if x < a[p]:
			(x, a[p]) = (a[p], x)
			c += 1
		p += 1
	return -1
t = int(input())
for _ in range(t):
	(n, x) = [int(i) for i in input().split()]
	l = [int(i) for i in input().split()]
	print(solve(l, x))


def check(arr):
	newarr = []
	for i in range(1, len(arr)):
		if arr[i - 1] > arr[i]:
			return False
	return True
t = int(input())
for i in range(t):
	(n, x) = input().split()
	n = int(n)
	x = int(x)
	a = [int(i) for i in input().split()]
	j = 0
	c = 0
	while j < n:
		if check(a):
			break
		if a[j] > x:
			(a[j], x) = (x, a[j])
			c += 1
		j += 1
	z = []
	for i in a:
		z.append(i)
	z.sort()
	c1 = 0
	for i in range(len(a)):
		if a[i] == z[i]:
			c1 += 1
	if c1 == len(a):
		pass
	else:
		c = -1
	print(c)


for i in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	l = 0
	sumo = 0
	for j in range(1, n):
		if a[j] < a[j - 1]:
			l = l + 1
	if l == 0:
		print('0')
	else:
		l = 0
		m = 0
		for j in range(n - 1, 1, -1):
			m = m + 1
			if a[j] < a[j - 1]:
				break
		for j in range(n):
			if a[j] > k:
				if j >= n - m:
					break
				t = a[j]
				a[j] = k
				k = t
				sumo = sumo + 1
		for j in range(1, n):
			if a[j] < a[j - 1]:
				l = l + 1
				break
		if l == 0:
			print(sumo)
		else:
			print('-1')


for test_var in range(int(input())):
	(n, x) = map(int, input().split())
	arr = list(map(int, input().split()))

	def get_index():
		for (i, m) in enumerate(arr):
			if m > x:
				return i
		return -1
	count = 0
	while arr != sorted(arr):
		index = get_index()
		if index < 0:
			break
		(arr[index], x) = (x, arr[index])
		count += 1
	if arr == sorted(arr):
		print(count)
	else:
		print(-1)


import sys
input = sys.stdin.readline
import copy
t = int(input())
for i in range(t):
	(n, x) = map(int, input().split())
	a = list(map(int, input().split()))
	b = copy.copy(a)
	flg = 0
	b.sort()
	if a == b:
		flg = 2
	a_copy = copy.copy(a)
	a_copy.append(x)
	a_copy.sort()
	a_copy = a_copy[:n]
	ans = 0
	for (i, k) in enumerate(a_copy):
		if a[i] != k:
			if x == k and k < a[i]:
				(x, a[i]) = (a[i], x)
				ans += 1
			else:
				flg = 1
				break
		b = copy.copy(a)
		b.sort()
		if a == b:
			break
	if flg == 1:
		print(-1)
	elif flg == 2:
		print(0)
	else:
		print(ans)


def check(arr):
	for i in range(len(arr) - 1):
		if arr[i] > arr[i + 1]:
			return False
	return True
for _ in range(int(input())):
	(n, x) = map(int, input().split())
	arr = list(map(int, input().split()))
	if check(arr):
		print(0)
	else:
		asn = 0
		for j in range(n):
			if arr[j] > x:
				(arr[j], x) = (x, arr[j])
				asn += 1
			if check(arr):
				break
		if check(arr) == False:
			print(-1)
		else:
			print(asn)


t = int(input())

def do_step(arr, x, i):
	while i < len(arr):
		if x < arr[i]:
			(arr[i], x) = (x, arr[i])
			return (x, i)
		i += 1
	return (x, i)
for _ in range(t):
	(n, x) = map(int, input().split())
	arr = [int(v) for v in input().split()]
	steps = 0
	i = 0
	while i != len(arr):
		if sorted(arr) == arr:
			break
		(x, i) = do_step(arr, x, i)
		if i != -1:
			steps += 1
	if sorted(arr) != arr:
		steps = -1
	print(steps)


for _ in range(int(input())):
	(n, x) = [int(x) for x in input().split()]
	l = list(map(int, input().split()))
	count = 0
	for i in range(len(l)):
		flag = True
		for j in range(len(l) - 1):
			if l[j] > l[j + 1]:
				flag = False
		if flag == True:
			break
		elif l[i] > x:
			temp = x
			x = l[i]
			l[i] = temp
			count = count + 1
	for j in range(len(l) - 1):
		if l[j] > l[j + 1]:
			flag = False
	if flag == False:
		print(-1)
	else:
		print(count)


import bisect
for _ in range(int(input())):
	(n, x) = map(int, input().split())
	ai = list(map(int, input().split()))
	li = []
	zi = [ai[0]]
	for i in range(1, n):
		if ai[i] >= ai[i - 1]:
			zi.append(ai[i])
		else:
			li.append(zi)
			zi = [ai[i]]
	li.append(zi)
	ans = 0
	for i in range(1, len(li)):
		if li[i][0] < x:
			print(-1)
			break
		else:
			d = 0
			for ii in range(len(li[i - 1])):
				if li[i - 1][ii] > x:
					ans += 1
					x = li[i - 1][ii]
				if li[i][0] < x and ii < len(li[i - 1]) - 1:
					print(-1)
					d = not d
					break
			if d:
				break
	else:
		print(ans)


t = int(input())
for i in range(t):
	(n, x) = map(int, input().split())
	arr = list(map(int, input().split()))
	if arr == sorted(arr):
		print(0)
	else:
		res = 0
		for i in range(n):
			if arr[i] > x:
				temp = x
				x = arr[i]
				arr[i] = temp
				res += 1
			if arr == sorted(arr):
				print(res)
				break
			elif i == n - 1:
				print(-1)
				break


import sys
import math
from collections import Counter, defaultdict
LI = lambda : list(map(int, input().split()))
MAP = lambda : map(int, input().split())
IN = lambda : int(input())
S = lambda : input()

def case():
	(n, x) = MAP()
	a = LI()
	b = a.copy()
	b.sort()
	for i in range(n - 1, -1, -1):
		if b[i] != a[i]:
			break
		else:
			a.pop()
	l = len(a)
	if l == 0:
		print(0)
		return
	b = b[:l]
	b.remove(max(a))
	b += [x]
	b.sort()
	c = []
	for i in range(l):
		if a[i] != b[i]:
			c += [a[i]]
	for i in range(1, len(c)):
		if c[i - 1] > c[i]:
			print(-1)
			return
	if x >= min(c):
		print(-1)
	else:
		print(len(c))
for _ in range(IN()):
	case()


t = int(input())
for o in range(t):
	(n, x) = map(int, input().split())
	l = list(map(int, input().split()))
	(c, flag) = (0, False)
	for i in range(n):
		m = l[:]
		m.sort()
		if m == l:
			flag = True
			break
		elif l[i] > x:
			(x, l[i]) = (l[i], x)
			c += 1
	if flag:
		print(c)
	else:
		print(-1)


import sys
input = sys.stdin.buffer.readline
t = int(input())
INF = 10 ** 18
for _ in range(t):
	(n, x) = map(int, input().split())
	a = list(map(int, input().split()))
	if sorted(a) == a:
		print(0)
		continue
	cnt = 0
	for i in range(n):
		if a[i] > x:
			(a[i], x) = (x, a[i])
			cnt += 1
		if sorted(a) == a:
			print(cnt)
			break
	else:
		print(-1)


import os
import sys
from io import BytesIO, IOBase
import heapq as h
import bisect
import math as mt
from types import GeneratorType
BUFSIZE = 8192

class SortedList:

	def __init__(self, iterable=[], _load=200):
		values = sorted(iterable)
		self._len = _len = len(values)
		self._load = _load
		self._lists = _lists = [values[i:i + _load] for i in range(0, _len, _load)]
		self._list_lens = [len(_list) for _list in _lists]
		self._mins = [_list[0] for _list in _lists]
		self._fen_tree = []
		self._rebuild = True

	def _fen_build(self):
		self._fen_tree[:] = self._list_lens
		_fen_tree = self._fen_tree
		for i in range(len(_fen_tree)):
			if i | i + 1 < len(_fen_tree):
				_fen_tree[i | i + 1] += _fen_tree[i]
		self._rebuild = False

	def _fen_update(self, index, value):
		if not self._rebuild:
			_fen_tree = self._fen_tree
			while index < len(_fen_tree):
				_fen_tree[index] += value
				index |= index + 1

	def _fen_query(self, end):
		if self._rebuild:
			self._fen_build()
		_fen_tree = self._fen_tree
		x = 0
		while end:
			x += _fen_tree[end - 1]
			end &= end - 1
		return x

	def _fen_findkth(self, k):
		_list_lens = self._list_lens
		if k < _list_lens[0]:
			return (0, k)
		if k >= self._len - _list_lens[-1]:
			return (len(_list_lens) - 1, k + _list_lens[-1] - self._len)
		if self._rebuild:
			self._fen_build()
		_fen_tree = self._fen_tree
		idx = -1
		for d in reversed(range(len(_fen_tree).bit_length())):
			right_idx = idx + (1 << d)
			if right_idx < len(_fen_tree) and k >= _fen_tree[right_idx]:
				idx = right_idx
				k -= _fen_tree[idx]
		return (idx + 1, k)

	def _delete(self, pos, idx):
		_lists = self._lists
		_mins = self._mins
		_list_lens = self._list_lens
		self._len -= 1
		self._fen_update(pos, -1)
		del _lists[pos][idx]
		_list_lens[pos] -= 1
		if _list_lens[pos]:
			_mins[pos] = _lists[pos][0]
		else:
			del _lists[pos]
			del _list_lens[pos]
			del _mins[pos]
			self._rebuild = True

	def _loc_left(self, value):
		if not self._len:
			return (0, 0)
		_lists = self._lists
		_mins = self._mins
		(lo, pos) = (-1, len(_lists) - 1)
		while lo + 1 < pos:
			mi = lo + pos >> 1
			if value <= _mins[mi]:
				pos = mi
			else:
				lo = mi
		if pos and value <= _lists[pos - 1][-1]:
			pos -= 1
		_list = _lists[pos]
		(lo, idx) = (-1, len(_list))
		while lo + 1 < idx:
			mi = lo + idx >> 1
			if value <= _list[mi]:
				idx = mi
			else:
				lo = mi
		return (pos, idx)

	def _loc_right(self, value):
		if not self._len:
			return (0, 0)
		_lists = self._lists
		_mins = self._mins
		(pos, hi) = (0, len(_lists))
		while pos + 1 < hi:
			mi = pos + hi >> 1
			if value < _mins[mi]:
				hi = mi
			else:
				pos = mi
		_list = _lists[pos]
		(lo, idx) = (-1, len(_list))
		while lo + 1 < idx:
			mi = lo + idx >> 1
			if value < _list[mi]:
				idx = mi
			else:
				lo = mi
		return (pos, idx)

	def add(self, value):
		_load = self._load
		_lists = self._lists
		_mins = self._mins
		_list_lens = self._list_lens
		self._len += 1
		if _lists:
			(pos, idx) = self._loc_right(value)
			self._fen_update(pos, 1)
			_list = _lists[pos]
			_list.insert(idx, value)
			_list_lens[pos] += 1
			_mins[pos] = _list[0]
			if _load + _load < len(_list):
				_lists.insert(pos + 1, _list[_load:])
				_list_lens.insert(pos + 1, len(_list) - _load)
				_mins.insert(pos + 1, _list[_load])
				_list_lens[pos] = _load
				del _list[_load:]
				self._rebuild = True
		else:
			_lists.append([value])
			_mins.append(value)
			_list_lens.append(1)
			self._rebuild = True

	def discard(self, value):
		_lists = self._lists
		if _lists:
			(pos, idx) = self._loc_right(value)
			if idx and _lists[pos][idx - 1] == value:
				self._delete(pos, idx - 1)

	def remove(self, value):
		_len = self._len
		self.discard(value)
		if _len == self._len:
			raise ValueError('{0!r} not in list'.format(value))

	def pop(self, index=-1):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		value = self._lists[pos][idx]
		self._delete(pos, idx)
		return value

	def bisect_left(self, value):
		(pos, idx) = self._loc_left(value)
		return self._fen_query(pos) + idx

	def bisect_right(self, value):
		(pos, idx) = self._loc_right(value)
		return self._fen_query(pos) + idx

	def count(self, value):
		return self.bisect_right(value) - self.bisect_left(value)

	def __len__(self):
		return self._len

	def __getitem__(self, index):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		return self._lists[pos][idx]

	def __delitem__(self, index):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		self._delete(pos, idx)

	def __contains__(self, value):
		_lists = self._lists
		if _lists:
			(pos, idx) = self._loc_left(value)
			return idx < len(_lists[pos]) and _lists[pos][idx] == value
		return False

	def __iter__(self):
		return (value for _list in self._lists for value in _list)

	def __reversed__(self):
		return (value for _list in reversed(self._lists) for value in reversed(_list))

	def __repr__(self):
		return 'SortedList({0})'.format(list(self))

class FastIO(IOBase):
	newlines = 0

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

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

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

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

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
import sys
from math import ceil, log2
INT_MAX = sys.maxsize
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import collections as col
import math, string

def getInts():
	return [int(s) for s in input().split()]

def getInt():
	return int(input())

def getStrs():
	return [s for s in input().split()]

def getStr():
	return input()

def listStr():
	return list(input())

def minVal(x, y):
	return x if x < y else y

def getMid(s, e):
	return s + (e - s) // 2

def RMQUtil(st, ss, se, qs, qe, index):
	if qs <= ss and qe >= se:
		return st[index]
	if se < qs or ss > qe:
		return INT_MAX
	mid = getMid(ss, se)
	return minVal(RMQUtil(st, ss, mid, qs, qe, 2 * index + 1), RMQUtil(st, mid + 1, se, qs, qe, 2 * index + 2))

def RMQ(st, n, qs, qe):
	if qs < 0 or qe > n - 1 or qs > qe:
		print('Invalid Input')
		return -1
	return RMQUtil(st, 0, n - 1, qs, qe, 0)

def constructSTUtil(arr, ss, se, st, si):
	if ss == se:
		st[si] = arr[ss]
		return arr[ss]
	mid = getMid(ss, se)
	st[si] = minVal(constructSTUtil(arr, ss, mid, st, si * 2 + 1), constructSTUtil(arr, mid + 1, se, st, si * 2 + 2))
	return st[si]

def constructST(arr, n):
	x = int(ceil(log2(n)))
	max_size = 2 * int(2 ** x) - 1
	st = [0] * max_size
	constructSTUtil(arr, 0, n - 1, st, 0)
	return st
MOD = 10 ** 9 + 7
mod = 10 ** 9 + 7
p = 10 ** 9 + 7

def ncr_util():
	inv[0] = inv[1] = 1
	fact[0] = fact[1] = 1
	for i in range(2, 300001):
		inv[i] = inv[i % p] * (p - p // i) % p
	for i in range(1, 300001):
		inv[i] = inv[i - 1] * inv[i] % p
		fact[i] = fact[i - 1] * i % p

def z_array(s1):
	n = len(s1)
	z = [0] * n
	(l, r, k) = (0, 0, 0)
	for i in range(1, n):
		if i > r:
			(l, r) = (i, i)
			while r < n and s1[r - l] == s1[r]:
				r += 1
			z[i] = r - l
			r -= 1
		else:
			k = i - l
			if z[k] < r - i + 1:
				z[i] = z[k]
			else:
				l = i
				while r < n and s1[r - l] == s1[r]:
					r += 1
				z[i] = r - l
				r -= 1
	return z

def fs(x):
	if x < 2:
		return 0
	return x * (x - 1) // 2
t = int(input())

def solve(n, x, l):
	suf = [0] * (n + 1)
	maxn = 501
	for i in range(n - 1, -1, -1):
		if l[i] <= maxn:
			suf[i] = 1
		else:
			break
		maxn = min(maxn, l[i])
	ans = 0
	mini = 0
	for i in range(n):
		if suf[i] == 1 and l[i] >= mini:
			return ans
		if l[i] > x:
			y = l[i]
			l[i] = x
			x = y
			ans += 1
		if l[i] >= mini:
			mini = l[i]
			if suf[i] == 1:
				return ans
		else:
			return -1
	return ans
for _ in range(t):
	(n, x) = map(int, input().split())
	l = list(map(int, input().split()))
	print(solve(n, x, l))


import math
for _ in range(int(input())):
	(n, x) = map(int, input().split())
	l = list(map(int, input().split()))
	ll = sorted(l)
	s = 0
	for i in range(n):
		if sorted(l) == l:
			break
		elif l[i] > x:
			(l[i], x) = (x, l[i])
			s += 1
	if sorted(l) == l:
		print(s)
	else:
		print(-1)


import os
import sys
from io import BytesIO, IOBase
import math as M
import itertools as ITR
from collections import defaultdict as D
from collections import Counter as C
from collections import deque as Q
import threading
from functools import lru_cache, reduce
from functools import cmp_to_key as CMP
from bisect import bisect_left as BL
from bisect import bisect_right as BR
import random as R
import string
import cmath, time
enum = enumerate
start_time = time.time()
MOD = 1000000007
MA = float('inf')
MI = float('-inf')

def increase_stack():
	sys.setrecursionlimit(2 ** 32 // 2 - 1)
	threading.stack_size(1 << 27)
	t = threading.Thread(target=main)
	t.start()
	t.join()

def prints(a):
	print(a, end=' ')

def binary(n):
	return bin(n)[2:]

def decimal(s):
	return int(s, 2)

def pow2(n):
	p = 0
	while n > 1:
		n //= 2
		p += 1
	return p

def maxfactor(n):
	q = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			q.append(i)
	if q:
		return q[-1]

def factors(n):
	q = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			q.append(i)
			q.append(n // i)
	return list(sorted(list(set(q))))

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n / 2
	for i in range(3, int(M.sqrt(n)) + 1, 2):
		while n % i == 0:
			l.append(i)
			n = n / i
	if n > 2:
		l.append(int(n))
	l.sort()
	return l

def isPrime(n):
	if n == 1:
		return False
	else:
		root = int(n ** 0.5)
		root += 1
		for i in range(2, root):
			if n % i == 0:
				return False
		return True

def seive(n):
	a = [1]
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p ** 2, n + 1, p):
				prime[i] = False
		p = p + 1
	for p in range(2, n + 1):
		if prime[p]:
			a.append(p)
	return a

def maxPrimeFactors(n):
	maxPrime = -1
	while n % 2 == 0:
		maxPrime = 2
		n >>= 1
	for i in range(3, int(M.sqrt(n)) + 1, 2):
		while n % i == 0:
			maxPrime = i
			n = n / i
	if n > 2:
		maxPrime = n
	return int(maxPrime)

def countchar(s, i):
	c = 0
	ch = s[i]
	for i in range(i, len(s)):
		if s[i] == ch:
			c += 1
		else:
			break
	return c

def str_counter(a):
	q = [0] * 26
	for i in range(len(a)):
		q[ord(a[i]) - 97] = q[ord(a[i]) - 97] + 1
	return q

def lis(arr):
	n = len(arr)
	lis = [1] * n
	maximum = 0
	for i in range(1, n):
		for j in range(0, i):
			if arr[i] > arr[j] and lis[i] < lis[j] + 1:
				lis[i] = lis[j] + 1
				maximum = max(maximum, lis[i])
	return maximum

def lcm(arr):
	a = arr[0]
	val = arr[0]
	for i in range(1, len(arr)):
		gcd = gcd(a, arr[i])
		a = arr[i]
		val *= arr[i]
	return val // gcd

def ncr(n, r):
	return M.factorial(n) // (M.factorial(n - r) * M.factorial(r))

def npr(n, r):
	return M.factorial(n) // M.factorial(n - r)

def inint():
	return int(inp())

def inarr():
	return list(map(int, inp().split()))

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

def instr():
	s = inp()
	return list(s)

def insarr():
	return inp().split()
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

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

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

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

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

class IOWrapper(IOBase):

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

def solve():
	(n, x) = invar()
	arr = inarr()
	c = 0
	q = 0
	last = -1
	for i in range(1, n):
		if arr[i - 1] > arr[i]:
			c += 1
			last = i - 1
	if c == 0:
		print(0)
		return
	c = 0
	for i in range(n):
		if x < arr[i] and i <= last:
			(arr[i], x) = (x, arr[i])
			c += 1
	for i in range(1, n):
		if arr[i - 1] > arr[i]:
			q += 1
	if q > 0:
		print(-1)
		return
	print(c)

def main():
	flag = 0
	try:
		sys.stdin = open('c:/Users/Manoj Chowdary/Documents/python/CodeForces/contest-div-2/input.txt', 'r')
		sys.stdout = open('c:/Users/Manoj Chowdary/Documents/python/CodeForces/contest-div-2/output.txt', 'w')
	except:
		flag = 1
	t = 1
	if TestCases:
		t = inint()
	for _ in range(1, t + 1):
		solve()
	if not flag:
		print('Time: %.4f sec' % (time.time() - start_time))
		localtime = time.asctime(time.localtime(time.time()))
		print(localtime)
		sys.stdout.close()
main()


import sys
import bisect
from bisect import bisect_right
import math
from sys import stdin, stdout

def sieve(N):
	primeNumbers = [True] * (N + 1)
	primeNumbers[0] = False
	primeNumbers[1] = False
	i = 2
	while i * i <= N:
		j = i
		if primeNumbers[j]:
			while j * i <= N:
				primeNumbers[j * i] = False
				j += 1
		i += 1
	return primeNumbers

def getPrime(N):
	primes = sieve(N)
	result = []
	for i in range(len(primes)):
		if primes[i]:
			result.append(i)
	return result

def factor(N):
	factors = []
	i = 1
	while i * i <= N:
		if N % i == 0:
			factors.append(i)
			if i != N // i:
				factors.append(N // i)
		i += 1
	return sorted(factors)

def gcd(a, b):
	if a < b:
		return gcd(b, a)
	if b == 0:
		return a
	return gcd(b, a % b)

def extendedGcd(a, b):
	if a < b:
		return extendedGcd(b, a)
	if b == 0:
		return [a, 1, 0]
	res = extendedGcd(b, a % b)
	x = res[2]
	y = res[1] - math.floor(a / b) * res[2]
	res[1] = x
	res[2] = y
	return res

def iterativeModularFunc(a, b, c):
	res = 1
	while b > 0:
		if b & 1:
			res = res * a % c
		a = a * a % c
		b = b // 2
	return res

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

def get_int():
	return int(input())

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

def get_list_of_list(n):
	return [list(map(int, sys.stdin.readline().strip().split())) for _ in range(n)]

def get_string():
	return sys.stdin.readline().strip()

def getFirst(arr, n, x):
	for i in range(n):
		if arr[i] > x:
			return i
	return -1

def swap(arr, idx, b):
	tmp = arr[idx]
	arr[idx] = b
	b = tmp

def isSorted(arr, n):
	for i in range(n - 1):
		if arr[i] > arr[i + 1]:
			return False
	return True

def Solution(arr, n, x):
	ans = 0
	while not isSorted(arr, n):
		idx = getFirst(arr, n, x)
		if idx < 0:
			break
		tmp = arr[idx]
		arr[idx] = x
		x = tmp
		ans += 1
	print(ans) if isSorted(arr, n) else print(-1)

def main():
	for _ in range(get_int()):
		(n, x) = get_ints_in_variables()
		arr = get_ints_in_list()
		Solution(arr, n, x)
main()