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;
template <typename T1, typename T2>
void chkmin(T1 &x, T2 y) {
  if (x > y) x = y;
}
template <typename T1, typename T2>
void chkmax(T1 &x, T2 y) {
  if (x < y) x = y;
}
inline void debug(int x) { fprintf(stderr, "ycx has aked ioi %d times\n", x); }
namespace fastio {
char rbuf[1 << 23], *p1 = rbuf, *p2 = rbuf, wbuf[1 << 23], *p3 = wbuf;
inline char getc() {
  return p1 == p2 &&
                 (p2 = (p1 = rbuf) + fread(rbuf, 1, 1 << 23, stdin), p1 == p2)
             ? -1
             : *p1++;
}
inline void putc(char x) { (*p3++ = x); }
template <typename T>
void read(T &x) {
  x = 0;
  char c = getchar();
  T neg = 0;
  while (!isdigit(c)) neg |= !(c ^ '-'), c = getchar();
  while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
  if (neg) x = (~x) + 1;
}
template <typename T>
void recursive_print(T x) {
  if (!x) return;
  recursive_print(x / 10);
  putc(x % 10 ^ 48);
}
template <typename T>
void print(T x) {
  if (!x) putc('0');
  if (x < 0) putc('-'), x = ~x + 1;
  recursive_print(x);
}
void print_final() { fwrite(wbuf, 1, p3 - wbuf, stdout); }
}  // namespace fastio
const int LOG = 60;
const int MAXM = 30;
const int MOD = 1e9 + 7;
long long n;
int m, d[LOG + 2], dc = -1;
struct mat {
  int a[MAXM + 2][MAXM + 2];
  mat() { memset(a, 0, sizeof(a)); }
  mat operator*(const mat &rhs) {
    mat ret;
    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[i][k] * rhs.a[k][j]) % MOD;
    return ret;
  }
} ans, mul, a[LOG + 2][MAXM + 2], suf[LOG + 2][MAXM + 2],
    pre[LOG + 2][MAXM + 2];
int main() {
  scanf("%lld%d", &n, &m);
  while (n) {
    d[++dc] = n % m;
    n /= m;
  }
  for (int i = 0; i <= dc; i++) {
    if (!i) {
      for (int j = 0; j < m; j++) {
        a[i][j].a[m][m] = 1;
        for (int k = 0; k < m; k++)
          if (k ^ j) a[i][j].a[k][k] = 1;
        for (int k = 0; k <= m; k++) a[i][j].a[j][k] = 1;
      }
    } else {
      for (int j = 0; j < m; j++) {
        if (!j)
          a[i][j] = suf[i - 1][0];
        else
          a[i][j] = suf[i - 1][j] * pre[i - 1][j - 1];
      }
    }
    pre[i][0] = a[i][0];
    for (int j = 1; j < m; j++) pre[i][j] = pre[i][j - 1] * a[i][j];
    suf[i][m - 1] = a[i][m - 1];
    for (int j = m - 2; ~j; j--) suf[i][j] = a[i][j] * suf[i][j + 1];
  }
  int sum = 0;
  ans.a[m][0] = 1;
  for (int i = 0; i <= m; i++) mul.a[i][i] = 1;
  for (int i = dc; ~i; i--) {
    while (d[i]) {
      d[i]--;
      mul = mul * a[i][sum];
      sum++;
      if (sum == m) sum = 0;
    }
  }
  ans = mul * ans;
  int ret = 0;
  for (int i = 0; i <= m; i++) ret = (ret + ans.a[i][0]) % MOD;
  printf("%d\n", ret % MOD);
  return 0;
}