Manao is taking part in a quiz. The quiz consists of n consecutive questions. A correct answer gives one point to the player. The game also has a counter of consecutive correct answers. When the player answers a question correctly, the number on this counter increases by 1. If the player answers a question incorrectly, the counter is reset, that is, the number on it reduces to 0. If after an answer the counter reaches the number k, then it is reset, and the player's score is doubled. Note that in this case, first 1 point is added to the player's score, and then the total score is doubled. At the beginning of the game, both the player's score and the counter of consecutive correct answers are set to zero.

Manao remembers that he has answered exactly m questions correctly. But he does not remember the order in which the questions came. He's trying to figure out what his minimum score may be. Help him and compute the remainder of the corresponding number after division by 1000000009 (10^9 + 9).


-----Input-----

The single line contains three space-separated integers n, m and k (2 ≤ k ≤ n ≤ 10^9; 0 ≤ m ≤ n).


-----Output-----

Print a single integer — the remainder from division of Manao's minimum possible score in the quiz by 1000000009 (10^9 + 9).


-----Examples-----
Input
5 3 2

Output
3

Input
5 4 2

Output
6



-----Note-----

Sample 1. Manao answered 3 questions out of 5, and his score would double for each two consecutive correct answers. If Manao had answered the first, third and fifth questions, he would have scored as much as 3 points.

Sample 2. Now Manao answered 4 questions. The minimum possible score is obtained when the only wrong answer is to the question 4.

Also note that you are asked to minimize the score and not the remainder of the score modulo 1000000009. For example, if Manao could obtain either 2000000000 or 2000000020 points, the answer is 2000000000 mod 1000000009, even though 2000000020 mod 1000000009 is a smaller number.

Difficulty: interview

Starter code:

Solutions:
MOD = 1000000009

n,m,k = [int(x) for x in input().split()]

num0 = n-m
num1fin = num0*(k-1)
if num1fin >= m:
    print(m)
else:
    num1open = m-num1fin
    sets = num1open//k
    rem = num1open%k
    print(((pow(2,sets,MOD)-1)*2*k+rem+num1fin)%MOD)


