#include <bits/stdc++.h>
using namespace std;
const int p = 1000000007;
int K;
struct Ma {
  int g[31][31];
  Ma() { memset(g, 0, sizeof(g)); }
  int* operator[](int x) { return g[x]; }
  Ma operator*(const Ma& b) const {
    Ma res;
    for (int i = 0; i <= K; i++)
      for (int k = 0; k <= K; k++)
        if (g[i][k])
          for (int j = 0; j <= K; j++)
            res.g[i][j] = (res.g[i][j] + (long long)g[i][k] * b.g[k][j]) % p;
    return res;
  }
  void w() {
    for (int i = 0; i <= K; i++) g[i][i] = 1;
  }
} A[105][30], res, pre[105][30], sub[105][30];
int num[105], tot;
int main() {
  ios::sync_with_stdio(0);
  long long n;
  cin >> n >> K;
  while (n) num[tot++] = n % K, n /= K;
  for (int i = 0; i < K; i++) {
    for (int j = 0; j <= K; j++) A[0][i][j][j] = 1;
    for (int j = 0; j <= K; j++) A[0][i][j][i] = 1;
  }
  for (int i = 1; i <= tot; i++) {
    pre[i - 1][0] = A[i - 1][0];
    for (int j = 1; j < K; j++) pre[i - 1][j] = pre[i - 1][j - 1] * A[i - 1][j];
    sub[i - 1][K - 1] = A[i - 1][K - 1];
    for (int j = K - 2; ~j; j--)
      sub[i - 1][j] = A[i - 1][j] * sub[i - 1][j + 1];
    for (int j = 0; j < K; j++) {
      A[i][j].w();
      A[i][j] = sub[i - 1][j];
      if (j) A[i][j] = A[i][j] * pre[i - 1][j - 1];
    }
  }
  res.g[K][K] = 1;
  for (int i = tot - 1, j = 0; ~i; i--)
    while (num[i]--) res = res * A[i][j], j = (j + 1) % K;
  int ans = 0;
  for (int i = 0; i <= K; i++) ans = (ans + res[K][i]) % p;
  cout << ans << endl;
  return 0;
}
