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 mod = 1000000007;
using namespace std;
long long n;
int m, D;
struct mat {
  int a[31][31];
  void clear() { memset(a, 0, sizeof a); }
  friend mat operator*(mat A, mat B) {
    mat ret;
    ret.clear();
    for (int i = 0; i <= m; ++i)
      for (int j = 0; j <= m; ++j)
        for (int k = 0; k <= m; ++k)
          ret.a[i][j] = (ret.a[i][j] + 1ll * A.a[i][k] * B.a[k][j] % mod) % mod;
    return ret;
  }
};
long long o[65];
int root, cur, ans, son[3005][35];
mat t[3005], F[35], R[35], C[35], I, A;
mat query(int u, int d, long long k) {
  if (o[d] == k) return t[u];
  mat ret = I;
  for (int j = 0; j < m; ++j) {
    if (k > o[d - 1]) {
      ret = ret * t[son[u][j]];
      k -= o[d - 1];
    } else {
      ret = ret * query(son[u][j], d - 1, k);
      break;
    }
  }
  return ret;
}
int main() {
  scanf("%I64d%d", &n, &m);
  for (int i = 0; i <= m; ++i) I.a[i][i] = 1;
  for (int k = 0; k < m; ++k) {
    for (int i = 0; i <= m; ++i) C[k].a[i][i] = 1;
    for (int i = 0; i <= m; ++i) C[k].a[i][k] = 1;
  }
  o[0] = 1;
  o[1] = m;
  D = 1;
  root = m + 1;
  t[root] = I;
  for (int i = 1; i <= m; ++i) {
    t[i] = C[i - 1];
    t[root] = t[root] * t[i];
    son[root][i - 1] = i;
  }
  while (o[D] != -1 && o[D] < n) {
    if (o[D] <= n / m)
      o[++D] = o[D - 1] * m;
    else
      o[++D] = -1;
    F[0] = t[son[root][0]];
    for (int i = 1; i < m; ++i) F[i] = F[i - 1] * t[son[root][i]];
    R[m - 1] = t[son[root][m - 1]];
    for (int i = m - 2; i >= 0; --i) R[i] = t[son[root][i]] * R[i + 1];
    cur = root;
    root += m;
    t[root] = t[cur];
    son[root][0] = cur;
    for (int i = 1; i < m; ++i) {
      son[root][i] = cur + i;
      for (int j = i; j < m; ++j) son[cur + i][j - i] = son[cur][j];
      for (int j = 0; j < i; ++j) son[cur + i][j - i + m] = son[cur][j];
      t[cur + i] = R[i] * F[i - 1];
      t[root] = t[root] * t[cur + i];
    }
  }
  A = query(root, D, n);
  ans = 0;
  for (int i = 0; i <= m; ++i) ans = (ans + A.a[m][i]) % mod;
  printf("%d\n", ans);
  return 0;
}