import sys
from collections import deque
sys.stdin = open("input.txt")
def rotate():
x = front_dq.pop()
y = back_dq.popleft()
front_dq.appendleft(y)
back_dq.append(x)
if front_dq[n - 1][1] == 1:
front_dq[n - 1][1] = 0
def move():
for i in range(n - 1, -1, -1):
if front_dq[i][1] == 1:
if front_dq[i + 1][0] > 0 and front_dq[i + 1][1] == 0:
front_dq[i][1] = 0
front_dq[i + 1][0] -= 1
if i + 1 != n - 1:
front_dq[i + 1][1] = 1
if front_dq[i + 1][0] == 0:
global cnt
cnt += 1
def add():
if front_dq[0][0] > 0:
front_dq[0][0] -= 1
front_dq[0][1] = 1
if front_dq[0][0] == 0:
global cnt
cnt += 1
n, k = map(int, sys.stdin.readline().split())
safe_list = list(map(int, sys.stdin.readline().split()))
front_dq = deque()
back_dq = deque()
for i in range(2 * n):
if i < n:
front_dq.append([safe_list[i], 0])
else:
back_dq.appendleft([safe_list[i], 0])
idx = 0
cnt = 0
while cnt < k:
rotate()
move()
add()
idx += 1
print(idx)