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 INF = 0x3f3f3f3f, N = 2e6 + 5, MOD = 1e9 + 7;
int k;
struct Matrix {
  int a[35][35];
  Matrix() { memset(a, 0, sizeof a); }
  Matrix(int x) {
    for (int i = 0; i <= k; i++)
      for (int j = 0; j <= k; j++)
        if (i == j)
          a[i][j] = x;
        else
          a[i][j] = 0;
  }
  int* operator[](int x) { return a[x]; }
  Matrix operator*(Matrix x) {
    Matrix c;
    for (int i = 0; i <= k; i++)
      for (int j = 0; j <= k; j++) {
        long long t = 0;
        for (int p = 0; p <= k; p++) t = (t + 1ll * a[i][p] * x[p][j]) % MOD;
        c[i][j] = t;
      }
    return c;
  }
};
Matrix qpow(Matrix a, long long b) {
  Matrix res(1);
  for (; b; b >>= 1, a = a * a)
    if (b & 1) res = res * a;
  return res;
}
int plc[N], len;
void getplc(long long x) {
  do {
    plc[len] = x % k, x /= k;
    len++;
  } while (x);
}
Matrix a[105][35], s1[105][35], s2[105][35], s;
int Solve(int cas = 0) {
  long long n;
  cin >> n >> k;
  getplc(n);
  for (int i = 0; i < len; i++) {
    if (!i) {
      for (int j = 0; j < k; j++) {
        a[0][j][k][k] = 1;
        for (int i2 = 0; i2 < k; i2++) {
          if (i2 == j) {
            for (int j2 = 0; j2 <= k; j2++) a[0][j][i2][j2] = 1;
          } else
            a[0][j][i2][i2] = 1;
        }
      }
    } else {
      for (int j = 0; j < k; j++) {
        if (!j)
          a[i][j] = s2[i - 1][j];
        else
          a[i][j] = s2[i - 1][j] * s1[i - 1][j - 1];
      }
    }
    s1[i][0] = a[i][0];
    for (int j = 1; j < k; j++) s1[i][j] = s1[i][j - 1] * a[i][j];
    s2[i][k - 1] = a[i][k - 1];
    for (int j = k - 2; j >= 0; j--) s2[i][j] = a[i][j] * s2[i][j + 1];
  }
  long long res = 1;
  for (int i = 0; i <= k; i++) {
    s[0][i] = 1;
  }
  for (int i = len - 1, j = 0; i >= 0; i--) {
    for (int t = 1; t <= plc[i]; t++) s = s * a[i][j], j = (j + 1) % k;
  }
  cout << s[0][k] << endl;
  return 0;
}
void Pre() {}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  Pre();
  while (Solve())
    ;
  return 0;
}