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;
struct Mat {
  int n;
  int a[32][32];
  Mat() {}
  Mat(int v, int n) : n(n) {
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < n; ++j) a[i][j] = (i == j ? v : 0);
  }
  int *operator[](int i) { return &a[i][0]; }
  const int *operator[](int i) const { return &a[i][0]; }
  Mat operator*(const Mat &b) const {
    Mat res(0, n);
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < n; ++j) {
        long long r = 0;
        for (int k = 0; k < n; ++k) {
          r += a[i][k] * 1LL * b[k][j] % 1000000007;
        }
        r %= 1000000007;
        res[i][j] = (int)r;
      }
    return res;
  }
  vector<long long> operator*(const vector<long long> &v) const {
    vector<long long> res(n, 0);
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < n; ++j) {
        res[i] += a[i][j] * v[j];
        res[i] %= 1000000007;
      }
    return res;
  }
};
long long sumd(long long n, long long k) {
  long long r = 0;
  while (n) {
    r += n % k;
    n /= k;
  }
  return r % k;
}
void naive(long long n, long long k) {
  vector<long long> dig(n, 0);
  vector<long long> d(n + 1, 0);
  d[0] = 1;
  for (int i = 0; i < n; ++i) dig[i] = sumd(i, k);
  for (int i = 0; i < n; ++i) {
    int j = i - 1;
    long long sum = 0;
    for (; 0 <= j && dig[j] != dig[i]; --j) {
      sum += d[j + 1];
    }
    d[i + 1] = (sum + d[j + 1]) % 1000000007;
  }
  long long sum = 0;
  for (int i = 0; i <= n; ++i) sum += d[i];
  sum %= 1000000007;
}
vector<long long> get_dig(long long n, long long k) {
  vector<long long> r;
  while (n) {
    r.push_back(n % k);
    n /= k;
  }
  if (r.empty()) r.push_back(0);
  return r;
}
void gen_m0(int io, int k, Mat &res) {
  res.n = k;
  for (int i = 0; i < k; ++i) {
    int c = (i + io) % k;
    for (int j = 0; j < k; ++j) res[i][j] = j < c ? 0 : 1;
    for (int l = 0; l < i; ++l)
      for (int j = 0; j < k; ++j) res[i][j] += res[l][j];
  }
}
void to_sum(Mat &m, Mat &s, int k) {
  s = Mat(0, k + 1);
  for (int i = 0; i < k; ++i)
    for (int j = 0; j < k; ++j) s[i][j] = m[i][j];
  for (int j = 0; j <= k; ++j) s[k][j] = 1;
}
void from_sum(Mat &m, Mat &s, int k) {
  m = Mat(0, k);
  for (int i = 0; i < k; ++i)
    for (int j = 0; j < k; ++j) m[i][j] = s[i][j];
}
int main() {
  ios::sync_with_stdio(false);
  long long n, k;
  cin >> n >> k;
  static Mat mm[64][32];
  static Mat s[64][32];
  for (int i = 0; i < k; ++i) {
    Mat &r = mm[0][i];
    gen_m0(i, k, r);
    to_sum(r, s[0][i], k);
  }
  Mat m0 = mm[0][0];
  vector<long long> dig = get_dig(n - 1, k);
  vector<long long> f0(k), f1(k), f2(k), s0(k + 1);
  for (int i = 0; i < k; ++i) f0[i] = i == k - 1 ? 1 : 0;
  for (int i = 1; i < dig.size(); ++i) {
    for (int j = 0; j < min(k, (long long)dig.size() - i + 1); ++j) {
      Mat &r = mm[i][j];
      Mat &rs = s[i][j];
      rs = Mat(1, k + 1);
      for (int l = 0; l < k; ++l) {
        rs = s[i - 1][l == k - 1 ? (j + 1) % k : 1] * rs;
      }
      from_sum(r, rs, k);
    }
  }
  long long ans = 0;
  Mat am(1, k);
  Mat sm(1, k + 1);
  am = m0 * am;
  sm = s[0][0] * sm;
  for (int i = dig.size() - 1; 1 <= i; --i) {
    for (int j = 0; j < dig[i]; ++j) {
      am = mm[i - 1][1] * am;
      sm = s[i - 1][1] * sm;
    }
  }
  f2 = am * f0;
  ans = sm[k][k - 1];
  for (int i = 0; i <= dig[0]; ++i) ans += f2[i];
  ans %= 1000000007;
  cout << ans << "\n";
  return 0;
}