#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
#define int long long

const int N = (int) 2e6 + 7;
int n, k, a[N];
vector<int> p[N];

signed main() {
  ios::sync_with_stdio(0); cin.tie(0);

  cin >> n >> k;
  for (int i = 0; i <= n; i++) p[i].push_back(0);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    p[a[i]].push_back(i);
  }
  for (int i = 0; i <= n; i++) p[i].push_back(n + 1);
  for (int x = 0; x <= n; x++) {
    for (int i = 0; i + 1 < (int) p[x].size(); i++) {
      int space = p[x][i + 1] - p[x][i] - 1;
      if (space >= k) {
        cout << x << "\n";
        return 0;
      }
    }
  }
  assert(0);



}

#include<bits/stdc++.h>

using namespace std;

#define gc c=getchar()
#define r(x) read(x)
#define ll long long
#define db double

template<typename T>
inline void read(T&x){
    x=0;T k=1;char gc;
    while(!isdigit(c)){if(c=='-')k=-1;gc;}
    while(isdigit(c)){x=x*10+c-'0';gc;}x*=k;
}

ll n,k;

inline ll calc(ll x){
    if(x<2||x>2*n)return 0;
    return min(n,x-1)-max(1ll,x-n)+1;
}

int main(){
    // freopen(".in","r",stdin);
    // freopen(".out","w",stdout);
    r(n),r(k);
    ll ret=0;
    for(int i=2;i<=2*n;++i){
        ret+=calc(i)*calc(i-k);
    }
    printf("%lld\n",ret);
}