#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
long long powmod(long long a, long long b) {
  long long res = 1;
  a %= mod;
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
typedef long long mat[33][33];
mat dp[65][33], pre[65][33], suc[65][33];
long long rt[33], ret[33], n;
int k;
vector<int> bt;
void mul(mat a, mat b, mat c) {
  memset(c, 0, sizeof(c));
  for (int z = 0; z < k + 2; z++)
    for (int x = 0; x < k + 2; x++)
      for (int y = 0; y < k + 2; y++)
        c[x][y] = (c[x][y] + a[x][z] * b[z][y]) % mod;
}
void gao(long long n) {
  while (n != 0) bt.push_back(n % k), n /= k;
  int s = 0;
  for (int i = ((int)(bt).size()) - 1; i >= 0; i--) {
    for (int j = 0; j < bt[i]; j++) {
      for (int z = 0; z < k + 2; z++) rt[z] = 0;
      for (int x = 0; x < k + 2; x++)
        for (int y = 0; y < k + 2; y++)
          rt[y] = (rt[y] + ret[x] * dp[i][(s + j) % k][x][y]) % mod;
      for (int z = 0; z < k + 2; z++) ret[z] = rt[z];
    }
    s = (s + bt[i]) % k;
  }
}
int main() {
  scanf("%I64d%d", &n, &k);
  for (int i = 0; i < k; i++) {
    dp[0][i][0][0] = 2;
    dp[0][i][i + 2][0] = mod - 1;
    dp[0][i][0][1] = 1;
    for (int j = 0; j < k; j++)
      if (j != i)
        dp[0][i][j + 2][j + 2] = 1;
      else
        dp[0][i][0][j + 2] = 1;
  }
  for (int a = 0; a < k + 2; a++)
    for (int b = 0; b < k + 2; b++)
      pre[0][0][a][b] = dp[0][0][a][b],
      suc[0][k - 1][a][b] = dp[0][k - 1][a][b];
  for (int i = 1; i < k; i++) mul(pre[0][i - 1], dp[0][i], pre[0][i]);
  for (int i = k - 1 - 1; i >= 0; i--) mul(dp[0][i], suc[0][i + 1], suc[0][i]);
  int t = 1;
  long long c = 1;
  while (c <= n) {
    for (int a = 0; a < k + 2; a++)
      for (int b = 0; b < k + 2; b++) dp[t][0][a][b] = pre[t - 1][k - 1][a][b];
    for (int i = 1; i < k; i++) mul(suc[t - 1][i], pre[t - 1][i - 1], dp[t][i]);
    for (int a = 0; a < k + 2; a++)
      for (int b = 0; b < k + 2; b++)
        pre[t][0][a][b] = dp[t][0][a][b],
        suc[t][k - 1][a][b] = dp[t][k - 1][a][b];
    for (int i = 1; i < k; i++) mul(pre[t][i - 1], dp[t][i], pre[t][i]);
    for (int i = k - 1 - 1; i >= 0; i--)
      mul(dp[t][i], suc[t][i + 1], suc[t][i]);
    if (c > n / k) break;
    c *= k;
    t++;
  }
  ret[0] = 1;
  gao(n);
  printf("%I64d\n", ret[0]);
}
