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;
const int Imx = 2147483647;
const int mod = 1000000007;
const long long Lbig = 2e18;
inline long long getnum() {
  register long long r = 0;
  register bool ng = 0;
  register char c;
  c = getchar();
  while (c != '-' && (c < '0' || c > '9')) c = getchar();
  if (c == '-') ng = 1, c = getchar();
  while (c >= '0' && c <= '9') r = r * 10 + c - '0', c = getchar();
  if (ng) r = -r;
  return r;
}
template <class T>
inline void putnum(T x) {
  if (x < 0) putchar('-'), x = -x;
  register 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]);
}
inline void putsp() { putchar(' '); }
inline void putendl() { putchar('\n'); }
inline char mygetchar() {
  register char c = getchar();
  while (c == ' ' || c == '\n') c = getchar();
  return c;
}
struct matrix {
  int a[31][31];
  void set0() { memset(a, 0, sizeof(a)); }
  void set1() {
    set0();
    for (int i = 0; i <= 30; i++) a[i][i] = 1;
  }
  matrix() { set0(); }
  matrix operator*(const matrix &t) const {
    matrix ret;
    for (int i = 0; i <= 30; i++) {
      for (int j = 0; j <= 30; j++) {
        if (!a[i][j]) continue;
        for (int k = 0; k <= 30; k++) {
          ret.a[i][k] = (ret.a[i][k] + 1ll * a[i][j] * t.a[j][k]) % mod;
        }
      }
    }
    return ret;
  }
  matrix shift(int x, int n) {
    matrix ret;
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= n; j++) {
        ret.a[i == n ? n : (i + x) % n][j == n ? n : (j + x) % n] = a[i][j];
      }
    }
    return ret;
  }
  void out(int n) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        cout << a[i][j] << " ";
      }
      cout << endl;
    }
  }
};
long long n;
int m, k;
matrix kissme(matrix x, int y) {
  assert(y > 0);
  if (y == 1) return x;
  if (y & 1)
    return kissme(x, y - 1) * x.shift(y - 1, k);
  else {
    matrix t = kissme(x, y >> 1);
    return t * t.shift(y >> 1, k);
  }
}
matrix pw[70];
int a[70];
int main() {
  n = getnum(), k = getnum();
  long long t = n;
  pw[0].set1();
  for (int i = 0; i <= k; i++) pw[0].a[i][0] = 1;
  while (t) {
    a[m] = t % k;
    m++;
    pw[m] = kissme(pw[m - 1], k);
    t /= k;
  }
  matrix ans;
  ans.set1();
  int pre = 0;
  for (int i = m - 1; i >= 0; i--) {
    if (a[i] == 0) continue;
    ans = ans * kissme(pw[i].shift(pre, k), a[i]);
    pre = (pre + a[i]) % k;
  }
  long long ret = 0;
  for (int i = 0; i <= k; i++) ret = (ret + ans.a[k][i]) % mod;
  cout << ret << endl;
  return 0;
}