#include <bits/stdc++.h>
using namespace std;
const long long Mod = 1000000007;
struct matrix {
  long long a[31][31], l1, l2;
  void clear() { memset(a, 0, sizeof(a)); }
} seg[1810], pre[62][30], suf[62][30], one, ans;
long long tot, rt;
long long son[1810][30], n, K, a[62], lg;
void up(long long &x, long long y) { x = (x + y) % Mod; }
matrix ttimes(matrix x, matrix y) {
  matrix ret;
  ret.l1 = x.l1;
  ret.l2 = y.l2;
  ret.clear();
  for (long long i = 0; i < ret.l1; i++) {
    for (long long j = 0; j < ret.l2; j++) {
      for (long long k = 0; k < x.l2; k++)
        ret.a[i][j] = (ret.a[i][j] + x.a[i][k] * y.a[k][j] % Mod) % Mod;
    }
  }
  return ret;
}
void build_tree(long long &now, long long dep) {
  now = ++tot;
  if (dep == lg) {
    seg[now] = one;
    for (long long i = 0; i < K; i++) {
      son[now][i] = ++tot;
      seg[tot].l1 = seg[tot].l2 = K + 1;
      for (long long j = 0; j <= K; j++)
        seg[tot].a[j][j] = 1, seg[tot].a[j][i] = 1;
      seg[now] = ttimes(seg[now], seg[tot]);
      pre[dep][i] = seg[now];
    }
    suf[dep][K - 1] = seg[son[now][K - 1]];
    for (long long i = K - 2; i >= 0; i--)
      suf[dep][i] = ttimes(seg[son[now][i]], suf[dep][i + 1]);
  } else {
    build_tree(son[now][0], dep + 1);
    seg[now] = seg[son[now][0]];
    pre[dep][0] = seg[now];
    for (long long i = 1; i < K; i++) {
      son[now][i] = ++tot;
      seg[tot] = ttimes(suf[dep + 1][i], pre[dep + 1][i - 1]);
      for (long long j = 0; j < K; j++)
        son[tot][j] = son[son[now][0]][(i + j) % K];
      seg[now] = ttimes(seg[now], seg[son[now][i]]);
      pre[dep][i] = seg[now];
    }
    suf[dep][K - 1] = seg[son[now][K - 1]];
    for (long long i = K - 2; i >= 0; i--)
      suf[dep][i] = ttimes(seg[son[now][i]], suf[dep][i + 1]);
  }
}
void getans(long long now, long long dep) {
  for (long long i = 0; i < a[dep]; i++) ans = ttimes(ans, seg[son[now][i]]);
  if (dep < lg)
    getans(son[now][a[dep]], dep + 1);
  else
    ans = ttimes(ans, seg[son[now][a[dep]]]);
}
int main() {
  long long i, j, k;
  scanf("%I64d%I64d", &n, &K);
  if (n == 1) {
    printf("2\n");
    return 0;
  }
  n--;
  while (n) a[++lg] = n % K, n /= K;
  one.l1 = one.l2 = K + 1;
  for (i = 0; i <= K; i++) one.a[i][i] = 1;
  build_tree(rt, 1);
  for (i = 1; i <= lg / 2; i++) swap(a[i], a[lg - i + 1]);
  ans = one;
  getans(rt, 1);
  long long ret = 0;
  for (i = 0; i <= K; i++) ret = (ret + ans.a[K][i]) % Mod;
  printf("%I64d\n", ret);
  return 0;
}
