inf = open("seq.in", "r")
ouf = open("seq.out", "w")

n, h = map(int, inf.readline().split())
a = list(map(int, inf.readline().split()))

prefsum = [0] * (n + 1)
for i in range(n):
    prefsum[i + 1] = prefsum[i] + a[i]

needsum = h * (h + 1) // 2

ans = -1
left = h
for i in range(n - 1, -1, -1):
    if a[i] > h:
        left = h
    elif a[i] <= left:
        left -= 1
    else:
        left = a[i] - 1

    if left == 0:
        cnt = needsum + prefsum[i] - prefsum[i + h]
        if ans == -1 or cnt < ans:
            ans = cnt
        
print(ans, file = ouf)

inf.close()
ouf.close()

