#include <bits/stdc++.h>

using namespace std;
template<typename T1, typename T2>inline void chkmin(T1 &x, T2 y) { if (x > y) x = y; }
template<typename T1, typename T2>inline void chkmax(T1 &x, T2 y) { if (x < y) x = y; }
#define sz(c) (int)(c).size()
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define left left228
#define right right228
#define rank rank228
#define y1 y1228
#define read(FILENAME) freopen((FILENAME + ".in").c_str(), "r", stdin)
#define write(FILENAME) freopen((FILENAME + ".out").c_str(), "w", stdout)
#define files(FILENAME) read(FILENAME), write(FILENAME)
#define pb push_back
#define mp make_pair
using ll = long long;
using ld = long double;
// const int MAXMEM = 200 * 1000 * 1024;
// char _memory[MAXMEM];
// size_t _ptr = 0;
// void* operator new(size_t _x) { _ptr += _x; return _memory + _ptr - _x; }
// void operator delete (void*) noexcept {}
const string FILENAME = "input";
const int MAXN = 200228;
const int Mod = 1000000009;
const int P = 1000003;
const int Mod1 = 1000000007;
const int P1 = 424243;

int n;
string t;
int f[MAXN];
int nxtz[MAXN];
int nxto[MAXN];
ll p[MAXN];
ll p1[MAXN];
vector<int> fg;
vector<int> start;
vector<pair<int, int> > st;
int ft[MAXN];

int get(int l, int r, int l1, int r1) {
	return min(r, r1) - max(l, l1) + 1;
}

ll hs[MAXN];
ll hs1[MAXN];

int gethashik(int l, int r) {
	return ((hs[r] - (l ? (ll)hs[l - 1] * p[r - l + 1] % Mod: 0)) % Mod + Mod) % Mod;
}

int gethashik1(int l, int r) {
	return ((hs1[r] - (l ? (ll)hs1[l - 1] * p1[r - l + 1] % Mod1: 0)) % Mod1 + Mod1) % Mod1;
}

int gethash(int l, int r) {
	int ls = upper_bound(all(start), l) - start.begin() - 1;
	int rs = upper_bound(all(start), r) - start.begin() - 1;
	if (ls == rs) {
		if (t[l] == '1') {
			return (r - l + 1) % 2 == 0 ? 0: 1;
		}
		return 0;
	}
	int llen = get(l, r, start[ls], start[ls] + st[ls].second - 1);
	int rlen = get(l, r, start[rs], start[rs] + st[rs].second - 1);
	int sl = lower_bound(all(fg), ls + 1) - fg.begin();
	int sr = lower_bound(all(fg), rs) - fg.begin() - 1;
	vector<pair<int, int> > s;
	int last = l - 1;
	if (t[l] == '1' && llen % 2 == 1) {
		s.pb({1, 1});
		last = l + llen - 1;
	}
	if (sl >= 0 && sl < sz(fg) && sr >= 0 && sr < sz(fg) && ls < fg[sl] && sl <= sr && fg[sr] < rs) {
	//	cout << ft[fg[sl]] - (l ? ft[l - 1]: 0) << endl;
		//cout << fg[sl] << endl;
		s.pb({ft[start[fg[sl]]] - (l ? ft[l - 1]: 0) + 1, 1});
		if (sl < sr) {
			s.pb({gethashik(sl + 1, sr), sr - sl});
		}
		last = start[fg[sr]] + st[fg[sr]].second - 1;
	}
	if (t[r] == '1' && rlen % 2 == 1) {
		//cout << ft[r] - (last >= 0 ? ft[last]: 0) << endl;
		s.pb({ft[r] - (last >= 0 ? ft[last]: 0) + 1, 1});
	}
	ll cur = 0;
	for (auto x: s) {
		cur = (cur * p[x.second] + x.first) % Mod;
	}
	return cur;
}

int gethash1(int l, int r) {
	int ls = upper_bound(all(start), l) - start.begin() - 1;
	int rs = upper_bound(all(start), r) - start.begin() - 1;
	if (ls == rs) {
		if (t[l] == '1') {
			return (r - l + 1) % 2 == 0 ? 0: 1;
		}
		return 0;
	}
	int llen = get(l, r, start[ls], start[ls] + st[ls].second - 1);
	int rlen = get(l, r, start[rs], start[rs] + st[rs].second - 1);
	int sl = lower_bound(all(fg), ls + 1) - fg.begin();
	int sr = lower_bound(all(fg), rs) - fg.begin() - 1;
	vector<pair<ll, int> > s;
	int last = l - 1;
	if (t[l] == '1' && llen % 2 == 1) {
		s.pb({1, 1});
		last = l + llen - 1;
	}
	if (sl >= 0 && sl < sz(fg) && sr >= 0 && sr < sz(fg) && ls < fg[sl] && sl <= sr && fg[sr] < rs) {
	//	cout << ft[fg[sl]] - (l ? ft[l - 1]: 0) << endl;
		//cout << fg[sl] << endl;
		s.pb({ft[start[fg[sl]]] - (l ? ft[l - 1]: 0) + 1, 1});
		if (sl < sr) {
			s.pb({gethashik1(sl + 1, sr), sr - sl});
		}
		last = start[fg[sr]] + st[fg[sr]].second - 1;
	}
	if (t[r] == '1' && rlen % 2 == 1) {
		//cout << ft[r] - (last >= 0 ? ft[last]: 0) << endl;
		s.pb({ft[r] - (last >= 0 ? ft[last]: 0) + 1, 1});
	}
	ll cur = 0;
	for (auto x: s) {
		cur = (cur * p1[x.second] + x.first) % Mod1;
	}
	return cur;
}

string getstring(int l, int r) {
	string ts;
	for (int i = l; i <= r; i++) {
		ts += t[i];
	}
	while (true) {
		bool was = false;
		for (int j = 0; j <= sz(ts) - 3; j++) {
			if (ts[j] == '0' && ts[j + 1] == '1' && ts[j + 2] == '1') {
				ts[j] = '1';
				ts[j + 2] = '0';
				was = true;
			}
		}
		if (!was) {
			break;
		}
	}
	return ts;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
//	read(FILENAME);
	cin >> n >> t;
	p[0] = 1;
	for (int i = 1; i <= n; i++) {
		p[i] = ((ll)p[i - 1] * P) % Mod;
	}
	p1[0] = 1;
	for (int i = 1; i <= n; i++) {
		p1[i] = ((ll)p1[i - 1] * P1) % Mod1;
	}
	for (int i = 0; i < n; i++) {
		int c = t[i] - '0';
		f[i] = c;
		ft[i] =1  - c;
		if (i) {
			f[i] += f[i - 1];
			ft[i] += ft[i - 1];
		}
		if (st.empty() || st.back().first != c) {
			st.pb({c, 1});
		} else {
			st.back().second++;
		}
	}
	int cur = 0;
	for (int i = 0; i < sz(st); i++) {
		start.pb(cur);
		if (st[i].first == 1 && st[i].second % 2 == 1) {
			fg.pb(i);
		}
		cur += st[i].second;
	}
	for (int i = 0; i < sz(fg); i++) {
		int kek = ft[start[fg[i]]] - (i ? ft[start[fg[i - 1]] + st[fg[i - 1]].second - 1]: 0) + 1;
		hs[i] = (i ? hs[i - 1] * P + kek: kek) % Mod;
	}
	for (int i = 0; i < sz(fg); i++) {
		int kek = ft[start[fg[i]]] - (i ? ft[start[fg[i - 1]] + st[fg[i - 1]].second - 1]: 0) + 1;
		hs1[i] = (i ? hs1[i - 1] * P1 + kek: kek) % Mod1;
	}
	//cout << t << endl;
	int q;
	cin >> q;
	for (int i = 0; i < q; i++) {
		int l1, l2, len;
		cin >> l1 >> l2 >> len;
		l1--, l2--;
		if (f[l1 + len - 1] - (l1 ? f[l1 - 1]: 0) != f[l2 + len - 1] - (l2 ? f[l2 - 1]: 0)) {
		 	cout << "No\n";
	 		continue;
		}
		bool ok = gethash(l1, l1 + len - 1) != gethash(l2, l2 +  len - 1) || gethash1(l1, l1 + len - 1) != gethash1(l2, l2 +  len - 1);
		if (ok) {
			cout << "No\n";
		} else {
			cout << "Yes\n";
		}
	}
}


#include<bits/stdc++.h>
using namespace std;

int main()
{

	int n;
	cin >> n;
	int arr[n];
	for(int i=0;i<n;i++)
		cin >> arr[i];
	int t;
	cin >> t;

	sort(arr,arr+n);

	int count;
	int max=0,j;
	for(int i=0;i<n;i++)
	{
		count = 0;
		j=i;
		int k=t;
		while(k>=0&&j<n)
		{
			count++;
			if(j<n-1)
				k-=(arr[j+1]-arr[j]);
			j++;
		}
		if(max < count)
			max = count;
	}

	cout << max << endl;

}


#include <vector>
#include <cstdio>
#include <deque>
#include <algorithm>

int span(std::vector<int> shts, int start, int time){
  int i=1;
  for(;start+i<shts.size();i++)
    if(shts[start+i]-shts[start]>time)
      return i;
  return i;
}

int main(){
  std::vector<int> shts;
  int qtty;
  int ts;
  scanf("%d",&qtty);
  for(int i=0;i<qtty;i++){
    int aux;
    scanf("%d",&aux);
    shts.push_back(aux);
  }
  scanf("%d",&ts);
  std::sort(shts.begin(),shts.end());
  int maxspan=0;
  for(int i=0;i<shts.size();i++){
    int aux=span(shts,i,ts);
    if(maxspan<aux)
      maxspan=aux;
  }
  printf("%d\n",maxspan);
}


#define _CRT_SECURE_NO_WARNINGS
//#pragma comment(linker, "/STACK:100000000")
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <set>
#include <stdio.h>
#include <vector>

#define ull unsigned ll
#define ll long long
#define ld long double
#define FOR(i, a, b) for(int i=(int)(a); i<(int)(b); ++i)
#define FORZ(i, a, b) for (int i=(int)(a); i>(int)(b); --i)
#define mp make_pair
#define fs first
#define sc second
#define pr pair<int, int>
#define pb push_back
#define ppb pop_back
#define bg begin()
#define ed end()
#define sz size()
#define rsz resize
#define all(a) (a).bg, (a).ed
#define len length()
#define ext return 0;
ll gcd(ll a, ll b) { if (a == 0 || b == 0) return (a + b); if (a > b) return (gcd(a%b, b)); return (gcd(b, b % a)); }

#define Nmax 1000009
#define INF 1000000007
#define y1 yy1
#define eps 1e-5
void out() { std::cout << "NO\n"; exit(0); }
using namespace std;

int n, T, a[101];
bool used[101];

int main(){
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);

	cin >> n;
	FOR(i, 0, n) cin >> a[i];
	cin >> T;

	int ans = 0;
	sort(a, a + n);
	FOR(i, 0, n) {
		int p = i+1;
		while (p < n && abs(a[i] - a[p]) <= T) p++;
		ans = max(ans, p - i);
	}
	cout << ans << endl;

	return 0;
}


#include<iostream>
#include<algorithm>
#include<iomanip>
#include<string>
#include<string.h>
#include<cmath>
#include<vector>
#include<cstdio>
#include<queue>
#include<stack>
#include<functional>
#define INF 999999999
#define ms(a,x) memset(a,x,sizeof(a))

using namespace std;

bool cmd(int a, int b)
{
	return a > b;
}

int main()
{
	int n, a[110] = { 0 };
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}
	int t;
	cin >> t;
	sort(a, a + n);
	int num = 0;
	for (int i = 0; i < n; i++)
	{
		int count = 0, last = a[i] + t;
		for (int j = i; j < n; j++)
		{
			if (a[j] >= a[i] && a[j] <= last)
			{
				count++;
			}
		}
		if (count > num)
		{
			num = count;
		}
	}

	cout << num;
}


