#include<bits/stdc++.h>
using namespace std;
typedef int ll;
unordered_map<ll,ll> ma,pre;
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	ll n,m,i,j,k,a,b,c,d,p,q,h,t,sum =0 ,maxn = 0,minx = 2e9,x,y,z,ans = 0;
	string str;
	cin>>n>>m;
	for(i=1;i<=n;i++){
		cin>>a;
		b = i - pre[a];
		ma[a] = max(ma[a],b);
		pre[a] = i;
	}
	for(i=0;i<=n;i++){
		if(ma[i]==0||ma[i]>=m+1||pre[i]<=n-m){
			ans = i;
			break;
		}
	}
	cout<<ans;
}
#include<bits/stdc++.h>
using namespace std;

const int maxn = 1e5 + 233;
int a[maxn], b[maxn];
typedef long long ll;
int n, l;
map<int, int> pre, suf;

int main(){
	scanf("%d %d", &n, &l);
	pre[0] = 0;
	suf[l - n] = n + 1;
	for(int i = 1; i <= n; i++)scanf("%d", &a[i]);
	for(int i = 1; i <= n; i++)scanf("%d", &b[i]);
	ll res = 0;
	for(int i = 1; i <= n; i++){
		pre[a[i] - i] = i;
		if(a[i] <= b[i]) {
			continue;
		}
		if(pre.find(b[i] - i) == pre.end()){
			puts("-1");
			return 0;
		}
		res += i - pre[b[i] - i];
		pre[b[i] - i] = i;
	}
	for(int i = n; i; i--){
		suf[a[i] - i] = i;
		if(a[i] >= b[i]){
			continue;
		}
		if(suf.find(b[i] - i) == suf.end()){
			puts("-1");
			return 0;
		}
		res += suf[b[i] - i] - i;
		suf[b[i] - i] = i;
	}
	printf("%lld\n", res);
	return 0;
}
