#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
inline void mine(T1 &x, const T2 &y) {
  if (y < x) x = y;
}
template <typename T1, typename T2>
inline void maxe(T1 &x, const T2 &y) {
  if (x < y) x = y;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &b) {
  for (auto &k : b) os << k << ' ';
  return os;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &b) {
  for (auto &k : b) is >> k;
  return is;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const long long MOD = 1e9 + 7;
const long long MOD2 = 1ll * MOD * MOD;
long long add(long long a, long long b) {
  a += b;
  if (a >= MOD) return a - MOD;
  return a;
}
long long add2(long long a, long long b) {
  a += b;
  if (a >= MOD2) return a - MOD2;
  return a;
}
long long mult(long long a, long long b) { return 1ll * a * b % MOD; }
long long n, k;
struct mat {
  vector<vector<long long>> a;
  mat() { a = vector<vector<long long>>(k + 1, vector<long long>(k + 1, 0)); }
  mat operator*(const mat &M) const {
    mat res;
    for (long long i = 0; i <= k; ++i) {
      for (long long j = 0; j <= k; ++j) {
        for (long long h = 0; h <= k; ++h) {
          res.a[i][j] = add2(res.a[i][j], mult(a[i][h], M.a[h][j]));
        }
        res.a[i][j] %= MOD;
      }
    }
    return res;
  }
};
ostream &operator<<(ostream &os, const mat &M) {
  cout << endl;
  for (long long i = 0; i < (long long)M.a.size(); ++i) {
    os << M.a[i] << '\n';
  }
  return os;
}
mat unit() {
  mat M;
  for (long long i = 0; i <= k; ++i) M.a[i][i] = 1;
  return M;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  vector<vector<mat>> M;
  for (long long len = 1, lvl = 0; len <= n; len *= k, ++lvl) {
    M.push_back(vector<mat>());
    if (len == 1) {
      for (long long i = 0; i < k; ++i) {
        M[lvl].push_back(unit());
        for (long long j = 0; j <= k; ++j) M[lvl][i].a[i][j] = 1;
      }
      continue;
    }
    for (long long sh = 0; sh < k; ++sh) {
      M[lvl].push_back(unit());
      for (long long x = 0; x < k; ++x) {
        M[lvl][sh] = M[lvl - 1][(sh + x) % k] * M[lvl][sh];
      }
    }
  }
  vector<long long> p;
  while (n) {
    p.push_back(n % k);
    n /= k;
  }
  mat T = unit();
  for (long long lvl = (long long)p.size() - 1, x = 0; lvl >= 0;
       x += p[lvl], --lvl) {
    for (long long sh = 0; sh < p[lvl]; ++sh) {
      T = M[lvl][(x + sh) % k] * T;
    }
  }
  long long ans = 0;
  for (long long i = 0; i <= k; ++i) {
    ans = add(ans, T.a[i][k]);
  }
  cout << ans << '\n';
}
