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>
const int X = 32;
const long long MOD = 1e9 + 7;
class matrix {
 public:
  int sz;
  long long e[X][X];
  matrix() {}
  matrix(int sz) : sz(sz) {
    for (int i = 0; i < sz; i++) {
      for (int j = 0; j < sz; j++) {
        e[i][j] = 0;
        if (i == j) {
          e[i][j] = 1;
        }
      }
    }
  }
  matrix operator*(const matrix &o) {
    matrix ans(sz);
    for (int i = 0; i < sz; i++) {
      for (int j = 0; j < sz; j++) {
        long long val = 0;
        for (int k = 0; k < sz; k++) {
          val = (val + e[i][k] * o.e[k][j] % MOD) % MOD;
        }
        ans.e[i][j] = val;
      }
    }
    return ans;
  }
};
matrix A[X][X << 1], P[X], S[X];
int bit[X << 1];
long long n;
int k;
void init_matrix(int m) {
  for (int u = 0; u < k; u++) {
    A[u][0].sz = k + 1;
    S[u].sz = k + 1;
    P[u].sz = k + 1;
    for (int i = 0; i <= k; i++) {
      for (int j = 0; j <= k; j++) {
        if (i == j) {
          A[u][0].e[i][j] = 1;
        } else {
          if (u == j) {
            A[u][0].e[i][j] = 1;
          } else {
            A[u][0].e[i][j] = 0;
          }
        }
        if (j == k && i < k) {
          A[u][0].e[i][j] = 0;
        }
      }
    }
  }
  for (int i = 0; i <= k; i++) {
    for (int j = 0; j <= k; j++) {
      S[0].e[i][j] = 0;
      P[k].e[i][j] = 0;
      if (i == j) {
        S[0].e[i][j] = 1;
        P[k].e[i][j] = 1;
      }
    }
  }
  for (int u = 1; u < k; u++) {
    S[u] = S[u - 1] * A[u - 1][0];
  }
  for (int u = k - 1; u >= 0; u--) {
    P[u] = A[u][0] * P[u + 1];
  }
  for (int v = 1; v < m; v++) {
    for (int u = 0; u < k; u++) {
      A[u][v] = P[u] * S[u];
    }
    for (int u = 1; u < k; u++) {
      S[u] = S[u - 1] * A[u - 1][v];
    }
    for (int u = k - 1; u >= 0; u--) {
      P[u] = A[u][v] * P[u + 1];
    }
  }
}
void show(int x) {
  for (int i = 0; i <= k; i++, puts("")) {
    for (int j = 0; j <= k; j++) {
      printf("%lld ", A[x][0].e[i][j]);
    }
  }
}
int main() {
  while (scanf("%lld%d", &n, &k) + 1) {
    int m = 0;
    long long e = n;
    for (; e; e /= k) {
      bit[m++] = e % k;
    }
    init_matrix(m);
    matrix ans(k + 1);
    int off = 0;
    for (int i = m - 1; i >= 0; i--) {
      int x = bit[i];
      for (int j = 0; j < x; j++) {
        ans = ans * A[(j + off) % k][i];
      }
      off = (off + x) % k;
    }
    long long res = 0;
    for (int i = 0; i < ans.sz; i++) {
      res = (res + ans.e[ans.sz - 1][i]) % MOD;
    }
    printf("%lld\n", res);
  }
  return 0;
}