#include <bits/stdc++.h>
using namespace std;
const int N = 35;
const int MOD = 1e9 + 7;
long long n;
int K, c, res;
struct mat {
  int d[N][N];
  mat() { memset(d, 0, sizeof d); }
} F[N * 2][N], P[N * 2][N], Q[N * 2][N], G;
int lst[N * 2];
mat operator*(mat a, mat b) {
  mat c;
  for (int i = 0; i <= K; ++i)
    for (int j = 0; j <= K; ++j)
      for (int k = 0; k <= K; ++k)
        c.d[i][k] = (c.d[i][k] + 1ll * a.d[i][j] * b.d[j][k]) % MOD;
  return c;
}
int main() {
  cin >> n >> K;
  while (n) lst[c++] = n % K, n /= K;
  for (int i = 0; i < c; ++i) {
    if (i == 0) {
      for (int j = 0; j < K; ++j) {
        for (int p = 0; p < K; ++p) F[i][j].d[p][p] = 1;
        for (int p = 0; p <= K; ++p) F[i][j].d[j][p] = 1;
        F[i][j].d[K][K] = 1;
      }
    } else {
      for (int j = 1; j < K; ++j) F[i][j] = Q[i - 1][j] * P[i - 1][j - 1];
      F[i][0] = Q[i - 1][0];
    }
    P[i][0] = F[i][0];
    for (int j = 1; j < K; ++j) P[i][j] = P[i][j - 1] * F[i][j];
    Q[i][K - 1] = F[i][K - 1];
    for (int j = K - 2; ~j; --j) Q[i][j] = F[i][j] * Q[i][j + 1];
  }
  for (int i = 0; i <= K; ++i) G.d[i][i] = 1;
  for (int i = c - 1, s = 0; ~i; --i) {
    for (int j = 0; j < lst[i]; ++j) G = G * F[i][(s + j) % K];
    s = (s + lst[i]) % K;
  }
  res = 1;
  for (int i = 0; i < K; ++i) res = (res + G.d[i][K]) % MOD;
  cout << res << endl;
}