/*
welcome to my code           hi from    s_agnik1511
Don't just copy it.
Analyze it.
coz that's what heroes do.....
*/
#include<bits/stdc++.h>
#include<algorithm>
#include<string.h>
#include<numeric>
#include<vector>
#include<math.h>
#include<map>
using namespace std;
#define dude             ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define mesc(number,character)   char f[number];memset(f,character,number);
#define mesi(number,value)   int f[number];memset(f,value,number);
#define specio           ifstream cin("input.txt");ofstream cout("output.txt");
#define fr(i,start,end)  for(i=start;i<=end;i++)
#define rf(i,start,end)  for(i=start;i>=end;i--)
#define tst              int t;cin>>t;while(t--)
#define ai               ll n,i,j;cin>>n;ll a[n];
#define pi               3.14159265358979323846
#define ll               long long int
#define acc              accumulate
#define ld               long double
#define pb               push_back
#define r0               return 0;
#define pp               pop_back
#define vec              vector
#define sec              second
#define S                string
#define D                double
#define len              length
#define br               break;
#define wh               while
#define fs               first
#define F                float
#define gcd              __gcd
#define sz               size
#define endl              "\n"
#define sp              " "
ll ciel(ll x,ll y) {
    if(x%y) {
        return x/y+1;
    }
    return x/y;
}
void what_should_be_the_logic() {
ai
ll k;ll ma=0,cnt=1;
fr(i,0,n-1) {
    cin>>a[i];
}cin>>k;
sort(a,a+n);
fr(j,0,n-2) {
fr(i,j+1,n-1) {
    if(a[i]-a[j]<=k) {
        cnt++;
    }
    else {
        ma=max(cnt,ma);cnt=1;
    }
}
ma=max(ma,cnt);cnt=1;
}
cout<<max(ma,cnt);
}
int main() {
    dude
    what_should_be_the_logic();
   r0
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;

int arr[1001];
int n;
int ans;
int itv;

int Get(int pos)
{
	int ret = 0;
	for(int i = pos; i < n; ++ i)
	{
		if(arr[i] <= itv + arr[pos])
			ret ++;
	}
	return ret;
}

int main()
{
#if _MSC_VER == 1500
	freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
#endif
	scanf("%d", &n);
	for(int i = 0; i < n; ++ i)
		scanf("%d", arr + i);
	sort(arr, arr + n);
	scanf("%d", &itv);
	ans = 0;
	for(int i = 0; i < n; ++ i)
		ans = max(ans, Get(i));
	printf("%d\n", ans);
	return 0;
}


#include <stdlib.h>
#include <stdio.h>
void swap(int &a, int &b){
    int tmp=a;
    a=b;
    b=tmp;
}

int main(){
    int n;
    scanf("%d",&n);
    int t;
    int a[10000];
    for(int i=0;i<10000;i++)a[i]=0;
    for(int i=0;i<n;i++){
        scanf("%d",&t);
        a[t]++;
    }
    scanf("%d",&t);
    int i=1;
    int ans=0;
    while(i<=1000){
        int cur=0;
        for(int j=i;j<=i+t;j++)cur+=a[j];
        if(cur>ans)ans=cur;
        i++;
    }
    printf("%d",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define pi pair<ll,ll>
ll arr[500005];
map<ll,ll>mp;

ll cnt[10005];

ll cumsum[10000];

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    ll i,j,n,m,t,a,b,x,y;

cin>>n;

for(i=0;i<n;i++){
    cin>>arr[i];
cnt[arr[i]]++;
}
cin>>t;
for(i=1;i<=1000;i++){
    cumsum[i]=cumsum[i-1]+cnt[i];
}

ll ans=0;
for(i=t;i<=1000;i++){
    ans=max(ans,cumsum[i]-cumsum[i-t-1]);
}
cout<<ans<<endl;
}


#include <iostream>
#include <string>
#include <map>
#include <iomanip>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;

int main(){
    int n;
    cin>>n;
    vector <int> a(n);
    for (auto &i:a){
        cin>>i;
    }
    sort (a.begin(),a.end());
    int t,ans=-1,up;
    cin>>t;
    for (int i=0; i<n; ++i){
        up=upper_bound(a.begin(),a.end(),a[i]+t)-a.begin();
        ans=max(ans,up-i);
    }
    cout<<ans;
}


#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <vector>
#include <set>
#include <algorithm>
#include <queue>
#define enter printf("\n")
#define pb push_back
#define ll long long
#define fors(it, r) for (set<int>::iterator it = r.begin(); it != r.end(); it++)
#define forvit(it, r) for (vector<int>::iterator it = r.begin(); it != r.end(); it++)
#define forv(i, vector) for (int i = 0; i < vector.size(); i++)
#define forn(i, n) for (int i = 0; i < n; i++)
#define forn1(i, n) for (int i = 1; i < n; i++)
#define fori(n) for (int i = 0; i < n; i++)
#define forj(n) for (int j = 0; j < n; j++)
#define vi vector<int>
#define vll vector<long long>
#define pii pair<int, int>

using namespace std;
int INF = 1000000007;
string FILENAME = "input";
string FILEINPUT = FILENAME;

void writeln(int a){printf("%d\n", a);}void writeln(int a, int b){printf("%d %d\n", a, b);}void writeln(int a, int b, int c){printf("%d %d %d\n", a, b, c);}void writeln(int a, int b, int c, int d){printf("%d %d %d %d\n", a, b, c, d);}void write(int a){printf("%d", a);}void write(int a, int b){printf("%d %d", a, b);}void write(int a, int b, int c){printf("%d %d %d", a, b, c);}void write(int a, int b, int c, int d){printf("%d %d %d %d", a, b, c, d);}void read(int &a){scanf("%d", &a);}void read(int &a, int &b){scanf("%d %d", &a, &b);}void read(int &a, int &b, int &c){scanf("%d %d %d", &a, &b, &c);}void read(int &a, int &b, int &c, int &d){scanf("%d %d %d %d", &a, &b, &c, &d);}void readln(int &a){scanf("%d\n", &a);}void readln(int &a, int &b){scanf("%d %d\n", &a, &b);}void readln(int &a, int &b, int &c){scanf("%d %d %d\n", &a, &b, &c);}void readln(int &a, int &b, int &c, int &d){scanf("%d %d %d %d\n", &a, &b, &c, &d);}
void readln(vector<int> &f, int n){int x;for (int i = 1; i <= n; i++){read(x);f.push_back(x);}}void writeln(vector<int> &f){for (int i = 0; i < f.size(); i++)printf("%d%c", f[i], i == f.size() - 1 ? '\n' : ' ');}

void run()
{
    int n;
    vi a;
    read(n);
    readln(a, n);
    sort(a.begin(), a.end());
    int i = -1, j = 0, t, mx = 0;
    read(t);
    while (j != n)
    {
        i++;
        while (j < n && a[j] - a[i] <= t)
            j++;
        mx = max(mx, j - i);
    }
    writeln(mx);
}

int main()
{
    //freopen(FILEINPUT.append(".in").c_str(), "r", stdin);
    //freopen(FILENAME.append(".out").c_str(), "w", stdout);
    run();
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <cstdio>
#include <stdlib.h>
#include <stack>
#include <numeric>
#include <bitset>
#include <cctype>
#include <list>
#include <assert.h>
#include <random>
typedef long long ll;
#define MAX 10005
using namespace std;
#define INF 2147483647
#define infL (1LL<<60)
#define inf (1e8)
#define MOD 1000000007
#define EPS 1e-9
#define Gr 9.8
#define PI acos(-1)
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define REQ(i,n) for(int (i)=1;(i)<=(int)(n);(i)++)
#define lch (rt<<1)
#define rch (rt<<1|1)
bool debug = 1;
int n,m, k;
int dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0};
ll ln, lk, lm;

int t[MAX];
int main(){
    scanf("%d", &n);
    REQ(i,n) {
        scanf("%d", &m); t[m]++;
    }
    scanf("%d", &k);
    REQ(i,3000) t[i]+=t[i-1];
    int ans = 0;
    for(int i=1;i+k<=5000;i++) {
        ans = max(ans, t[i+k]-t[i-1]);
    }
    printf("%d\n", ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define LET(x, a)  __typeof(a) x(a)
#define TR(v, it) for(LET(it, v.begin()); it != v.end(); it++)
#define si(x) scanf("%d",&x)
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define INF 1000000000
#define MOD 1000000007
#define SET(x,y) memset(x,y,sizeof(x));
#define LL long long int
#define ULL unsigned LL
#define PII pair<int, int>

class Matrix {
    public:
    int m[31][31];
    int N;
    Matrix(int N) {
        this->N = N;
        memset(m, 0, sizeof(m));
        for (int i = 0; i < N; i++)
            m[i][i] = 1;
    }
    Matrix* operator *(Matrix b);
} *M[71][31];
Matrix* Matrix::operator *(Matrix b) {
    int i, j, k;
    Matrix* ret = new Matrix(N);
    for (i = 0; i < N; i++) {
        for (j = 0; j < N; j++) {
            LL cans = 0;
            for (k = 0; k < N; k++) {
                cans += m[i][k] * ((LL)b.m[k][j]);
                cans %= MOD;
            }
            ret->m[i][j] = cans;
        }
    }
    return ret;
}

LL kp[100];
int main() {
    int i, j, k, l;
    LL n;
    cin >> n >> k;
    for (i = 0; i < k; i++) {
        M[0][i] = new Matrix(k + 1);
        for (j = 0; j <= k; j++) {
            M[0][i]->m[i][j] = 1;
        }
    }
    kp[0] = 1;
    kp[1] = k;
    int lg = 2;
    while (kp[lg - 1] <= n / k) {
        kp[lg] = kp[lg - 1] * k;
        lg++;
    }
    for (i = 1; i < lg; i++) {
        M[i][0] = new Matrix(k + 1);
        for (j = 1; j < k; j++) {
            M[i][j] = *M[i - 1][j - 1] * *M[i][j - 1];
        }
        Matrix *cpro = new Matrix(k + 1);
        for (j = k - 1; j >= 0; j--) {
            cpro = *cpro * *M[i - 1][j];
            M[i][j] = *M[i][j] * *cpro;
        }
    }
    Matrix *ans = new Matrix(k + 1);
    int cc = 0;
    for (i = lg - 1; i >= 0; i--) {
        while (kp[i] <= n) {
            n -= kp[i];
            ans = *M[i][cc] * *ans;
            cc++;
            cc %= k;
        }
    }
    LL ret = 0;
    for (i = 0; i <= k; i++) {
        ret += ans->m[i][k];
        ret %= MOD;
    }
    cout << ret << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define pb push_back

const int maxn = 32;
const int maxb = 60;
const int mod = 1e9 + 7;
const ll inf = 2e18;

inline void mkay(int &a)
{
	if(a >= mod)
		a -= mod;
	else if(a < 0)
		a += mod;
}

struct matrix
{
	int val[maxn][maxn];
	int n;
	matrix(int nn=0, int x = 0)
	{
		n = nn;
		for (int i=0; i<n; i++)
			for (int j=0; j<n; j++)
				if (i != j)
					val[i][j] = 0;
				else
					val[i][j] = x;
	}
	int* operator[] (int x)
	{
		return val[x];
	}
	matrix operator * (matrix b)
	{
		matrix ans(n, 0);
		for(int i=0; i<n; i++)
			for(int j=0; j<n; j++)
				for(int k=0; k<n; k++)
					mkay(ans[i][k] += 1LL * val[i][j] * b[j][k] % mod);
		return ans;
	}
	matrix operator + (matrix b)
	{
		matrix ans(n , 0);
		for(int i = 0; i < n; i++)
			for(int j = 0; j < n; j++)
				ans[i][j] = (val[i][j] + b[i][j]);
		return ans;
	}
	bool operator == (matrix b)
	{
		if(n != b.n)
			return 0;

		for(int i = 0; i < n; i++)
			for(int j = 0; j < n; j++)
				if(val[i][j] != b[i][j])
					return 0;
		return 1;
	}
};

matrix operator ^ (matrix a, ll b)
{
	matrix ans(a.n, 1);
	while(b)
	{
		if (b & 1)
			ans = ans * a;
		a = a * a;
		b >>= 1;
	}
	return ans;
}

int k;

matrix a[maxb] , shift[maxb];

ll val[maxb];

matrix get(ll n , int ind)
{
	if(!n)
		return matrix(k + 1 , 1);

	for(int i = ind; i >= 0; i--)
		if(n >= val[i])
		{
			matrix tmp = get(n % val[i] , i - 1);
			int x = n / val[i];

			matrix ans = a[i];
			for(int j = 1; j < x; j++)
			{
				ans = ans * shift[k - 1];
				ans = ans * a[i];
			}
			ans = ans * shift[k - 1];
			ans = ans * tmp;
			ans = ans * shift[x - 1];
			return ans;
		}

	cout << 1/0 << endl;
	return matrix(k + 1 , -800);
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	ll n;
	cin >> n >> k;

	val[0] = 1;

	int last = maxb;
	for(int i = 1; i < maxb; i++)
	{
		if(val[i - 1] >= inf / k)
		{
		    val[i] = inf;
		    last = i;
		    break;
		}
		else
			val[i] = val[i - 1] * k;
	}

	shift[0] = matrix(k + 1 , 1);
	shift[1] = matrix(k + 1);

	for(int i = 0; i < k; i++)
		shift[1][i][(i - 1 + k) % k] = 1;
	shift[1][k][k] = 1;

	for(int i = 2; i < k; i++)
		shift[i] = shift[i - 1] * shift[1];

	for(int i = 0; i < maxb; i++)
		a[i] = matrix(k + 1 , 1);

	for(int i = 0; i <= k; i++)
		a[0][i][0] = 1;

	for(int i = 1; i < last; i++)
	{
		matrix tmp = a[i - 1];

		a[i] = a[i - 1];

		for(int j = 1; j < k; j++)
		{
			a[i] = a[i] * shift[k - 1];
			a[i] = a[i] * tmp;
		}

		a[i] = a[i] * shift[k - 1];
	}

	matrix ans(k + 1);
	ans[0][k] = 1;

	ans = ans * get(n , last - 1);

	int res = 0;
	for(int i = 0; i <= k; i++)
		mkay(res += ans[0][i]);

	cout << res << endl;
}


#include <bits/stdc++.h>

using namespace std;

template<class T, class U>
void ckmin(T &a, U b)
{
	if (a > b) a = b;
}
template<class T, class U>
void ckmax(T &a, U b)
{
	if (a < b) a = b;
}

#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
#define SZ(x) ((int) ((x).size()))
#define ALL(x) (x).begin(), (x).end()
#define INF 1000000007
#define LLINF 2696969696969696969ll

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
typedef vector<vl> mat;

ll N, K;
vi num;
mat trans;
mat base[60][30], pref[60][30], suf[60][30];
ll ans;

mat mat_mul(mat a, mat b)
{
	mat res(K + 1);
	FOR(i, 0, K + 1) res[i].resize(K + 1);
	FOR(i, 0, K + 1)
	{
		FOR(j, 0, K + 1)
		{
			res[i][j] = 0;
			FOR(k, 0, K + 1)
			{
				res[i][j] += a[i][k] * b[k][j];
				res[i][j] %= INF;
			}
		}
	}
	return res;
}
mat shift(mat a, int x)
{
	mat res(K + 1);
	FOR(i, 0, K + 1) res[i].resize(K + 1);
	FOR(i, 0, K + 1)
	{
		FOR(j, 0, K + 1)
		{
			if (a[i][j] == 0) continue;
			if (i == j) res[i][j] = a[i][j];
			else res[i][(j + x) % K] = a[i][j];
		}
	}
	return res;
}

int32_t main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin >> N >> K;
	while(N)
	{
		num.PB(N % K);
		N /= K;
	}
	// for (int x : num)
	// {
	// 	cerr << x << ' ';
	// }
	// cerr << endl;
	FOR(i, 0, K)
	{
		base[0][i].resize(K + 1);
		FOR(j, 0, K + 1) base[0][i][j].resize(K + 1);
		FOR(j, 0, K + 1)
		{
			base[0][i][j][j] = 1;
		}
		FOR(j, 0, K + 1)
		{
			base[0][i][j][i] = 1;
		}
	}
	FOR(i, 1, SZ(num))
	{
		pref[i - 1][0] = base[i - 1][0];
		FOR(j, 1, K)
		{
			pref[i - 1][j] = mat_mul(pref[i - 1][j - 1], base[i - 1][j]);
		}
		suf[i - 1][K - 1] = base[i - 1][K - 1];
		FORD(j, K - 1, 0)
		{
			suf[i - 1][j] = mat_mul(base[i - 1][j], suf[i - 1][j + 1]);
		}
		base[i][0] = pref[i - 1][K - 1];
		FOR(j, 1, K)
		{
			base[i][j] = mat_mul(suf[i - 1][j], pref[i - 1][j - 1]);
		}
	}
	trans.resize(K + 1);
	FOR(i, 0, K + 1) trans[i].resize(K + 1);
	FOR(i, 0, K + 1)
	{
		FOR(j, 0, K + 1)
		{
			trans[i][j] = 0;
		}
		trans[i][i] = 1;
	}
	int sum = 0;
	FORD(i, SZ(num), 0)
	{
		int x = num[i];
		//multiply by
		FOR(j, 0, x)
		{
			trans = mat_mul(trans, base[i][sum]);
			sum++; if (sum >= K) sum -= K;
		}
	}
	ans = 1;
	FOR(i, 0, K)
	{
		ans += trans[K][i];
		ans %= INF;
	}
	cout << ans << '\n';
	return 0;
	//base[i] tells you the transition matrix for 0....k^i - 1
	//trans has the transition matrix for 1!
}


#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<bitset>
#include<utility>
#include<functional>
#include<iomanip>
#include<sstream>
#include<ctime>
#include<cassert>
using namespace std;
#define y0 y0z
#define y1 y1z
#define yn ynz
#define j0 j0z
#define j1 j1z
#define jn jnz
#define tm tmz
#define buli(x) (__builtin_popcountll(x))
#define bur0(x) (__builtin_ctzll(x))
#define bul2(x) (63-__builtin_clzll(x))
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define fil(a,b) memset((a),(b),sizeof(a))
#define cl(a) fil(a,0)
#define siz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define rep(i,a,b) for (int i=(a),_ed=(b);i<_ed;i++)
#define per(i,a,b) for (int i=(b)-1,_ed=(a);i>=_ed;i--)
#define pw(x) ((ll(1))<<(x))
#define upmo(a,b) (((a)=((a)+(b))%mo)<0?(a)+=mo:(a))
#define mmo(a,b) (((a)=1ll*(a)*(b)%mo)<0?(a)+=mo:(a))
void getre(){int x=0;printf("%d\n",1/x);}
void gettle(){int res=1;while(1)res<<=1;printf("%d\n",res);}
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
template<typename T,typename S>inline bool upmin(T&a,const S&b){return a>b?a=b,1:0;}
template<typename T,typename S>inline bool upmax(T&a,const S&b){return a<b?a=b,1:0;}
template<typename N,typename PN>inline N flo(N a,PN b){return a>=0?a/b:-((-a-1)/b)-1;}
template<typename N,typename PN>inline N cei(N a,PN b){return a>0?(a-1)/b+1:-(-a/b);}
template<typename N>N gcd(N a,N b){return b?gcd(b,a%b):a;}
template<typename N>inline int sgn(N a){return a>0?1:(a<0?-1:0);}
#if ( ( _WIN32 || __WIN32__ ) && __cplusplus < 201103L)
    #define lld "%I64d"
#else
    #define lld "%lld"
#endif
inline void gn(long long&x){
	int sg=1;char c;while(((c=getchar())<'0'||c>'9')&&c!='-');c=='-'?(sg=-1,x=0):(x=c-'0');
	while((c=getchar())>='0'&&c<='9')x=x*10+c-'0';x*=sg;
}
inline void gn(int&x){long long t;gn(t);x=t;}
inline void gn(unsigned long long&x){long long t;gn(t);x=t;}
inline void gn(double&x){double t;scanf("%lf",&t);x=t;}
inline void gn(long double&x){double t;scanf("%lf",&t);x=t;}
inline void gs(char *s){scanf("%s",s);}
inline void gc(char &c){while((c=getchar())>126 || c<33);}
inline void pc(char c){putchar(c);}
#ifdef JCVB
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif
typedef long long ll;
typedef double db;
inline ll sqr(ll a){return a*a;}
inline db sqrf(db a){return a*a;}
const int inf=0x3f3f3f3f;
const db pi=3.14159265358979323846264338327950288L;
const db eps=1e-6;
const int mo=1e9+7;
int qp(int a,ll b){int n=1;do{if(b&1)n=1ll*n*a%mo;a=1ll*a*a%mo;}while(b>>=1);return n;}

ll n;
int k;

// mo < 2^30 = 1073741824
// first call mm_init(sz);
// indices from 0 to mm_n - 1
// call a.plus(),b.plus() before a*b

const int MM_N =33;

int mm_n;
struct mat{
	int a[MM_N][MM_N];
	void plus(){
		rep(i,0,mm_n)rep(j,0,mm_n)a[i][j]=(a[i][j]%mo+mo)%mo;
	}
}one;
mat operator*(const mat&a,const mat&b){
	static ll ans[MM_N][MM_N];
	cl(ans);
	ll infa=8e18;
	rep(k,0,mm_n)rep(i,0,mm_n)rep(j,0,mm_n){
		if((ans[i][j]+=1ll*a.a[i][k]*b.a[k][j])>=infa)ans[i][j]%=mo;
	}
	mat c;
	rep(i,0,mm_n)rep(j,0,mm_n)c.a[i][j]=ans[i][j]%mo;
	return c;
}
mat qp(mat a,ll b){a.plus();mat n=one;do{if(b&1)n=n*a;a=a*a;}while(b>>=1);return n;}
void mm_init(int sz){
	mm_n=sz;
	rep(i,0,mm_n)one.a[i][i]=1;
}

int nu[111];int tot=0;

mat a[100];

mat jia(const mat&x,int d){
	mat ret;
	rep(i,0,k+1)rep(j,0,k+1)ret.a[i!=k?(i+d)%k:k][j!=k?(j+d)%k:k]=x.a[i][j];
	return ret;
}
int main()
{
#ifdef JCVB
	//freopen("1.in","r",stdin);
	//freopen("1.out","w",stdout);
	int _time_jc=clock();
#endif
	gn(n);gn(k);
	mm_init(k+1);

	while(n){
		nu[tot++]=n%k;
		n/=k;
	}
	for (int j=0;j<=k;j++)a[0].a[0][j]=1;
	for (int j=0;j<=k;j++)a[0].a[j][j]=1;
	for (int i=1;i<tot;i++){
		mat now=one;
		rep(j,0,k)now=now*jia(a[i-1],j);
		a[i]=now;
	}
	int ad=0;
	mat ans=one;
	for (int i=tot-1;i>=0;i--){
		for (int j=0;j<nu[i];j++)
			ans=ans*jia(a[i],(j+ad)%k);
		ad=(ad+nu[i])%k;
	}

	int ret=0;
	rep(i,0,k+1)upmo(ret,ans.a[i][k]);
	printf("%d\n",ret);
#ifdef JCVB
	debug("time: %d\n",int(clock()-_time_jc));
#endif
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MOD = 1e9+7;
#define LL long long

struct matrix{
	int A[31][31],N;

	matrix operator *(const matrix &B)const{
		matrix C; C.N = N;
		for(int i = 0 ; i <= N ; ++i)
			for(int j = 0 ; j <= N ; ++j){
				C.A[i][j] = 0;
				for(int k = 0 ; k <= N ; ++k)
					C.A[i][j] = ((LL)A[i][k]*B.A[k][j] + C.A[i][j])%MOD;
			}
		return C;
	}
}A[64][30],sum[64][30],suf[64][30],ans;

LL N,K,dig[70],cnt;

int main(){
	scanf("%lld%lld",&N,&K);
	while(N){dig[cnt++] = N % K; N /= K;}
	for(int i = 0 ; i < K ; ++i){
		for(int j = 0 ; j <= K ; ++j)
			A[0][i].A[j][j] = 1,A[0][i].A[j][i] = 1;
		A[0][i].N = K;
	}
	sum[0][0] = A[0][0]; suf[0][K-1] = A[0][K-1];
	for(int i = 1 ; i < K ; ++i) sum[0][i] = sum[0][i-1]*A[0][i];
	for(int i = K - 2; ~i ; --i) suf[0][i] = A[0][i]*suf[0][i+1];
	for(int i = 1 ; i < cnt ; ++i){
		A[i][0] = suf[i-1][0];
		for(int j = 1 ; j < K ; ++j)
			A[i][j] = suf[i-1][j]*sum[i-1][j-1];
		sum[i][0] = A[i][0]; suf[i][K-1] = A[i][K-1];
		for(int j = 1 ; j < K ; ++j) sum[i][j] = sum[i][j-1]*A[i][j];
		for(int j = K - 2 ; ~j; --j) suf[i][j] = A[i][j]*suf[i][j+1];
	}
	ans.A[0][K] = 1; ans.N = K;
	ans = ans*sum[cnt-1][dig[cnt-1] - 1];
	int last = dig[cnt-1];
	for(int i = cnt - 2 ; ~i ; --i){
		for(int j = 0 ; j < dig[i] ; ++j)
			ans = ans*A[i][(j+last)%K];
		last += dig[i];
	}
	LL res = 0;
	for(int i = 0 ; i <= K ; ++i) res = res + ans.A[0][i];
	printf("%lld\n",res%MOD);
}


#include <bits/stdc++.h>
using namespace std;

const int N = 100, D = 31, MOD = 1E9 + 7;

int d, cur, a[N];
long long n;

struct matrix {
    long long f[D][D];

    matrix() {
        for (int i = 0; i <= d; i++) {
            for (int j = 0; j <= d; j++) {
                f[i][j] = 0;
            }
        }
    }

    matrix operator*(const matrix& oth) const {
        matrix ans;
        for (int i = 0; i <= d; i++) {
            for (int j = 0; j <= d; j++) {
                for (int k = 0; k <= d; k++) {
                    (ans.f[i][j] += f[i][k] * oth.f[k][j]) %= MOD;
                }
            }
        }
        return ans;
    }
} bas[N];

matrix rotate(int u) {
    (u += d) %= d;
    matrix ans;
    for (int i = 0; i < d; i++) {
        ans.f[i][(i + u) % d] = 1;
    }
    ans.f[d][d] = 1;
    return ans;
}

void init() {
    for (int i = 1; i < d; i++) {
        bas[0].f[i][i] = 1;
    }
    bas[0].f[d][0] = 1;
    bas[0].f[d][d] = 2; bas[0].f[0][d] = -1;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n >> d;
    for (cur = 0; n > 0; cur++, n /= d) {
        a[cur] = n % d;
    }
    init();
    for (int i = 1; i < cur; i++) {
        bas[i] = rotate(0);
        for (int j = 0; j < d; j++) {
            bas[i] = bas[i] * bas[i - 1] * rotate(1);
        }
    }
    matrix ans = rotate(0);
    for (int i = cur - 1; i >= 0; i--) {
        for (int j = 0; j < a[i]; j++) {
            ans = ans * bas[i] * rotate(1);
        }
    }
    cout << (ans.f[d][d] + MOD) % MOD;
}


//                             In The Name Of Allah
//                             	Mohammad Hosseini
#include <bits/stdc++.h>
#define	ss second
#define ff first
#define use_fast ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define ret(n) return cout << n, 0
#define se(n) cout << setprecision(n) << fixed
#define pb push_back
#define int long long
#define ld long double
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
#pragma GCC optimize("no-stack-protector,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;

const int N = 1e6 + 100, OO = 1e9 + 7, M = 1e9 + 7, P = 6151, T = 2e3, SQ = 280, lg = 64, lg2 = 34;
typedef pair <int, int> pii;
int n, d;
vector <int> v;

struct node {
	int dp[lg2][lg2], cnt[lg2][lg2];
	bool mark[lg2], tark[lg2];
	void mrg(node &nd) {
		for(int i = 0; i <= d; i++) {
			for(int j = 0; j <= d; j++) {
				cnt[i][j] = 0;
				for(int k = 0; k <= d; k++)
					cnt[i][j] = (cnt[i][j] + dp[k][j] * nd.dp[i][k]) % M;
			}
		}
		for(int i = 0; i < d; i++) {
			if(mark[i] == true && nd.mark[i] == false) {
				for(int j = 0; j <= d; j++)
					cnt[i][j] = dp[i][j];
			}
			if(mark[i] == false && nd.mark[i] == true) {
				for(int j = 0; j <= d; j++)
					cnt[j][i] = nd.dp[j][i];
			}
			mark[i] |= nd.mark[i];
		}
		for(int i = 0; i <= d; i++)
			for(int j = 0; j <= d; j++)
				dp[i][j] = cnt[i][j];
	}
	void clean() {
		for(int i = 0; i <= d; i++)
			for(int j = 0; j <= d; j++)
				dp[i][j] = cnt[i][j] = 0;
	}
	void shift(int x) {
		for(int i = 0; i <= d; i++)
			for(int j = 0; j <= d; j++)
				cnt[i][j] = 0;
		for(int i = 0; i < d; i++) {
			for(int j = 0; j < d; j++)
				cnt[(i + x) % d][(j + x) % d] = dp[i][j];
			cnt[(i + x) % d][d] = dp[i][d];
		}
		for(int i = 0; i < d; i++)
			cnt[d][(i + x) % d] = dp[d][i];
		cnt[d][d] = dp[d][d];
		for(int i = 0; i <= d; i++)
			for(int j = 0; j <= d; j++)
				dp[i][j] = cnt[i][j];
		for(int i = 0; i < d; i++)
			tark[i] = mark[(i + x) % d];
		for(int i = 0; i < d; i++)
			mark[i] = tark[i];
	}
} md[lg + 5], hp[lg + 5], ht;

node get(int x) {
	int h = x, pw = 1, t = -1;
	while(h >= d)
		h /= d, pw *= d, t++;
	x %= pw;
	if(t == -1)
		return hp[h];
	node ans = md[t];
	ht = md[t];
	for(int i = 1; i < h; i++) {
		ht.shift(1);
		ans.mrg(ht);
	}
	ht = get(x);
	ht.shift(h);
	ans.mrg(ht);
	return ans;
}

int32_t main() {
    use_fast;
	cin >> n >> d;
	n--;
	int m = n;
	while(n) {
		v.pb(n % d);
		n /= d;
	}
	hp[0].dp[d][d] = 2;
	hp[0].dp[d][0] = -1;
	hp[0].dp[0][d] = 1;
	hp[0].mark[0] = true;
	md[0] = hp[0];
	for(int i = 1; i < d; i++) {
		hp[i].dp[d][d] = 2;
		hp[i].dp[d][i] = -1;
		hp[i].dp[i][d] = 1;
		hp[i].mark[i] = true;
		md[0].mrg(hp[i]);
		hp[i] = md[0];
	}
	for(int i = 1; i <= (int)v.size(); i++) {
		md[i] = md[i - 1];
		ht = md[i - 1];
		for(int j = 1; j < d; j++) {
			ht.shift(1);
			md[i].mrg(ht);
		}
	}
	node ans = get(m);
	cout << ans.dp[d][d] << endl;
	return 0;
}
/*
be carefull :
1- if not solve after 20 min, read again twice
2- after submit read the code again
3- fun with contest
4- uploaded by ubuntu 20.04
5- ...
*/


//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <cassert>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <unordered_set>
#include <unordered_map>
#define  RD(x)      scanf("%d", &x)
#define  REP(i, n)  for (int i=0; i<int(n); i++)
#define  FOR(i, n)  for (int i=1; i<=int(n); i++)
#define  pii        pair<int, int>
#define  piL        pair<int, long long>
#define  mp         make_pair
#define  pb         push_back
inline   int read(){int ret; scanf("%d", &ret); return ret;}
int dx8[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int dy8[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int dx4[4] = {1, 0, -1, 0};
int dy4[4] = {0, 1, 0, -1};

using namespace std;
#define  N   33
#define  eps 1e-8
#define  pi  acos(-1.0)
#define  inf 0XFFFFFFFll
#define  mod 1000000007ll
#define  LL  long long
#define  ULL unsigned long long

inline void add(LL &x, LL y) {
	x = (x + y) % mod;
}

int r;

struct Matrix {
	LL a[N][N];
	void toZero() {
		memset(a, 0, sizeof a);
	}
	void toE() {
		toZero();
		REP(i, r)
			a[i][i] = 1;
	}
	friend Matrix operator * (const Matrix &a, const Matrix &b) {
		Matrix ret; ret.toZero();
		REP(i, r) REP(j, r) REP(k, r)
				add(ret.a[i][j], a.a[i][k] * b.a[k][j]);
		return ret;
	}
};

LL n, K;
Matrix one[70];
Matrix two[70];
Matrix rotr[70], rotc[70];

int Main() {
    ios_base::sync_with_stdio(0);

    cin >> n >> K;
    r = K + 1;

    // 0 - > single 1
    // i - > i - 1
    REP(i, K+1) {
    	one[i].toE();
    	if (i)
    		REP(j, K+1)
    			one[i].a[i][j] = 1;
    }

    REP(i, K+1) {
    	rotr[i].toE();
    	if (i == 1) {
    		rotr[i].toZero();
    		rotr[i].a[0][0] = 1;
    		rotr[i].a[1][K] = 1;
    		for (int j = 2; j <= K; j++)
    			rotr[i].a[j][j-1] = 1;
    	} else if (i > 1)
    		rotr[i] = rotr[1] * rotr[i-1];
    }

    REP(i, K+1) {
    	rotc[i].toE();
    	if (i == 1) {
    		rotc[i].toZero();
    		rotc[i].a[0][0] = 1;
    		rotc[i].a[K][1] = 1;
    		for (int j = 2; j <= K; j++)
    			rotc[i].a[j-1][j] = 1;
    	} else if (i > 1)
    		rotc[i] = rotc[i-1] * rotc[1];
    }

    two[1].toE();
    FOR(i, K)
    	two[1] = one[i] * two[1];

    int bit = 0;
    int num[70];
    while (n) {
    	num[bit++] = n % K;
    	n /= K;
    }

    for (int i = 2; i <= bit; i++) {
    	two[i].toE();
    	REP(j, K)
    		two[i] = rotr[j] * two[i-1] * rotc[j] * two[i];
    }

    reverse(num, num + bit);

    Matrix ret; ret.toE();

    int ps = 0;
    REP(i, bit) {
    	if (i == bit - 1)
    		REP(j, num[i])
    			ret = one[ps%K + 1] * ret, ps++;
    	else
    		REP(j, num[i])
    			ret = rotr[ps%K] * two[bit-i-1] * rotc[ps%K] * ret, ps++;
    }

    LL res = 1;
    FOR(i, K)
    	add(res, ret.a[i][0]);

    cout << res << endl;

    return 0;
}

int main() {
    return Main();
}


#include<cstdio>
#include<algorithm>
#include<cstring>
#define LL long long
using namespace std;
const int N=65;
const int mod=1e9+7;
int m,len;
LL n,v[N];
struct node
{
	int v[35][35];
	node(){memset(v,0,sizeof(v));}
}S,T[N][35],s1[N][35],s2[N][35];
node operator * (node a,node b)
{
	node c;
	for(int i=0;i<=m;i++)
		for(int j=0;j<=m;j++)
			for(int k=0;k<=m;k++)
				c.v[i][j]=(c.v[i][j]+1ll*a.v[i][k]*b.v[k][j]%mod)%mod;
	return c;
}
int main()
{
	scanf("%lld%d",&n,&m);
	v[0]=n;while(v[len])v[len+1]=v[len]/m,v[len]%=m,len++;
	for(int i=0;i<len;i++)
	{
		if(!i)
		{
			for(int j=0;j<m;j++)
			{
				T[i][j].v[m][m]=1;
				for(int a=0;a<m;a++)
				{
					if(a!=j){T[i][j].v[a][a]=1;continue;}
					for(int b=0;b<=m;b++)T[i][j].v[a][b]=1;
				}
			}
		}
		else
		{
			for(int j=0;j<m;j++)
				if(!j)T[i][j]=s2[i-1][0];
				else T[i][j]=s2[i-1][j]*s1[i-1][j-1];
		}
		s1[i][0]=T[i][0];
		for(int j=1;j<m;j++)s1[i][j]=s1[i][j-1]*T[i][j];
		s2[i][m-1]=T[i][m-1];
		for(int j=m-2;j>=0;j--)s2[i][j]=T[i][j]*s2[i][j+1];
	}
	for(int i=0;i<=m;i++)S.v[0][i]=1;
	for(int i=len-1,j=0;i>=0;i--)
		while(v[i]--)S=S*T[i][j],j=(j+1)%m;
	printf("%d",S.v[0][m]);
	return 0;
}


#include <bits/stdc++.h>
#define MOD 1000000007LL

using namespace std;

struct vec {
  long long a[30];
};

struct mat {
  long long a[30][30];
};

long long n, k;

int val (long long um) {
  if (um < k) return um;
  return (um + val (um / k)) % k;
}

mat M[30], MI[30];
mat E[200][40], EI[200][40];
vec cur;

mat mult (mat m1, mat m2) {
  mat m3;
  for (int i = 0; i < k; i++) {
    for (int j = 0; j < k; j++) {
      m3.a[i][j] = 0;
      for (int l = 0; l < k; l++) {
    m3.a[i][j] += (m1.a[i][l] * m2.a[l][j]) % MOD;
      }
      m3.a[i][j] %= MOD;
    }
  }
  return m3;
}

vec mult (mat m, vec v) {
  vec v2;
  for (int i = 0; i < k; i++) {
    v2.a[i] = 0;
    for (int j = 0; j < k; j++) {
      v2.a[i] += (v.a[j] * m.a[i][j]) % MOD;
    }
    v2.a[i] %= MOD;
  }
  return v2;
}

void pre_comp() {
  for (int i = 0; i < k; i++) {
    E[0][i] = M[i];
    EI[0][i] = MI[i];
  }
  int LOG = 0;
  long long t = n;
  while (t > 0) {
    LOG++;
    t /= k;
  }
  for (int j = 1; j <= LOG; j++) {
    E[j][0] = E[j - 1][0];
    EI[j][0] = EI[j - 1][0];
    for (int l = 1; l < k; l++) {
      E[j][0] = mult (E[j - 1][l], E[j][0]);
      EI[j][0] = mult (EI[j][0], EI[j - 1][l]);
    }
    for (int i = 1; i < k; i++) {
      E[j][i] = mult (E[j - 1][i - 1], mult (E[j][i - 1], EI[j - 1][i - 1]));
      EI[j][i] = mult (E[j - 1][i - 1], mult (EI[j][i - 1], EI[j - 1][i - 1]));
    }
  }
  /*  for (int j = 0; j <= LOG; j++) {
    for (int i = 0; i < k; i++) {
      printf ("E[%d][%d]:\n", j, i);
      for (int a = 0; a < k; a++) {
    for (int b = 0; b < k; b++) {
      printf ("%lld ", E[j][i].a[a][b]);
    }
    printf ("\n");
      }
      printf ("\n");
      printf ("EI[%d][%d]:\n", j, i);
      for (int a = 0; a < k; a++) {
    for (int b = 0; b < k; b++) {
      printf ("%lld ", EI[j][i].a[a][b]);
    }
    printf ("\n");
      }
      printf ("\n");
    }
    } */
}

vec solve (long long n, vec v, int at) {
  if (n == 0) return v;
  int w = val (n / k);
  v = solve (n / k, v, at + 1);
  for (int i = 0; i < n % k; i++) {
    v = mult (E[at][(w + i) % k], v);
  }
  return v;
}

int main() {
  scanf ("%lld %lld", &n, &k);
  for (int i = 0; i < k; i++) {
    cur.a[i] = 1;
    for (int j = 0; j < k; j++) {
      for (int l = 0; l < k; l++) {
    if (j == l) {
      M[i].a[j][l] = 1;
      MI[i].a[j][l] = 1;
    }
    else if (j == i) {
      M[i].a[j][l] = 1;
      MI[i].a[j][l] = -1;
    }
    else {
      M[i].a[j][l] = 0;
      MI[i].a[j][l] = 0;
    }
      }
    }
  }
  pre_comp();
  cur = solve (n, cur, 0);
  //  printf ("%lld %lld\n", cur.a[0], cur.a[1]);
  long long tot = 0;
  for (int i = 0; i < k; i++) {
    tot += cur.a[i];
  }
  tot--;
  tot %= MOD;
  if (tot < 0) tot += MOD;
  while (tot % (k - 1) != 0) {
    tot += MOD;
  }
  printf ("%lld\n", tot / (k - 1));
  // printf ("%lld\n", (1 << n) % MOD);
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int mod = (int)1e9 + 7;
int sum(int a, int b) {
    int s = a + b;
    if (s >= mod) s -= mod;
    return s;
}
int sub(int a, int b) {
    int s = a - b;
    if (s < 0) s += mod;
    return s;
}
int mult(int a, int b) {
    return (1LL * a * b) % mod;
}
ll n;
int k;
vector < vector < int > > matrix_mult(vector < vector < int > > a, vector < vector < int > > b) {
    vector < vector < int > > c(k + 1, vector < int >(k + 1, 0));
    for (int i = 0; i <= k; i++) {
        for (int j = 0; j <= k; j++) {
            for (int t = 0; t <= k; t++) {
                c[i][t] = sum(c[i][t], mult(a[i][j], b[j][t]));
            }
        }
    }
    return c;
}
vector < vector < int > > pref[100];
vector < vector < int > > shift_by_1(vector < vector < int > > mat) {
    vector < vector < int > > c(k + 1, vector < int >(k + 1, 0));
    for (int i = 0; i <= k; i++) {
        for (int j = 0; j <= k; j++) {
            int ni = i;
            if (ni < k) ni = (ni + k - 1) % k;
            int nj = j;
            if (nj < k) nj = (nj + k - 1) % k;
            c[i][j] = mat[ni][nj];
        }
    }
    return c;
}
void print(vector < vector < int > >& c) {
    for (auto t : c) {
        for (int v : t) cout << v << " ";
        cout << '\n';
    }
    cout << "------------" << endl;
}
void init() {
    ll digs = 0;
    ll cop = n;
    while (cop > 0) {
        cop /= k;
        digs++;
    }
    pref[0].resize(k + 1, vector < int >(k + 1, 0));
    for (int i = 1; i < k; i++) {
        pref[0][i][i] = 1;
    }
    pref[0][0][k] = 1;
    pref[0][k][0] = mod - 1;
    pref[0][k][k] = 2;
   // print(pref[0]);
   // print(pref[0]);
    //auto t = shift_by_1(pref[0]);
  //  print(t);
    //print(shift_by_1(pref[0]));
    for (int step = 0; step < digs; step++) {
        pref[step + 1] = pref[step];
        auto cur = pref[step];
        //if (step == 0) print(cur);
        for (int i = 1; i <= k - 1; i++) {
            cur = shift_by_1(cur);
            //if (step == 0) print(cur);
            pref[step + 1] = matrix_mult(pref[step + 1], cur);
        }
    }
    // digs =
}
vector < vector < int > > solve(ll n) {
    //cout << n << endl;
    if (n == 0) {
        vector < vector < int > > c(k + 1, vector < int >(k + 1, 0));
        for (int i = 0; i <= k; i++) {
            c[i][i] = 1;
        }
        return c;
    }
    ll dig = 0;
    ll pw = 1;
    while (pw <= n / k) {
        pw *= k;
        dig++;
    }
    if (n == pw) return pref[dig];
    ll steps = n / pw;
    ll left = n - steps * pw;
    auto cur_mat = solve(0);
    auto cur_coef = pref[dig];
    for (int i = 0; i < steps; i++) {
        cur_mat = matrix_mult(cur_mat, cur_coef);
        cur_coef = shift_by_1(cur_coef);
    }
    auto other_mat = solve(left);
    for (int i = 0; i < steps; i++) {
        other_mat = shift_by_1(other_mat);
    }
    //print(cur_mat);
   // print(other_mat);
    return matrix_mult(cur_mat, other_mat);
}
int main() {
    //freopen("input.txt", "r", stdin);
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    srand(239);
    cin >> n >> k;
    init();
    auto mat = solve(n);
    cout << mat[k][k];
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cassert>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <stack>
#include <queue>
#include <deque>
#include <complex>

using namespace std;

#define pb push_back
#define mp make_pair
#define sz(s) int((s).size())
#define len(s) int((s).size())
#define all(s) (s).begin(), (s).end()
#ifdef _WIN32
#define LLD "%I64d"
#else
#define LLD "%lld"
#endif
#ifdef LOCAL42
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...) 42
#endif
#define y0 yy0
#define y1 yy1
#define next _next
#define prev _prev
#define rank _rank
#define link _link
#define hash _hash
#define fs first
#define sc second

typedef long long ll;
typedef long long llong;
typedef long long int64;
typedef unsigned int uint;
typedef long double ld;
typedef unsigned long long ull;
typedef unsigned long long ullong;
typedef unsigned long long lint;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int inf = int(1e9);
const double eps = 1e-9;
const double pi = 4 * atan(double(1));
const int K = 31;
const int MOD = int(1e9) + 7;

int k = 0;

struct matr {

    int a[K][K];

    matr() {
        for (int i = 0; i <= k; ++i) {
            for (int j = 0; j <= k; ++j) {
                a[i][j] = 0;
            }
            a[i][i] = 1;
        }
    }

};

matr operator * (const matr &a, const matr &b) {
    matr c;
    for (int i = 0; i <= k; ++i) {
        for (int j = 0; j <= k; ++j) {
            c.a[i][j] = 0;
            for (int z = 0; z <= k; ++z) {
                c.a[i][j] = (c.a[i][j] + 1LL * a.a[i][z] * b.a[z][j]) % MOD;
            }
        }
    }
    return c;
}

matr M[K], NM[K], suff[K], pref[K];

inline matr get(int num) {
    matr res;
    for (int i = 0; i <= k; ++i) {
        res.a[num][i] = 1;
    }
    return res;
}

int main() {
#ifdef LOCAL42
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#else

#endif
    ll n;
    cin >> n >> k;
    vi v;
    while (n > 0) {
        v.pb(n % k);
        n /= k;
    }
    matr res;
    int shift = 0;
    for (int i = 0; i < sz(v); ++i) {
        shift += v[i];
    }
    for (int i = 0; i < k; ++i) {
        M[i] = matr();
        pref[i] = matr();
        suff[i] = matr();
    }
    for (int i = 0; i < sz(v); ++i) {
        shift -= v[i];
        for (int j = v[i] - 1; j >= 0; --j) {
            if (i == 0) {
                res = get((shift + j) % k) * res;
                continue;
            }
            res = M[(shift + j) % k] * res;
        }
        if (i == 0) {
            for (int i = 0; i < k; ++i) {
                M[i] = matr();
                for (int j = k - 1; j >= 0; --j) {
                    M[i] = get((i + j) % k) * M[i];
                }
            }
        } else {
            for (int i = 0; i < k; ++i) {
                M[i] = matr();
                if (i > 0) {
                    M[i] = pref[i - 1] * M[i];
                }
                M[i] = suff[i] * M[i];
            }
        }
        for (int i = 0; i < k; ++i) {
            pref[i] = (i > 0 ? pref[i - 1] : matr()) * M[i];
        }
        for (int i = k - 1; i >= 0; --i) {
            suff[i] = M[i] * (i + 1 < k ? suff[i + 1] : matr());
        }
    }
    int ans = 0;
    for (int i = 0; i <= k; ++i) {
        ans = (ans + res.a[i][k]) % MOD;
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define sz(v) ((int)(v).size())
typedef long long lld;

const int MOD = 1e9+7;
lld N; int K;

struct MAT{
  int m[31][31];
  MAT operator * (const MAT &ot)const{
    MAT ret = {0, };
    for (int i=0;i<=K;i++) {
      for (int j=0;j<=K;j++) {
        ret.m[i][j] = 0;
        for (int k=0;k<=K;k++)
          ret.m[i][j] = (ret.m[i][j]+(lld)m[i][k]*ot.m[k][j])%MOD;
      }
    } return ret;
  }
} A[65][30], P[65][30], S[65][30], res;

int main() {
  cin >> N >> K;
  vector <int> base_k;
  for (;N;N/=K) base_k.push_back(N%K);
  for (int i=0;i<K;i++){
    for (int j=0;j<=K;j++) A[0][i].m[j][j] = 1;
    for (int j=0;j<=K;j++) A[0][i].m[i][j] = 1;
  }
  for (int i=1;i<sz(base_k);i++){
    P[i-1][0] = A[i-1][0];
    for (int j=1;j<K;j++) P[i-1][j] = P[i-1][j-1]*A[i-1][j];
    S[i-1][K-1] = A[i-1][K-1];
    for (int j=K-1;j--;) S[i-1][j] = A[i-1][j]*S[i-1][j+1];
    for (int j=0;j<K;j++){
      A[i][j] = S[i-1][j];
      if (j) A[i][j] = A[i][j]*P[i-1][j-1];
    }
  }
  int x = 0;
  for (int i=0;i<=K;i++) res.m[i][i] = 1;
  for (int i=sz(base_k);i--;){
    for (int j=0;j<base_k[i];j++){
      res = res * A[i][x];
      x = (x+1)%K;
    }
  }
  int ans = 0;
  for (int i=0;i<=K;i++) ans = (ans+res.m[i][K])%MOD;
  cout << ans << endl;
}


#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <cassert>
#include <set>
#include <map>
#include <vector>
#include <iostream>
#include <utility>
#include <sstream>
#include <cmath>
#include <ctime>
#include <iostream>
#include <unordered_set>

#define clr(a) memset(a, 0, sizeof(a))
#define fill(a, b) memset(a, b, sizeof(a))

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int,int> pii;

#ifdef DBG1
    #define dbg(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#else
    #define dbg(...)
#endif

const ll P = ll(1e9) + 7;

struct Matrix {
    static const int N = 33;
    int n;
    ll a[N][N];

    Matrix(int n = 0, int k = 0) : n(n) {
        memset(a, 0, sizeof(a));
        for (int i = 0; i < n; ++i) {
            a[i][i] = k;
        }
    }

    Matrix operator * (const Matrix & m) const {
        assert(n == m.n);
        Matrix res(n);
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                for (int k = 0; k < n; ++k) {
                    (res.a[i][k] += a[i][j] * m.a[j][k]) %= P;
                }
            }
        }
        return res;
    }

    Matrix& operator *= (const Matrix & m) {
        return *this = *this * m;
    }

    Matrix pow(int x) const {
        Matrix res = Matrix(n, 1);
        Matrix a = *this;
        while (x) {
            if (x & 1) {
                res *= a;
            }
            a *= a;
            x /= 2;
        }
        return res;
    }

    void print() const {
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                dbg("%d ", int(a[i][j]));
            }
            dbg("\n");
        }
        dbg("\n");
    }

    ll* operator[](int index) {
        return a[index];
    }

    const ll* operator[](int index) const {
        return a[index];
    }
};

int main()
{
#ifdef DBG1
    freopen(".in", "r", stdin);
    freopen(".out", "w", stdout);
    freopen(".err", "w", stderr);
#endif

    ll n;
    int k;
    cin >> n >> k;
    if (n <= k) {
        cout << (1LL << n) % P << endl;
        return 0;
    }

    int logN = int(log(n + 0.0) / log(k + 0.0) + 2);

    Matrix shift(k + 1, 1);
    for (int j = 0; j < k; ++j) {
        shift[j][j] = 0;
        shift[(j + 1) % k][j] = 1;
    }
    vector <Matrix> block(logN);
    block[0] = Matrix(k + 1, 1);
    for (int i = 1; i <= k; ++i) {
        block[0][0][i] = 1;
    }
    for (int i = 1; i < logN; ++i) {
        block[i] = (block[i - 1] * shift).pow(k);
    }

    int i = 0;
    ll len = 1;
    while (n / len >= k) {
        i++;
        len *= k;
    }

    Matrix m(k + 1, 1);
    while (n) {
        if (len <= n) {
            int x = int(n / len);
            m *= (block[i] * shift).pow(x);
            n -= len * x;
        }
        --i;
        len /= k;
    }

    ll ans = 0;
    for (int i = 0; i <= k; ++i) {
        (ans += m[i][k]) %= P;
    }
    cout << ans << endl;

    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <cassert>

using namespace std;

typedef long long ll;

const int MOD = 1000000007;

typedef vector< vector< int > > mt;

vector< mt > dp;
mt tmp;
mt base;

mt
shift(const mt &a, int shift)
{
    int k = int(a.size()) - 1;
    // left shift instead of right
    shift = (k - shift) % k;
    mt res = a;
    for (int i = 0; i < k; ++i) {
        for (int j = 0; j < k; ++j) {
            res[i][j] = a[(i + shift) % k][(j + shift) % k];
        }
    }
    for (int j = 0; j < k; ++j) {
        res[k][j] = a[k][(j + shift) % k];
    }
    return res;
}

mt
operator *(const mt &a, const mt &b)
{
    static int aa[200][200], bb[200][200];
    int n = int(a.size()), k = int(a[0].size()), m = int(b[0].size());
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < k; ++j) {
            aa[i][j] = a[i][j];
        }
    }
    for (int i = 0; i < k; ++i) {
        for (int j = 0; j < m; ++j) {
            bb[j][i] = b[i][j];
        }
    }
    mt res(n, vector< int >(m, 0));
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            int tmp = 0;
            for (int l = 0; l < k; ++l) {
                tmp = (tmp + 1ll * aa[i][l] * bb[j][l]) % MOD;
            }
            res[i][j] = tmp;
        }
    }
    return res;
}

int
main()
{
    ll n;
    int k;
    cin >> n >> k;
    int max_depth = 0;
    {
        ll tmp = n;
        while (tmp != 0) {
            ++max_depth;
            tmp /= k;
        }
    }
    dp.resize(max_depth);
    // constructing matrices for dynamic programming
    { // matrices for initial level
        dp[0].assign(k + 1, vector< int >(k + 1, 0));
        for (int i = 0; i <= k; ++i) {
            for (int j = 0; j <= k; ++j) {
                dp[0][i][j] = (i == j || j == 0);
            }
        }
        mt e(k + 1, vector< int >(k + 1, 0));
        for (int i = 0; i <= k; ++i) {
            e[i][i] = 1;
        }
        for (int depth = 1; depth < max_depth; ++depth) {
            dp[depth] = e;
            mt bs = dp[depth - 1];
            int curx = k;
            for (int i = 0; (1 << i) <= curx; ++i) {
                if (curx & (1 << i)) {
                    dp[depth] = bs * shift(dp[depth], (1 << i));
                    curx ^= (1 << i);
                }
                bs = bs * shift(bs, (1 << i));
            }
        }
    }
    base.assign(1, vector< int >(k + 1, 0));
    base[0][k] = 1;
    int sl = 0;
    ll x = n;
    vector< int > vec;
    while (x != 0) {
        vec.push_back(x % k);
        x /= k;
    }
    int len = int(vec.size());
    for (int cur_depth = len - 1; cur_depth >= 0; --cur_depth) {
        int rest = vec[cur_depth];
        for (int it = 0; it < rest; ++it, sl = (sl + 1) % k) {
            base = base * shift(dp[cur_depth], sl);
        }
    }
    {
        int res = 0;
        for (int i = 0; i <= k; ++i) {
            res = (res + base[0][i]) % MOD;
        }
        cout << res << '\n';
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> PI;
typedef long long LL;
typedef double D;
#define FI first
#define SE second
#define MP make_pair
#define PB push_back
#define R(I,N) for(int I=0;I<N;I++)
#define F(I,A,B) for(int I=A;I<B;I++)
#define FD(I,N) for(int I=N-1;I>=0;I--)
#define make(A) scanf("%d",&A)
#define maka2(A,B) scanf("%d%d",&A,&B)
#define db(A) printf("%s: %d\n",#A,A);
template<typename C> void ma(C& a,C b){if(a<b)a=b;}
template<typename C> void mi(C& a,C b){if(a>b)a=b;}
#define MAX 31
const int P = 1e9 + 7;
int n;
LL pom[MAX][MAX];
struct m{
  LL t[MAX][MAX];
  void id(){
    R(i,n)R(j,n)t[i][j] = (i==j);
  }
  void operator*=(m& b){
    R(i,n)R(j,n)pom[i][j] = 0;
    R(i,n)R(j,n)R(k,n)
      pom[i][k] += t[i][j] * b.t[j][k] % P;
    R(i,n)R(j,n)
      t[i][j] = pom[i][j] % P;
  }
//   void pri(){
//     R(i,n){R(j,n)printf("%d ",t[i][j]);puts("");}
//     puts("");
//   }
}obr,nl,ak,d[MAX*2];
LL m;
LL pw[MAX];
LL wyn[MAX];
int k;
vector<int> roz;
main(){
  scanf("%lld%d",&m,&k);
  while(m){
    roz.PB(m%k);
    m/=k;
  }
  n = k+1;
  R(i,k)obr.t[i][(i+1)%k] = 1;
  obr.t[k][k] = 1;
//  obr.pri();
  R(i,k+1){
    nl.t[0][i] = 1;
    nl.t[i][i] = 1;
  }
 // nl.pri();
  d[0] = nl;
  d[0] *= obr;
  F(i,1,roz.size()){
    d[i].id();
    R(_,k)d[i]*=d[i-1];
    d[i] *= obr;
    //printf("d%d\n",i);
   // d[i].pri();
  }
  R(i,n)wyn[i] = 1;
  for(int ii = roz.size()-1;ii>=0;ii--){
    R(_,roz[ii]){
      R(i,n)pw[i] = 0;
      R(i,n)R(j,n)pw[j] += wyn[i] * d[ii].t[i][j] % P;
      R(i,n)wyn[i] = pw[i] % P;
    }
  }
  printf("%lld\n",wyn[k]);
}


#include <bits/stdc++.h>

#define For(i, l, r) for (register int i = (l), i##end = (int)(r); i <= i##end; ++i)
#define Fordown(i, r, l) for (register int i = (r), i##end = (int)(l); i >= i##end; --i)
#define Rep(i, r) for (register int i = (0), i##end = (int)(r); i < i##end; ++i)
#define Set(a, v) memset(a, v, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define debug(x) cout << #x << ": " << (x) << endl
#define pb push_back

using namespace std;

typedef long long ll;

template<typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a, T b) { return b > a ? a = b, 1 : 0; }

void File() {
	freopen ("rudy.in", "r", stdin);
	freopen ("rudy.out", "w", stdout);
}

const int K = 105, Mod = 1e9 + 7;

ll n; int k;

template<int Maxn>
struct Matrix {

	int a[Maxn][Maxn];

	Matrix() { Set(a, 0); }

	inline void Unit(int len) {
		For (i, 1, len) a[i][i] = 1;
	}

	inline void Out() {
		For (i, 1, k + 1) For (j, 1, k + 1)
			printf ("%d%c", a[i][j], j == jend ? '\n' : ' ');
		putchar ('\n');
	}

};

typedef Matrix<K> Mat;

inline int id(int x, int step) {
	return x <= k ? ((x + step - 1) % k + 1) : x;
}

inline Mat Move(const Mat &Base, int step) {
	step %= k; Mat res;
	For (i, 1, k + 1) For (j, 1, k + 1)
		res.a[id(i, step)][id(j, step)] = Base.a[i][j];
	return res;
}

inline Mat operator * (const Mat &lhs, const Mat &rhs) {
	Mat res; int cur = k + 1;
	For (i, 1, cur) For(k, 1, cur) if (lhs.a[i][k]) For (j, 1, cur)
		res.a[i][j] = (res.a[i][j] + 1ll * lhs.a[i][k] * rhs.a[k][j]) % Mod;
	return res;
}

Mat A[K]; int ans[K];

int main () {

	cin >> n >> k;

	A[0].Unit(k + 1);
	For (i, 1, k + 1)
		A[0].a[1][i] = 1;

	vector<int> V;
	for (int len = 0; n; V.pb(n % k), n /= k) {
		Mat x = A[len]; A[++ len].Unit(k + 1);
		for (ll power = k, p = 0; power; power >>= 1) {
			if (power & 1) A[len] = A[len] * Move(x, (power ^ 1) << p);
			x = Move(x, 1 << p) * x; ++ p;
		}
	}

	int step = 0; ans[k + 1] = 1;

	Fordown (T, V.size() - 1, 0) {
		int cur = V[T];
		if (cur) {
			static int tmp[K];
			For (times, 0, cur - 1) {
				For (i, 1, k + 1) tmp[i] = ans[i], ans[i] = 0;
				Mat B = Move(A[T], times + step);
				For (i, 1, k + 1) For (j, 1, k + 1)
					ans[i] = (ans[i] + 1ll * tmp[j] * B.a[i][j]) % Mod;
			}
		}
		step += cur;
	}

	int res = 0;
	For (i, 1, k + 1) (res += ans[i]) %= Mod;
	printf ("%d\n", res);

	return 0;

}


/*
⣿⣿⣿⣿⣿⣿⡷⣯⢿⣿⣷⣻⢯⣿⡽⣻⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣇⠸⣿⣿⣆⠹⣿⣿⢾⣟⣯⣿⣿⣿⣿⣿⣿⣽⣻⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣻⣽⡿⣿⣎⠙⣿⣞⣷⡌⢻⣟⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣿⣿⡄⠹⣿⣿⡆⠻⣿⣟⣯⡿⣽⡿⣿⣿⣿⣿⣽⡷⣯⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣟⣷⣿⣿⣿⡀⠹⣟⣾⣟⣆⠹⣯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢠⡘⣿⣿⡄⠉⢿⣿⣽⡷⣿⣻⣿⣿⣿⣿⡝⣷⣯⢿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣯⢿⣾⢿⣿⡄⢄⠘⢿⣞⡿⣧⡈⢷⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢸⣧⠘⣿⣷⠈⣦⠙⢿⣽⣷⣻⣽⣿⣿⣿⣿⣌⢿⣯⢿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣟⣯⣿⢿⣿⡆⢸⡷⡈⢻⡽⣷⡷⡄⠻⣽⣿⣿⡿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣏⢰⣯⢷⠈⣿⡆⢹⢷⡌⠻⡾⢋⣱⣯⣿⣿⣿⣿⡆⢻⡿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⡎⣿⢾⡿⣿⡆⢸⣽⢻⣄⠹⣷⣟⣿⣄⠹⣟⣿⣿⣟⣿⣿⣿⣿⣿⣿⣽⣿⣿⣿⡇⢸⣯⣟⣧⠘⣷⠈⡯⠛⢀⡐⢾⣟⣷⣻⣿⣿⣿⡿⡌⢿⣻⣿⣿
⣿⣿⣿⣿⣿⣿⣧⢸⡿⣟⣿⡇⢸⣯⣟⣮⢧⡈⢿⣞⡿⣦⠘⠏⣹⣿⣽⢿⣿⣿⣿⣿⣯⣿⣿⣿⡇⢸⣿⣿⣾⡆⠹⢀⣠⣾⣟⣷⡈⢿⣞⣯⢿⣿⣿⣿⢷⠘⣯⣿⣿
⣿⣿⣿⣿⣿⣿⣿⡈⣿⢿⣽⡇⠘⠛⠛⠛⠓⠓⠈⠛⠛⠟⠇⢀⢿⣻⣿⣯⢿⣿⣿⣿⣷⢿⣿⣿⠁⣾⣿⣿⣿⣧⡄⠇⣹⣿⣾⣯⣿⡄⠻⣽⣯⢿⣻⣿⣿⡇⢹⣾⣿
⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⡽⡇⢸⣿⣿⣿⣿⣿⣞⣆⠰⣶⣶⡄⢀⢻⡿⣯⣿⡽⣿⣿⣿⢯⣟⡿⢀⣿⣿⣿⣿⣿⣧⠐⣸⣿⣿⣷⣿⣿⣆⠹⣯⣿⣻⣿⣿⣿⢀⣿⢿
⣿⣿⣿⣿⣿⣿⣿⣿⠘⣯⡿⡇⢸⣿⣿⣿⣿⣿⣿⣿⣧⡈⢿⣳⠘⡄⠻⣿⢾⣽⣟⡿⣿⢯⣿⡇⢸⣿⣿⣿⣿⣿⣿⡀⢾⣿⣿⣿⣿⣿⣿⣆⠹⣾⣷⣻⣿⡿⡇⢸⣿
⣿⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⠇⢸⣿⣿⣿⣿⣿⣿⣿⣿⣷⣄⠻⡇⢹⣆⠹⣟⣾⣽⣻⣟⣿⣽⠁⣾⣿⣿⣿⣿⣿⣿⣇⣿⣿⠿⠛⠛⠉⠙⠋⢀⠁⢘⣯⣿⣿⣧⠘⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⡈⣿⡃⢼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⡙⠌⣿⣆⠘⣿⣞⡿⣞⡿⡞⢠⣿⣿⣿⣿⣿⡿⠛⠉⠁⢀⣀⣠⣤⣤⣶⣶⣶⡆⢻⣽⣞⡿⣷⠈⣿
⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⠘⠁⠉⠉⠉⠉⠉⠉⠉⠉⠉⠙⠛⠛⢿⣄⢻⣿⣧⠘⢯⣟⡿⣽⠁⣾⣿⣿⣿⣿⣿⡃⢀⢀⠘⠛⠿⢿⣻⣟⣯⣽⣻⣵⡀⢿⣯⣟⣿⢀⣿
⣿⣿⣿⣟⣿⣿⣿⣿⣶⣶⡆⢀⣿⣾⣿⣾⣷⣿⣶⠿⠚⠉⢀⢀⣤⣿⣷⣿⣿⣷⡈⢿⣻⢃⣼⣿⣿⣿⣿⣻⣿⣿⣿⡶⣦⣤⣄⣀⡀⠉⠛⠛⠷⣯⣳⠈⣾⡽⣾⢀⣿
⣿⢿⣿⣿⣻⣿⣿⣿⣿⣿⡿⠐⣿⣿⣿⣿⠿⠋⠁⢀⢀⣤⣾⣿⣿⣿⣿⣿⣿⣿⣿⣌⣥⣾⡿⣿⣿⣷⣿⣿⢿⣷⣿⣿⣟⣾⣽⣳⢯⣟⣶⣦⣤⡾⣟⣦⠘⣿⢾⡁⢺
⣿⣻⣿⣿⡷⣿⣿⣿⣿⣿⡗⣦⠸⡿⠋⠁⢀⢀⣠⣴⢿⣿⣽⣻⢽⣾⣟⣷⣿⣟⣿⣿⣿⣳⠿⣵⣧⣼⣿⣿⣿⣿⣿⣾⣿⣿⣿⣿⣿⣽⣳⣯⣿⣿⣿⣽⢀⢷⣻⠄⠘
⣿⢷⣻⣿⣿⣷⣻⣿⣿⣿⡷⠛⣁⢀⣀⣤⣶⣿⣛⡿⣿⣮⣽⡻⣿⣮⣽⣻⢯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣯⢀⢸⣿⢀⡆
⠸⣟⣯⣿⣿⣷⢿⣽⣿⣿⣷⣿⣷⣆⠹⣿⣶⣯⠿⣿⣶⣟⣻⢿⣷⣽⣻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢀⣯⣟⢀⡇
⣇⠹⣟⣾⣻⣿⣿⢾⡽⣿⣿⣿⣿⣿⣆⢹⣶⣿⣻⣷⣯⣟⣿⣿⣽⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⢀⡿⡇⢸⡇
⣿⣆⠹⣷⡻⣽⣿⣯⢿⣽⣻⣿⣿⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⢸⣿⠇⣼⡇
⡙⠾⣆⠹⣿⣦⠛⣿⢯⣷⢿⡽⣿⣿⣿⣿⣆⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠃⠎⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠏⢀⣿⣾⣣⡿⡇
⣿⣷⡌⢦⠙⣿⣿⣌⠻⣽⢯⣿⣽⣻⣿⣿⣿⣧⠩⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡏⢰⢣⠘⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⢀⢀⢿⣞⣷⢿⡇
⣿⣽⣆⠹⣧⠘⣿⣿⡷⣌⠙⢷⣯⡷⣟⣿⣿⣿⣷⡀⡹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣈⠃⣸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⢀⣴⡧⢀⠸⣿⡽⣿⢀
⢻⣽⣿⡄⢻⣷⡈⢿⣿⣿⢧⢀⠙⢿⣻⡾⣽⣻⣿⣿⣄⠌⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢁⣰⣾⣟⡿⢀⡄⢿⣟⣿⢀
⡄⢿⣿⣷⢀⠹⣟⣆⠻⣿⣿⣆⢀⣀⠉⠻⣿⡽⣯⣿⣿⣷⣈⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⢀⣠⠘⣯⣷⣿⡟⢀⢆⠸⣿⡟⢸
⣷⡈⢿⣿⣇⢱⡘⢿⣷⣬⣙⠿⣧⠘⣆⢀⠈⠻⣷⣟⣾⢿⣿⣆⠹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⣠⡞⢡⣿⢀⣿⣿⣿⠇⡄⢸⡄⢻⡇⣼
⣿⣷⡈⢿⣿⡆⢣⡀⠙⢾⣟⣿⣿⣷⡈⠂⠘⣦⡈⠿⣯⣿⢾⣿⣆⠙⠻⠿⠿⠿⠿⡿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⠛⢋⣠⣾⡟⢠⣿⣿⢀⣿⣿⡟⢠⣿⢈⣧⠘⢠⣿
⣿⣿⣿⣄⠻⣿⡄⢳⡄⢆⡙⠾⣽⣿⣿⣆⡀⢹⡷⣄⠙⢿⣿⡾⣿⣆⢀⡀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⣀⣠⣴⡿⣯⠏⣠⣿⣿⡏⢸⣿⡿⢁⣿⣿⢀⣿⠆⢸⣿
⣿⣿⣿⣿⣦⡙⣿⣆⢻⡌⢿⣶⢤⣉⣙⣿⣷⡀⠙⠽⠷⠄⠹⣿⣟⣿⣆⢙⣋⣤⣤⣤⣄⣀⢀⢀⢀⢀⣾⣿⣟⡷⣯⡿⢃⣼⣿⣿⣿⠇⣼⡟⣡⣿⣿⣿⢀⡿⢠⠈⣿
⣿⣿⣿⣿⣿⣷⣮⣿⣿⣿⡌⠁⢤⣤⣤⣤⣬⣭⣴⣶⣶⣶⣆⠈⢻⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣷⣶⣤⣌⣉⡘⠛⠻⠶⣿⣿⣿⣿⡟⣰⣫⣴⣿⣿⣿⣿⠄⣷⣿⣿⣿
*/

#include <bits/stdc++.h>

using namespace std;

//#define MULTITEST

#define x first
#define y second
#define mp make_pair
#define pb push_back
#define sqr(a) ((a) * (a))
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define forn(i, n) for(int i = 0; i < int(n); i++)
#define fore(i, l, r) for(int i = int(l); i < int(r); i++)

typedef long long li;
typedef long double ld;
typedef pair<int, int> pt;

template <class A, class B> ostream& operator << (ostream& out, const pair<A, B> &a) {
	return out << "(" << a.x << ", " << a.y << ")";
}

template <class A> ostream& operator << (ostream& out, const vector<A> &v) {
	out << "[";
	forn(i, sz(v)) {
		if(i) out << ", ";
		out << v[i];
	}
	return out << "]";
}

mt19937 rnd(time(NULL));

const int INF = int(1e9);
const li INF64 = li(1e18);
const int MOD = int(1e9) + 7;
const ld EPS = 1e-9;
const ld PI = acos(-1.0);

const int N = 32;

li n;
int k;

bool read () {
	if (scanf("%lld%d", &n, &k) != 2)
		return false;
	return true;
}

int add(int a, int b){
	a += b;
	if (a >= MOD)
		a -= MOD;
	return a;
}

int mul(int a, int b){
	return a * li(b) % MOD;
}

typedef array<array<int, N>, N> mat;

mat operator *(const mat &a, const mat &b){
	mat c;
	forn(i, k + 1) forn(j, k + 1) c[i][j] = 0;
	forn(i, k + 1) forn(j, k + 1) forn(t, k + 1) c[i][j] = add(c[i][j], mul(a[i][t], b[t][j]));
	return c;
}

mat amat[62][N];

void solve(){
	vector<int> num;
	while (n > 0){
		num.pb(n % k);
		n /= k;
	}

	forn(d, k){
		forn(i, k + 1) forn(j, k + 1)
			amat[0][d][i][j] = 0;
		forn(i, k + 1)
			amat[0][d][i][i] = 1;
		forn(i, k + 1)
			amat[0][d][i][d] = 1;
	}

	fore(r, 1, sz(num)){
		vector<mat> pr;
		pr.pb(mat());
		forn(i, k + 1) forn(j, k + 1)
			pr[0][i][j] = i == j;
		forn(i, k)
			pr.pb(pr.back() * amat[r - 1][i]);
		forn(sh, k){
			amat[r][sh] = pr[0];
			forn(i, k - sh)
				amat[r][sh] = amat[r][sh] * amat[r - 1][(i + sh) % k];
			amat[r][sh] = amat[r][sh] * pr[sh];
		}
	}

	mat dp;
	forn(i, k + 1) forn(j, k + 1)
		dp[i][j] = 0;
	dp[0][k] = 1;

	int rem = 0;
	for (int i = sz(num) - 1; i >= 0; --i){
		while (num[i] > 0){
			dp = dp * amat[i][rem];
			rem = (rem + 1) % k;
			--num[i];
		}
	}

	int ans = 0;
	forn(i, k + 1)
		ans = add(ans, dp[0][i]);
	printf("%d\n", ans);
}

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);

	int tt = clock();

#endif

	cerr.precision(15);
	cout.precision(15);
	cerr << fixed;
	cout << fixed;

#ifdef MULTITEST
	int tc;
	scanf("%d", &tc);
	while(tc--){
		read();
#else
	while(read()) {
#endif
		solve();

#ifdef _DEBUG
	cerr << "TIME = " << clock() - tt << endl;
	tt = clock();
#endif

	}
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#define MAXK 30
#define MAXD 60

using namespace std;

typedef long long lint;

const lint mod = 1e9 + 7;

int K;
struct res_t {
  int many[MAXK][MAXK];
  int inside[MAXK];
  bool exist[MAXK];

  res_t() {
    memset(many, 0, sizeof(many));
    memset(inside, 0, sizeof(inside));
    memset(exist, 0, sizeof(exist));
  }

  res_t(res_t &copy, int rotate) {
    for (int i = 0; i < K; i++) {
      exist[(i+rotate)%K] = copy.exist[i];
      inside[(i+rotate)%K] = copy.inside[i];

      for (int j = 0; j < K; j++)
	many[(i+rotate)%K][(j+rotate)%K] = copy.many[i][j];
    }
  }

  res_t(const res_t &a, const res_t &b) {
    for (int i = 0; i < K; i++) {
      exist[i] = a.exist[i] || b.exist[i];

      inside[i] = a.inside[i];
      if (!a.exist[i])
	inside[i] = (inside[i] + b.inside[i]) % mod;
      for (int j = 0; j < K; j++) {
	inside[i] = (inside[i] + (lint)a.many[i][j] * b.inside[j]) % mod;

	many[i][j] = 0;
	if (!b.exist[j])
	  many[i][j] = a.many[i][j];
	if (!a.exist[i])
	  many[i][j] = (many[i][j] + b.many[i][j]) % mod;
	for (int m = 0; m < K; m++)
	  many[i][j] = (many[i][j] + (lint)a.many[i][m] * b.many[m][j]) % mod;
      }
    }
  }
} retpk[MAXD];
int digits[MAXD];

int main() {
  lint n;

  cin >> n >> K;

  int nd = 0;
  for (lint x = n; x; x /= K)
    digits[nd++] = x % K;

  retpk[0].exist[0] = true;
  retpk[0].inside[0] = 1;
  for (int j = 0; j < K; j++)
    retpk[0].many[0][j] = 1;

  for (int pk = 1; pk < nd; pk++) {
    retpk[pk] = retpk[pk-1];
    for (int v = 1; v < K; v++)
      retpk[pk] = res_t(retpk[pk], res_t(retpk[pk-1], v));
  }

  res_t result;
  int sum = 0;

  for (int d = nd-1; d >= 0; d--) {
    for (int v = 0; v < digits[d]; v++)
      result = res_t(result, res_t(retpk[d], (sum + v) % K));
    sum += digits[d];
  }

  int result_sum = 1;
  for (int i = 0; i < K; i++)
    result_sum = (result_sum + result.inside[i]) % mod;

  cout << result_sum << endl;

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

using int64 = long long;

constexpr int64 P = 1000000007;

struct FiniteField {
 private:
  int64 x;
 public:
  FiniteField(int64 x) : x(x) {
    if (x > P || x < 0) { cerr << "Invalied FiniteField!" << endl; exit(1); }
  }
  FiniteField() : x(0) {}
  int64 Value() const { return x; }
  inline FiniteField operator+(FiniteField o) const {
    FiniteField r(*this); r += o; return r;
  }
  inline FiniteField operator-(FiniteField o) const {
    FiniteField r(*this); r -= o; return r;
  }
  inline FiniteField operator* (FiniteField o) const {
    FiniteField r(*this); r *= o; return r;
  }
  inline FiniteField operator/ (FiniteField o) const {
    FiniteField r(*this); r /= o; return r;
  }
  inline void operator+= (FiniteField o) { x = (x + o.x) % P; }
  inline void operator-= (FiniteField o) { x = (x + P - o.x) % P; }
  inline void operator*= (FiniteField o) { x = (x * o.x) % P; }
  void operator/=(FiniteField o) {
    int64 p = P - 2; while (p) { if (p % 2) { *this *= o; } o *= o; p /= 2; }
  }
};

template<typename T>
vector<vector<T>> operator*(
    const vector<vector<T>>& a1, const vector<vector<T>>& a2) {
  int n = a1.size();
  int m = a1[0].size();
  if (a2.size() != m) {
    cerr << "Matrix dimensions don't match for multiplication!" << endl;
    exit(1);
  }
  int l = a2[0].size();
  vector<vector<T>> result(n, vector<T>(l));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < l; j++) {
      for (int k = 0; k < m; k++) {
        result[i][j] += a1[i][k] * a2[k][j];
      }
    }
  }
  return result;
}

template<typename T>
vector<T> operator*(const vector<vector<T>>& matrix, const vector<T>& v) {
  int n = matrix.size();
  int m = matrix[0].size();
  if (v.size() != m) {
    cerr << "Matrix and vector dimensions don't match for multiplication!"
         << endl;
    exit(1);
  }
  vector<T> result(n);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      result[i] += matrix[i][j] * v[j];
    }
  }
  return result;
}

template<typename T>
vector<vector<T>> Make2DVector(int d1, int d2, T default_value) {
  return vector<vector<T>>(d1, vector<T>(d2, default_value));
}

template<typename T>
vector<vector<vector<T>>>
Make3DVector(int d1, int d2, int d3, T default_value) {
  return vector<vector<vector<T>>>(
      d1, vector<vector<T>>(d2, vector<T>(d3, default_value)));
}

constexpr int DEBUG = 0;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int64 n;
  int d;
  cin >> n >> d;

  auto shifts = Make3DVector<FiniteField>(d, d + 1, d + 1, 0);
  for (int k = 0; k < d; k++) {
    for (int i = 0; i < d; i++) {
      shifts[k][i][(i + k) % d] = 1;
    }
    shifts[k][d][d] = 1;
  }

  auto units = Make3DVector<FiniteField>(d, d + 1, d + 1, 0);
  for (int k = 0; k < d; k++) {
    for (int i = 0; i < d + 1; i++) {
      units[k][i][i] = 1;
    }
    for (int i = 0; i < d + 1; i++) {
      units[k][k][i] = 1;
    }
  }

  auto identity = Make2DVector<FiniteField>(d + 1, d + 1, 0);
  for (int i = 0; i < d + 1; i++) {
    identity[i][i] = 1;
  }

  map<int64, vector<vector<FiniteField>>> cache;

  function<vector<vector<FiniteField>>(int64)> find_matrix =
      [&](int64 p) -> vector<vector<FiniteField>> {
    if (cache.find(p) != cache.end()) {
      return cache.find(p) -> second;
    }

    if (p <= d) {
      auto matrix = identity;
      for (int k = 0; k < p; k++) {
        matrix = units[k] * matrix;
      }
      return matrix;
    }

    int64 child_p = d;
    while ((p + child_p - 1) / child_p > d) child_p *= d;
    auto child = find_matrix(child_p);
    auto matrix = child;
    int64 remaining_p = p;
    remaining_p -= child_p;
    for (int k = 1; k < d; k++) {
      if (remaining_p >= child_p) {
        matrix = shifts[d - k] * child * shifts[k] * matrix;
        remaining_p -= child_p;
      } else if (remaining_p > 0) {
        matrix = shifts[d - k] * find_matrix(remaining_p) * shifts[k] * matrix;
        remaining_p = 0;
      } else {
        break;
      }
    }

    cache[p] = matrix;

    return matrix;
  };

  if (DEBUG) cerr << "PHASE 1" << endl;

  auto matrix = find_matrix(n);
  if (DEBUG) cerr << "PHASE 2" << endl;

  vector<FiniteField> base_vector(d + 1);
  base_vector[d] = 1;
  auto final_vector = matrix * base_vector;
  FiniteField ans = 0;
  for (const auto& x : final_vector) ans += x;
  cout << ans.Value() << endl;
}