n, m, k = map(int, input().split())
mod = 1000000000 + 9
x = m - (n // k * (k-1) + (n%k))
if (x <= 0):
	print (m % mod)
else:
	print(((m-x) + ((pow(2, x + 1, mod) + 2 * mod)-2)*k - x*(k-1))%mod)

n, corecte, k = list(map(int, input().split()))
incorecte = n - corecte
mod = 10**9 + 9


corecte_consecutive = max(0, n - incorecte * k)
dublari = corecte_consecutive // k
corecte_ramase = corecte - corecte_consecutive

def power(b, exp):
    if exp == 0: return 1

    half = power(b, exp//2)
    if exp%2 == 0: return (half*half) % mod
    return (half*half*b) % mod

score = (power(2, dublari+1) - 2) * k + corecte_ramase + corecte_consecutive % k

print(score % mod)


def chk(x):
    d = (m - x) // (k - 1) * k
    if (m - x) % (k - 1):
        d += 1 + (m - x) % (k - 1)
    if d <= n - x:
        return True
    else:
        return False

def calc(e):
    if e == 1:
        return 2
    if e & 1:
        d = 2
    else:
        d = 1
    f = calc(e >> 1)
    d = d * f % D * f % D
    return d

n, m, k = list(map(int, input().split()))
D = 1000000009

l = 0
h = n

while l < h:
    mid = l + h >>1
    if chk(mid):
        h = mid
    else:
        l = mid + 1

h = calc(l // k + 1) - 2 
if h < 0:
    h += D

print((k * h % D + m - l // k * k) % D)


MOD = 1000000009
n, m, k = list(map(int, input().split()))
x = m - (n // k * (k - 1) + n % k)

if x <= 0:
    print(m)
else:
    print(((m - x) + (pow(2, x + 1, MOD) - 2) * k - x * (k - 1)) % MOD)


MOD=1000000009;
def solve(a,n):
    ans=1;
    p=a;
    while(n>0):
        if(n%2==1): ans*=p;
        n//=2;
        ans%=MOD;
        p=(p*p)%MOD;
    return ans;
n,m,k=map(int,input().split());
p=n-m+1;
if((k-1)*p>=m):
    print(m);
else:
    v=m-(p-1)*(k-1);
    ans=(p-1)*(k-1);
    t=v//k;
    ans=(ans+k*(solve(2,t)-1)*2%MOD+MOD+v%k)%MOD;
    print(ans);

MOD = 10 ** 9 + 9
dp = {0:1, 1:2}

def two_pow (n):
  if n not in dp: 
    dp[n] = (two_pow(n // 2) * two_pow(n // 2)
             * (2 if n%2 == 1 else 1) % MOD)
  return dp[n]

n,m,k = list(map (int, input().split()));
fit = n - n//k
if m <= fit:
  print (m)
else:
  a = m - fit
  rem = m - k*a
  print(((two_pow(a+1)-2)*k + rem) % MOD)


n, m, k = map(int, input().split())

d = max(0, n // k - (n - m))

M = 1000000009

if d > 0:
  res = ((2 * k * (pow(2, d, M) - 1)) % M + m - k * d) % M
else:
  res = m

print(res)

d = 1000000009
n, m, k = map(int, input().split())
if (n - m) * k > n: print(m)
else:
    t = n // k - n + m + 1
    print(((pow(2, t, d) - 1 - t) * k + m) % d)

3

def modulo_power(modulo, power, base):
    if power == 0:
        return 1
    if power == 1:
        return base

    remainder = power % 2
    power -= remainder

    half = modulo_power(modulo, power/2, base)

    return (half * half * modulo_power(modulo, remainder, base)) % modulo

data = [int(token) for token in input().split()]
num_questions, num_correct, doubler = data

num_wrong = num_questions - num_correct
tail = num_wrong * (doubler - 1)
head = num_correct - tail

if head < 0:
    print(num_correct)
    return

num_doublings = head // doubler
remainder = head - num_doublings * doubler
modulo = 10**9 + 9
result = ((2 * doubler * (modulo_power(modulo, num_doublings, 2) - 1)) +
            remainder + tail) % modulo

print(result)



d = 1000000009
n, m, k = map(int, input().split())
if (n - m) * k > n:
    print(m)
else:
    t = n // k - n + m + 1
    print(((pow(2, t, d) - 1 - t) * k + m) % d)

MOD = 1000000009

n,m,k = [int(x) for x in input().split()]

num0 = n-m
num1fin = num0*(k-1)
if num1fin >= m:
    print(m)
else:
    num1open = m-num1fin
    sets = num1open//k
    rem = num1open%k
    print(((pow(2,sets,MOD)-1)*2*k+rem+num1fin)%MOD)

n, m, k = list(map(int, input().split()))

if (n - m) >= n//k:
	print (m)
else:
	longest_correct_streak = n - k*(n - m)
	p = longest_correct_streak//k
	print((k*(pow(2, p+1, 1000000009) - 2) + (longest_correct_streak % k) + (n - m)*(k - 1)) % 1000000009)


MOD = 1000000009
def p2(e):
    if e == 0:
        return 1
    elif e % 2 == 1:
        return 2 * p2(e - 1) % MOD
    else:
        return p2(e // 2) ** 2 % MOD

n, m, k = map(int, input().split())
x = (m - min(n - m, m // (k - 1)) * (k - 1)) // k
print((2 * k * (p2(x) - 1) + m - x * k) % MOD)

MOD  =1000000009
n,m,k=map(int,input().split())
x=max(0,m-(n-n%k)//k*(k-1)-n%k)
res=((((pow(2,x+1,MOD))-2)%MOD)*k)%MOD
z=(m-x*k)%MOD
res=(res+z)%MOD
print (res)


 #COPIED

MOD = 1000000009

n,m,k = [int(x) for x in input().split()]

num0 = n-m
num1fin = num0*(k-1)
if num1fin >= m:
    print(m)
else:
    num1open = m-num1fin
    sets = num1open//k
    rem = num1open%k
    print(((pow(2,sets,MOD)-1)*2*k+rem+num1fin)%MOD)

MOD  =1000000009
n,m,k=map(int,input().split())
x=max(0,m-(n-n%k)//k*(k-1)-n%k)
res=((((pow(2,x+1,MOD))-2)%MOD)*k)%MOD
z=(m-x*k)%MOD
res=(res+z)%MOD
print (res)

# Allah is the most gracious and the Most Marchiful
num,m,k=list(map(int,input().split()))
na=num-m
if((na+1)*(k-1)>=m):
    print(m)
else:
    sa=na*(k-1)
    nsa=m-sa
    #print(nsa)
    M=(10**9)+9
    ans=(pow(2,(nsa//k)+1,M)-2)*k
    ans+=nsa%k
    ans+=sa
    print((ans+0)%1000000009)















n,m,k=map(int,input().split())
if n-n//k>=m:print(m);return
mod=int(1e9+9)
x=n//k+m-n
r=pow(2,x+1,mod)-2
a=r*k+m-x*k
print((a%mod+mod)%mod)

MOD = 1000000009

def log_mult(a, p):
  if p == 0:
    return 1
  else:
    z = log_mult(a, p//2)
    z = (z*z) % MOD
    if (p % 2) == 1:
      return (a*z) % MOD
    else:
      return z
      
n, m, k = list(map(int, input().split()))
c = (n//k) * (k-1) + n % k 
if c >= m:
  print(m)
else:
  d = m - c
  res = log_mult(2, d+1) - 2
  res = (res * k) % MOD
  print((res + m-d*k) % MOD)


MOD = 1000000009

def log_mult(a, p):
  if p == 0:
    return 1
  else:
    z = log_mult(a, p//2)
    z = (z*z) % MOD
    if (p % 2) == 1:
      return (a*z) % MOD
    else:
      return z
      
def log_iterative(a, p):
  res = 1
  while p > 0:
    if p % 2 == 1:
      res = (res*a) % MOD
    a = (a*a) % MOD
    p //= 2
  return res
  
n, m, k = list(map(int, input().split()))
c = (n//k) * (k-1) + n % k 
if c >= m:
  print(m)
else:
  d = m - c
  res = log_iterative(2, d+1) - 2
  res = (res * k) % MOD
  print((res + m-d*k) % MOD)


n,m,k=map(int,input().split())
if n-n//k>=m:print(m);return
mod=int(1e9+9)
x=n//k+m-n
r=pow(2,x+1,mod)-2
a=r*k+m-x*k
print((a%mod+mod)%mod)

n,m,k=map(int,input().split())
MOD=1000000009
x=m-(n//k*(k-1)+(n%k))
if (x<=0):print(m%MOD);return
print(((m-x)+((pow(2,x+1, MOD)+2*MOD)-2)*k-x*(k-1))%MOD)

b = 10**9 + 9
def f(q): 
    x = q//1000
    y = q%1000
    num = 2**1000 % b
    res = 1
    for i in range(x):
        res = (res * num) % b
    res = (res * 2**y) %b
    return res

def F(n,m,k):
    r = n%k
    if m <= n//k * (k-1) + r :
        print(m%b) 
    else:
        q = m - (n//k * (k-1) + r)
        print((m + (f(q+1)-q-2)*k)%b)
        
n,m,k = [int(x) for x in input().split(' ')]
F(n,m,k)


n,m,k=map(int,input().split())
MOD=1000000009
x=m-(n//k*(k-1)+(n%k))
if (x<=0):print(m%MOD);return
print(((m-x)+((pow(2,x+1, MOD)+2*MOD)-2)*k-x*(k-1))%MOD)