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;
long long N, K;
const long long MOD = 1000000007;
long long Power[65], Res[35][35];
long long S[65][35][35][35], A[65][35][35][35], P[65][35][35][35], Aux[35][35];
pair<long long, long long> X[105];
void copy(long long X[][35], long long Y[][35], long long n) {
  for (long long i = 0; i < n; i++)
    for (long long j = 0; j < n; j++) X[i][j] = Y[i][j];
}
inline void Add(long long& x, long long y) {
  x += y;
  if (x >= MOD) x -= MOD;
}
void mul(long long X[][35], long long Y[][35], long long Z[][35], long long n,
         long long m, long long p) {
  for (long long i = 0; i < n; i++)
    for (long long j = 0; j < p; j++) {
      Z[i][j] = 0;
      for (long long k = 0; k < m; k++) {
        Add(Z[i][j], (1LL * X[i][k] * Y[k][j]) % MOD);
      }
    }
}
void precalcA() {
  for (long long x = 0; x < K; x++) {
    for (long long i = 0; i < K + 1; i++) A[0][x][i][i] = 1;
    for (long long j = 0; j < K + 1; j++) A[0][x][j][x] = 1;
  }
  copy(P[0][0], A[0][0], K + 1);
  copy(S[0][K - 1], A[0][K - 1], K + 1);
  for (long long x = 1; x < K; x++) {
    mul(P[0][x - 1], A[0][x], P[0][x], K + 1, K + 1, K + 1);
  }
  for (long long x = K - 2; x >= 0; x--) {
    mul(A[0][x], S[0][x + 1], S[0][x], K + 1, K + 1, K + 1);
  }
  long long cnt = 1;
  for (long long m = K; m <= N; m *= K, cnt++) {
    for (long long x = 0; x < K; x++) {
      if (x > 0) {
        copy(A[cnt][x], S[cnt - 1][x], K + 1);
        mul(A[cnt][x], P[cnt - 1][x - 1], Aux, K + 1, K + 1, K + 1);
        copy(A[cnt][x], Aux, K + 1);
      } else
        copy(A[cnt][x], S[cnt - 1][x], K + 1);
    }
    copy(P[cnt][0], A[cnt][0], K + 1);
    copy(S[cnt][K - 1], A[cnt][K - 1], K + 1);
    for (long long x = 1; x < K; x++) {
      mul(P[cnt][x - 1], A[cnt][x], P[cnt][x], K + 1, K + 1, K + 1);
    }
    for (long long x = K - 2; x >= 0; x--) {
      mul(A[cnt][x], S[cnt][x + 1], S[cnt][x], K + 1, K + 1, K + 1);
    }
    if (m > N / K) break;
  }
}
int main() {
  cin >> N >> K;
  Power[0] = 1;
  int i = 1;
  while (Power[i - 1] <= N / K) {
    Power[i] = Power[i - 1] * K;
    ++i;
  }
  int ind = i;
  --ind;
  precalcA();
  long long init = N, last = -1, count = -1, ind2 = 0;
  Res[0][K] = 1;
  for (int i = ind; i >= 0; i--) {
    while (Power[i] <= init) {
      mul(Res, A[i][ind2], Aux, K + 1, K + 1, K + 1);
      copy(Res, Aux, K + 1);
      ind2++;
      if (ind2 == K) ind2 = 0;
      init -= Power[i];
    }
  }
  long long ans = 0;
  for (int i = 0; i <= K; i++) Add(ans, Res[0][i]);
  cout << ans << "\n";
  return 0;
}