#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <list>
#include <cassert>
#include <ctime>
#include <climits>
using namespace std;

#define PB push_back
#define MP make_pair
#define SZ(v) ((int)(v).size())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define FORE(i,a,b) for(int i=(a);i<=(b);++i)
#define REPE(i,n) FORE(i,0,n)
#define FORSZ(i,a,v) FOR(i,a,SZ(v))
#define REPSZ(i,v) REP(i,SZ(v))
typedef long long ll;
int gcd(int a,int b) { return b==0?a:gcd(b,a%b); }

const int MOD=1000000007;
const ll LIM=LLONG_MAX-(ll)MOD*MOD;
typedef int Mat[32][32];
ll n; int base;

void mmult(const Mat &A,const Mat &B,Mat &C) {
	REP(i,base+1) REP(j,base+1) {
		ll cur=0;
		REP(k,base+1) { if(cur>LIM) cur%=MOD; cur+=(ll)A[i][k]*B[k][j]; }
		C[i][j]=cur%MOD;
	}
}
void mcopy(const Mat &A,Mat &B) {
	REP(i,base+1) REP(j,base+1) B[i][j]=A[i][j];
}
void printmat(const Mat &A) {
	REP(i,base+1) { REP(j,base+1) printf("%2d ",A[i][j]); puts(""); }
}

