#include<iostream> #include<cmath> #include<cstdio> #include<cstring> #include<string> #include<vector> #include<queue> #include<map> #include<algorithm> #define put putchar #define max(a, b) a > b ? a : b #define min(a, b) a < b ? a : b usingnamespace std;
constint N = 4e5 + 10; constint INF = 1e9; constint mod = 1e9 + 7;
int n, k; int a[N]; int ans = INF, l, r; int sum[N];
inlinelonglongread(){ longlong res = 0, f = 1; char c = getchar(); while(! isdigit(c)){ if(c == '-') f = -1; c = getchar(); } while(isdigit(c)) res = (res << 1) + (res << 3) + c - 48, c = getchar(); return res * f; } inlinevoidwrite(longlong x){ if(x >= 10) write(x / 10); put(x % 10 + 48); return ; } inlinevoidprint(longlong x){ if(x < 0) x = -x, put('-'); write(x); return ; }
intchange(int x) { if(sum[1] >= x) return0; int l = 1, r = k, mid; int res; while(l <= r) { mid = l + r >> 1; if(sum[mid] >= x) r = mid - 1; else l = mid + 1, res = mid; } return res; }
booltp() { if(n == k) returntrue; int mina = INF, maxa = -INF; for(int i = 1; i <= k; ++ i) mina = min(mina, a[i]); for(int i = k + 1; i <= n; ++ i) maxa = max(maxa, a[i]); if(maxa <= mina) returntrue; returnfalse; }
intmain() { memset(sum, 0x3f3f3f3f, sizeof sum); n = read(), k = read(); for(int i = 1; i <= n; ++ i) a[i] = read(); for(int i = k; i ; -- i) sum[i] = min(a[i], sum[i + 1]); if(tp()) { print(-1); return0; } int w; for(int i = k + 1; i <= n; ++ i) { w = change(a[i]); if(! w) continue; ans = min(ans, i - w); } print(ans); return0; }