Problem: Assume that sk(n) equals the sum of digits of number n in the k-based notation. For example, s2(5) = s2(1012) = 1 + 0 + 1 = 2, s3(14) = s3(1123) = 1 + 1 + 2 = 4.

The sequence of integers a0, ..., an - 1 is defined as <image>. Your task is to calculate the number of distinct subsequences of sequence a0, ..., an - 1. Calculate the answer modulo 109 + 7.

Sequence a1, ..., ak is called to be a subsequence of sequence b1, ..., bl, if there is a sequence of indices 1 ≤ i1 < ... < ik ≤ l, such that a1 = bi1, ..., ak = bik. In particular, an empty sequence (i.e. the sequence consisting of zero elements) is a subsequence of any sequence.

Input

The first line contains two space-separated numbers n and k (1 ≤ n ≤ 1018, 2 ≤ k ≤ 30).

Output

In a single line print the answer to the problem modulo 109 + 7.

Examples

Input

4 2


Output

11


Input

7 7


Output

128

Note

In the first sample the sequence ai looks as follows: (0, 1, 1, 0). All the possible subsequences are: 

(), (0), (0, 0), (0, 1), (0, 1, 0), (0, 1, 1), (0, 1, 1, 0), (1), (1, 0), (1, 1), (1, 1, 0).

In the second sample the sequence ai looks as follows: (0, 1, 2, 3, 4, 5, 6). The subsequences of this sequence are exactly all increasing sequences formed from numbers from 0 to 6. It is easy to see that there are 27 = 128 such sequences.

Solution (in CPP):

Solution:
#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;
}