#include using namespace std; int main() { long long A,B,C; cin >> A >> B >> C; if(C==1){ cout << B*A << endl; return 0; } if(A v; long long D = C; while(A>0){ v.push_back(A%D); A /= D; } reverse(v.begin(),v.end()); long long ans = 0; long long cnt = v[0]*C+v[1]; if(v[1]==0) ans += 1; else if(2*C-2>=cnt) ans += 2; else ans += 3; ans += (int)v.size() - 2; for(int i = 2; i < v.size(); ++i) if(v[i]) ans += 1; cout << ans*B << endl; return 0; }