#include<iostream>
#include<deque>
using namespace std;
int n, k;
deque<int> belt; //컨베이어벨트 내구도
deque<bool> check; //로봇 체크
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
for (int i = 0; i < 2 * n; i++) {
int x;
cin >> x;
belt.push_back(x);
check.push_back(false);
}
int tot = 0;
while (1) {
int cnt = 0;
for (int i = 0; i < 2 * n; i++) {
if (belt[i] == 0)cnt++;
}
if (cnt >= k) {
cout << tot;
return 0;
}
//컨베이어벨트 회전
tot++;
belt.push_front(belt.back());
belt.pop_back();
//로봇도 회전
check.push_front(check.back());
check.pop_back();
//n-1번째에 로봇이 있으면 내려주기
if (check[n - 1] == true) {
check[n - 1] = false;
}
//로봇 이동
for (int i = n - 2; i >= 0; i--) {
if (check[i] && !check[i + 1] && belt[i + 1] > 0) {
check[i] = false;
belt[i+1] -= 1;
if (i == n - 2)continue;
check[i + 1] = true;
}
}
//로봇 0인덱스에 올리기
if (!check[0] && belt[0] > 0) {
check[0] = true;
belt[0]--;
}
}
}