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>
inline void mine(T1 &x, const T2 &y) {
  if (y < x) x = y;
}
template <typename T1, typename T2>
inline void maxe(T1 &x, const T2 &y) {
  if (x < y) x = y;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &b) {
  for (auto &k : b) os << k << ' ';
  return os;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &b) {
  for (auto &k : b) is >> k;
  return is;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const long long MOD = 1e9 + 7;
const long long MOD2 = 1ll * MOD * MOD;
long long add(long long a, long long b) {
  a += b;
  if (a >= MOD) return a - MOD;
  return a;
}
long long add2(long long a, long long b) {
  a += b;
  if (a >= MOD2) return a - MOD2;
  return a;
}
long long mult(long long a, long long b) { return 1ll * a * b % MOD; }
long long n, k;
struct mat {
  vector<vector<long long>> a;
  mat() { a = vector<vector<long long>>(k + 1, vector<long long>(k + 1, 0)); }
  mat operator*(const mat &M) const {
    mat res;
    for (long long i = 0; i <= k; ++i) {
      for (long long j = 0; j <= k; ++j) {
        for (long long h = 0; h <= k; ++h) {
          res.a[i][j] = add2(res.a[i][j], mult(a[i][h], M.a[h][j]));
        }
        res.a[i][j] %= MOD;
      }
    }
    return res;
  }
};
ostream &operator<<(ostream &os, const mat &M) {
  cout << endl;
  for (long long i = 0; i < (long long)M.a.size(); ++i) {
    os << M.a[i] << '\n';
  }
  return os;
}
mat unit() {
  mat M;
  for (long long i = 0; i <= k; ++i) M.a[i][i] = 1;
  return M;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  vector<vector<mat>> M;
  for (long long len = 1, lvl = 0; len <= n; len *= k, ++lvl) {
    M.push_back(vector<mat>());
    if (len == 1) {
      for (long long i = 0; i < k; ++i) {
        M[lvl].push_back(unit());
        for (long long j = 0; j <= k; ++j) M[lvl][i].a[i][j] = 1;
      }
      continue;
    }
    for (long long sh = 0; sh < k; ++sh) {
      M[lvl].push_back(unit());
      for (long long x = 0; x < k; ++x) {
        M[lvl][sh] = M[lvl - 1][(sh + x) % k] * M[lvl][sh];
      }
    }
  }
  vector<long long> p;
  while (n) {
    p.push_back(n % k);
    n /= k;
  }
  mat T = unit();
  for (long long lvl = (long long)p.size() - 1, x = 0; lvl >= 0;
       x += p[lvl], --lvl) {
    for (long long sh = 0; sh < p[lvl]; ++sh) {
      T = M[lvl][(x + sh) % k] * T;
    }
  }
  long long ans = 0;
  for (long long i = 0; i <= k; ++i) {
    ans = add(ans, T.a[i][k]);
  }
  cout << ans << '\n';
}