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 T>
inline bool maxit(T& a, const T& b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
inline bool minit(T& a, const T& b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
inline long long gint() {
  char c = getchar();
  int f = 1;
  while (c < '0' || c > '9') {
    if (c == '-') {
      f = -1;
    }
    c = getchar();
  }
  long long a = c - '0';
  while ((c = getchar()) >= '0' && c <= '9') {
    a = a * 10 + c - '0';
  }
  return a * f;
}
const int maxk = 31, MOD = int(1e9 + 7);
typedef int Mat[maxk][maxk];
int kb;
inline void mat_mul(Mat A, Mat B, Mat res) {
  static Mat C;
  memset(C, 0, sizeof(C));
  for (int i = 0, _end_ = (kb + 1); i < _end_; i++)
    for (int k = 0, _end_ = (kb + 1); k < _end_; k++)
      for (int j = 0, _end_ = (kb + 1); j < _end_; j++) {
        C[i][j] = (C[i][j] + (long long)A[i][k] * B[k][j]) % MOD;
      }
  memcpy(res, C, sizeof(C));
}
Mat A[70][maxk], pre[maxk], suf[maxk], ans, I;
void init_I() {
  memset((I), 0, sizeof((I)));
  for (int i = 0, _end_ = (kb + 1); i < _end_; i++) I[i][i] = 1;
}
int dig[70], len;
void prepocess() {
  for (int i = 0, _end_ = (kb); i < _end_; i++) {
    for (int j = 0, _end_ = (kb + 1); j < _end_; j++) {
      A[0][i][j][j] = A[0][i][i][j] = 1;
    }
  }
  for (int i = (1), _end_ = (len - 1); i <= _end_; i++) {
    memcpy(pre[0], A[i - 1][0], sizeof(A[i - 1][0]));
    for (int j = (1), _end_ = (kb - 1); j <= _end_; j++) {
      mat_mul(A[i - 1][j], pre[j - 1], pre[j]);
    }
    memcpy(suf[kb - 1], A[i - 1][kb - 1], sizeof(A[i - 1][kb - 1]));
    for (int j = (kb - 2), _beg_ = (0); j >= _beg_; j--) {
      mat_mul(suf[j + 1], A[i - 1][j], suf[j]);
    }
    for (int j = 0, _end_ = (kb); j < _end_; j++) {
      mat_mul(j ? pre[j - 1] : I, suf[j], A[i][j]);
    }
  }
}
int main() {
  long long n = gint();
  kb = gint();
  init_I();
  while (n) {
    dig[len++] = n % kb;
    n /= kb;
  }
  prepocess();
  ans[kb][0] = 1;
  int x = 0;
  for (int i = (len - 1), _beg_ = (0); i >= _beg_; i--) {
    for (int j = 0, _end_ = (dig[i]); j < _end_; j++) {
      mat_mul(A[i][(j + x) % kb], ans, ans);
    }
    x = (x + dig[i]) % kb;
  }
  int res = 0;
  for (int i = 0; i < kb; i++) {
    res = (res + ans[i][0]) % MOD;
  }
  printf("%d\n", res + 1);
  return 0;
}