Mat mat[60][32]; // mat[i][j] = matrix for sequence of base^i integers, starting with j

Mat have,tmp;

void run() {
	cin>>n>>base;
	REP(i,base) REP(j,base+1) REP(k,base+1) mat[0][i][j][k]=i==j||j==k?1:0;
	int ci=0,cj=0; ll step=1;
	while(step<=n/base) ++ci,step*=base;

	FORE(i,1,ci) REP(j,base) {
		REP(k,base+1) REP(l,base+1) mat[i][j][k][l]=k==l?1:0;
		REP(k,base) { mmult(mat[i][j],mat[i-1][(j+k)%base],tmp); mcopy(tmp,mat[i][j]); }
	}

	REP(k,base+1) REP(l,base+1) have[k][l]=k==l?1:0;
	for(;ci>=0;--ci,step/=base) {
		int times=(n/step)%base;
		REP(x,times) {
			mmult(have,mat[ci][cj],tmp); mcopy(tmp,have);
			//printf("after %d times %d^%d (starting with %d)\n",x+1,base,ci,cj); printmat(have); printf("\tmult with\n"); printmat(mat[ci][cj]);
			if(++cj>=base) cj-=base;
		}
	}
	//printf("have\n"); printmat(have);
	int ret=0; REP(k,base+1) { ret+=have[k][base]; if(ret>=MOD) ret-=MOD; }
	printf("%d\n",ret);
}

