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 long long mod = 1e9 + 7;
void add(int& a, const int& b) {
  a += b;
  if (a >= mod) a -= mod;
  if (a < 0) a += mod;
}
vector<vector<int> > mul(const vector<vector<int> >& a,
                         const vector<vector<int> >& b) {
  vector<vector<int> > m(a.size(), vector<int>(a.size(), 0));
  for (int i = 0; i < a.size(); i++)
    for (int j = 0; j < a.size(); j++)
      for (int k = 0; k < a.size(); k++)
        add(m[i][k], (a[i][j] * 1ll * b[j][k]) % mod);
  return m;
}
void print(const vector<vector<int> >& a) {
  cout << "\n";
  for (int i = 0; i < a.size(); i++) {
    for (int j = 0; j < a[i].size(); j++) cout << a[i][j];
    cout << "\n";
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long n, k;
  cin >> n >> k;
  vector<int> d;
  while (n) {
    d.insert(d.begin(), n % k);
    n /= k;
  }
  int m = d.size();
  vector<vector<vector<vector<int> > > > a(
      m, vector<vector<vector<int> > >(
             k, vector<vector<int> >(k + 1, vector<int>(k + 1, 0))));
  vector<vector<vector<vector<int> > > > pf(
      m, vector<vector<vector<int> > >(
             k, vector<vector<int> >(k + 1, vector<int>(k + 1, 0))));
  vector<vector<vector<vector<int> > > > sf(
      m, vector<vector<vector<int> > >(
             k, vector<vector<int> >(k + 1, vector<int>(k + 1, 0))));
  for (int i = 0; i < m; i++) {
    if (i == 0) {
      for (int x = 0; x < k; x++) {
        for (int d = 0; d <= k; d++) {
          a[i][x][d][d] = 1;
          a[i][x][x][d] = 1;
        }
      }
    } else {
      a[i][0] = sf[i - 1][0];
      for (int x = 1; x < k; x++) a[i][x] = mul(sf[i - 1][x], pf[i - 1][x - 1]);
    }
    pf[i][0] = a[i][0], sf[i][k - 1] = a[i][k - 1];
    for (int d = 1; d < k; d++) pf[i][d] = mul(pf[i][d - 1], a[i][d]);
    for (int d = k - 2; d >= 0; d--) sf[i][d] = mul(a[i][d], sf[i][d + 1]);
  }
  vector<vector<int> > ans(k + 1, vector<int>(k + 1, 0));
  for (int i = 0; i <= k; i++) ans[i][i] = 1;
  int rem = 0;
  for (int i = 0; i < d.size(); i++) {
    for (int j = 0; j < d[i]; j++, rem = (rem + 1) % k)
      ans = mul(ans, a[d.size() - i - 1][rem]);
  }
  int res = 0;
  for (int i = 0; i <= k; i++) add(res, ans[i][k]);
  cout << res << "\n";
  return 0;
}