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 T>
void get_int(T& x) {
  char t = getchar();
  bool neg = false;
  x = 0;
  for (; (t > '9' || t < '0') && t != '-'; t = getchar())
    ;
  if (t == '-') neg = true, t = getchar();
  for (; t <= '9' && t >= '0'; t = getchar()) x = x * 10 + t - '0';
  if (neg) x = -x;
}
template <typename T>
void print_int(T x) {
  if (x < 0) putchar('-'), x = -x;
  short a[20] = {}, sz = 0;
  while (x > 0) a[sz++] = x % 10, x /= 10;
  if (sz == 0) putchar('0');
  for (int i = sz - 1; i >= 0; i--) putchar('0' + a[i]);
}
const int inf = 0x3f3f3f3f;
const long long Linf = 1ll << 61;
const double pi = acos(-1.0);
const int mod = 1e9 + 7;
struct matrix {
  int a[33][33];
  void clear(int x) {
    for (int i = 0; i < 33; i++)
      for (int j = 0; j < 33; j++) a[i][j] = x * (i == j);
  }
  void rev() {
    for (int i = 0; i < 33; i++)
      for (int j = 0; j < 33; j++)
        if (i != j) a[i][j] = (mod - a[i][j]) % mod;
  }
  int* operator[](const int x) { return a[x]; }
};
matrix v[65][33], pre[33], suf[33];
long long n;
int k;
matrix operator*(matrix a, matrix b) {
  matrix ret;
  ret.clear(0);
  for (int i = 0; i < 33; i++)
    for (int j = 0; j < 33; j++)
      for (int k = 0; k < 33; k++)
        ret[i][j] = (ret[i][j] + 1ll * a[i][k] * b[k][j]) % mod;
  return ret;
}
matrix calc(long long x, int st) {
  matrix ret;
  ret.clear(1);
  if (x == 0) return ret;
  long long prod = 1;
  int lg = 0;
  while (prod <= x / k) {
    lg++;
    prod *= k;
  }
  int tmp = x / prod;
  for (int i = 0; i < tmp; i++) ret = ret * v[lg][(st + i) % k];
  ret = ret * calc(x - tmp * prod, (st + tmp) % k);
  return ret;
}
int main() {
  get_int(n), get_int(k);
  int lg = 0;
  long long prod = 1;
  while (prod <= n / k) {
    lg++;
    prod *= k;
  }
  for (int _ = 0; _ < k; _++) {
    for (int i = 0; i <= k; i++)
      for (int j = 0; j <= k; j++)
        if (i == j || i == _)
          v[0][_][i][j] = 1;
        else
          v[0][_][i][j] = 0;
  }
  for (int _ = 1; _ <= lg; _++) {
    pre[0].clear(1);
    for (int i = 0; i < k; i++) pre[i + 1] = pre[i] * v[_ - 1][i];
    suf[k].clear(1);
    for (int i = k - 1; i >= 0; i--) suf[i] = v[_ - 1][i] * suf[i + 1];
    for (int i = 0; i < k; i++) v[_][i] = suf[i] * pre[i];
  }
  matrix tran = calc(n, 0);
  int ans = 0;
  for (int i = 0; i <= k; i++) ans = (ans + tran[i][k]) % mod;
  print_int(ans), puts("");
  return 0;
}