#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 */
scalar_t__ EOF;
int /*<<< orphan*/ printf(char *, int);
scalar_t__ scanf(char *, int *, int *);
int main() {
  int n, k, b;
  int total;
  while (scanf("%d %d\n", &n, &k) != EOF) {
    total = 0;
    b = 0;
    while (n > 0) {
      total += n;
      b += n;
      n = b / k;
      b = b % k;
    }
    printf("%d\n", total);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeca(%rip),%rdi
lea    -0x8(%rbp),%rsi
lea    -0xc(%rbp),%rdx
call   1040 <scanf@plt>
mov    0x2e9e(%rip),%rcx
cmp    (%rcx),%rax
je     11aa <main+0x8a>
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x10(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    1196 <main+0x76>
mov    -0x8(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%eax
add    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cltd
idivl  -0xc(%rbp)
mov    %eax,-0x8(%rbp)
mov    -0x10(%rbp),%eax
cltd
idivl  -0xc(%rbp)
mov    %edx,-0x10(%rbp)
jmp    1161 <main+0x41>
mov    -0x14(%rbp),%esi
lea    0xe63(%rip),%rdi
call   1030 <printf@plt>
jmp    112f <main+0xf>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
lea    0xecc(%rip),%rdi
lea    0x8(%rsp),%rsi
lea    0xc(%rsp),%rdx
call   1040 <scanf@plt>
mov    0x2e9e(%rip),%r13
cmp    0x0(%r13),%rax
jne    1160 <main+0x40>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xe9c(%rip),%r14
lea    0xe92(%rip),%r15
lea    0x8(%rsp),%r12
lea    0xc(%rsp),%rbx
jmp    119c <main+0x7c>
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1030 <printf@plt>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1040 <scanf@plt>
cmp    0x0(%r13),%rax
je     1150 <main+0x30>
mov    0x8(%rsp),%ecx
xor    %esi,%esi
test   %ecx,%ecx
jle    1180 <main+0x60>
mov    0xc(%rsp),%edi
xor    %edx,%edx
nopl   0x0(%rax)
add    %ecx,%edx
mov    %edx,%eax
cltd
idiv   %edi
add    %ecx,%esi
mov    %eax,%ecx
test   %eax,%eax
jg     11b0 <main+0x90>
mov    %ecx,0x8(%rsp)
jmp    1180 <main+0x60>

LLVM IR (-O0):
@.str = [7 x i8] c"%d %d\0A\00"
@EOF = global i64 0
@.str.1 = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
br label %6
6: ; preds = %27, %0
%7 = call i64 @scanf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32* %2, i32* %3)
%8 = load i64, i64* @EOF
%9 = icmp ne i64 %7, %8
br i1 %9, label %10, label %30
10: ; preds = %6
store i32 0, i32* %5
store i32 0, i32* %4
br label %11
11: ; preds = %14, %10
%12 = load i32, i32* %2
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %27
14: ; preds = %11
%15 = load i32, i32* %2
%16 = load i32, i32* %5
%17 = add nsw i32 %16, %15
store i32 %17, i32* %5
%18 = load i32, i32* %2
%19 = load i32, i32* %4
%20 = add nsw i32 %19, %18
store i32 %20, i32* %4
%21 = load i32, i32* %4
%22 = load i32, i32* %3
%23 = sdiv i32 %21, %22
store i32 %23, i32* %2
%24 = load i32, i32* %4
%25 = load i32, i32* %3
%26 = srem i32 %24, %25
store i32 %26, i32* %4
br label %11
27: ; preds = %11
%28 = load i32, i32* %5
%29 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %28)
br label %6
30: ; preds = %6
ret i32 0
}

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