#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int *malloc(int);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ scanf(char *, int *, ...);
int main() {
  int n;
  int k;
  int e = 100;
  scanf("%d %d", &n, &k);
  int *c = malloc(sizeof(int) * n);
  for (int c_i = 0; c_i < n; c_i++) {
    scanf("%d", &c[c_i]);
  }
  int i = 0;
  while (1) {
    int p = (i + k) % n;
    i = p;
    if (c[p] == 1) {
      e -= 3;
    } else {
      e--;
    }
    if (p == 0) {
      break;
    }
  }
  printf("%d", e);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x64,-0x10(%rbp)
lea    0xeb3(%rip),%rdi
lea    -0x8(%rbp),%rsi
lea    -0xc(%rbp),%rdx
mov    $0x0,%al
call   1050 <scanf@plt>
movslq -0x8(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11ad <main+0x7d>
mov    -0x18(%rbp),%rsi
movslq -0x1c(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
lea    0xe6b(%rip),%rdi
mov    $0x0,%al
call   1050 <scanf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1176 <main+0x46>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
add    -0xc(%rbp),%eax
cltd
idivl  -0x8(%rbp)
mov    %edx,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
cmpl   $0x1,(%rax,%rcx,4)
jne    11e7 <main+0xb7>
mov    -0x10(%rbp),%eax
sub    $0x3,%eax
mov    %eax,-0x10(%rbp)
jmp    11f0 <main+0xc0>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    11ff <main+0xcf>
jmp    1204 <main+0xd4>
jmp    11b4 <main+0x84>
mov    -0x10(%rbp),%esi
lea    0xdf5(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xebe(%rip),%rdi
xor    %ebx,%ebx
mov    %rsp,%rsi
lea    0x4(%rsp),%rdx
xor    %eax,%eax
call   1050 <scanf@plt>
mov    (%rsp),%r13d
lea    0x0(,%r13,4),%edi
call   1040 <malloc@plt>
mov    %rax,%r14
test   %r13d,%r13d
jle    119e <main+0x6e>
lea    0xe90(%rip),%r15
mov    %r14,%r12
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1050 <scanf@plt>
add    $0x1,%rbp
movslq (%rsp),%r13
add    $0x4,%r12
cmp    %r13,%rbp
jl     1180 <main+0x50>
mov    $0x64,%esi
mov    0x4(%rsp),%ecx
nopw   0x0(%rax,%rax,1)
add    %ecx,%ebx
mov    %ebx,%eax
cltd
idiv   %r13d
mov    %edx,%ebx
movslq %edx,%rax
xor    %edx,%edx
cmpl   $0x1,(%r14,%rax,4)
setne  %dl
lea    (%rsi,%rdx,2),%esi
add    $0xfffffffd,%esi
test   %eax,%eax
jne    11b0 <main+0x80>
lea    0xe2b(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"%d %d\00"
@.str.1 = [3 x i8] c"%d\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %1
store i32 100, i32* %4
%9 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32* %2, i32* %3)
%10 = load i32, i32* %2
%11 = sext i32 %10 to i64
%12 = mul i64 4, %11
%13 = trunc i64 %12 to i32
%14 = call i32* @malloc(i32 %13)
store i32* %14, i32** %5
store i32 0, i32* %6
br label %15
15: ; preds = %25, %0
%16 = load i32, i32* %6
%17 = load i32, i32* %2
%18 = icmp slt i32 %16, %17
br i1 %18, label %19, label %28
19: ; preds = %15
%20 = load i32*, i32** %5
%21 = load i32, i32* %6
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i32, i32* %20, i64 %22
%24 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %23)
br label %25
25: ; preds = %19
%26 = load i32, i32* %6
%27 = add nsw i32 %26, 1
store i32 %27, i32* %6
br label %15
28: ; preds = %15
store i32 0, i32* %7
br label %29
29: ; preds = %28, %52
%30 = load i32, i32* %7
%31 = load i32, i32* %3
%32 = add nsw i32 %30, %31
%33 = load i32, i32* %2
%34 = srem i32 %32, %33
store i32 %34, i32* %8
%35 = load i32, i32* %8
store i32 %35, i32* %7
%36 = load i32*, i32** %5
%37 = load i32, i32* %8
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i32, i32* %36, i64 %38
%40 = load i32, i32* %39
%41 = icmp eq i32 %40, 1
br i1 %41, label %42, label %45
42: ; preds = %29
%43 = load i32, i32* %4
%44 = sub nsw i32 %43, 3
store i32 %44, i32* %4
br label %48
45: ; preds = %29
%46 = load i32, i32* %4
%47 = add nsw i32 %46, -1
store i32 %47, i32* %4
br label %48
48: ; preds = %45, %42
%49 = load i32, i32* %8
%50 = icmp eq i32 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %48
br label %53
52: ; preds = %48
br label %29
53: ; preds = %51
%54 = load i32, i32* %4
%55 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %54)
ret i32 0
}

LLVM IR (-O2):
@.str = [6 x i8] c"%d %d\00"
@.str.1 = [3 x i8] c"%d\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = bitcast i32* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32* %1, i32* %2)
%6 = load i32, i32* %1
%7 = shl i32 %6, 2
%8 = call i32* @malloc(i32 %7)
%9 = icmp sgt i32 %6, 0
br i1 %9, label %13, label %10
10: ; preds = %13, %0
%11 = phi i32 [ %6, %0 ], [ %18, %13 ]
%12 = load i32, i32* %2
br label %21
13: ; preds = %0, %13
%14 = phi i64 [ %17, %13 ], [ 0, %0 ]
%15 = getelementptr inbounds i32, i32* %8, i64 %14
%16 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %15)
%17 = add nuw nsw i64 %14, 1
%18 = load i32, i32* %1
%19 = sext i32 %18 to i64
%20 = icmp slt i64 %17, %19
br i1 %20, label %13, label %10
21: ; preds = %10, %21
%22 = phi i32 [ %31, %21 ], [ 100, %10 ]
%23 = phi i32 [ %25, %21 ], [ 0, %10 ]
%24 = add nsw i32 %12, %23
%25 = srem i32 %24, %11
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i32, i32* %8, i64 %26
%28 = load i32, i32* %27
%29 = icmp eq i32 %28, 1
%30 = select i1 %29, i32 -3, i32 -1
%31 = add nsw i32 %30, %22
%32 = icmp eq i32 %25, 0
br i1 %32, label %33, label %21
33: ; preds = %21
%34 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %31)
ret i32 0
}