#include <bits/stdc++.h>
using namespace std;
const long double pi = 3.14159265359;
template <typename T>
T abs(T x) {
  return x > 0 ? x : -x;
}
template <typename T>
T sqr(T x) {
  return x * x;
}
const long long mod = 1000000007;
const int maxn = 31;
int k;
struct matr {
  long long a[maxn][maxn];
  matr() {
    for (int i = 0; i < k + 1; i++)
      for (int j = 0; j < k + 1; j++) a[i][j] = 0;
  }
  matr operator*(const matr &other) const {
    matr res;
    long long mod2 = mod * mod;
    for (int i = 0; i < k + 1; i++)
      for (int j = 0; j < k + 1; j++) {
        for (int h = 0; h < k + 1; h++) {
          res.a[i][j] += a[i][h] * other.a[h][j];
          if (res.a[i][j] >= mod2) res.a[i][j] -= mod2;
        }
        res.a[i][j] %= mod;
      }
    return res;
  }
};
map<pair<int, int>, matr> cache;
matr rot(const matr &x, int add) {
  matr res;
  for (int i = 0; i < k; i++)
    for (int j = 0; j < k; j++) res.a[(i + add) % k][(j + add) % k] = x.a[i][j];
  for (int i = 0; i < k; i++) res.a[(i + add) % k][k] = x.a[i][k];
  res.a[k][k] = 1;
  return res;
}
matr get(int n, int add) {
  if (cache.find(make_pair(n, add)) != cache.end())
    return cache[make_pair(n, add)];
  matr res;
  if (n == 0) {
    if (add == 0) {
      for (int i = 0; i <= k; i++) res.a[0][i] = 1;
      for (int i = 1; i <= k; i++) res.a[i][i] = 1;
    } else {
      res = rot(get(n, add - 1), 1);
    }
    return cache[make_pair(n, add)] = res;
  } else {
    if (add == 0) {
      res = get(n - 1, 0);
      for (int i = 1; i < k; i++) res = get(n - 1, i) * res;
    } else {
      res = rot(get(n, add - 1), 1);
    }
    return cache[make_pair(n, add)] = res;
  }
}
int main() {
  long long n;
  cin >> n >> k;
  matr ans;
  for (int i = 0; i < k + 1; i++) ans.a[i][i] = 1;
  int add = 0;
  while (n) {
    long long g = 1;
    int pw = 0;
    while (g <= (n / k + 5) && g * k <= n) {
      g *= k;
      pw++;
    }
    while (g <= n) {
      ans = get(pw, add % k) * ans;
      add++;
      n -= g;
    }
  }
  long long ans2 = 1;
  for (int i = 0; i < k; i++) ans2 = (ans2 + ans.a[i][k]) % mod;
  cout << ans2 << endl;
  return 0;
}
