Problem: Assume that sk(n) equals the sum of digits of number n in the k-based notation. For example, s2(5) = s2(1012) = 1 + 0 + 1 = 2, s3(14) = s3(1123) = 1 + 1 + 2 = 4.

The sequence of integers a0, ..., an - 1 is defined as <image>. Your task is to calculate the number of distinct subsequences of sequence a0, ..., an - 1. Calculate the answer modulo 109 + 7.

Sequence a1, ..., ak is called to be a subsequence of sequence b1, ..., bl, if there is a sequence of indices 1 ≤ i1 < ... < ik ≤ l, such that a1 = bi1, ..., ak = bik. In particular, an empty sequence (i.e. the sequence consisting of zero elements) is a subsequence of any sequence.

Input

The first line contains two space-separated numbers n and k (1 ≤ n ≤ 1018, 2 ≤ k ≤ 30).

Output

In a single line print the answer to the problem modulo 109 + 7.

Examples

Input

4 2


Output

11


Input

7 7


Output

128

Note

In the first sample the sequence ai looks as follows: (0, 1, 1, 0). All the possible subsequences are: 

(), (0), (0, 0), (0, 1), (0, 1, 0), (0, 1, 1), (0, 1, 1, 0), (1), (1, 0), (1, 1), (1, 1, 0).

In the second sample the sequence ai looks as follows: (0, 1, 2, 3, 4, 5, 6). The subsequences of this sequence are exactly all increasing sequences formed from numbers from 0 to 6. It is easy to see that there are 27 = 128 such sequences.

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:256000000")
using namespace std;
const int INF = (1 << 29) + 5;
const long long int LLINF = (1ll << 60) + 5;
const int MOD = 1000 * 1000 * 1000 + 7;
const int MAX_K = 31;
const int MAX_DIV = 70;
long long int n, k;
long long int dp[MAX_DIV][MAX_K][MAX_K];
long long int calc[MAX_K][MAX_K][MAX_K];
long long int mat[MAX_K][MAX_K];
long long int member[MAX_K][MAX_K];
long long int buff[MAX_K][MAX_K];
long long int powers[MAX_DIV];
bool used[MAX_DIV];
long long int mult(long long int a, long long int b) {
  if (a == 0 || b == 0) {
    return 0;
  } else if (a > LLINF / b) {
    return LLINF;
  } else {
    return a * b;
  }
}
void mult_mat(long long int a[MAX_K][MAX_K], long long int b[MAX_K][MAX_K],
              long long int out[MAX_K][MAX_K]) {
  for (int i = 0; i < MAX_K; ++i) {
    for (int j = 0; j < MAX_K; ++j) {
      buff[i][j] = 0;
      for (int t = 0; t < MAX_K; ++t) {
        buff[i][j] = (buff[i][j] + a[i][t] * b[t][j]) % MOD;
      }
    }
  }
  for (int i = 0; i < MAX_K; ++i) {
    for (int j = 0; j < MAX_K; ++j) {
      out[i][j] = buff[i][j];
    }
  }
}
void get_answer(long long int start, long long int now,
                long long int mat_now[MAX_K][MAX_K]) {
  if (mult(start, powers[now]) < n) {
    mult_mat(mat_now, calc[start % k], mat_now);
    if (now == 0) {
      mult_mat(mat_now, mat, mat_now);
    } else {
      auto at_now =
          ((mult(start, powers[now]) + powers[now] <= n) ? dp[now] : mat_now);
      if (mult(start, powers[now]) + powers[now] <= n && !used[now]) {
        for (int i = 0; i < MAX_K; ++i) {
          at_now[i][i] = 1;
        }
      }
      if (mult(start, powers[now]) + powers[now] > n || !used[now]) {
        for (int i = 0; i < k; ++i) {
          get_answer(start * k + i, now - 1, at_now);
        }
      }
      if (mult(start, powers[now]) + powers[now] <= n) {
        used[now] = true;
        mult_mat(mat_now, at_now, mat_now);
      }
    }
    mult_mat(mat_now, calc[(k - start % k) % k], mat_now);
  }
}
int main() {
  scanf("%I64d %I64d", &n, &k);
  memset(mat, 0, sizeof(mat));
  for (int i = 0; i <= k; ++i) {
    mat[i][i] = 1;
    mat[i][0] = 1;
  }
  memset(calc, 0, sizeof(calc));
  for (int i = 0; i < k; ++i) {
    for (int j = 0; j < k; ++j) {
      calc[i][j][(i + j) % k] = 1;
    }
    for (int j = k; j < MAX_K; ++j) {
      calc[i][j][j] = 1;
    }
  }
  memset(member, 0, sizeof(member));
  member[0][k] = 1;
  powers[0] = 1;
  for (int i = 1; i < MAX_DIV; ++i) {
    powers[i] = mult(powers[i - 1], k);
  }
  memset(used, 0, sizeof(used));
  memset(dp, 0, sizeof(dp));
  get_answer(0, MAX_DIV - 1, member);
  long long int answer = 1;
  for (int i = 0; i < k; ++i) {
    answer = (answer + member[0][i]) % MOD;
  }
  printf("%I64d", answer);
  fclose(stdin);
  fclose(stdout);
  return 0;
}