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.
#include <bits/stdc++.h>
using namespace std;
const int maxN = 3e1 + 5;
const int LOG = 6e1 + 5;
const long long INF = 1e18;
const long long MOD = 1e9 + 7;
int d[LOG];
struct Matrix {
  int n, m, M[maxN][maxN];
  Matrix(int _n = 0, int _m = 0) {
    n = _n;
    m = _m;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) M[i][j] = 0;
    return;
  }
  int* operator[](int i) { return M[i]; }
  Matrix operator*(const Matrix& A) {
    Matrix B(n, A.m);
    for (int i = 0; i < n; i++)
      for (int k = 0; k < m; k++)
        for (int j = 0; j < A.m; j++)
          B[i][j] = (0ll + B[i][j] + 1ll * M[i][k] * A.M[k][j] % MOD) % MOD;
    return B;
  }
  void print() {
    for (int i = 0; i < n; i++, printf("\n"))
      for (int j = 0; j < m; j++) printf("%d ", M[i][j]);
    printf("\n");
    return;
  }
} K[LOG];
int main() {
  time_t START = clock();
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n;
  int k;
  scanf("%lld%d", &n, &k);
  for (int i = 0; i < LOG; i++) K[i] = Matrix(k + 1, k + 1);
  for (int i = 1; i < LOG; i++)
    for (int j = 0; j <= k; j++) K[i][j][j] = 1;
  Matrix S(k + 1, k + 1), T(k + 1, k + 1);
  Matrix R(k + 1, k + 1), t(k + 1, k + 1);
  for (int i = 0; i < k; i++) {
    S[i][(k + i - 1) % k] = 1;
    T[(k + i - 1) % k][i] = 1;
  }
  S[k][k] = T[k][k] = 1;
  for (int i = 0; i <= k; i++) K[0][0][i] = K[0][i][i] = 1;
  long long m = n;
  int cnt = 0;
  for (int i = 1; m >= k; m /= k, i++) {
    d[cnt++] = m % k;
    for (int j = 0; j <= k; j++) K[i][j][j] = 1;
    t = K[i - 1];
    for (int j = 0; j < k; j++) {
      K[i] = t * K[i];
      t = (S * t) * T;
    }
  }
  d[cnt] = m;
  for (int i = 0; i <= k; i++) R[i][i] = 1;
  int sum = 0;
  for (cnt; cnt > -1; cnt--) {
    t = K[cnt];
    for (int i = 0; i < sum; i++) t = (S * t) * T;
    for (int i = 0; i < d[cnt]; i++) {
      R = t * R;
      t = (S * t) * T;
    }
    sum = (sum + d[cnt]) % k;
  }
  int ans = 0;
  for (int i = 0; i <= k; i++) ans = (ans + R[i][k]) % MOD;
  printf("%d\n", ans);
  time_t FINISH = clock();
  cerr << "Execution time: "
       << (long double)(FINISH - START) / CLOCKS_PER_SEC * 1000.0
       << " milliseconds.\n";
  return 0;
}
