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 mod = (int)1e9 + 7;
int sum(int a, int b) {
  int s = a + b;
  if (s >= mod) s -= mod;
  return s;
}
int sub(int a, int b) {
  int s = a - b;
  if (s < 0) s += mod;
  return s;
}
int mult(int a, int b) { return (1LL * a * b) % mod; }
long long n;
int k;
vector<vector<int> > matrix_mult(vector<vector<int> > a,
                                 vector<vector<int> > b) {
  vector<vector<int> > c(k + 1, vector<int>(k + 1, 0));
  for (int i = 0; i <= k; i++) {
    for (int j = 0; j <= k; j++) {
      for (int t = 0; t <= k; t++) {
        c[i][t] = sum(c[i][t], mult(a[i][j], b[j][t]));
      }
    }
  }
  return c;
}
vector<vector<int> > pref[100];
vector<vector<int> > shift_by_1(vector<vector<int> > mat) {
  vector<vector<int> > c(k + 1, vector<int>(k + 1, 0));
  for (int i = 0; i <= k; i++) {
    for (int j = 0; j <= k; j++) {
      int ni = i;
      if (ni < k) ni = (ni + k - 1) % k;
      int nj = j;
      if (nj < k) nj = (nj + k - 1) % k;
      c[i][j] = mat[ni][nj];
    }
  }
  return c;
}
void print(vector<vector<int> >& c) {
  for (auto t : c) {
    for (int v : t) cout << v << " ";
    cout << '\n';
  }
  cout << "------------" << endl;
}
void init() {
  long long digs = 0;
  long long cop = n;
  while (cop > 0) {
    cop /= k;
    digs++;
  }
  pref[0].resize(k + 1, vector<int>(k + 1, 0));
  for (int i = 1; i < k; i++) {
    pref[0][i][i] = 1;
  }
  pref[0][0][k] = 1;
  pref[0][k][0] = mod - 1;
  pref[0][k][k] = 2;
  for (int step = 0; step < digs; step++) {
    pref[step + 1] = pref[step];
    auto cur = pref[step];
    for (int i = 1; i <= k - 1; i++) {
      cur = shift_by_1(cur);
      pref[step + 1] = matrix_mult(pref[step + 1], cur);
    }
  }
}
vector<vector<int> > solve(long long n) {
  if (n == 0) {
    vector<vector<int> > c(k + 1, vector<int>(k + 1, 0));
    for (int i = 0; i <= k; i++) {
      c[i][i] = 1;
    }
    return c;
  }
  long long dig = 0;
  long long pw = 1;
  while (pw <= n / k) {
    pw *= k;
    dig++;
  }
  if (n == pw) return pref[dig];
  long long steps = n / pw;
  long long left = n - steps * pw;
  auto cur_mat = solve(0);
  auto cur_coef = pref[dig];
  for (int i = 0; i < steps; i++) {
    cur_mat = matrix_mult(cur_mat, cur_coef);
    cur_coef = shift_by_1(cur_coef);
  }
  auto other_mat = solve(left);
  for (int i = 0; i < steps; i++) {
    other_mat = shift_by_1(other_mat);
  }
  return matrix_mult(cur_mat, other_mat);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  srand(239);
  cin >> n >> k;
  init();
  auto mat = solve(n);
  cout << mat[k][k];
  return 0;
}