/*
ID: kadir_k2
PROG: test
LANG: C++
*/
#include <bits/stdc++.h>
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>
#include <deque>
#include <vector>
#include <algorithm>
#include <time.h>
#include <map>
#include <set>
#include <string>
using namespace std;
#define IF ifstream cin("input.txt")
#define OF ofstream cout("output.txt")
#define rep(st,i,n) for(i = st; n > i ; i ++ )
#define pb push_back
#define fi first
#define se second
#define ins insert
#define make make_pair
#define NAME "seq"
typedef long long ll;
const double PI = 3.1415926535897932384626433832795;
int a[ 200009 ];
long long s[ 200009 ];
long long P ;
long long all ;
const long long inf = (ll)1e18;
int main(){
freopen("input.txt" , "r" ,stdin);
freopen("output.txt" , "w" ,stdout);
int i , n , h;
cin >> n >> h;
for(i = 0; n > i ; i++){
cin >> a[i];
s[i] = a[i];
if(i)s[i] += s[i-1];
}
all = ( h * 1LL * (h + 1) ) / (2*1LL);
int T = h;
long long ans = inf;
for(i = n-1; i>=-1 ; i--)
{
if(!T){
if(i!=-1)ans = min(ans , all - (s[i+h] - s[i]) );
else ans = min(ans , all - (s[i+h]) );
T = 1;
}
if(i ==-1);
else if( a[i] <= T )T -- ;
else if( a[i] <= h)T = a[i] - 1;
else T = h;
}
if(ans == inf)cout << -1 << endl;
else cout << ans << endl;
return 0;
}
LyoKSUQ6IGthZGlyX2syClBST0c6IHRlc3QKTEFORzogQysrCiovCgojaW5jbHVkZSA8Yml0cy9zdGRjKysuaD4KCiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUgPGZzdHJlYW0+CiNpbmNsdWRlIDxjbWF0aD4KI2luY2x1ZGUgPGlvbWFuaXA+CiNpbmNsdWRlIDxkZXF1ZT4KI2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPHRpbWUuaD4KI2luY2x1ZGUgPG1hcD4KI2luY2x1ZGUgPHNldD4KI2luY2x1ZGUgPHN0cmluZz4KCnVzaW5nIG5hbWVzcGFjZS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Aoc1tpK2hdIC0gc1tpXSkgKTsKICAgICAgICAgICAgZWxzZSBhbnMgPSBtaW4oYW5zICwgYWxsIC0gKHNbaStoXSkgKTsKICAgICAgICAgICAgVCA9IDE7CiAgICAgICAgfQogICAgICAgIGlmKGkgPT0tMSk7CiAgICAgICAgZWxzZSBpZiggYVtpXSA8PSBUIClUIC0tIDsKICAgICAgICBlbHNlIGlmKCBhW2ldIDw9IGgpVCA9IGFbaV0gLSAxOwogICAgICAgIGVsc2UgVCA9IGg7CiAgICB9CiAgICBpZihhbnMgPT0gaW5mKWNvdXQgPDwgLTEgPDwgZW5kbDsKICAgIGVsc2UgY291dCA8PCBhbnMgPDwgZW5kbDsKICAgIHJldHVybiAwOwp9