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 = 1e9 + 7;
long long add(long long a, const long long b) {
  a += b;
  if (a >= Mod) a -= Mod;
  return a;
}
long long sub(long long a, const long long b) {
  a -= b;
  if (a < 0) a += Mod;
  return a;
}
long long mul(const long long a, const long long b) {
  return (long long)a * b % Mod;
}
long long power(long long x, long long cs) {
  long long ras = 1;
  while (cs) {
    if (cs & 1) ras = mul(ras, x);
    x = mul(x, x);
    cs >>= 1;
  }
  return ras;
}
const long long P = 32, S = 70;
long long n, K, num = 0, ans = 0;
long long cf[S];
struct Mat {
  long long v[P][P];
  Mat() {}
  Mat(long long x) {
    memset(v, 0, sizeof(v));
    if (x == 1) {
      for (long long i = 0; i <= K; i++) v[i][i] = 1;
    }
  }
};
Mat f[S][P], pre[P], suf[P];
Mat t;
const Mat operator*(Mat a, Mat b) {
  Mat c = Mat(0);
  long long i, j, k;
  for (i = 0; i <= K; i++) {
    for (j = 0; j <= K; j++) {
      for (k = 0; k <= K; k++) {
        c.v[i][j] = add(c.v[i][j], mul(a.v[i][k], b.v[k][j]));
      }
    }
  }
  return c;
}
int main() {
  long long i, j, x, now;
  cin >> n >> K;
  x = n;
  while (x > 0) {
    num++;
    x /= K;
  }
  cf[0] = 1;
  for (i = 1; i < num; i++) cf[i] = cf[i - 1] * K;
  for (i = 0; i < K; i++) {
    f[0][i] = Mat(1);
    for (j = 0; j <= K; j++) f[0][i].v[i][j] = 1;
  }
  for (i = 1; i < num; i++) {
    pre[0] = f[i - 1][0];
    for (j = 1; j < K; j++) pre[j] = f[i - 1][j] * pre[j - 1];
    suf[K - 1] = f[i - 1][K - 1];
    for (j = K - 2; j >= 0; j--) suf[j] = suf[j + 1] * f[i - 1][j];
    for (j = 0; j < K; j++) {
      f[i][j] = suf[j];
      if (j > 0) f[i][j] = pre[j - 1] * f[i][j];
    }
  }
  t = Mat(0);
  t.v[K][0] = 1;
  now = 0;
  for (i = num - 1; i >= 0; i--) {
    while (n >= cf[i]) {
      t = f[i][now] * t;
      n -= cf[i];
      now = (now + 1) % K;
    }
  }
  ans = 1;
  for (i = 0; i < K; i++) ans = add(ans, t.v[i][0]);
  cout << ans;
  return 0;
}