#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 *, long);
scalar_t__ scanf(char *, long *, long *);
int main() {
  long int n, k, ans = 0, carry;
  while (scanf("%ld %ld", &n, &k) != EOF) {
    if (k == 1)
      continue;
    ans = n;
    while (n >= k) {
      carry = n / k;
      ans += carry;
      n = carry + (n % k);
    }
    printf("%ld\n", ans);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x20(%rbp)
lea    0xec2(%rip),%rdi
lea    -0x10(%rbp),%rsi
lea    -0x18(%rbp),%rdx
call   1040 <scanf@plt>
mov    0x2e96(%rip),%rcx
cmp    (%rcx),%rax
je     11d2 <main+0xb2>
cmpq   $0x1,-0x18(%rbp)
jne    116b <main+0x4b>
jmp    1137 <main+0x17>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x18(%rbp),%rax
jl     11bd <main+0x9d>
mov    -0x10(%rbp),%rax
cqto
idivq  -0x18(%rbp)
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
cqto
idivq  -0x18(%rbp)
mov    -0x30(%rbp),%rax
add    %rdx,%rax
mov    %rax,-0x10(%rbp)
jmp    1173 <main+0x53>
mov    -0x20(%rbp),%rsi
lea    0xe40(%rip),%rdi
call   1030 <printf@plt>
jmp    1137 <main+0x17>
xor    %eax,%eax
add    $0x30,%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
mov    %rsp,%rsi
lea    0x8(%rsp),%rdx
call   1040 <scanf@plt>
mov    0x2ea0(%rip),%rbx
cmp    (%rbx),%rax
jne    115d <main+0x3d>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xe9c(%rip),%r14
mov    %rsp,%r15
lea    0x8(%rsp),%r13
lea    0xe95(%rip),%r12
jmp    119f <main+0x7f>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rax,(%rsp)
mov    %r12,%rdi
call   1030 <printf@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r13,%rdx
call   1040 <scanf@plt>
cmp    (%rbx),%rax
je     114d <main+0x2d>
mov    0x8(%rsp),%rdi
cmp    $0x1,%rdi
je     118c <main+0x6c>
mov    (%rsp),%rsi
mov    %rsi,%rax
cmp    %rdi,%rsi
jge    11d6 <main+0xb6>
jmp    1184 <main+0x64>
nopl   0x0(%rax,%rax,1)
cqto
idiv   %rdi
mov    %rax,%rcx
mov    %rdx,%rax
add    %rcx,%rsi
add    %rcx,%rax
cmp    %rdi,%rax
jl     1180 <main+0x60>
mov    %rax,%rcx
or     %rdi,%rcx
shr    $0x20,%rcx
jne    11c0 <main+0xa0>
xor    %edx,%edx
div    %edi
mov    %eax,%ecx
mov    %edx,%eax
jmp    11cb <main+0xab>

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

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