int main() {
	run();
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
const ll mod = 1000000007;
ll N, K;
template< class T >
struct Matrix {
  vector< vector< T > > A;

  Matrix() {}

  Matrix(size_t n, size_t m) : A(n, vector< T >(m, 0)) {}

  Matrix(size_t n) : A(n, vector< T >(n, 0)) {};

  size_t height() const {
    return (A.size());
  }

  size_t width() const {
    return (A[0].size());
  }

  inline const vector< T > &operator[](int k) const {
    return (A.at(k));
  }

  inline vector< T > &operator[](int k) {
    return (A.at(k));
  }

  static Matrix I(size_t n) {
    Matrix mat(n);
    for(int i = 0; i < n; i++) mat[i][i] = 1;
    return (mat);
  }

  Matrix &operator+=(const Matrix &B) {
    size_t n = height(), m = width();
    assert(n == B.height() && m == B.width());
    for(int i = 0; i < n; i++)
      for(int j = 0; j < m; j++)
        (*this)[i][j] = ((*this)[i][j] + B[i][j]) % mod;
    return (*this);
  }

  Matrix &operator-=(const Matrix &B) {
    size_t n = height(), m = width();
    assert(n == B.height() && m == B.width());
    for(int i = 0; i < n; i++)
      for(int j = 0; j < m; j++)
        (*this)[i][j] = ((*this)[i][j] - B[i][j] + mod) % mod;
    return (*this);
  }

  Matrix &operator*=(const Matrix &B) {
    size_t n = height(), m = B.width(), p = width();
    assert(p == B.height());
    vector< vector< T > > C(n, vector< T >(m, 0));
    for(int i = 0; i < n; i++)
      for(int j = 0; j < m; j++)
        for(int k = 0; k < p; k++)
          C[i][j] = ((C[i][j] + (*this)[i][k] * B[k][j])) % mod;
    A.swap(C);
    return (*this);
  }

  Matrix &operator^=(long long k) {
    Matrix B = Matrix::I(height());
    while(k > 0) {
      if(k & 1) B *= *this;
      *this *= *this;
      k >>= 1LL;
    }
    A.swap(B.A);
    return (*this);
  }

  Matrix operator+(const Matrix &B) const {
    return (Matrix(*this) += B);
  }

  Matrix operator-(const Matrix &B) const {
    return (Matrix(*this) -= B);
  }

  Matrix operator*(const Matrix &B) const {
    return (Matrix(*this) *= B);
  }

  Matrix operator^(const long long k) const {
    return (Matrix(*this) ^= k);
  }

  friend ostream &operator<<(ostream &os, Matrix &p) {
    size_t n = p.height(), m = p.width();
    for(int i = 0; i < n; i++) {
      os << "[";
      for(int j = 0; j < m; j++) {
        os << p[i][j] << (j + 1 == m ? "]\n" : ",");
      }
    }
    return (os);
  }

  T determinant() {
    Matrix B(*this);
    assert(width() == height());
    T ret = 1;
    for(int i = 0; i < width(); i++) {
      int idx = -1;
      for(int j = i; j < width(); j++) {
        if(B[j][i] != 0) idx = j;
      }
      if(idx == -1) return (0);
      if(i != idx) {
        ret *= -1;
        swap(B[i], B[idx]);
      }
      ret *= B[i][i];
      T vv = B[i][i];
      for(int j = 0; j < width(); j++) {
        B[i][j] /= vv;
      }
      for(int j = i + 1; j < width(); j++) {
        T a = B[j][i];
        for(int k = 0; k < width(); k++) {
          B[j][k] -= B[i][k] * a;
        }
      }
    }
    return (ret);
  }

  void rot() {
      Matrix<ll> B(A.size());
      for(int i = 0; i < K; i++) {
          for(int j = 0; j < K; j++) {
              B[i][j] = A[(i+K-1)%K][(j+K-1)%K];
          }
          B[i][K] = A[(i+K-1)%K][K];
          B[K][i] = A[K][(i+K-1)%K];
          B[K][K] = A[K][K];
      }
      for(int i = 0; i < A.size(); i++) {
          for(int j = 0; j < A[0].size(); j++) {
              A[i][j] = B[i][j];
          }
      }
      /*
      for(int i = A.size()-2; i >= 0; i--) {
          swap(A[i], A[i+1]);
      }
      for(int i = 0; i < A.size(); i++) {
          for(int j = A[0].size() - 2; j >= 0; j--) {
              swap(A[i][j], A[i][j+1]);
          }
      }
      */
  }
};

Matrix<ll> mat[70][35];

ll beki(ll a, ll b) {
    if(b == 0) return 1;
    ll ret = beki(a, b / 2);
    ret = ret * ret;
    if(b & 1) ret *= a;
    return ret;
}

int main() {
    cin >> N >> K;
    ll INDEX = 0;
    ll copy = N;
    while(copy >= K) {
        INDEX++;
        copy /= K;
    }
    mat[0][0] = Matrix<ll>::I(K+1);
    for(int i = 1; i < K + 1; i++) mat[0][0][0][i] = 1;
    //cerr << mat[0][0] << endl;
    for(int i = 1; i < K; i++) {
        mat[0][i] = mat[0][i-1];
        mat[0][i].rot();
        //cerr << mat[0][i] << endl;
    }
    for(int index = 1; index <= INDEX; index++) {
    //for(int index = 1; index < 3; index++) {
        mat[index][0] = mat[index-1][0];
        for(int i = 1; i < K; i++) {
            mat[index][0] = mat[index-1][i] * mat[index][0];
        }
        //cerr << index << " " << 0 << endl;
        //cerr << mat[index][0] << endl;
        for(int i = 1; i < K; i++) {
            mat[index][i] = mat[index][i-1];
            mat[index][i].rot();
            //cerr << index << " " << i << endl;
            //cerr << mat[index][i] << endl;
        }
    }
    cerr << INDEX << endl;
    //cerr << INDEX << endl;
    Matrix<ll> ans(K + 1, 1);
    ans[K][0] = 1;
    int used = 0;
    while(INDEX >= 0) {
        for(int i = 0; ; i++) {
            if(beki(K, INDEX) <= N) {
                N -= beki(K, INDEX);
                //ans = mat[INDEX][i] * ans;
                ans = mat[INDEX][used] * ans;
                used++;
                used %= K;
                /*
                cerr << INDEX << " " << i << endl;
                cerr << mat[INDEX][i] << endl;
                cerr << ans << endl;
                */
            } else break;
        }
        INDEX--;
    }
    ll val = 0;
    for(ll i = 0; i < K + 1; i++) {
        val += ans[i][0];
        val %= mod;
    }
    cout << val << endl;
    return 0;
}


#include<cstdio>
#include<iostream>
using namespace std;
typedef long long ll;
const int mod=1e9+7;

struct matri{
    int m[33][33];
}em,a[70],r,pre[70],suf[70];

ll N,K;

void iprint(matri x){
    int i,j;
    for(i=0;i<=K;i++){
        for(j=0;j<=K;j++)
        cout<<x.m[i][j]<<' ';
        cout<<endl;
    }
    cout<<endl;
}

matri imulti(matri a,matri b){
    int i,j,k;
    matri c=em;
    for(k=0;k<=K;k++){
        for(i=0;i<=K;i++){
            for(j=0;j<=K;j++){
                ll tmp=((ll)a.m[i][k]*b.m[k][j])%mod;
                c.m[i][j]=(c.m[i][j]+tmp)%mod;
            }
        }
    }
    //iprint(a);iprint(b);iprint(c);
    return c;
}

void ical(){
    int i;
    pre[0]=a[0];
    for(i=1;i<K;i++)
        pre[i]=imulti(pre[i-1],a[i]);
    suf[K-1]=a[K-1];
    for(i=K-2;i>=0;i--)
        suf[i]=imulti(a[i],suf[i+1]);
    a[0]=suf[0];
    for(i=1;i<K;i++)
        a[i]=imulti(suf[i],pre[i-1]);
}

void iWork(){
    cin>>N>>K;
    int i,j;
    r=em;
    for(i=0;i<K;i++){
        a[i]=em;
        for(j=0;j<=K;j++)
            a[i].m[j][j]=1;
        for(j=0;j<=K;j++)
            a[i].m[j][i]=1;
        r.m[i][i]=1;
    }
    //iprint(a[1]);
    r.m[K][K]=1;
    //iprint(r);
    for(;N>0;N/=K){
        int tt=N%K;
        if(tt){
            ll pos=0,tmp=N;
            while(tmp)
                pos=(pos+(tmp/=K))%K;
            for(i=tt-1;i>=0;i--){
                r=imulti(a[(pos+i)%K],r);
                //iprint(r);
            }
        }
        ical();
    }
    int ans=1;
    for(i=0;i<K;i++)
    ans=(ans+r.m[K][i])%mod;
    cout<<ans<<endl;
}

int main(){
    iWork();
    return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<deque>
#include<fstream>
#include<functional>
#include<iomanip>
#include<list>
#include<map>
#include<numeric>
#include<queue>
#include<set>
#include<stack>
#include<stdexcept>
#include<string>
#include<vector>
#include<utility>
using namespace std;
const double eps(1e-8);
const double pi(3.14159265358979);
const int K=33,mod=1000000007;
struct matrix
{
	long long a[K][K];
}t[K]={},ans={{}},pre[K]={},suf[K]={};
long long n;
int k;
inline void multi(const matrix &m1,const matrix &m2,matrix &to)
{
	static matrix tmp={{}};
	for(int i=0;i<=k;++i)
		for(int j=0;j<=k;++j)
		{
			tmp.a[i][j]=0;
			for(int p=0;p<=k;++p)
				(tmp.a[i][j]+=m1.a[i][p]*m2.a[p][j])%=mod;
		}
	to=tmp;
}
void init()
{
	cin>>n>>k;
	for(int i=0;i<=k;++i)
		for(int j=0;j<=k;++j)
			ans.a[i][j]=i==j;
	for(int p=0;p<k;++p)
		for(int i=0;i<=k;++i)
			for(int j=0;j<=k;++j)
				t[p].a[i][j]=i==j || i==p;
}
void work()
{
	vector<int> bit;
	for(;n;n/=k)
		bit.push_back(n%k);
	bit.push_back(0);
	partial_sum(bit.rbegin(),bit.rend(),bit.rbegin());
	for(auto p=0u;p<bit.size()-1;++p)
	{
		for(int i=bit[p]-1;i>=bit[p+1];--i)
			multi(t[i%k],ans,ans);
		pre[0]=t[0];
		for(int i=1;i<k;++i)
			multi(pre[i-1],t[i],pre[i]);
		suf[k-1]=t[k-1];
		for(int i=k-2;i>=0;--i)
			multi(t[i],suf[i+1],suf[i]);
		t[0]=suf[0];
		for(int i=1;i<k;++i)
			multi(suf[i],pre[i-1],t[i]);
	}
	long long sum=0;
	for(int i=0;i<=k;++i)
		sum+=ans.a[i][k];
	cout<<sum%mod<<endl;
}
int main()
{
	init();
	work();
	return 0;
}


#include <iostream>
using namespace std;
long long N, K;
const long long MOD = 1000000007;
long long Power[65], Res[35][35];
long long S[65][35][35][35], A[65][35][35][35], P[65][35][35][35], Aux[35][35];
pair <long long, long long> X[105];
void copy(long long X[][35], long long Y[][35], long long n)
{
    for(long long i = 0; i < n; i++)
        for(long long j = 0; j < n; j++)
            X[i][j] = Y[i][j];
}
inline void Add(long long& x, long long y)
{
    x += y;
    if(x >= MOD)
        x -= MOD;
}
void mul(long long X[][35], long long Y[][35], long long Z[][35], long long n, long long m, long long p)
{
    for(long long i = 0; i < n; i++)
        for(long long j = 0; j < p; j++)
        {
            Z[i][j] = 0;
            for(long long k = 0; k < m; k++)
            {
                Add(Z[i][j], (1LL * X[i][k] * Y[k][j]) % MOD);
            }
        }
}

void precalcA()
{
    for(long long x = 0; x < K; x++)
    {
        for(long long i = 0; i < K + 1; i++)
                A[0][x][i][i] = 1;
        for(long long j = 0; j < K + 1; j++)
            A[0][x][j][x] = 1;
        //A[0][x][x][x] = 0;
    }
    copy(P[0][0], A[0][0], K + 1);
    copy(S[0][K - 1], A[0][K - 1], K + 1);
    for(long long x = 1; x < K; x++)
    {
        mul(P[0][x - 1], A[0][x], P[0][x], K + 1, K + 1, K + 1);
    }
    for(long long x = K - 2; x >= 0; x--)
    {
        mul(A[0][x], S[0][x + 1], S[0][x], K + 1, K + 1, K + 1);
    }
    long long cnt = 1;
    for(long long m = K; m <= N; m *= K, cnt++)
    {
        for(long long x = 0; x < K; x++)
        {
            if(x > 0)
            {
                copy(A[cnt][x], S[cnt - 1][x], K + 1);
                mul(A[cnt][x], P[cnt - 1][x - 1], Aux, K + 1, K + 1, K + 1);
                copy(A[cnt][x], Aux, K + 1);
            }
            else
                copy(A[cnt][x], S[cnt - 1][x], K + 1);
        }
        copy(P[cnt][0], A[cnt][0], K + 1);
        copy(S[cnt][K - 1], A[cnt][K - 1], K + 1);
        for(long long x = 1; x < K; x++)
        {
            mul(P[cnt][x - 1], A[cnt][x], P[cnt][x], K + 1, K + 1, K + 1);
        }
        for(long long x = K - 2; x >= 0; x--)
        {
            mul(A[cnt][x], S[cnt][x + 1], S[cnt][x], K + 1, K + 1, K + 1);
        }
        if(m > N / K)
            break;
    }
}
int main()
{
    cin >> N >> K;
    Power[0] = 1;
    int i = 1;
    while(Power[i - 1] <= N / K)
    {
        Power[i] = Power[i - 1] * K;
        ++i;
    }
    int ind = i;
    --ind;
    precalcA();
    long long init = N, last = -1, count = -1, ind2 = 0;
    Res[0][K] = 1;
    for(int i = ind; i >= 0; i--)
    {
        while(Power[i] <= init)
        {
            mul(Res, A[i][ind2], Aux, K + 1, K + 1, K + 1);
            copy(Res, Aux, K + 1);
            ind2++;
            if(ind2 == K)
                ind2 = 0;
            init -= Power[i];
        }
    }
    long long ans = 0;
    for(int i = 0; i <= K; i++)
        Add(ans, Res[0][i]);
    cout << ans << "\n";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int mod=1000000007;
inline int add(int a,int b)
{
	if((a+=b)>=mod)a-=mod;
	return a;
}
inline int mult(int a,int b)
{
	long long t=1ll*a*b;
	if(t>=mod)t%=mod;
	return t;
}
namespace matrix
{
	template<int N,int M>
	class matr
	{
		public:
			int dat[N][M];
			void init()
			{
				memset(dat,0,sizeof(dat));
			}
			void iden()
			{
				init();
				for(int i=0; i<min(N,M); i++)dat[i][i]=1;
			}
			matr<M,N> trans()
			{
				matr<M,N> out;
				for(int i=0; i<M; i++)
					for(int j=0; j<N; j++)out[i][j]=dat[j][i];
			}
			const int* operator [](const int &x)const
			{
				return dat[x];
			}
			int* operator [](const int &x)
			{
				return dat[x];
			}
			matr<N,M> operator +(const matr<N,M> a)
			{
				matr<N,M> out;
				for(int i=0; i<N; i++)
					for(int j=0; j<M; j++)out[i][j]=add(dat[i][j],a[i][j]);
				return out;
			}
			matr<N,M> operator *(const int &x)
			{
				matr<N,M> out=*this;
				for(int i=0; i<N; i++)
					for(int j=0; j<M; j++)out[i][j]=mult(out[i][j],x);
				return out;
			}
			template<int X>
			friend matr<N,M> operator *(const matr<N,X> a,const matr<X,M> b)
			{
				matr<N,M> out;
				out.init();
				for(int i=0; i<N; i++)
				{
					for(int j=0; j<X; j++)
					{
						if(a[i][j])
						{
							for(int k=0; k<M; k++)
								out[i][k]=add(out[i][k],mult(a[i][j],b[j][k]));
						}
					}
				}
				return out;
			}
	};
};
using namespace matrix;
typedef long long ll;
ll n;
int K;
vector<int> digit;
matr<35,35> A[65][35],S[65][35],P[65][35],ans;
int main()
{
	scanf("%lld%d",&n,&K);
	for(int i=0;i<K;i++)
	{
		A[0][i].iden();
		for(int j=0;j<=K;j++)A[0][i][i][j]=1;
	}
	ll len=K;
	for(int i=1;len<=n;i++,len*=K)
	{
		S[i-1][K-1]=A[i-1][K-1];
		for(int j=K-2;j>=0;j--) S[i-1][j]=A[i-1][j]*S[i-1][j+1];
        P[i-1][0]=A[i-1][0];
        for(int j=1;j<K;j++)P[i-1][j]=P[i-1][j-1]*A[i-1][j];
        A[i][0]=S[i-1][0];
        for(int j=1;j<K;j++)A[i][j]=S[i-1][j]*P[i-1][j-1];
	}
	while(n)
	{
        digit.push_back(n%K);
        n/=K;
    }
    int s=0;
    ans.iden();
    for(int i=digit.size()-1;i>=0;i--)
	{
        for(int j=0;j<digit[i];j++)
            ans=ans*A[i][(j+s)%K];
        s=(s+digit[i])%K;
    }
	int res=1;
	for(int i=0;i<K;i++)res=add(res,ans[i][K]);
	printf("%d\n",res);
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define uint unsigned
#define pii pair<int,int>
#define pll pair<ll,ll>
#define IT iterator
#define PB push_back
#define fi first
#define se second
#define For(i,j,k) for (int i=(int)(j);i<=(int)(k);i++)
#define Rep(i,j,k) for (int i=(int)(j);i>=(int)(k);i--)
#define CLR(a,v) memset(a,v,sizeof(a));
#define CPY(a,b) memcpy(a,b,sizeof(a));
#define debug puts("wzpakking")
#define y1 ysghysgsygsh
using namespace std;
const int mo=1000000007;
int q[65],k;
ll n;
struct Mat{
	int a[32][32];
	Mat(){
		memset(a,0,sizeof(a));
	}
	Mat operator *(const Mat &b)const{
		Mat c;
		For(i,0,::k) For(j,0,::k) For(k,0,::k)
			c.a[i][k]=(c.a[i][k]+1ll*a[i][j]*b.a[j][k])%mo;
		return c;
	}
};
Mat pre[32],suf[32];
Mat f[65][32],ans;
int main(){
	scanf("%lld%d",&n,&k);
	ans.a[0][k]=1;
	for (;n;n/=k) q[++*q]=n%k;
	For(i,0,k-1) For(j,0,k)
		f[1][i].a[j][i]=f[1][i].a[j][j]=1;
	//For(i,1,*q) cout<<q[i]<<' '; cout<<endl;
	For(i,2,*q){
		pre[0]=f[i-1][0];
		suf[k-1]=f[i-1][k-1];
		For(j,1,k-1) pre[j]=pre[j-1]*f[i-1][j];
		Rep(j,k-2,0) suf[j]=f[i-1][j]*suf[j+1];
		f[i][0]=suf[0];
		For(j,1,k-1) f[i][j]=suf[j]*pre[j-1];
	}
	int sum=0;
	Rep(i,*q,1){
		For(j,0,q[i]-1)
			ans=ans*f[i][(sum+j)%k];
		sum=(sum+q[i])%mo;
	}
	sum=0;
	For(i,0,k) sum=(sum+1ll*ans.a[0][i])%mo;
	printf("%d\n",sum);
}


#include <bits/stdc++.h>
using namespace std;
template<typename T> inline void checkMin(T &a, T b) { if(b<a) a=b; }
template<typename T> inline void checkMax(T &a, T b) { if(a<b) a=b; }
#define X first
#define Y second
#define MP make_pair
#define PB push_back
#define SZ(c) int((c).size())
#define ALL(c) (c).begin(),(c).end()
#define REP(i,n) for (int i=0;i<int(n);++i)
typedef long long lint;
typedef vector<int> VI;
typedef pair<int, int> PII;

const int MOD = 1e9 + 7;

struct Matrix {
  static const int MAXN = 32;
  int N;
  int a[MAXN][MAXN];

  void init(int _N, int _V) {
    N = _N;
    fill(a[0], a[N], _V);
  }

  void mul(const Matrix& b) {
    static Matrix l, r;
    REP (i, N) REP (j, N)
      l.a[i][j] = a[i][j], r.a[i][j] = b.a[i][j];
    REP (i, N) REP (j, N) {
      lint t = 0;
      REP (k, N) t += lint(l.a[i][k]) * r.a[k][j] % MOD;
      a[i][j] = t % MOD;
    }
  }
} A[64][32], prefix[64][32], suffix[64][32];

lint n;
int k;

void pre_calc(int i) {
  prefix[i][1] = A[i][0];
  for (int j = 2; j <= k; ++j) {
    prefix[i][j] = prefix[i][j - 1];
    prefix[i][j].mul(A[i][j - 1]);
  }
  suffix[i][k - 1] = A[i][k - 1];
  for (int j = k - 2; j >= 0; --j) {
    suffix[i][j] = A[i][j];
    suffix[i][j].mul(suffix[i][j + 1]);
  }
}

int digsum(lint n) {
  int s = 0;
  for (; n; n /= k) {
    (s += n % k) %= k;
  }
  return s;
}

Matrix solve(lint n, int m) {
  if (n < k) {
    Matrix mat = A[m][0];
    for (int i = 1; i < n; ++i) {
      mat.mul(A[m][i]);
    }
    return mat;
  }
  Matrix mat = solve(n / k, m + 1);
  int x = digsum(n / k);
  for (int i = 0; i < n % k; ++i) {
    mat.mul(A[m][(x + i) % k]);
  }
  return mat;
}

int main() {
  cin >> n >> k;
  REP (i, k) {
    A[0][i].init(k + 1, 0);
    REP (j, k + 1) {
      A[0][i].a[j][j] = 1;
      A[0][i].a[j][i] = 1;
    }
  }
  for (int m = 1; pow(k, m) - 1000 <= n; ++m) {
    pre_calc(m - 1);
    REP (x, k) {
      A[m][x] = suffix[m - 1][x];
      if (x > 0) {
        A[m][x].mul(prefix[m - 1][x]);
      }
    }
  }
  Matrix mat = solve(n, 0);
  int ans = 0;
  REP (i, k + 1) {
    (ans += mat.a[k][i]) %= MOD;
  }
  cout << ans << endl;
  return 0;
}


#include <algorithm>
#include <cassert>
#include <cstring>
#include <iostream>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define TRACE(x) cout << #x << " = " << x << endl
#define _ << " _ " <<

typedef long long llint;

const int mod = 1e9 + 7;

int add(int a, int b) {
  return a+b >= mod ? a+b-mod : a+b;
}

int mul(int a, int b) {
  return (llint(a)*b)%mod;
}

const int M = 32;

struct matrix {
  int a[M][M];
  matrix() { REP(i, M) REP(j, M) a[i][j] = 0; }

  int* operator [] (int i) { return a[i]; }
  friend matrix operator * (matrix a, matrix b) {
    matrix r;
    REP(i, M) REP(j, M) REP(k, M)
      r[i][j] = add(r[i][j], mul(a[i][k], b[k][j]));
    return r;
  }
};

const int MAXD = 64;
const int MAXK = 32;

matrix f[MAXD];
int k;

matrix calc_matrix(int n, int off) {
  matrix ans;
  REP(i, k+1) REP(j, k+1) {
    int ni = i == k ? i : (i + off)%k;
    int nj = j == k ? j : (j + off)%k;
    ans[ni][nj] = f[n][i][j];
  }
  return ans;
}

int sum_digits(llint n) {
  int ans = 0;
  while (n > 0) {
    ans += n%k;
    n /= k;
  }
  return ans%k;
}

int main(void) {
  llint n;
  scanf("%lld %d", &n, &k);

  int ndigits = 0;
  llint tmp = n;
  while (tmp > 0) {
    tmp /= k;
    ndigits++;
  }

  matrix id;
  REP(i, k+1) id[i][i] = 1;

  f[0] = id;
  REP(i, k+1) f[0][i][0] = 1;
  FOR(i, 1, ndigits) {
    f[i] = id;
    REP(j, k) f[i] = f[i] * calc_matrix(i-1, j);
  }

  llint cur = 0;
  llint pw = 1;

  ndigits--;
  REP(i, ndigits) pw *= k;

  matrix ans = id;
  while (cur < n) {
    assert(ndigits >= 0);
    while (cur + pw <= n) {
      ans = ans * calc_matrix(ndigits, sum_digits(cur));
      cur += pw;
    }
    pw /= k;
    ndigits--;
  }

  int sum = 0;
  REP(i, k+1) sum = add(sum, ans[k][i]);
  printf("%d\n", sum);
  return 0;
}


#include <iostream>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <string.h>
#include <map>
#include <fstream>
#include <functional>
#include <bitset>
#include <iomanip>
#include <stack>
#include <set>
#include <climits>
#define MAX_N 1000100
#define PI 3.141592653589
#define EPS 1e-20
#define BS 10
#define MOD 1000000007
#define ZERO 10001
#define YJSNPI 8101919
#define INF (1 << 28)
#define ADD(a, b) a = (a + (ll)b) % MOD
#define MUL(a, b) a = (a * (ll)b) % MOD
#define MAX(a, b) a = max(a, b)
#define MIN(a, b) a = min(a, b)

using namespace std;

typedef pair<int, int> pi;
typedef unsigned long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll N; int K;
mat R[40];
mat D[100][40];

void mat_init(mat& A, int a, int b) {
	A.resize(a);
	for(int i = 0; i < a; i++) A[i].resize(b, 0);
}

mat mul(const mat& A, const mat& B) {
	mat C(A.size(), vec(B[0].size()));
	for(int i = 0; i < A.size(); i++)
		for(int k = 0; k < B.size(); k++)
			for(int j = 0; j < B[0].size(); j++)
				ADD(C[i][j], A[i][k] * B[k][j]);
	return C;
}

void show_mat(const mat& A) {
	for(int i = 0; i < A.size(); i++) {
		for(int j = 0; j < A[0].size(); j++) {
			cout << A[i][j] << " ";
		}
		cout << endl;
	}
	cout << endl;
}

int main() {
	cin >> N >> K;
	int cnt = 0; ll nN = 1;
	while(nN <= N) { nN *= K; cnt++; }
	nN /= K;
	vector<int> d(cnt);
	for(int i = 0; i < cnt; i++) {
		d[i] = N / nN;
		N %= nN; nN /= K;
		//cout << d[i];
	}
	//cout << endl;
	for(int i = 0; i <= K; i++) mat_init(R[i], K + 1, K + 1);
	for(int i = 0; i <= cnt; i++)
		for(int j = 0; j < K; j++) mat_init(D[i][j], K + 1, K + 1);
	R[0][K][K] = 1; R[1][K][K] = 1;
	for(int j = 0; j < K; j++) R[0][j][j] = 1;
	for(int j = 0; j < K; j++) R[1][(j + K - 1) % K][j] = 1;
	for(int j = 2; j <= K; j++) R[j] = mul(R[j - 1], R[1]);
	for(int j = 0; j <= K; j++) D[0][0][j][j] = 1;
	for(int j = 0; j <= K; j++) D[0][0][j][0] = 1;
	for(int i = 0; i <= cnt; i++) {
		for(int j = 1; j < K; j++) {
			D[i][j] = mul(D[i][j - 1], mul(mul(R[K - j], D[i][0]), R[j]));
		}
		D[i + 1][0] = D[i][K - 1];
		//for(int j = 0; j < K; j++) show_mat(D[i][j]);
	}
	mat ans(1, vec(K + 1, 0)); ans[0][K] = 1;
	int md = 0;
	for(int i = 0; i < cnt; i++) {
		if(d[i] != 0) ans = mul(ans, mul(mul(R[K - md], D[cnt - i - 1][d[i] - 1]), R[md]));
		//show_mat(ans);
		md += d[i]; md %= K;
	}
	ll res = 0;
	//show_mat(ans);
	for(int i = 0; i < K + 1; i++) ADD(res, ans[0][i]);
	cout << res << endl;
}


//CLOCKS_PER_SEC
using namespace std;
#include<bits/stdc++.h>
#define sqr(x) (x)*(x)
//#define sort stable_sort
#define ll long long
#define mk make_pair
#define pb push_back
#define in insert
#define mtr(x,y,z) mk(mk(x,y),z)
#define fi first
#define se second
#define lch(x) ((x)<<1)
#define rch(x) (((x)<<1)|1)
#define all(x) (x).begin(),(x).end()
#define titose CLOCKS_PER_SEC
#define fpi(x) freopen(x,"r",stdin);
#define fpo(x) freopen(x,"w",stdout);
#define fprio fpi("in.txt");fpo("out.txt");
#define fast ios_base::sync_with_stdio(false);
inline void read(int &x){int v=0,f=1;char c=getchar();while (!isdigit(c)&&c!='-') c=getchar();if (c=='-') f=-1; else v=(c&15);while (isdigit(c=getchar())) v=(v<<1)+(v<<3)+(c&15);x=v*f;}
inline void read(ll &x){ll v=0ll,f=1ll;char c=getchar();while (!isdigit(c)&&c!='-') c=getchar();if (c=='-') f=-1; else v=(c&15);while (isdigit(c=getchar())) v=(v<<1)+(v<<3)+(c&15);x=v*f;}
inline void readc(char &x){char c;while (((c=getchar())==' ')||c=='\n');x=c;}
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vi vector<int>
#define vl vector<ll>
#define si set<int>
#define sl set<ll>
#define mii map<int,int>
#define mll map<ll,ll>
#define msi map<string,int>
#define msl map<string,ll>
#define piii pair<int,pii >
#define piipi pair<pii,int>
#define plll pair<ll,pll >
#define pllpl pair<pll,ll>
#define pqi priority_queue<int>
#define pql priority_queue<ll>
#define npqi priority_queue<int,vector<int>,greater<int> >
#define npql priority_queue<ll,vector<ll>,greater<ll> >
#define forup(i,a,b) for ((i)=(a);(i)<=(b);(i)++)
#define fordo(i,a,b) for ((i)=(a);(i)>=(b);(i)--)
#define rep(i,x) forup ((i),1,(x))
#define repd(i,x) fordo ((i),(x),1)
#define rep0(i,x) forup ((i),0,(x)-1)
#define rep0d(i,x) fordo ((i),((int)(x))-1,0)
#define itr iterator
#define fe(itcalc,c) for(__typeof((c).begin()) itcalc=(c).begin();itcalc!=(c).end();itcalc++)
#define NO {cout<<"NO";return 0;}
#define YES {cout<<"YES";return 0;}
#define y0 y000000000000000000000000000
#define y1 y111111111111111111111111111
#define j0 j000000000000000000000000000
#define j1 j111111111111111111111111111
#define cl0(a) memset((a),(0),(sizeof((a))))
#define clz(a) memset((a),(0x16),(sizeof((a))))
#define clf(a) memset((a),(-(0x16)),(sizeof((a))))
#define inf 0x3bbbbbbb
#define lnf 0x1bbbbbbbbbbbbbbbll
#define sqrt divi
#define p2(i) (1ll<<(i))
#define readi read
#define readll read
/*************************************************/
const int mod=1e9+7;
long long n,m,i,j,dig[75],cnt;
struct mat
{
	long long a[35][35];
}f[75][35],g,pre[35],suf[35];
mat mul(mat x,mat y)
{
	mat z;
	int i,j,k;
	forup(i,0,m){
		forup(j,0,m){
			z.a[i][j]=0;
			forup(k,0,m){
				z.a[i][j]=(z.a[i][j]+1ll*x.a[i][k]*y.a[k][j])%mod;
			}
		}
	}
	return z;
}
mat pw(mat x,long long y)
{
	mat z;
	int i,j;
	forup(i,0,m)forup(j,0,m){
		z.a[i][j]=(i==j);
	}
	while(y){
		if(y&1){
			z=mul(z,x);
		}
		x=mul(x,x);
		y/=2;
	}
	return z;
}
int main()
{
	read(n);read(m);
	long long tmp=n;
	cnt=-1;
	while(tmp){
		dig[++cnt]=tmp%m;
		tmp/=m;
	}
	g.a[0][m]=1;
	rep0(i,m)rep0(j,m+1) f[0][i].a[j][i]=f[0][i].a[j][j]=1;
	rep(i,cnt){
		pre[0]=f[i-1][0];
		rep(j,m-1){
			pre[j]=mul(pre[j-1],f[i-1][j]);
		}
		suf[m-1]=f[i-1][m-1];
		fordo(j,m-2,0){
			suf[j]=mul(f[i-1][j],suf[j+1]);
		}
		f[i][0]=suf[0];
		rep(j,m-1) f[i][j]=mul(suf[j],pre[j-1]);
	}
	int cur=0;
	fordo(i,cnt,0){
		while(dig[i]--){
			g=mul(g,f[i][cur]);
//			rep0(j,m+1) cerr<<g.a[0][j]<<' ';cerr<<endl;
//			rep0(j,m+1) cerr<<g.a[1][j]<<' ';cerr<<endl;
			cur=(cur+1)%m;
		}
	}
	int ans=0;rep0(i,m+1) ans=(ans+g.a[0][i])%mod;
	cout<<ans<<endl;
	return 0;
}