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>
using namespace std;
const long long mod = 1000000007;
long long powmod(long long a, long long b) {
  long long res = 1;
  a %= mod;
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
typedef long long mat[33][33];
mat dp[65][33], pre[65][33], suc[65][33];
long long rt[33], ret[33], n;
int k;
vector<int> bt;
void mul(mat a, mat b, mat c) {
  memset(c, 0, sizeof(c));
  for (int z = 0; z < k + 2; z++)
    for (int x = 0; x < k + 2; x++)
      for (int y = 0; y < k + 2; y++)
        c[x][y] = (c[x][y] + a[x][z] * b[z][y]) % mod;
}
void gao(long long n) {
  while (n != 0) bt.push_back(n % k), n /= k;
  int s = 0;
  for (int i = ((int)(bt).size()) - 1; i >= 0; i--) {
    for (int j = 0; j < bt[i]; j++) {
      for (int z = 0; z < k + 2; z++) rt[z] = 0;
      for (int x = 0; x < k + 2; x++)
        for (int y = 0; y < k + 2; y++)
          rt[y] = (rt[y] + ret[x] * dp[i][(s + j) % k][x][y]) % mod;
      for (int z = 0; z < k + 2; z++) ret[z] = rt[z];
    }
    s = (s + bt[i]) % k;
  }
}
int main() {
  scanf("%I64d%d", &n, &k);
  for (int i = 0; i < k; i++) {
    dp[0][i][0][0] = 2;
    dp[0][i][i + 2][0] = mod - 1;
    dp[0][i][0][1] = 1;
    for (int j = 0; j < k; j++)
      if (j != i)
        dp[0][i][j + 2][j + 2] = 1;
      else
        dp[0][i][0][j + 2] = 1;
  }
  for (int a = 0; a < k + 2; a++)
    for (int b = 0; b < k + 2; b++)
      pre[0][0][a][b] = dp[0][0][a][b],
      suc[0][k - 1][a][b] = dp[0][k - 1][a][b];
  for (int i = 1; i < k; i++) mul(pre[0][i - 1], dp[0][i], pre[0][i]);
  for (int i = k - 1 - 1; i >= 0; i--) mul(dp[0][i], suc[0][i + 1], suc[0][i]);
  int t = 1;
  long long c = 1;
  while (c <= n) {
    for (int a = 0; a < k + 2; a++)
      for (int b = 0; b < k + 2; b++) dp[t][0][a][b] = pre[t - 1][k - 1][a][b];
    for (int i = 1; i < k; i++) mul(suc[t - 1][i], pre[t - 1][i - 1], dp[t][i]);
    for (int a = 0; a < k + 2; a++)
      for (int b = 0; b < k + 2; b++)
        pre[t][0][a][b] = dp[t][0][a][b],
        suc[t][k - 1][a][b] = dp[t][k - 1][a][b];
    for (int i = 1; i < k; i++) mul(pre[t][i - 1], dp[t][i], pre[t][i]);
    for (int i = k - 1 - 1; i >= 0; i--)
      mul(dp[t][i], suc[t][i + 1], suc[t][i]);
    if (c > n / k) break;
    c *= k;
    t++;
  }
  ret[0] = 1;
  gao(n);
  printf("%I64d\n", ret[0]);
}