#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e6 + 9;
int n,ans;
int a[N],min_a,max_a;
int c[N];
int K;
struct BinaryIndexedTree{
int t[N];
int lowbit(int x){
return x & (-x);
}
void update(int pos,int v){
if(pos <= 0)
return;
for(int i = pos;i <= n;i += lowbit(i))
t[i] += v;
}
int query(int pos){
if(pos <= 0)
return 0;
int ret = 0;
for(int i = pos;i;i -= lowbit(i))
ret += t[i];
return ret;
}
void clear(int pos){
if(pos <= 0)
return;
for(int i = pos;i <= n;i += lowbit(i))
t[i] = 0;
}
} BIT;
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin >> n >> K;
ans = n;
min_a = LLONG_MAX;
for(int i = 1;i <= n;i++){
cin >> a[i];
min_a = min(min_a,a[i]);
max_a = max(max_a,a[i]);
}
if(min_a == max_a){
cout << -1;
return 0;
}
int l = 1,r = 1;
int tmp = 0;
for(;l <= n;l++){
for(;r <= n && tmp < K;r++){
BIT.update(c[a[r]],-1);
c[a[r]]++;
BIT.update(c[a[r]],1);
tmp += 1ll * (BIT.query(n) - BIT.query(c[a[r]] - 1) - 1);
}
if(r > n)
break;
ans = min(ans,r - l);
BIT.update(c[a[l]],-1);
c[a[l]]--;
BIT.update(c[a[l]],1);
tmp -= 1ll * (BIT.query(n) - BIT.query(c[a[l]]));
}
cout << ans;
return 0;
}