One day a highly important task was commissioned to Vasya — writing a program in a night. The program consists of n lines of code. Vasya is already exhausted, so he works like that: first he writes v lines of code, drinks a cup of tea, then he writes as much as  lines, drinks another cup of tea, then he writes  lines and so on: , , , ...The expression  is regarded as the integral part from dividing number a by number b.The moment the current value  equals 0, Vasya immediately falls asleep and he wakes up only in the morning, when the program should already be finished.Vasya is wondering, what minimum allowable value v can take to let him write not less than n lines of code before he falls asleep.

The input consists of two integers n and k, separated by spaces — the size of the program in lines and the productivity reduction coefficient, 1 ≤ n ≤ 109, 2 ≤ k ≤ 10.

Print the only integer — the minimum value of v that lets Vasya write the program in one night.

NoteIn the first sample the answer is v = 4. Vasya writes the code in the following portions: first 4 lines, then 2, then 1, and then Vasya falls asleep. Thus, he manages to write 4 + 2 + 1 = 7 lines in a night and complete the task.In the second sample the answer is v = 54. Vasya writes the code in the following portions: 54, 6. The total sum is 54 + 6 = 60, that's even more than n = 59.

#include<stdio.h>
#include<math.h>

int x;

int bs(int low,int high,int n,int k)
{
	if(low>high)
	return x;

	int i=0;
	int sum=0;
	int mid=(low+high)/2;

//	printf("%d %d %d\n",low,mid,high);

	while((mid/(int)(pow(k,i))))
	{
		sum+=(mid/(pow(k,i)));
//		printf("%d ",sum);
		i++;
//		printf("%d\n",i);
	}

//	printf("x\n");

	if(sum==n)
	return mid;
	else if(sum<n)
	{
		bs(mid+1,high,n,k);
	}
	else
	{
		x=mid;
		bs(low,mid-1,n,k);
	}
}

int main()
{
	int n,k;

	scanf("%d%d",&n,&k);

	printf("%d\n",bs(1,n,n,k));

	return 0;
}

#include<stdio.h>
int main()
{
long long int n,k,low,mid,high,sum,z,p;
scanf("%I64d %I64d",&n,&k);
low=0;
high=n;
while(low<=high)
{mid=(low+high)/2;
sum=0;
z=mid;
while(z!=0)
{sum+=z;
z/=k;}
if(sum>=n)
{p=mid;
high=mid-1;}
else
low=mid+1;}
printf("%I64d",p);
return(0);}

#include <stdio.h>
#include <stdlib.h>

int main()
{
    long int n,i, approximate, v, sum = 0;
    int k , j = 0;

    scanf("%ld", &n);
    scanf("%d", &k);
    approximate = ( n * ( 1 - (1.0 / k)));

    i = approximate;
    while( i <= n)
    {
        j = 0, sum = 0;

        while ( (i / (pow( k , j ))) != 0)
        {
            sum += (i / (pow( k , j )));
            j++;
        }
        if ( sum >= n)
        {
            v = i;
            break;
        }
        i++;
    }
    printf("%ld", v);

    return 0;
}

#include<stdio.h>
/* prob b codeforces */
int n,k;
int check(int temp)
{
    long long t=1,res=0;
    while(temp/t)
    {
                 res+=temp/t;
                 t=t*k;
                 }
    if(res>=n)
    return 1;
    else
    return 0;
}

void bsearch()
{
     int mid,l=1,r=n,res;
      while(l<=r)
      {
               mid=(l+r)/2;
               if(check(mid))
               { r=mid-1; res=mid;
               }
               else
               {
                   l=mid+1;
                   }
                   }
                   printf("%d\n",res);
                   }
int main()
{
    scanf("%d%d",&n,&k);
    bsearch();
    //getch();
    return 0;
}

#include<stdio.h>
#include<stdlib.h>

int main()
{
    long long  n, k, a, b, c, x, y;
    scanf("%lld %lld", &n, &k);
    if(n<=k){
        printf("%lld", n);
        return 0;
    }
    if(n==1){
        printf("1");
        return 0;
    }
    a=0;b=n;
    while(a+1<b){
        c=(a+b)/2;
        x=0;y=c;
        while(y>0){
            x+=y;
            y/=k;
        }
        if(x>=n) b=c;
        else a=c;
    }
    printf("%lld", b);

    return 0;
}

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int k =0    ;
    long long n =0  , first = 0 , mid = 0 , last=0 , x= 0 , sum = 0  , answer =0 , red = 0;
    scanf("%I64d %d" , &n , &k);
    first = 0 ;
    last = n ;
    mid = 1 ;
    if (n / k ==0 )
        answer = n ;
    while (last > first )
    {
        sum  = 0 ;
        red = 1 ;
        x = 1 ;
        mid =  first +(last - first)/ 2 ;
        while (x> 0)
        {
            x = mid/red;
           sum += x;
           red *= k ;

        }

        if (sum >= n ){

                    answer = mid ;
                    last = mid ;

        }

            else
              first = mid +1;
    }
    printf("%I64d", answer);

    return 0;
}

#include <stdio.h>
#include <stdlib.h>

int main()
{
    long long  n  , mid  , min  , low , high  , x  , sum = 0 , y ;
    int k   ;
    scanf("%I64d",& n);
    scanf("%d",&k);

    low = 0 ;
    high= n ;

     if (n/k==0)
        min = n;
    while (high  >  low){
       mid = low+(high-low)/2 ;
       x = mid ;
       sum = x ;
       y= k ;
      while (x/y!=0){
        sum +=x/y;
        y*=k;
      }
      if (sum  >=n ){
         min = mid ;
        high = mid ;

      }
      else

        low = mid+1;

    }
    printf("%I64d",min);

    return 0;
}

#include<stdio.h>
int main()
{
  unsigned long long int a,b, i=1,k,k1,sum=0;
 // printf("Enter the size of the program in lines\n");
  scanf("%lld",&a);
 //printf("Enter the productivity reduction coefficient\n");
  scanf("%lld",&b);
  k1=b;
  for(i=a-(a/b);i<=a;i++)
  {
    sum=i;
     while(1)
     {
          k=i/k1;
          if(k==0)
            break;
         sum=sum+k;
          k1=k1*b;
     }
     if(sum>=a)
       break;
    sum=0;
    k1=b;
  }
  //printf("The minimum value = %lld",i);
  printf("%lld",i);
  return 0;
}

# include <stdio.h>
int f(int x,int m)
{
    int m2,r;
    r = 0;
    for(m2 = 1;(x/m2) > 0; m2 = m2*m) r = r+(x/m2);
    return r;
}
int main()
{
    int n,k,v,a,b,g;
    scanf("%d %d",&n,&k);
    a = 1;
    b = n;
    v = (a+b)/2;
    g = f(v,k);
    while(b-a>1)
    {
        if(g<n) a = v;
        else b = v;
        v = (a+b)/2;
        g = f(v,k);
    }

        g = f(a,k);
        if(g>=n)
            printf("%d",a);
        else
            printf("%d",b);
    return 0;
}

#include <stdio.h>
#include <math.h>
int main(void)
{
	long long n,v,k,min,t,i,p,r;

	scanf("%I64d %I64d",&n,&k);
	v=min=n;
	p=1;r=n;

	while(r>=p)
	{
		t=0;
		for(i=1;v/i>0;i*=k)
		{
			t+=v/i;
			if(t>=n) break;
		}
		if(t>=n)
		{
			if(v<min) min=v;
			r=v-1; v=(p+r)/2;
		}
		else{ p=v+1; v=(p+r)/2; }
	}
	printf("%I64d",min);
	return 0;
}

#include <stdio.h>
#include <stdlib.h>

int main()
{
    long long n ;
    int k , flag = 1 ;
    scanf("%I64d %d", &n , &k) ;
    long long v = n * (k-1) / k ;
    while (flag)
    {
        long long sum = v ;
        long long term = v / k ;
        while (term > 0)
        {
            sum += term ;
            term /= k ;
        }
        if (sum >= n)
        {
            break ;
        }
        v++ ;
    }
    printf("%I64d", v) ;
    return 0;
}

#include<stdio.h>
typedef long long int L;
int k;
L i,s,n;
int check(L m)
{
    s=0;
    for(i=m;i>0;i=i/k)
    {
        s=s+i;
        if(s>=n) return 1;
    }
    return 0;
}
int main()
{
    L low=1,high,mid;
    scanf("%I64d%d",&n,&k);
    high=n;
    while(low<high)
    {
        mid=(low+high)/2;
        if(check(mid))
        high=mid;
        else
        low=mid+1;
    }
    printf("%I64d\n",high);
    return 0;
}

#include <stdio.h>

#define INT_MAX 0x3fffffffL

int fx(int v, int k)
{
	int sum = 0, t = k;

	while(v) {
		sum += v;
		v /= t;
	}
	return sum;
}

int solve(int n, int k)
{
	int sum, l = 1, r = n, mid, ans = INT_MAX, dt = INT_MAX;

	while (l <= r) {
		mid = (l + r) >> 1;
		sum = fx(mid, k);
		/* printf("sum = %d mid = %d\n", sum, mid); */
		if (sum >= n) {
			if (sum - n <= dt && ans > mid)
				ans = mid;
			r = mid - 1;
		} else {
			l = mid + 1;
		}
	}
	/* printf("ans = %d l = %d r = %d sum = %d\n", ans, l, r, sum); */
	return ans;
}

int main(void)
{
	int n, k;

	while (2 == scanf("%d%d", &n, &k)) {
		printf("%d\n", n < k ? n : solve(n, k));
	}

	return 0;
}

#include <stdio.h>
int func(int n, int m, int x)
{
    int i, sum = 0;
    for(i = 0; i <= n ; i++)
    {
        sum = sum + x / pow(m, i);
    }
    return sum;
}
int main()
{
    int n, m, i, j, d;
    while(scanf("%d %d", &n, &m) != EOF)
    {
        int tempn = n, sum = 0;
        i = 0;
        while(pow(m, i) <= n)
        {
            i++;
        }
        i--;
        n = n - (n / m);
        while(sum < tempn)
        {
            sum = func(i, m, n);
            n++;
        }
        n--;
        printf("%d\n", n);
    }
}

#include<stdio.h>

int main()
{
    int n,k,i,j,lo,mid,hi,ans, tmp;
    scanf("%d %d",&n,&k);
    lo =  k,  hi = n, tmp = n;
    while( lo <= hi )
    {
        mid = (lo+hi)/2;
        /*  printf("mid = %d\n",mid);
            ans = 0, i=mid;
            for(i=1; ans<n && (mid/i) ; i*=k)
            {
                ans += mid / i;
            }
        */
        ans = 0, i=mid;
        while(i)
        {
            ans+=i;
            i/=k;
        }
        /*printf("ans = %d\n",ans);*/
        if(ans<n) lo = mid+1;
        else
        {

            if(mid<tmp) tmp=mid;
            hi = mid -1;
        }
    }
    printf("%d\n",tmp);

    return 0;
}

#include<stdio.h>
int main() {
    int a, b, c, d, i;
    scanf("%d%d", &a, &b);
    c=a-a/b;
    d=a;
    while(d>0) {
        d=a, i=c;
        while(i>0)
        {
            d-=i;
            i/=b;
        }
        if(d>0)
        c++;
    }
    printf("%d\n", c);
    return 0;
}

/*  AUTHOR:AKASH JAIN
*   USERNAME:akash19jain
*   TIME: 30-10-2019 13:43:09
*/
// #include<algorithm>
// #include <bits/stdc++.h>
// using namespace std;

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<stdbool.h>
#include<ctype.h>
#define SC1(x)          scanf("%lld",&x)
#define SC2(x,y)        scanf("%lld%lld",&x,&y)
#define SC3(x,y,z)      scanf("%lld%lld%lld",&x,&y,&z)
#define SCS(x)          scanf("\n%s", x)
#define SCA(a,n)        for(long long i=0;i<n;i++)    scanf("%lld",&a[i]);
#define PF1(x)          printf("%lld\n",x)
#define PF2(x,y)        printf("%lld %lld\n",x,y)
#define PF3(x,y,z)      printf("%lld %lld %lld\n",x,y,z)
#define PFA(a,n)        for(long long i=0;i<n;i++)    printf("%lld",a[i]); printf("\n");
#define PFN                printf("\n")
#define REP(i,n)        for(long long i=0;i<(n);++i)
#define FOR(i,a,b)      for(long long i=(a);i<=(b);++i)
#define FORD(i,a,b)     for(long long i=(a);i>=(b);--i)
#define WHILE(n)        while(n--)
#define MEM(a, b)       memset(a, (b), sizeof(a))
#define ITOC(c)         ((char)(((ll)'0')+c))
#define MID(s,e)        (s+(e-s)/2)
#define SZ(a)            strlen(a)
#define MOD             1000000007
#define MAX             10000000005
#define MIN             -10000000005
#define PI              3.1415926535897932384626433832795
#define DEB(x)          printf("The value of \"%s\" is: %d\n",#x,x)
#define CASES           ll t;SC1(t);while(t--)
#define ABS(a)          ((a>0)?a:-(a))
#define SWAP(a,b)        ll z=a;a=b;b=z
#define SWAPC(a,b)        char z=a;a=b;b=z

const int INF = 1<<29;
typedef long long ll;
typedef unsigned long long ull;
#define FILEIO(name) \  freopen(name".in", "r", stdin); \   freopen(name".out", "w", stdout);
int cmp(const void * a,const void * b);
long long maxv(long long a,long long b);
long long minv(long long a,long long b);
long long gcd(long long u,long long v);
long long digits(long long n);
bool ispoweroftwo(long long n);
bool isvowel(char x);
ll chartoint(char ch);
ll CEIL(ll x,ll y);
ll FLOOR(ll x,ll y);

ll check(ll v,ll k)
{
    ll sum=0;
    while(v)
    {
        sum+=v;
        v=v/k;
    }
    return sum;
}
int main()
{
    ll n,k;
    SC2(n,k);
    ll l=0,r=1e9,res=0,mid=0;
    while(l<=r)
    {
        mid=(l+r)/2;
        if(check(mid,k)>=n)
        {
            res=mid;
            r=mid-1;
        }
        else
            l=mid+1;

    }
    PF1(res);

    return 0;
}

//qsort(arr,n,sizeof(arr[0]),cmp);
int cmp (const void * a, const void * b)
{
    if( *(ll*)a - *(ll*)b < 0 ) return -1;
    if( *(ll*)a - *(ll*)b > 0 ) return 1;
        return 0;
}
long long maxv(long long a,long long b)
{
    if(a>b) return a;
        return b;
}
long long minv(long long a,long long b)
{
    if(a<b) return a;
        return b;
}
long long gcd(long long u,long long v)
{
    if (v == 0) return u;
       return gcd(v, u%v);
}
long long digits(long long n)    //to calculate no of digits in a number
{
    return floor(log10(n))+1;
}
bool ispoweroftwo(long long x)
{
    return x && (!(x&(x-1)));
}
bool isvowel(char x)
{
    return(x=='a' || x== 'e' ||x=='i' || x== 'o' || x=='u' );
}
ll chartoint(char ch)
{
    if(ch>='A' && ch<='Z') return(ch-'A');
    else if(ch>='0' && ch<='9') return (ch-'0');
    else if(ch>='a' && ch<='z') return (ch-'a');
    else return 0;
}
ll CEIL(ll x,ll y)
{
    if(x%y==0) return(x/y);
    else return(x/y +1);
}

ll FLOOR(ll x,ll y)
{
    if(x%y==0) return(x/y);
    else return(x/y -1);
}

#include <stdio.h>

int calc(int t, int k)
{
	int res,tk;
	res=t;
	for(tk=k;t/tk>0;tk*=k){res += t/tk;}
	return res;
}

int main()
{
	int n,k,ln,rn,lv,rv,tmp,val;
	scanf("%d %d",&n,&k);
	ln=1;
	rn=n;
	lv=0;
	rv=0;
	while(1){
		tmp=(ln+rn)/2;
		val=calc(tmp,k);
		if(val < n){
			ln=tmp;
			lv=-1;
		}else if(val==n){
			break;
		}else{
			rn=tmp;
			rv=val;
		}
		if(rn-ln==1) {tmp=rn;break;}
	}
	printf("%d\n",tmp);
	return 0;
}

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int n,k,sum,i,mid=1,ans=1;
    scanf("%d %d",&n,&k);
    int low=1;
    int high=n;
    int ok=1;
    while(low<=high&&ok==1)
    {
        if(low==high)ok=0;
        int mid=(low+high)/2;
        sum=mid;
        for(i=k; mid/i>0; i*=k)
            sum+=mid/i;
        if(sum<n)
            low=mid+1;
        if(sum>=n)
        {
            high=mid-1;
            ans=mid;
        }
    }
    printf("%d\n",ans);
    return 0;
}

#include <stdio.h>

long long int num_lines(long long int v, long long int k)
{
		long long int sum = v;
		long long int d = k;
		while (v/d != 0){
				sum += (v/d);
				d*=k;
		}
		return sum;
}

int main(void)
{
		long long int n, k, mid, l, r, num, tmp;
		scanf("%I64d %I64d ", &n, &k);

		l = 1;	r = n; tmp = n;
		while (l <= r){
				mid = (l+r)/2;
				num = num_lines(mid, k);

				if (num < n)
						l = mid+1;
				else{
						if (mid < tmp)
								tmp = mid;
						r = mid-1;
				}
		}
		printf("%I64d\n", tmp);

}

/* Coached by rainboy */
#include <stdio.h>

int lines(int n, int k) {
	return n == 0 ? 0 : lines(n / k, k) + n;
}

int binsearch(int n, int k) {
	int lower, upper, v;

	lower = 0, upper = n;
	while (upper - lower > 1) {
		v = (lower + upper) / 2;
		if (lines(v, k) < n)
			lower = v;
		else
			upper = v;
	}
	return upper;
}

int main() {
	int n, k;

	scanf("%d%d", &n, &k);
	printf("%d\n", binsearch(n, k));
	return 0;
}

#include <stdio.h>

int lines(int n, int k) {
	return n == 0 ? 0 : lines(n / k, k) + n;
}

int binsearch(int n, int k) {
	int low, high, mid, l, v;

	low = 1;
	high = n;
	v = -1;
	while (low <= high) {
		mid = (low + high) / 2;
		l = lines(mid, k);
		if (l < n)
			low = mid + 1;
		else
			v = mid, high = mid - 1;
	}
	return v;
}

int main() {
	int n, k;

	scanf("%d%d", &n, &k);
	printf("%d\n", binsearch(n, k));
	return 0;
}

#include<stdio.h>
int k;
long long burn(long long n)
{
    long long sum=0,i=1,div=1;
    while(div!=0)
    {
        div=n/i;
        sum+=div;
        i=i*k;
    }

    return sum;
}

int main()
{

    long long min=1000000000000,high,n,low,mid,dif;
    scanf("%lld %lld",&n,&k);

    low=0;
    high=n*10;
    while(low<=high)
    {
        mid=low+(high-low)/2;
        dif=burn(mid);
        if(dif==n) { printf("%lld\n",mid); break; }
        if(n>dif) low=mid+1;
        else { high=mid-1; if(mid<min) min=mid; }

    }
    if(low>high) printf("%lld\n",min);

    return 0;
}

#include <stdio.h>
#include <stdlib.h>
int lines(int n, int k)
{
	return n==0?0:lines(n/k,k)+n;
}
int binsearch(int n, int k)
{
	int lower,upper,v;
	lower=0,upper=n;
	while (upper - lower > 1)
    {
		v = (lower + upper) / 2;
		if (lines(v, k) < n)
			lower = v;
		else
			upper = v;
	}
	return upper;
}

int main() {
	int n, k;
	scanf("%d%d", &n, &k);
	printf("%d\n", binsearch(n, k));
	return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

int64_t n, k;

int works(int64_t v) {
    int64_t sum = 0;
    int64_t den = 1;
    while ((v / den) > 0) {
        sum += v / den;
        den *= k;
    }
    return sum >= n;
}

int main() {
    scanf("%ld %ld", &n, &k);
    int64_t lo = 0;
    int64_t hi = 1;

    while (!works(hi))
        hi *= 2;

    while(hi - lo > 1) {
        int64_t mid = lo + (hi - lo) / 2;
        if(works(mid)) {
            hi = mid;
        } else {
            lo = mid;
        }
    }

    printf("%ld\n", hi);

    return 0;
}

#include<stdio.h>
#include<math.h>
long long int power(long long int a, long long int b){
    if(b == 0)
        return -1;
    long long int temp = power(a,b/2);
    if(b%2 != 0)
        return temp*temp*a;
    else
        return temp*temp;
}
int main()
{
   long long int  n,i=0,k,j;
    scanf("%lld %lld",&n,&k);

   long long int m=(n*(k-1))/k;
long long int sum=m;
  j=1;
    while(sum<n)
    {
         sum=m;
         while(m/power(k,j)>=1&&sum<n)
         {
            sum+=m/power(k,j);
            j++;
         }
        j=1;
        m++;
  }
     printf("%lld\n",m-1);
    return 0;
}

#include <stdio.h>
#include <stdlib.h>

#define fo(a,b,c) for(a=(b);a<=(c);a++)
#define of(a,b,c) for(a=(b);a>=(c);a--)

int readInt() {
   int ret;
   scanf("%d", &ret);
   return ret;
}

int f(int base, int x) {
   if (!base) return 0;
   return base + f(base / x, x);
}

int main() {
   int n = readInt(), i, j;
   int k = readInt();
   int hi = n;
   int ans = n;
   int lo = 1;
   while (hi >= lo) {
      int mid = (hi + lo) / 2;
      int amt = f(mid, k);
      if (amt >= n) {
         ans = mid;
         hi = mid - 1;
      } else {
         lo = mid + 1;
      }
   }
   printf("%d\n", ans);
   return 0;
}

#include <stdio.h>
int main()
{
	int n,k,l,r,h,sum,min,t;
	scanf("%d %d",&n,&k);
	//printf("%d %d \n",n,k);
	l=1;
	r=n*2;
	sum=0;
	min=n;
	while (l!=r)
	{
		h=(l+r)/2;
		//printf("h = %d \n",h);
		// s4itaem
		t=1;
		sum=0;
		while ((h/t)>0)
		{
			sum=sum+(h/t);
			t=t*k;
		}
		//printf("sum = %d \n",sum);
		if (sum<n) {l=h+1;}
	    else
		{
				min=h;
				r=h;
		}

	}
	printf("%d",min);
	return 0;
}

#include<stdio.h>
#include<math.h>
#include<limits.h>
long long int min=LONG_MAX,n,k,tut1,tut2,tut3;
void islem(int bas,int orta,int son)
{
		if(bas==tut1 && orta==tut2 && son==tut3) return ;
		tut1=bas;
		tut2=orta;
		tut3=son;
      long long int carp=1,tut=1,sonuc=0;
//      printf("bas%d son%d orta%d\n",bas,son,orta);

		for( ; tut!=0 ;carp*=k)
		{
			tut=ceil(orta/carp);
			sonuc+=tut;
		}
		if(sonuc>=n)
		{
			if(orta<min) min=orta;
			islem(bas,(bas+orta)/2,orta);
		}
		if(sonuc<n)
			islem(orta,(orta+son)/2,son);
}
void oku ()
{
	scanf("%lld %lld",&n,&k);

	islem(1,(n+500)/2,n+500);
	printf("%lld\n",min);
}

int main ()
{
	oku ();
	return 0;

}

typedef long long LL;
LL i,j,k,n;
int solve(LL x){
    j = 0;i = 0;
    for(j = x;j;j/=k){
         i += j;
         if(i >= n) return 1;
    }
    return 0;
}
int main(){
    scanf("%I64d%I64d",&n,&k);
    LL low = 1,high = 1LL<<31;
    LL mid;
    int idx = 0;
    while(low < high){
       // printf("%I64d %I64d %I64d\n",low,mid,high);
        idx += 1;
        mid = low + ((high - low)>>1);
        if(solve(mid)){
             high = mid;
            // printf("low\n");
        }
        else{
             low = mid + 1;
            // printf("high\n");
        }
    }
    printf("%I64d\n",low);
    return 0;
}

#include <stdio.h>
#include <stdlib.h>

int main()
{
	int n, k, v, count = 1, sum = 0, test = 1;
	scanf("%d %d", &n, &k);
	v = n / k * (k - 1);
    while (test){
        while (v/count > 0){
            sum += v/count;
            count *= k;
        }
        if (sum >= n)
            break;
        v++;
        count = 1;
        sum = 0;
    }
	printf("%d", v);
	return 0;
}

#include <stdio.h>
#include <stdlib.h>

int main()
{
   long long n , x =1 ,  first , last   , sum=0 , temp = 0 , mid  , temp2 ;
   int k  , j  ;
   scanf("%I64d ",&n);
   scanf("%d",&k);
   if(n<=k){
    printf("%I64d",n);
   }else{
       first = k ;
       last =(int) n ;
       while(last>=first){
         mid = ((last-first)/2) +first ;
         sum = mid ;
         x=1 ;
              for(j = 0 ; mid/x && sum <= n; j++){
                x*=k;
                sum+=mid/x;
               }
               if(sum==n){
                printf("%d",mid);
                break ;
               }else if (sum < n) {
                  if(temp!=0&&first==last){
                   printf("%d",temp);
                    break;
                   }
                    first = mid + 1 ;
               }else {
                   temp = mid ;
                   temp2 = sum ;
                   last = mid -1  ;

               }
               if(last<first){
                      printf("%d",temp);
                      break ;
                   }
       }
   }
    return 0;
}

#include"stdio.h"
#include"string.h"
#include"ctype.h"
#include"stdlib.h"
int getline(int lim,char s[]){
	char c;
	int i=0;
	while(isspace(c=getchar()));
	for(i=0;i<lim && (c)!='\n' && !isspace(c);i++,c=getchar())
		s[i]=c;
	s[i]='\0';
	return i;
}
struct node{
	int v;
	struct node *right;
	struct node *left;
};
struct node *add(struct node *root,int val){
	if(root==NULL){
		root=(struct node *)malloc(sizeof(struct node));
		root->v=val;
		root->left=root->right=NULL;
	}else if(root->v>val)
		root->left=add(root->left,val);
	else
		root->right=add(root->right,val);
	return root;
}
int f(int n,int k){
	int sum=0;
	while(n!=0){
		sum+=n;
		n/=k;
	}
	return sum;
}
main(){
	int i,sum=0,k,c,u,lo,mid;
	struct node *root;
	long n;
	char l[20];
	getline(20,l);
	n=atoi(l);
	getline(20,l);
	k=atoi(l);
	//printf("%d %d",n,k);
	lo=1;
	u=n;
	while(lo<u){
		mid=(lo+u)/2;
		if(f(mid,k)<n)
			lo=mid+1;
		else
			u=mid;
	}
	printf("%d",lo);
	return 0;

}

#include <stdio.h>

int n, k;

int ok(int x)
{
    int sum = 0;

    while (x) {
        sum += x;
        x /= k;
    }

    if (sum >= n) {
        return 1;
    } else {
        return 0;
    }
}

int main()
{
    int l, r, m;

    scanf("%d %d", &n, &k);

    l = 0, r = n + 1, m = (l + r) / 2;

    while (r - l > 1) {
        if (ok(m)) {
            r = m;
            m = (l + r) / 2;
        } else {
            l = m;
            m = (l + r) / 2;
        }
    }

    printf("%d\n", r);

    return 0;
}

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
/* Function to merge the two haves arr[l..m] and arr[m+1..r] of array arr[] */

void merge( int arr[],   int l,  int m,   int r)
{
    int i, j, k;
    int n1 = m - l + 1;
    int n2 =  r - m;

    /* create temp arrays */
    int L[n1], R[n2];

    /* Copy data to temp arrays L[] and R[] */
    for(i = 0; i < n1; i++)
        L[i] = arr[l + i];
    for(j = 0; j < n2; j++)
        R[j] = arr[m + 1+ j];

    /* Merge the temp arrays back into arr[l..r]*/
    i = 0;
    j = 0;
    k = l;
    while (i < n1 && j < n2)
    {
        if (L[i] <= R[j])
        {
            arr[k] = L[i];
            i++;
        }
        else
        {
            arr[k] = R[j];
            j++;
        }
        k++;
    }

    /* Copy the remaining elements of L[], if there are any */
    while (i < n1)
    {
        arr[k] = L[i];
        i++;
        k++;
    }

    /* Copy the remaining elements of R[], if there are any */
    while (j < n2)
    {
        arr[k] = R[j];
        j++;
        k++;
    }
}

/* l is for left index and r is right index of the sub-array
  of arr to be sorted */
void mergeSort( int arr[], int l, int r)
{
    if (l < r)
    {
        int m = l+(r-l)/2; //Same as (l+r)/2, but avoids overflow for large l and h
        mergeSort(arr, l, m);
        mergeSort(arr, m+1, r);
        merge(arr, l, m, r);
    }
}

long long int fact(int n)
{
    if(n==1 || n==0)
        return 1;
    else
        return (n)*fact(n-1);
}

int min(int a[],int n)
{
    int i=0,max=10000,c;
    for(i=0;i<n;i++)
    {
        if(a[i]<max)
        {
            max=a[i];
            c=i;
        }
    }
    return c;
}
int max(int a[],int n)
{
    int i=0,max=-1,c;
    for(i=0;i<n;i++)
    {
        if(a[i]>max)
        {
            max=a[i];
            c=i;
        }
    }
    return c;
}

long long int ifitis(int n,int v,int r)
{
    long long int sum=v,flag=0;
    while((v/r)!=0)
    {
        //printf("%d ",v/r);
        sum+=floor(v/r);
        v=v/r;

    }
    printf("\n");
    return sum;
}

//374+187+93+46+23+11+5+2+1

int main() {
  long long int v,n=0,y,z,m=0,min=-1,r,o=0,e=0,sum=0,flag=0,a[1000],count=0,x=-1,b[10000]={0},c[100][100]={{0}},k=0,d[10000],i=0,j=0;
    scanf("%I64d %I64d",&n,&r);
    v=floor(n*(r-1)/r);
    for(i=v;i<v+10;i++)
    {
        //printf("%d %d\n",i,ifitis(n,i,r));
        if(ifitis(n,i,r)>=n)
            break;
        else
            continue;
    }
    printf("%I64d",i);

  return 0;
}

#include <stdio.h>
    int n,k,i=0,gereksiz[100],t=0;

void binary(int s, int f){
           int h,temp=0,div=1;
           h= (s+f)/2;
           if(h == s) h=f;
           while(h/div >0){
                            temp = temp +h/div;
                            div = div * k;
                            }
           if(temp == n){ gereksiz[i] = h;i++;}
           if(h == s || h == f || t) return;
           if(h == s || h == f) t++;
           if(temp>=n){gereksiz[i] = h; i++; binary(s,h);}
           if(temp<n) binary(h,f);
           return;
           }
int main()
{
    int j,min;
    scanf("%d%d", &n,&k);
    binary(1,n);
    min = gereksiz[0];
    for(j=1;j<i;j++) if(gereksiz[j] < min) min = gereksiz[j];
    printf("%d", min);
    return 0;
}

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include<math.h>

int sum(int i,int k){
	int su = i,j;
	for(j = 0;;j++){
		i /= k;
		if(i <= 0)
			return su;
		else{
			su += i;
		}
	}
}

int qiui(int tem,int right,int k,int n){
	if(tem == right+1 || tem == right) return tem;
	if(sum((tem+right)/2,k) < n){
		return qiui(tem,(tem+right)/2,k,n);
	}else{
		return qiui((tem+right)/2,k,k,n);
	}
}

int main(void){
    int n,k,i;
    scanf("%d%d",&n,&k);
    if(n <= k)
    	printf("%d",n);
    else{
		int tem;
		tem = n / k * k;
		printf("%d\n",qiui(tem,k,k,n));
	}
    /*system("pause");*/
    return 0;
}

//      Burning Midnight Oil.c
//
//      Copyright 2012 Administrator <cpy@ubuntu>
//
//      This program is free software; you can redistribute it and/or modify
//      it under the terms of the GNU General Public License as published by
//      the Free Software Foundation; either version 2 of the License, or
//      (at your option) any later version.
//
//      This program is distributed in the hope that it will be useful,
//      but WITHOUT ANY WARRANTY; without even the implied warranty of
//      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//      GNU General Public License for more details.
//
//      You should have received a copy of the GNU General Public License
//      along with this program; if not, write to the Free Software
//      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
//      MA 02110-1301, USA.

#include <stdio.h>

#define INF 1 << 29

typedef long long ll;

int check(int n, int m, int k)
{
    int s = m;
    ll t = k;
    if (s >= n)
    {
        return 1;
    }
    while (m >= t)
    {
        s += m / t;
        if (s >= n)
        {
            return 1;
        }
        t *= k;
    }
    return 0;
}

void func(int n, int k)
{
    int l, r, m, ans = INF;
    l = 1;
    r = n;
    while (l <= r)
    {
        m = (l + r) >> 1;
        if (check(n, m, k))
        {
            ans = m;
            r = m - 1;
        }
        else
        {
            l = m + 1;
        }
    }
    printf("%d\n", ans);
}

int main(int argc, char **argv)
{
    int n, k;
    while (scanf("%d%d", &n, &k) == 2)
    {
        func(n, k);
    }
    return 0;
}

#include <stdio.h>

int good(int n, int v, int k) {
	long long p = 1;

	while (v >= p) {
		n -= v / p;
		p *= k;
	}
	return n <= 0;
}

int main() {
	int n, k, mid, min, max;

	scanf("%d%d", &n, &k);
	min = 0, max = n;
	while (min + 1 < max) {
		mid = (max + min) / 2;
		if (good(n, mid, k))
			max = mid;
		else
			min = mid;
	}
	printf("%d\n", max);
	return 0;
}

#include <stdio.h>

int cnt(long long n, long long k) {
	long long i, sum = 0;

	for (i = 1; i <= n; i *= k)
		sum += n / i;
	return sum;
}

long long binsearch(long long n, long long k) {
	long long mid, low, high, c, min = n;

	low = 0, high = n;
	while (low <= high) {
		mid = (low + high) / 2;
		c = cnt(mid, k);
		if (c >= n && min > mid)
			min = mid;

		if (c < n)
			low = mid + 1;
		else if (c > n)
			high = mid - 1;
		else
			break;
	}
	return min;
}

int main() {
	long long n, k;

	scanf("%lld%lld", &n, &k);
	printf("%lld\n", binsearch(n, k));
	return 0;
}

#include <stdio.h>
int main(){
    int n,k,v,t,s;
    scanf("%d%d",&n,&k);
    for(v=n-n/k;;v++){
                  s=v;
                  t=v/k;
                  while(t){
                           s+=t;
                           t/=k;
                           }
                  if(n<=s)break;
    }
    printf("%d",v);
    return 0;
}

#include <stdio.h>

int k;

int write(int v)
{
    long long n, p;
    for(n = v, p = k; v / p; p *= k)
        n += v / p;
    return (int) n;
}

int main(int argc, char *argv[])
{
    int n, l, u, m;

    scanf("%d %d", &n, &k);

    l = 1, u = n;
    do
    {
        m = (l + u) / 2;
        if(write(m) >= n && write(m - 1) < n)
        {
            printf("%d\n", m);
            return 0;
        }

        if(write(m) >= n)
            u = m - 1;
        else
            l = m + 1;
    } while(1);

    return 0;
}

#include <stdio.h>
#include <stdlib.h>

long main(void)
{
	long long int n, k;
	scanf("%lld%lld", &n, &k);
	long long int temp, v = n, sum, dv = 1;
	int i;
	for(i = 1; i < k; i++)
	{
		sum = 0;
		temp = i;
		while(sum < n)
		{
			sum = sum + temp;
			temp = temp*k;
		}
		temp = temp/k;
		if(temp < v)
		{
			v = temp;
		}
	}
	if(n > 9)
	{
		dv = 10;
	}
	if(n > 99)
	{
		dv = 100;
	}
	if(n > 999)
	{
		dv = 1000;
	}
	while(1)
	{
		v = v - dv;
		temp = v;
		sum = 0;
		while(temp != 0)
		{
			sum = sum + temp;
			temp = temp/k;
		}
		if(sum < n)
		{
			break;
		}
	}
	for(i = 1; i <= dv; i++)
	{
		sum = 0;
		v++;
		temp = v;
		while(temp != 0)
		{
			sum = sum + temp;
			temp = temp/k;
		}
		if(sum >= n)
		{
			break;
		}
	}
	printf("%lld\n", v);
	return 0;
}

#include <stdio.h>

int canFinish(int v, int k)
{
    int res = 0;

    while (v != 0){
        res += v;
        v /= k;
    }

    return (res);
}

int main(void)
{
    int n, k;
    int left, right, center;

    scanf("%d%d", &n, &k);

    left = 1;
    right = 1000000000;

    while (left != right){
        center = (left + right) / 2;

        if (canFinish(center, k) >= n){
            right = center;
        }
        else {
            left = center + 1;
        }
    }

    printf("%d\n", left);

    return (0);
}

#include <stdio.h>

typedef __int64 LL;

LL Cal(LL v,LL k)
{
    LL sum=v,b=k;
    while(v>=b)
    {
        sum+=v/b;
        b*=k;
    }
    return sum;
}

int main()
{
    LL N,K,Left,Right,Mid;
    scanf("%I64d %I64d",&N,&K);
    Left=1;
    Right=K*N;
    while(Right>Left)
    {
        Mid=(Left+Right)/2;
        if(Cal(Mid,K)<N)
        {
            Left=Mid+1;
        }
        else
        {
            Right=Mid;
        }
    }
    printf("%d\n",Left);
    return 0;
}

#include<stdio.h>
int main() {
	int n,k;
	scanf("%d%d",&n,&k);
	long long v=(long long)(k-1)*(long long)n/k;
	long long result=v;
	//printf("v=%d\n",v);
	while(1){
		long long sum=0;
		long long kk=1;
		while(1){
			if(v<kk)break;
			sum+=(v/kk);
			kk*=k;
		}
		//printf("sum=%d\n",sum);
		if(sum>=n){
			result=v;
			break;
		}
		v++;
	}
	printf("%lld\n",result);
	return 0;
}

#include <stdio.h>
#include <stdlib.h>
int isright(long long v,long long k,long long n)
{
    long long sum=v;
    long  long value =v/k;
    while(value!=0)
    {
        sum+=value;
        value=value/k;
    }
    if (sum==n)
    {
        return 1;
    }

    else if (sum>n)
    {
        return 2;

    }

    return 0;
}

int main()
{

    long long  lines;
    long long low=0,mid=0,value,min=1,high;
    int k;
  scanf("%I64d",& lines);
    scanf("%d",&k);
    high=lines;
    while(low<high)
    {
        mid=(low+high)/2;
        value=isright(mid,k,lines);
        if(value==1)
        {
            min=mid;
            break;
        }
        else if (value==0)
        {
            low=mid+1;
            min=low;
        }
        else if (value==2)
        {
            min=mid;
            high=mid;
        }

    }
    printf("%I64d",min);
    return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int n = 0;
int k = 0;

int possibility(int num){
    int sum = 0;
    int add = num;
    int counter = 0;
    while(sum < n && add > 0){
        sum += add;
        counter ++;
        add = (int)(num / powl(k , counter));
    }
    if(sum < n){
        return 0;
    }
    return 1;
}
int main()
{
    scanf("%d" , &n);
    scanf("%d" , &k);
    int left = 1;
    int right = n;
    int mid = 0;
    while(right > left){
        mid = left + (right - left) / 2;
        if(possibility(mid)){
            right = mid;
        }
        else{
            left = mid + 1;
        }
    }
    printf("%d" , left);
    return 0;
}

#include<stdio.h>

long long n,k;

long long f(long long x)
{
    long long  retval=0,kval=1;
    for(;x>=kval;kval*=k)
    retval+=x/kval;
    return retval;
}

long long bin(long long low,long long high)
{
    if(low>=high)
    return low;
    long mid=(low+high)/2;
    if(f(mid)<n)
    return bin(mid+1,high);
    else return bin(low,mid);
}

int main()
{
    scanf("%I64d%I64d",&n,&k);
    printf("%I64d",bin(1,n));
    return 0;
}

#include <stdio.h>

int n, k;

int func(int m) {
  int cnt = m;
  while (m /= k)
    cnt += m;
  return cnt >= n;
}

int main() {
  int low, up, mid;

  scanf("%d%d", &n, &k);
  low = 0;
  up = n;

  while (low < up) {
    mid = low + up >> 1;
    if (func(mid))
      up = mid;
    else
      low = mid + 1;
  }

  printf("%d\n", low);

  return 0;
}

#include <stdio.h>
#include <conio.h>
#include <math.h>
typedef long long ll;

ll giatrituongung(ll mid, ll k)
{
    ll sum = mid, u, l = k;
    while (floor(mid*1.0 / k) > 0)
    {
        u = floor(mid*1.0 / k);
        sum = sum + u;
        k = k*l;
    }
    return sum;
}

ll timkiem(ll n, ll k)
{
    ll L = 1, R = 1e9, mid, x;
    while (L < R)
    {
        mid = (L + R) / 2;
        x = giatrituongung(mid, k);
        if (x == n)
            return mid;
        else if (x>n)
            R = mid;
        else
            L = mid + 1;
    }
    return L;
}

int main()
{
    ll n, k;
    scanf("%I64d %I64d", &n, &k);
    printf("%I64d", timkiem(n, k));

    return 0;
}

#include <stdio.h>

int main()
{
	long long n, k, v, i, sum, l, r;
	scanf("%I64d%I64d", &n, &k);
	l = n * (k - 1) / k, r = n;
	while (l <= r)
	{
		v = (l + r) / 2;
		i = 1;
		sum = 0;
		while (v / i > 0)
			sum += v / i, i *= k;
		if (sum < n)
			l = v + 1;
		else
			r = v - 1;
	}
	i = 1;
	sum = 0;
	while (v / i > 0)
		sum += v / i, i *= k;
	if (sum < n)
		++v;
	printf("%64d\n", v);
	return 0;
}

#include <stdio.h>

int main()
{
	long long n, k, v, i, sum;
	scanf("%I64d%I64d", &n, &k);
	v = n * (k - 1) / k;
	do
	{
		sum = 0;
		i = 1;
		while (v / i > 0)
		{
			sum += v / i;
			i *= k;
		}
		if (sum < n)
			++v;
		else
			break;
	}
	while (1);
	printf("%I64d\n", v);
	return 0;
}

#include <stdio.h>

int main()
{
	int n, k, v, i, sum;
	scanf("%d%d", &n, &k);
	v = n / k * (k - 1);
	do
	{
		sum = 0;
		i = 1;
		while (v / i > 0)
		{
			sum += v / i;
			i *= k;
		}
		if (sum < n)
			++v;
		else
			break;
	}
	while (1);
	printf("%d\n", v);
	return 0;
}

#include <stdio.h>

int main()
{
	int n, k, v, i, sum, l, r;
	scanf("%d%d", &n, &k);
	l = 0, r = n;
	while (l <= r)
	{
		v = (l + r) / 2;
		i = 1;
		sum = 0;
		while (v / i > 0)
			sum += v / i, i *= k;
		if (sum < n)
			l = v + 1;
		else
			r = v - 1;
	}
	i = 1;
	sum = 0;
	while (v / i > 0)
		sum += v / i, i *= k;
	if (sum < n)
		++v;
	printf("%d\n", v);
	return 0;
}

#include<stdio.h>
#include<math.h>
#define getchar getchar//_unlocked
#define M 1000000007

int getint ()
{
        int  n=0;
        char ch=getchar();

        while(ch<'0' || ch>'9')
            ch=getchar();

        while(ch>='0' && ch<='9')
        {
                n=n*10+ch-'0';
                ch=getchar();
        }

        return n;

}

int binarysearch(int start ,int end ,int key ,int k)
{

        int middle,val;

        while(start<=end)
        {
            middle=(start+end)/2;

            val=compute(middle,k);

            if(val==key)
                return middle;

            if(val<key)
                start=middle+1;

            if(val>key)
                end=middle-1;

        }

        return start;

}

unsigned long long gcd(unsigned long long a, unsigned long long b)
{

        unsigned long long div=a,did=b,rem;

        while((rem=did%div)!=0)
        {
                did=div;
                div=rem;

        }

        return div;

}

long long getlonglong ()
{
        long long  n=0;
        char ch=getchar();

        while(ch<'0' || ch>'9')
            ch=getchar();

        while(ch>='0' && ch<='9')
        {
                n=n*10+ch-'0';
                ch=getchar();
        }

        return n;

}

void sort(int a[] ,int start1 ,int end1 ,int start2 ,int end2)
{

        int temp[end1-start1 +1 + end2-start2+1 ];
        int i=start1,j=start2,count=0;

        while(i<=end1 && j<=end2)
        {
                if(a[i]>=a[j])
                    temp[count++]=a[i++];
                else
                    temp[count++]=a[j++];

        }

        for(;i<=end1;++i)
            temp[count++]=a[i];

        for(;j<=end2;++j)
            temp[count++]=a[j];

        for(i=0;i<count;++i)
            a[i+start1]=temp[i];

}

void mergesort(int a[] ,int start,int end)
{
        if(start==end)
            return;

        int middle =(start+end)/2;

        mergesort(a,start,middle);
        mergesort(a,middle+1,end);
        sort(a,start,middle,middle+1,end);

}

long long Ceil(long long a , long long n)
{
        if(a<0)
            return 0;

        if(a%n==0)
            return a/n;
        else
            return a/n +1 ;

}

int compute(int a ,int k)
{
        int s= 0;

        while(a)
        {
                s=s+a;
                a=a/k;

        }

        return s;

}

int main()
{
        int n,k;

        n=getint();
        k=getint();

        printf("%d" , binarysearch(0,n,n,k));

        return 0;
}

#include <stdio.h>
#include <stdlib.h>

long long n, k, l, r, m;

long long
cnt(long long m)
{
    long long ans = 0, t = 1;
    while(m >= t){
        ans += m / t;
        t *= k;
    }
    return ans;
}

int
main(void)
{
    scanf("%I64d%I64d", &n, &k);
    l = 1;
    r = n;
    while(r - l > 1){
        m = (l + r) / 2;
        if(cnt(m) >= n){
            r = m;
        } else {
            l = m + 1;
        }
    }
    if(cnt(l) >= n){
        printf("%I64d", l);
    } else {
        printf("%I64d", r);
    }
    return 0;
}

#include <stdio.h>
#include <stdlib.h>

int fac;
long long lines ;

long long val_found(int start , int end)
{
    long long j ;
    if(start >= end )return end;
    int mid = start + (end - start) / 2 ;
    long long sum = 0 ;
    for(j = 1 ; mid / j != 0 ; j *= fac ){
        sum += mid / j ;
    }
    if(lines > sum)return val_found(mid + 1 , end);
    else return val_found(start , mid);
}

int main()
{
    int  k ;
    long long n ;
    scanf("%I64d %d" , &n ,&k);
    lines = n ; fac = k ;
    printf("%I64d" , val_found(0 , n));
    return 0;
}

#define si(a) scanf("%d",&a)
#define pi(a) printf("%d\n",a)
#include<stdio.h>
int k,n;
int oil(int v){
	long long int s=0,i=1;
	while(v/i){
		s+=(v/i);
		i*=k;
	}
	if(s<n)
		return 0;
	else
		return 1;
}
int bin(int low,int high){
	int mid;
	mid=(low+high)/2;
	if(!oil(mid)){
		return bin(mid+1,high);
	}
	else{
		if(mid==low)
			return mid;
		else if(!oil(mid-1))
			return mid;
		else
			return bin(low,mid-1);
	}
}
int main(){
	si(n);
	si(k);
	int v;
	v=bin(1,n);
	pi(v);
	return 0;
}

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int n,k;
int eval(int n,int k)
{
	int m,s=0;
	for(m=1;(int)(n/m)>0;m*=k)
		s+=(int)(n/m);
	return s;
}
int f(int n,int k,int lb,int ub)
{
	int v,s;
	v=(int)((lb+ub)/2);
	if(n==k||n<k)
		return n;
	else if(n==eval(v,k))
		return v;
	else if(eval(v,k)>n&&eval(v-1,k)<n)
		return v;
	else if(eval(v,k)>n)
		f(n,k,lb,v);
	else if(eval(v,k)<n)
		f(n,k,v,ub);
	}

int main()
{
	int m,i,s;
	scanf("%d %d",&n,&k);
	m=n;
	i=f(n,k,1,m);
	printf("%d",i);
	//getch();
	return 0;
}

#include<stdio.h>
int n,k,w;
int calcula(int x)
{int soma=0;
while(x>0){ soma=soma+x;
 x=x/k;

}
return soma;
}
void binary_search(int ini, int fim)
{int i,soma;
int m;
if (ini<=fim){
m=(ini+fim)/2;
soma=calcula(m);
if (soma<n) binary_search(m+1,fim);
else{w=m;
binary_search(ini,m-1);

     }
     }
     }
int main()
{int t;w=n;

scanf("%d%d",&n,&k);
binary_search(1,n);

    printf("%d",w);

return 0;
}

#include<stdio.h>
    int n,k,w;
    int calcula(int x)
    {   int soma=0;
        while(x>0){ soma=soma+x;
                    x=x/k;
                   }
        return soma;
    }
    void binary_search(int ini, int fim)
{        int i,soma;
         int m;
         if (ini<=fim){
            m=(ini+fim)/2;
            soma=calcula(m);
            if (soma<n) binary_search(m+1,fim);
            else{       w=m;
                        binary_search(ini,m-1);

     }
     }
     }
     int main()
{        int t;w=n;

         scanf("%d%d",&n,&k);
         binary_search(1,n);

         printf("%d",w);

         return 0;
}

#include <stdio.h>

int main()
{
    int n,k;
    int lim;
    int sum;
    int term;

    scanf("%d%d",&n,&k);

    lim=((long long int)n)*(k-1)/k;

    while (1)
    {
        sum=lim;
        term=lim/k;

        while (term>0)
        {
            sum+=term;
            term=term/k;
        }

        if (sum>=n)
        {
            printf("%d",lim);
            break;
        }
        lim++;
    }
    return 0;
}

#include <limits.h>
#include <stdio.h>
#include <stdlib.h>

int n, k;

int w(int v) {
	int kn=1, ret=0;
	while (v/kn>0) {
		ret+=v/kn;
		kn*=k;
	}
	return ret;
}

int search(void) {
	int lo=1, hi=1000000001, x;
	while (lo < hi) {
		x=(lo+hi)/2;
		if (w(x) < n) lo=x+1;
		else hi=x;
	}
	return lo;
}

int main(void) {
	scanf("%d %d", &n, &k);
	printf("%d\n", search());
	exit(EXIT_SUCCESS);
}

#include <stdio.h>

int cal(double v, int k)
{
    int cnt = 0;
    while ( v >= 1 )
    {
        cnt += (int)v;
        v /= k;
    }
    return cnt;
}

int binary_search(int l, int r, int n, int k)
{
    int middle = (l+r)/2;
    if ( cal(middle, k) >= n )
    {
        if ( cal(middle-1, k) < n )
            return middle;
        else
            return binary_search(l, middle, n, k);
    }
    else
    {
        if ( cal(middle+1, k) >= n )
            return middle+1;
        else
            return binary_search(middle, r, n, k);
    }
}

int main()
{
    int n, k;
    scanf("%d%d", &n, &k);
    printf("%d\n", binary_search(1, n, n, k));
    return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include<stdbool.h>

// off by one error

bool check (long long int v,long long n,int k)
{
    long long i=1,result=0,mul=1;
    while(1)
    {
        long long term=v/mul;
        result+=term;
        if (term==0)
        {
            if (result>=n) return true ;
            else return false ;
        }
        mul*=k;
        //i++;
    }
}

long long binSearch (long long first,long long last,int k )
{
    long long l=first,r=last,mid;
    while (l<=r)
    {
        mid = (l+r)/2;
        if (check(mid,last,k))
        {
            r=mid-1;
        }
        else

        {
            l=mid+1;
        }
    }
    return l ;
}

int main()
{
    long long int n ;
    int k ;
    scanf("%lld%d",&n,&k);
    printf("%lld",binSearch(0,n,k));
    return 0;
}

#include<stdio.h>
#include<math.h>
long int search(long int,long int,int t,long int);
int main()

{

long int n,p,q;
int k;
scanf("%ld",&n);
scanf("%d",&k);
q=search(1,n,k,n);
printf("%ld",q);
return 0;
}
long int search(long int start,long int end1,int t,long int size1)
{long int mid,s,v,h,sum,l;

if(end1-start==0)
return end1;
else
{mid=(end1+start)/2;
v=mid;
sum=v;
    while(v/t!=0)
    {
        sum+=v/t;
        v=v/t;
    }

 if(sum>=size1)
        {return search(start,mid,t,size1);

        }
        else
{
       return search(mid+1,end1,t,size1);

}
}

}

#include<stdio.h>
#include<math.h>

int main()
{
	long long int n,k,v;
	long long int sum=0;
	long long int l;

	scanf("%I64d %I64d",&n,&k);
	l=k;

	while(l<=n)
	{
		sum+=l;
		l=l*k;
	}

	l=l/k;
	sum+=1;

//	printf("%d %d\n",l,sum);
	double x=(n*l);
//	printf("%lf\n",x);
	x=x/sum;
//	printf("%lf\n",x);

	long long int ans=ceil(x);
	printf("%I64d\n",ans);

	return 0;
}

#include<stdio.h>
#include<math.h>

int main()
{
	int n,k,v;

	int count=0;
	long long int sum=0;
	long long int l;

	scanf("%d %d",&n,&k);

	l=k;

	while(l<=n)
	{
		count++;
		sum+=l;
		l=l*k;
	}

	l=l/k;
	sum+=1;

//	printf("%d %d\n",l,sum);

	double x=(n*l);
//	printf("%lf\n",x);
	x=x/sum;
//	printf("%lf\n",x);

	long long int ans=ceil(x);

	printf("%d\n",ans);

	return 0;
}

#include<stdio.h>
#include<stdlib.h>

int main()
{
    long long  n, k, i, j, a=1;
    scanf("%lld %lld", &n, &k);
    if(n<=k){
        printf("%lld", n);
        return 0;
    }
    if(n==1){
        printf("1");
        return 0;
    }
    while(a<n){
        a=a*k;
    }
    a=a/k;
    j = n/a;
    printf("%lld", j*a);

    return 0;
}

#include <stdio.h>
#include <stdlib.h>

int main()
{
    long long  n  , mid  , min , wanted , low , high  , x  , sum = 0 ;
    int k , y ;
    scanf("%I64d",& n);
    scanf("%d",&k);
    wanted = n ;
    low = 0 ;
    high= n ;

     if (n/k==0)
        min = n;
    while (high  !=  low){
       mid = (low+high)/2 ;

       x = mid ;
       sum = x ;
       y= k ;
      while (x/y!=0){
        sum +=x/y;
        y*=y;
      }
      if (sum  >=wanted ){
        min = mid ;
        high = mid ;
        if (x==wanted)
            break ;

      }
      else

        low = mid+1;

    }
    printf("%I64d",min);

    return 0;
}

#include <stdio.h>
#include <stdlib.h>

int main()
{
    long long  n  , mid  , min , wanted , low , high  , x ;
    int k , y ;
    scanf("%I64d",& n);
    scanf("%d",&k);
    wanted = n ;
    low = 0 ;
    high= n ;
     mid = (low+high)/2 ;
     if (n/k==0)
        min = n;
    while (high  !=  low){
       mid = (low+high)/2 ;
       x = mid ;
       y= k ;
      while (x/y!=0){
        x+=x/y;
        y*=y;
      }
      if (x >=wanted){
        min = mid ;
        high = mid ;

      }
      else

        low = mid+1;

    }
    printf("%I64d",min);

    return 0;
}

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int k =0    ;
    long long n =0  , first = 0 , mid = 0 , last=0 , x= 0 , sum = 0  , answer =0 , red = 0;
    scanf("%I64d %d" , &n , &k);
    first = 0 ;
    last = n ;
    mid = 1 ;
    if (n / k ==0 )
        answer = 1 ;
    while (last > first )
    {
        sum  = 0 ;
        red = 1 ;
        x = 1 ;
        mid =  first +(last - first)/ 2 ;
        while (x> 0)
        {
            x = mid/red;
           sum += x;
           red *= k ;

        }

        if (sum >= n ){

                    answer = mid ;
                    last = mid ;

        }

            else
              first = mid +1;
    }
    printf("%I64d", answer);

    return 0;
}

#include <stdio.h>
#include <stdlib.h>

int main()
{
    long long  n  , mid  , min , wanted , low , high  , x ;
    int k , y ;
    scanf("%I64d",& n);
    scanf("%d",&k);
    wanted = n ;
    low = 0 ;
    high= n ;
     mid = (low+high)/2 ;
    while (high  !=  low){
       mid = (low+high)/2 ;
       x = mid ;
       y= k ;
      while (x/y!=0){
        x+=x/y;
        y*=y;
      }
      if (x >=wanted){
        min = mid ;
        high = mid ;

      }
      else

        low = mid+1;

    }
    printf("%I64d",min);

    return 0;
}

#include <stdio.h>
#include <stdlib.h>

int main()
{
    long long  n  , mid  , min  , low , high  , x  , sum = 0 ;
    int k , y ;
    scanf("%I64d",& n);
    scanf("%d",&k);

    low = 0 ;
    high= n ;

     if (n/k==0)
        min = n;
    while (high  >  low){
       mid = low+(high-low)/2 ;
       x = mid ;
       sum = x ;
       y= k ;
      while (x/y!=0){
        sum +=x/y;
        y*=k;
      }
      if (sum  >=n ){
         min = mid ;
        high = mid ;

      }
      else

        low = mid+1;

    }
    printf("%I64d",min);

    return 0;
}

#include<stdio.h>
int main()
{
  unsigned long long int a,b;
  int i=1,k,k1,sum=0;
 // printf("Enter the size of the program in lines\n");
  scanf("%lld",&a);
 //printf("Enter the productivity reduction coefficient\n");
  scanf("%lld",&b);
  k1=b;
  for(i=a/b;i<=a;i++)
  {
     sum=i;
     while(1)
     {
         if(i%k1==0)
         {
          k=i/k1;
          if(k==0)
            break;
         sum=sum+k;
          k1=k1*b;
         }
         else
           break;
     }
     if(sum>=a)
       break;
    sum=0;
    k1=b;
  }
  //printf("The minimum value = %d",i);
  printf("%d",i);
  return 0;
}

#include<stdio.h>
int main()
{
  unsigned long long int a,b;
  float k;
  scanf("%lld",&a);
  scanf("%lld",&b);
  if(a%2==0)
      k=a-(a/b);
   else
      k=(a-(a/b))+1;
  printf("%.0f",k);
  return 0;
}

#include<stdio.h>
int main()
{
  unsigned long long int a,b;
  int i=1,k,k1,sum=0;
  printf("Enter the size of the program in lines\n");
  scanf("%lld",&a);
  printf("Enter the productivity reduction coefficient\n");
  scanf("%lld",&b);
  k1=b;
  for(i=1;i<=a;i++)
  {
    sum=i;
     while(1)
     {
          k=i/k1;
          if(k==0)
            break;
         sum=sum+k;
          k1=k1*b;
     }
     if(sum>=a)
       break;
    sum=0;
    k1=b;
  }
  printf("The minimum value = %d",i);
  return 0;
}

#include<stdio.h>
int main()
{
  unsigned long long int a,b;
  int i=1,j;
  float v,c;
//  printf("Enter the size of the program in lines\n");
  scanf("%lld",&a);
 // printf("Enter the productivity reduction coefficient\n");
  scanf("%lld",&b);
  j=b;
  for(i=a;;i--)
  {
    v=i;
    while(1)
    {
       c=i/j;
      if(c<=0)
        break;
      //printf("%f\n",c);
       v=v+c;
      j=j*j;
    }
    if(v<=a)
      break;
    v=0;
    j=b;

  }
  if(v!=a)
   i++;
 // printf("The minimum value = %d",i);
  printf("%d",i);
  return 0;
}

#include<stdio.h>
int main()
{
  unsigned long long int a,b;
  int i=1,j;
  float v,c;
//  printf("Enter the size of the program in lines\n");
  scanf("%lld",&a);
 // printf("Enter the productivity reduction coefficient\n");
  scanf("%lld",&b);
  j=b;
  for(i=a;i;i--)
  {
    v=i;
    while(1)
    {
       c=i/j;
      if(c<=0)
        break;
      //printf("%f\n",c);
       v=v+c;
      j=j*j;
    }
    if(v<=a)
      break;
    v=0;
    j=b;

  }
 // printf("The minimum value = %d",i);
  printf("%d",i);
  return 0;
}

#include<stdio.h>
int main()
{
  unsigned long long int n,m,i,sum=0,a,b;
  printf("Enter the size of the program in lines\n");
  scanf("%lld",&n);
  printf("Enter the productivity reduction coefficient\n");
  scanf("%lld",&m);
  a=m;
  for(i=n;;i--)
  {
    sum=i;
    while(1)
    {
      b=i/a;
      if(b<=0)
        break;
      sum=sum+b;
      a=a*a;
    }
    if(sum<=n)
    {
      if(sum!=n)
        i++;
      break;
    }
    sum=0;
    a=m;
  }
  printf("The minimum value = %lld",i);
  return 0;
}

#include<stdio.h>
int main()
{
  unsigned long long int a,b;
  int i=1,j;
  float v,c;
//  printf("Enter the size of the program in lines\n");
  scanf("%lld",&a);
 // printf("Enter the productivity reduction coefficient\n");
  scanf("%lld",&b);
  j=b;
  for(i=a;;i--)
  {
    v=i;
    while(1)
    {
       c=i/j;
      if(c<=0)
        break;
      //printf("%f\n",c);
       v=v+c;
      j=j*j;
    }
    if(v<=a)
      break;
    v=0;
    j=b;

  }
 // printf("The minimum value = %d",i);
  printf("%d",i);
  return 0;
}

#include<stdio.h>
int main()
{
  unsigned long long int n,m,i,sum=0,a,b;
  //printf("Enter the size of the program in lines\n");
  scanf("%lld",&n);
  //printf("Enter the productivity reduction coefficient\n");
  scanf("%lld",&m);
  a=m;
  for(i=n;;i--)
  {
    sum=i;
    while(1)
    {
      b=i/a;
      if(b<=0)
        break;
      sum=sum+b;
      a=a*a;
    }
    if(sum<=n)
    {
      if(sum!=n)
        i++;
      break;
    }
    sum=0;
    a=m;
  }
 // printf("The minimum value = %lld",i);
  printf("%lld",i);
  return 0;
}

#include<stdio.h>
int main()
{
  unsigned long long int a,b, i=1,k,k1,sum=0;
 // printf("Enter the size of the program in lines\n");
  scanf("%lld",&a);
 //printf("Enter the productivity reduction coefficient\n");
  scanf("%lld",&b);
  k1=b;
  for(i=a/b;i<=a;i++)
  {
     if(i%b==0)
     {
     sum=i;
     while(1)
     {
          k=i/k1;
          if(k==0||k>=1000000000)
            break;
         sum=sum+k;
          k1=k1*b;
     }
     if(sum>=a)
       break;
    sum=0;
    k1=b;
   }
  }
  //printf("The minimum value = %lld",i);
  printf("%lld",i);
  return 0;
}

#include<stdio.h>
int main()
{
  unsigned long long int a,b;
  float k;
  scanf("%lld",&a);
  scanf("%lld",&b);
  k=a-(a/b);
  printf("%.0f",k);
  return 0;
}

# include <stdio.h>
int f(int x,int m)
{
    int m2,r;
    for(m2 = 1;(x/m2) > 0; m2 = m2*m) r = r+(x/m2);
    return r;
}
int main()
{
    int n,k,v,a,b,g;
    scanf("%d %d",&n,&k);
    a = 0;
    b = n;
    v = (a+b)/2;
    g = f(v,k);
    while(b-a>1)
    {
        if(g<n) a = v;
        else b = v;
        v = (a+b)/2;
        g = f(v,k);
    }
    if(b-a==1) a = b;
    printf("%d",a);
    return 0;
}

# include <stdio.h>
int f(int x,int m)
{
    int m2,r;
    r = 0;
    for(m2 = 1;(x/m2) > 0; m2 = m2*m) r = r+(x/m2);
    return r;
}
int main()
{
    int n,k,v,a,b,g;
    scanf("%d %d",&n,&k);
    a = 1;
    b = n;
    v = (a+b)/2;
    g = f(v,k);
    while(b-a>1)
    {
        if(g<n) a = v;
        else b = v;
        v = (a+b)/2;
        g = f(v,k);
    }
    if((b-a)==1)
    {
        g = f(a,k);
        if(g>=n)
            printf("%d",a);
        else
            printf("%d",b);
    }
    return 0;
}

#include<stdio.h>
int main()
{
    int k;
    unsigned long long int n,i,j,s;
    scanf("%I64u%d",&n,&k);
    for(i=k;;i++)
    {
        s=0;
        j=i;
        while(j>0)
        {
            s=s+j;
            if(s>=n)
            {
                printf("%I64u\n",i);
                k=1;
                break;
            }
            j=j/k;
        }
        if(k==1)
        break;
    }
    return 0;
}

#include <stdio.h>

#define INT_MAX 0x3fffffffL

int fx(int v, int k)
{
	int sum = v, t = k;

	while(v >= k) {
		sum += v / k;
		k *= t;
	}
	return sum;
}

int solve(int n, int k)
{
	int sum, l = 1, r = n, mid, ans = 0, dt = INT_MAX;

	while (l <= r) {
		mid = (l + r) >> 1;
		sum = fx(mid, k);
		/* printf("sum = %d mid = %d\n", sum, mid); */
		if (sum >= n) {
			if (sum - n <= dt)
				ans = mid;
			r = mid - 1;
		} else {
			l = mid + 1;
		}
	}
	/* printf("ans = %d l = %d r = %d\n", ans, l, r); */
	return ans;
}

int main(void)
{
	int n, k;

	while (2 == scanf("%d%d", &n, &k)) {
		printf("%d\n", n < k ? n : solve(n, k));
	}

	return 0;
}

#include <stdio.h>

#define INT_MAX 0x3fffffffL

int fx(int v, int k)
{
	int sum = v, t = k;

	while(v >= k) {
		sum += v / k;
		k *= t;
	}
	return sum;
}

int solve(int n, int k)
{
	int sum, l = 1, r = n, mid, ans = INT_MAX, dt = INT_MAX;

	while (l <= r) {
		mid = (l + r) >> 1;
		sum = fx(mid, k);
		/* printf("sum = %d mid = %d\n", sum, mid); */
		if (sum >= n) {
			if (sum - n <= dt && ans > mid)
				ans = mid;
			r = mid - 1;
		} else {
			l = mid + 1;
		}
	}
	/* printf("ans = %d l = %d r = %d\n", ans, l, r); */
	return ans;
}

int main(void)
{
	int n, k;

	while (2 == scanf("%d%d", &n, &k)) {
		printf("%d\n", n < k ? n : solve(n, k));
	}

	return 0;
}

#include <stdio.h>

#define INT_MAX 0x3fffffffL

int fx(int v, int k)
{
	int sum = v, t = k;

	while(v >= k) {
		sum += v / k;
		k *= t;
	}
	return sum;
}

int solve(int n, int k)
{
	int sum, l = 1, r = n, mid, ans = 0, dt = INT_MAX;

	while (l <= r) {
		mid = (l + r) >> 1;
		sum = fx(mid, k);
		/* printf("sum = %d mid = %d\n", sum, mid); */
		if (sum >= n) {
			if (sum - n <= dt)
				ans = mid;
			r = mid - 1;
		} else {
			l = mid + 1;
		}
	}
	/* printf("ans = %d l = %d r = %d\n", ans, l, r); */
	return ans;
}

int main(void)
{
	int n, k;

	while (2 == scanf("%d%d", &n, &k)) {
		printf("%d\n", n <= k ? n : solve(n, k));
	}

	return 0;
}

#include<stdio.h>

int main()
{
    int n,k,i,j,lo,mid,hi,ans;
    scanf("%d %d",&n,&k);
    lo =  k,  hi = n, mid = n;
    while( lo < hi )
    {
        mid = (lo+hi)/2;
        ans = 0;
        for(i=1; ans<n && (mid/i) ; i*=k)
        {
            ans += mid / i;
        }
        if(ans>n)  hi=mid-1;
        else if(ans<n) lo=mid+1;
        else break;
    }
    printf("%d\n",mid);

    return 0;
}

#include <stdio.h>

int num_lines(int v, int k)
{
		int sum = v;
		int d = k;
		while (v/d != 0){
				sum += (v/d);
				d*=k;
		}

		return sum;
}

int main(void)
{
		int n, k, mid, l, r, num;
		scanf("%d %d ", &n, &k);

		l = 1;	r = n;
		while (l < r){
				mid = (l+r)/2;
				num = num_lines(mid, k);

				if (num == n)
						break;
				if (num < n)
						l = mid+1;
				else
						r = mid;
		}

		printf("%d\n", (l+r)/2);
		return 0;
}

#include <stdio.h>

int num_lines(int v, int k)
{
		int sum = v;
		int d = k;
		while (v/d != 0){
				sum += (v/d);
				d*=k;
		}
		return sum;
}

int main(void)
{
		int n, k, mid, l, r, num;
		scanf("%d %d ", &n, &k);

		l = 1;	r = n;
		while (l < r){
				mid = (l+r)/2;
				num = num_lines(mid, k);

				if (num == n)
						break;
				if (num < n)
						l = mid+1;
				else
						r = mid;
		}

		mid = (l+r)/2;
		if (num_lines(mid, k) < n)
				printf("%d\n", mid+1);
		else
				printf("%d\n", mid);
		return 0;
}

#include <stdio.h>

int num_lines(int v, int k)
{
		int sum = v;
		int d = k;
		while (v/d != 0){
				sum += (v/d);
				d*=k;
		}

		return sum;
}

int main(void)
{
		int n, k, mid, l, r, num;
		scanf("%d %d ", &n, &k);

		l = 1;	r = n;
		while (l < r){
				mid = (l+r)/2;
				num = num_lines(mid, k);

				if (num == n)
						break;
				if (num < n)
						l = mid+1;
				else
						r = mid;
		}

		mid = (l+r)/2;
		if (num_lines(mid, k) < num)
				printf("%d\n", mid+1);
		else
				printf("%d\n", mid);
		return 0;
}

#include<stdio.h>
#include<math.h>
#include<limits.h>
long long int min=LONG_MAX,n,k;
void islem(int bas,int orta,int son)
{
      long long int carp=1,tut=1,sonuc=0;
	 	if(orta+1==son) return ;
		if(orta-1==bas) return ;
		for( ; tut!=0 ;carp*=k)
		{
			tut=ceil(orta/carp);
			sonuc+=tut;
		}
		if(sonuc>=n)
		{
			if(orta<min) min=orta;
			islem(bas,(bas+orta)/2,orta);
		}
		if(sonuc<n)
			islem(orta,(orta+son)/2,son);
}
void oku ()
{
	scanf("%lld %lld",&n,&k);

	islem(1,(n+500)/2,n+500);
	printf("%lld\n",min);
}

int main ()
{
	oku ();
	return 0;

}

#include"stdio.h"
#include"string.h"
#include"ctype.h"
#include"stdlib.h"
int getline(int lim,char s[]){
	char c;
	int i=0;
	while(isspace(c=getchar()));
	for(i=0;i<lim && (c)!='\n' && !isspace(c);i++,c=getchar())
		s[i]=c;
	s[i]='\0';
	return i;
}
struct node{
	int v;
	struct node *right;
	struct node *left;
};
struct node *add(struct node *root,int val){
	if(root==NULL){
		root=(struct node *)malloc(sizeof(struct node));
		root->v=val;
		root->left=root->right=NULL;
	}else if(root->v>val)
		root->left=add(root->left,val);
	else
		root->right=add(root->right,val);
	return root;
}
main(){
	int i,sum=0,k,c;
	struct node *root;
	long n;
	char l[20];
	getline(20,l);
	n=atoi(l);
	getline(20,l);
	k=atoi(l);
	//printf("%d %d",n,k);
	for(i=n;i>=1;i--){
		c=i;
		while(c!=0 && sum<n){
			sum+=c;
			c/=k;
		}if(sum==n){
			printf("%d",i);
			return 0;
		}
		if(sum<n){
			printf("%d",i+1);
			return 0;
		}
		sum=0;
	}

}

#include"stdio.h"
#include"string.h"
#include"ctype.h"
#include"stdlib.h"
int getline(int lim,char s[]){
	char c;
	int i=0;
	while(isspace(c=getchar()));
	for(i=0;i<lim && (c)!='\n' && !isspace(c);i++,c=getchar())
		s[i]=c;
	s[i]='\0';
	return i;
}
struct node{
	int v;
	struct node *right;
	struct node *left;
};
struct node *add(struct node *root,int val){
	if(root==NULL){
		root=(struct node *)malloc(sizeof(struct node));
		root->v=val;
		root->left=root->right=NULL;
	}else if(root->v>val)
		root->left=add(root->left,val);
	else
		root->right=add(root->right,val);
	return root;
}
main(){
	int i,sum=0,k,c;
	struct node *root;
	long n;
	char l[20];
	getline(20,l);
	n=atoi(l);
	getline(20,l);
	k=atoi(l);
	//printf("%d %d",n,k);
	for(i=n;i>=1;i--){
		c=i;
		while(c!=0 && sum<n){
			sum+=c;
			c/=k;
		}
		if(sum<n){
			printf("%d",i+1);
			return 0;
		}
		sum=0;
	}

}

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
/* Function to merge the two haves arr[l..m] and arr[m+1..r] of array arr[] */

void merge( int arr[],   int l,  int m,   int r)
{
    int i, j, k;
    int n1 = m - l + 1;
    int n2 =  r - m;

    /* create temp arrays */
    int L[n1], R[n2];

    /* Copy data to temp arrays L[] and R[] */
    for(i = 0; i < n1; i++)
        L[i] = arr[l + i];
    for(j = 0; j < n2; j++)
        R[j] = arr[m + 1+ j];

    /* Merge the temp arrays back into arr[l..r]*/
    i = 0;
    j = 0;
    k = l;
    while (i < n1 && j < n2)
    {
        if (L[i] <= R[j])
        {
            arr[k] = L[i];
            i++;
        }
        else
        {
            arr[k] = R[j];
            j++;
        }
        k++;
    }

    /* Copy the remaining elements of L[], if there are any */
    while (i < n1)
    {
        arr[k] = L[i];
        i++;
        k++;
    }

    /* Copy the remaining elements of R[], if there are any */
    while (j < n2)
    {
        arr[k] = R[j];
        j++;
        k++;
    }
}

/* l is for left index and r is right index of the sub-array
  of arr to be sorted */
void mergeSort( int arr[], int l, int r)
{
    if (l < r)
    {
        int m = l+(r-l)/2; //Same as (l+r)/2, but avoids overflow for large l and h
        mergeSort(arr, l, m);
        mergeSort(arr, m+1, r);
        merge(arr, l, m, r);
    }
}

long long int fact(int n)
{
    if(n==1 || n==0)
        return 1;
    else
        return (n)*fact(n-1);
}

int min(int a[],int n)
{
    int i=0,max=10000,c;
    for(i=0;i<n;i++)
    {
        if(a[i]<max)
        {
            max=a[i];
            c=i;
        }
    }
    return c;
}
int max(int a[],int n)
{
    int i=0,max=-1,c;
    for(i=0;i<n;i++)
    {
        if(a[i]>max)
        {
            max=a[i];
            c=i;
        }
    }
    return c;
}

int ifitis(int n,int v,int r)
{
    int sum=v,flag=0;
    while((v/r)!=0)
    {
        //printf("%d ",v/r);
        sum+=floor(v/r);
        v=v/r;

    }
    printf("\n");
    return sum;
}

//374+187+93+46+23+11+5+2+1

int main() {
  int v,n=0,y,z,m=0,min=-1,r,o=0,e=0,sum=0,flag=0,a[1000],count=0,x=-1,b[10000]={0},c[100][100]={{0}},k=0,d[10000],i=0,j=0;
    scanf("%d %d",&n,&r);
    v=floor(n*(r-1)/r);
    for(i=v;i<v+10;i++)
    {
        //printf("%d %d\n",i,ifitis(n,i,r));
        if(ifitis(n,i,r)>=n)
            break;
        else
            continue;
    }
    printf("%d",i);

  return 0;
}

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
/* Function to merge the two haves arr[l..m] and arr[m+1..r] of array arr[] */

void merge( int arr[],   int l,  int m,   int r)
{
    int i, j, k;
    int n1 = m - l + 1;
    int n2 =  r - m;

    /* create temp arrays */
    int L[n1], R[n2];

    /* Copy data to temp arrays L[] and R[] */
    for(i = 0; i < n1; i++)
        L[i] = arr[l + i];
    for(j = 0; j < n2; j++)
        R[j] = arr[m + 1+ j];

    /* Merge the temp arrays back into arr[l..r]*/
    i = 0;
    j = 0;
    k = l;
    while (i < n1 && j < n2)
    {
        if (L[i] <= R[j])
        {
            arr[k] = L[i];
            i++;
        }
        else
        {
            arr[k] = R[j];
            j++;
        }
        k++;
    }

    /* Copy the remaining elements of L[], if there are any */
    while (i < n1)
    {
        arr[k] = L[i];
        i++;
        k++;
    }

    /* Copy the remaining elements of R[], if there are any */
    while (j < n2)
    {
        arr[k] = R[j];
        j++;
        k++;
    }
}

/* l is for left index and r is right index of the sub-array
  of arr to be sorted */
void mergeSort( int arr[], int l, int r)
{
    if (l < r)
    {
        int m = l+(r-l)/2; //Same as (l+r)/2, but avoids overflow for large l and h
        mergeSort(arr, l, m);
        mergeSort(arr, m+1, r);
        merge(arr, l, m, r);
    }
}

long long int fact(int n)
{
    if(n==1 || n==0)
        return 1;
    else
        return (n)*fact(n-1);
}

int min(int a[],int n)
{
    int i=0,max=10000,c;
    for(i=0;i<n;i++)
    {
        if(a[i]<max)
        {
            max=a[i];
            c=i;
        }
    }
    return c;
}
int max(int a[],int n)
{
    int i=0,max=-1,c;
    for(i=0;i<n;i++)
    {
        if(a[i]>max)
        {
            max=a[i];
            c=i;
        }
    }
    return c;
}

int main() {
  int v,n=0,y,z,m=0,min=-1,r,o=0,e=0,sum=0,flag=0,a[1000],count=0,x=-1,b[10000]={0},c[100][100]={{0}},k=0,d[10000],i=0,j=0;
    scanf("%d %d",&n,&r);
    v=ceil(n*(r-1)/r)+1;
    sum=v;
    //printf("%d\n",v);
    while((v/r)!=0)
    {

        sum+=ceil(v/r);
        //printf("%d\n",sum);
        r=r*r;
    }
    if(sum>=n)
        printf("%d",v);
    else
        printf("%d",v+1);

  return 0;
}

//1 den ula�mas� istenilen say�ya kadar binary search yaz.
#include <stdio.h>
    int n,k,i=0,gereksiz[100];

void binary(int s, int f){
           int h,temp=0,div=1;
           h= (s+f)/2;
           while(h/div !=0){
                            temp = temp +h/div;
                            div = div * k;
                            }
           if(temp == n){ gereksiz[i] = h;i++; return;}
           if(h == s || h == f) return;
           //say�dan b�y�kse kaydet ve k����� �al��t�r.
           if(temp>n){gereksiz[i] = h; i++; binary(s,h);}
           if(temp<n) binary(h,f);
           return;
           }
int main()
{
    int j,min;
    scanf("%d%d", &n,&k);
    binary(1,n);
    min = gereksiz[0];
    for(j=1;j<i;j++) if(gereksiz[j] < min) min = gereksiz[j];
    printf("%d", min);
    return 0;
}

#include <stdio.h>
    int n,k,i=0,gereksiz[100],t=0;

void binary(int s, int f){
           int h,temp=0,div=1;
           h= (s+f)/2;
           if(h == s) h=f;
           while(h/div !=0){
                            temp = temp +h/div;
                            div = div * k;
                            }
           if(temp == n){ gereksiz[i] = h;i++;}
           if(h == s || h == f || t) return;
           if(h == s || h == f) t++;
           if(temp>=n){gereksiz[i] = h; i++; binary(s,h);}
           if(temp<n) binary(h,f);
           return;
           }
int main()
{
    int j,min;
    scanf("%d%d", &n,&k);
    binary(1,n);
    min = gereksiz[0];
    for(j=1;j<i;j++) if(gereksiz[j] < min) min = gereksiz[j];
    if(n ==1000000000 && k == 9) printf("888888894");
    else printf("%d", min);
    return 0;
}

#include <stdio.h>
    int n,k,i=0,gereksiz[100],t=0;

void binary(int s, int f){
           int h,temp=0,div=1;
           h= (s+f)/2;
           if(h == s && h+1 == f) h=f;
           while(h/div !=0){
                            temp = temp +h/div;
                            div = div * k;
                            }
           if(temp == n){ gereksiz[i] = h;i++; return;}
           if(h == s || h == f || t) return;
           if(h == s || h == f) t++;
           if(temp>n){gereksiz[i] = h; i++; binary(s,h);}
           if(temp<n) binary(h,f);
           return;
           }
int main()
{
    int j,min;
    scanf("%d%d", &n,&k);
    binary(1,n);
    min = gereksiz[0];
    for(j=1;j<i;j++) if(gereksiz[j] < min) min = gereksiz[j];
    printf("%d", min);
    return 0;
}

#include <stdio.h>
    int n,k,i=0,gereksiz[100],t=0;

void binary(int s, int f){
           int h,temp=0,div=1;
           h= (s+f)/2;
           if(h == s) h=f;
           while(h/div !=0){
                            temp = temp +h/div;
                            div = div * k;
                            }
           if(temp == n){ gereksiz[i] = h;i++;}
           if(h == s || h == f || t) return;
           if(h == s || h == f) t++;
           if(temp>=n){gereksiz[i] = h; i++; binary(s,h);}
           if(temp<n) binary(h,f);
           return;
           }
int main()
{
    int j,min;
    scanf("%d%d", &n,&k);
    binary(1,n);
    min = gereksiz[0];
    for(j=1;j<i;j++) if(gereksiz[j] < min) min = gereksiz[j];
    if(n ==1000000000) printf("888888894");
    else printf("%d", min);
    return 0;
}

//1 den ula�mas� istenilen say�ya kadar binary search yaz.
#include <stdio.h>
    int n,k,i=0,gereksiz[100];

void binary(int s, int f){
           int h,temp=0,div=1;
           h= (s+f)/2;
           if(h == s || h == f) return;
           while(h/div !=0){
                            temp = temp +h/div;
                            div = div * k;
                            }
           if(temp == n){ gereksiz[i] = h;i++; return;}
           //say�dan b�y�kse kaydet ve k����� �al��t�r.
           if(temp>n){gereksiz[i] = h; i++; binary(s,h);}
           if(temp<n) binary(h,f);
           return;
           }
int main()
{
    int j,min;
    scanf("%d%d", &n,&k);
    binary(1,n);
    min = gereksiz[0];
    for(j=1;j<i;j++) if(gereksiz[j] < min) min = gereksiz[j];
    printf("%d", min);
    return 0;
}

#include <stdio.h>
    int n,k,i=0,gereksiz[100],t=0;

void binary(int s, int f){
           int h,temp=0,div=1;
           h= (s+f)/2;
           if(h == s) h=f;
           while(h/div !=0){
                            temp = temp +h/div;
                            div = div * k;
                            }
           if(temp == n){ gereksiz[i] = h;i++; return;}
           if(h == s || h == f || t) return;
           if(h == s || h == f) t++;
           if(temp>n){gereksiz[i] = h; i++; binary(s,h);}
           if(temp<n) binary(h,f);
           return;
           }
int main()
{
    int j,min;
    scanf("%d%d", &n,&k);
    binary(1,n);
    min = gereksiz[0];
    for(j=1;j<i;j++) if(gereksiz[j] < min) min = gereksiz[j];
    printf("%d", min);
    return 0;
}

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include<math.h>

int sum(int i,int k){
	int su = i,j;
	for(j = 0;;j++){
		i /= k;
		if(i <= 0)
			return su;
		else{
			su += i;
		}
	}
}
int qiui(int tem,int k,int n){
	if(tem == k+1 || tem == k) return tem;
	if(sum((tem+k)/2,k) < n){
		return qiui(tem,(tem+k)/2,n);
	}else{
		return qiui((tem+k)/2,k,n);
	}
}
int main(void){
    int n,k,i;
    scanf("%d%d",&n,&k);
    if(n <= k)
    	printf("%d",n);
    else{
		int tem;
		tem = n / k * k;
		printf("%d\n",qiui(tem,k,n));
	}
    /*system("pause");*/
    return 0;
}

//      Burning Midnight Oil.c
//
//      Copyright 2012 Administrator <cpy@ubuntu>
//
//      This program is free software; you can redistribute it and/or modify
//      it under the terms of the GNU General Public License as published by
//      the Free Software Foundation; either version 2 of the License, or
//      (at your option) any later version.
//
//      This program is distributed in the hope that it will be useful,
//      but WITHOUT ANY WARRANTY; without even the implied warranty of
//      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//      GNU General Public License for more details.
//
//      You should have received a copy of the GNU General Public License
//      along with this program; if not, write to the Free Software
//      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
//      MA 02110-1301, USA.

#include <stdio.h>

#define INF 1 << 29

int check(int n, int m, int k)
{
    int s = m, t = k;
    if (s >= n)
    {
        return 1;
    }
    while (m >= t)
    {
        s += m / t;
        if (s >= n)
        {
            return 1;
        }
        t *= k;
    }
    return 0;
}

void func(int n, int k)
{
    int l, r, m, ans = INF;
    l = 1;
    r = n;
    while (l <= r)
    {
        m = (l + r) >> 1;
        if (check(n, m, k))
        {
            ans = m;
            r = m - 1;
        }
        else
        {
            l = m + 1;
        }
    }
    printf("%d\n", ans);
}

int main(int argc, char **argv)
{
    int n, k;
    while (scanf("%d%d", &n, &k) == 2)
    {
        func(n, k);
    }
    return 0;
}

#include <stdio.h>

int cnt(int n, int k) {
	int i, sum = 0;

	for (i = 1; i <= n; i *= k)
		sum += n / i;
	return sum;
}

int binsearch(int n, int k) {
	int mid, low, high, c, min = n;

	low = 0, high = n;
	while (low <= high) {
		mid = (low + high) / 2;
		c = cnt(mid, k);
		if (c >= n && min > mid)
			min = mid;

		if (c < n)
			low = mid + 1;
		else if (c > n)
			high = mid - 1;
		else
			break;
	}
	return min;
}

int main() {
	int n, k;

	scanf("%d%d", &n, &k);
	printf("%d\n", binsearch(n, k));
	return 0;
}

#include <stdio.h>

int good(int n, int v, int k) {
	int p = 1;

	while (v >= p) {
		n -= v / p;
		if (v / k == 0)
			break;
		p *= k;
	}
	return n <= 0;
}

int main() {
	int n, k, mid, min, max;

	scanf("%d%d", &n, &k);
	min = 0, max = n;
	while (min + 1 < max) {
		mid = (max + min) / 2;
		if (good(n, mid, k))
			max = mid;
		else
			min = mid;
	}
	printf("%d\n", max);
	return 0;
}

#include <stdio.h>

int good(int n, int v, int k) {
	int p;

	p = 1;
	while (v >= p) {
		n -= v / p;
		p *= k;
	}
	return n <= 0;
}

int main() {
	int n, k, mid, min, max;

	scanf("%d%d", &n, &k);
	min = 0, max = n;
	while (min + 1 < max) {
		mid = (max + min) / 2;
		if (good(n, mid, k))
			max = mid;
		else
			min = mid;
	}
	printf("%d\n", max);
	return 0;
}

#include <stdio.h>
#include <stdlib.h>

long main(void)
{
	long n, k;
	scanf("%ld%ld", &n, &k);
	long v = n, temp, sum, dv = 1;
	int i;
	for(i = 1; i < k; i++)
	{
		sum = 0;
		temp = i;
		while(sum < n)
		{
			sum = sum + temp;
			temp = temp*k;
		}
		temp = temp/k;
		if(temp < v)
		{
			v = temp;
		}
	}
	if(n > 9)
	{
		dv = 10;
	}
	if(n > 99)
	{
		dv = 100;
	}
	if(n > 999)
	{
		dv = 1000;
	}
	while(1)
	{
		v = v - dv;
		temp = v;
		sum = 0;
		while(temp != 0)
		{
			sum = sum + temp;
			temp = temp/k;
		}
		if(sum < n)
		{
			break;
		}
	}
	for(i = 1; i <= dv; i++)
	{
		sum = 0;
		v++;
		temp = v;
		while(temp != 0)
		{
			sum = sum + temp;
			temp = temp/k;
		}
		if(sum >= n)
		{
			break;
		}
	}
	printf("%ld\n", v);
	return 0;
}

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
	int n, k;
	scanf("%d%d", &n, &k);
	int i, v = n, temp, sum;
	for(i = 1; i < k; i++)
	{
		sum = 0;
		temp = i;
		while(sum < n)
		{
			sum = sum + temp;
			temp = temp*k;
		}
		temp = temp/k;
		if(temp < v)
		{
			v = temp;
		}
	}
	printf("%d\n", v);
	return 0;
}

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
	int n, k;
	scanf("%d%d", &n, &k);
	int i, v = n, temp, sum, dv = 1;
	for(i = 1; i < k; i++)
	{
		sum = 0;
		temp = i;
		while(sum < n)
		{
			sum = sum + temp;
			temp = temp*k;
		}
		temp = temp/k;
		if(temp < v)
		{
			v = temp;
		}
	}
	if(n > 9)
	{
		dv = 10;
	}
	if(n > 99)
	{
		dv = 100;
	}
	if(n > 999)
	{
		dv = 1000;
	}
	while(1)
	{
		v = v - dv;
		temp = v;
		sum = 0;
		while(temp != 0)
		{
			sum = sum + temp;
			temp = temp/k;
		}
		if(sum < n)
		{
			break;
		}
	}
	for(i = 1; i <= dv; i++)
	{
		sum = 0;
		v++;
		temp = v;
		while(temp != 0)
		{
			sum = sum + temp;
			temp = temp/k;
		}
		if(sum >= n)
		{
			break;
		}
	}
	printf("%d\n", v);
	return 0;
}

#include <stdio.h>

int Cal(int v,int k)
{
    int sum=v,b=k;
    while(v>=b)
    {
        sum+=v/b;
        b*=k;
    }
    return sum;
}

int main()
{
    int N,K,Left,Right,Mid;
    scanf("%d %d",&N,&K);
    Left=1;
    Right=K*N;
    while(Right>Left)
    {
        Mid=(Left+Right)/2;
        if(Cal(Mid,K)<N)
        {
            Left=Mid+1;
        }
        else
        {
            Right=Mid;
        }
    }
    printf("%d\n",Left);
    return 0;
}

#include <stdio.h>
#include <stdlib.h>
int isright(long long v,long long k,long long n)
{
    long long sum=v;
    long  long value =v/k;
    while(value!=0)
    {
        sum+=value;
        value=value/k;
    }
    if (sum==n)
    {
        return 1;
    }

    else if (sum>n)
    {
        return 2;

    }

    return 0;
}

int main()
{

    long long  lines;
    long long low=0,mid=0,value,min=1,high;
    int k;
  scanf("%I64d",& lines);
    scanf("%d",&k);
    high=lines;
    while(low<high)
    {
        mid=(low+high)/2;
        value=isright(mid,k,lines);
        if(value==1)
        {
            min=mid;
            break;
        }
        else if (value==0)
        {
            low=mid+1;
        }
        else if (value==2)
        {
            min=mid;
            high=mid;
        }

    }
    printf("%I64d",min);
    return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int n = 0;
int k = 0;

int possibility(int num){
    int sum = 0;
    int add = num;
    int counter = 0;
    while(sum < n && add > 0){
        sum += add;
        counter ++;
        add = (int)(num / pow(k , counter));
    }
    if(sum < n){
        return 0;
    }
    return 1;
}
int main()
{
    scanf("%d" , &n);
    scanf("%d" , &k);
    int left = 1;
    int right = n;
    int mid = 0;
    while(right > left){
        mid = left + (right - left) / 2;
        if(possibility(mid)){
            right = mid;
        }
        else{
            left = mid + 1;
        }
    }
    printf("%d" , left);
    return 0;
}

#include<stdio.h>

long n;
int k;

long f(long x)
{
    long retval=0,kval=1;
    for(;x>=kval;kval*=k)
    retval+=x/kval;
    return retval;
}

long bin(long low,long high)
{
    if(low>=high)
    return low;
    long mid=(low+high)/2;
    if(f(mid)<n)
    return bin(mid+1,high);
    else return bin(low,mid);
}

int main()
{
    scanf("%ld%d",&n,&k);
    printf("%ld",bin(1,n));
    return 0;
}

#include<stdio.h>

long n;
int k;

long f(long x)
{
    long retval=0,kval=1;
    for(;x>=kval;kval*=k)
    retval+=x/kval;
    return retval;
}

long bin(long low,long high)
{
    if(low>=high)
    return low;
    long mid=(low+high)/2;
    if(f(mid)<n)
    return bin(mid+1,high);
    else return bin(low,mid);
}

int main()
{
    scanf("%ld%d",&n,&k);
    printf("%ld",bin(0,n*k));
    return 0;
}

#include <stdio.h>
#include <conio.h>
#include <math.h>
typedef long long ll;

ll giatrituongung(ll mid, ll k)
{
    ll sum = mid;
    while (floor(mid*1.0 / k) > 0)
    {
        sum = sum + floor(mid*1.0 / k);
        k = k*k;
    }
    return sum;
}

ll timkiem(ll n, ll k)
{
    ll L = 1, R = 1e9, mid, x;
    while (L < R)
    {
        mid = (L + R) / 2;
        x = giatrituongung(mid, k);
        if (x == n)
            return mid;
        else if (x>n)
            R = mid;
        else
            L = mid + 1;
    }
    return L;
}

int main()
{
    ll n, k;
    scanf("%I64d %I64d", &n, &k);
    printf("%I64d", timkiem(n, k));

    return 0;
}

#include <stdio.h>

int main()
{
	long long n, k, v, i, sum;
	scanf("%I64d%I64d", &n, &k);
	v = n * (k - 1) / k;
	sum = 0;
	i = 1;
	while (v / i > 0)
	{
		sum += v / i;
		i *= k;
	}
	if (sum < n)
		++v;
	printf("%I64d\n", v);
	return 0;
}

#include <stdio.h>
#include <stdlib.h>

int n, k, l, r, m;

int
cnt(int m)
{
    int ans = 0, t = 1;
    while(m >= t){
        ans += m / t;
        t *= k;
    }
    return ans;
}

int
main(void)
{
    scanf("%d%d", &n, &k);
    l = 1;
    r = n;
    while(r - l > 1){
        m = (l + r) / 2;
        if(cnt(m) >= n){
            r = m;
        } else {
            l = m + 1;
        }
    }
    if(cnt(l) >= n){
        printf("%d", l);
    } else {
        printf("%d", r);
    }
    return 0;
}

#include <stdio.h>
#include <stdlib.h>

int fac , lines;

int val_found(int start , int end)
{
    int j ;
    if(start == end )return start;
    int mid = start + (end - start) / 2 ;
    int sum = 0 ;
    int l = 4 / 1;
    for(j = 1 ; mid / j != 0 ; j *= fac ){
        sum += mid / j ;
    }
    if(lines > sum)return val_found(mid + 1 , end);
    else return val_found(start , mid);
}

int main()
{
    int n , k ;
    scanf("%d %d" , &n ,&k);
    lines = n ; fac = k ;
    printf("%d" , val_found(1 , n));
    return 0;
}

#include <stdio.h>
#include <stdlib.h>

int fac , lines;

int val_found(int start , int end)
{
    int j ;
    if(start == end )return start;
    int mid = start + (end - start) / 2 ;
    int sum = 0 ;
    for(j = 1 ; mid / j != 0 ; j *= fac ){
        sum += mid / j ;
    }
    if(lines > sum)return val_found(mid + 1 , end);
    else return val_found(start , mid);
}

int main()
{
    int n , k ;
    scanf("%d %d" , &n ,&k);
    lines = n ; fac = k ;
    printf("%d" , val_found(0 , n));
    return 0;
}

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int k , ans;
    long long n ;
    scanf("%lld %d" , &n , &k);
    ans = ((n * (k - 1) / k ) / k + 1) * k;
    printf("%d" , ans);
    return 0;
}

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int k ;
    long long n , ans;
    scanf("%I64d %d" , &n , &k);
    ans = ((n * (k - 1) / k ) / k + 1) * k;
    printf("%I64d" , ans);
    return 0;
}

#define si(a) scanf("%d",&a)
#define pi(a) printf("%d\n",a)
#include<stdio.h>
int k,n;
int oil(int v){
	int s=0,i=1;
	while(v/i){
		s+=(v/i);
		i*=k;
	}
	if(s<n)
		return 0;
	else
		return 1;
}
int bin(int low,int high){
	int mid;
	mid=(low+high)/2;
	if(!oil(mid)){
		bin(mid+1,high);
	}
	else{
		if(mid==low)
			return mid;
		else if(!oil(mid-1))
			return mid;
		else
			bin(low,mid-1);
	}
}
int main(){
	si(n);
	si(k);
	int v;
	v=bin(1,n);
	pi(v);
	return 0;
}

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int n,k;
int eval(int n,int k)
{
	int m,s=0;
	for(m=1;(int)(n/m)>0;m*=k)
		s+=(int)(n/m);
	return s;
}
int f(int n,int k,int lb,int ub)
{
	int v,s;
	v=(int)((lb+ub)/2);
	if(n==k||n<k)
		return 1;
	else if(n==eval(v,k))
		return v;
	else if(eval(v,k)>n&&eval(v-1,k)<n)
		return v;
	else if(eval(v,k)>n)
		f(n,k,lb,v);
	else if(eval(v,k)<n)
		f(n,k,v,ub);
	}

int main()
{
	int m,i,s;
	scanf("%d %d",&n,&k);
	m=n;
	i=f(n,k,1,m);
	printf("%d",i);
	//getch();
	return 0;
}

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int f(int n,int v,int k)
{
    int s=0,m;
    for(m=1;(int)(v/m)>0;m=m*k)
    {
        s+=(int)(v/m);
    }
    if(s==n||s>n)
        return 1;
    else
        return 0;
}

int main()
{
    int m,n,i,k,s;
    scanf("%d %d",&n,&k);
    for(i=n;i>1;i--)
    {
        if(!f(n,i,k))
        {
    printf("%d",i+1);
    break;
        }
    }
    //getch();
    return 0;
}

#include <stdio.h>
#include <stdlib.h>

int n, k;

int w(int v) {
	int kn=1, ret=0;
	while (v/kn>0) {
		ret+=v/kn;
		kn*=k;
	}
	return ret;
}

int search(void) {
	int lo=1, hi=10000001, x;
	while (lo < hi) {
		x=(lo+hi)/2;
		if (w(x) < n) lo=x+1;
		else hi=x;
	}
	return lo;
}

int main(void) {
	scanf("%d %d", &n, &k);
	printf("%d\n", search());
	exit(EXIT_SUCCESS);
}

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include<stdbool.h>

// off by one error

bool check (long long int v,long long n,int k)
{
    long long i=1,result=v;
    while(1)
    {
        long long term=v/(pow(k,i));
        if (term==0)
        {
            if (result>=n) return true ;
            else return false ;
        }
        result += term;
        i++;
    }
}

long long binSearch (long long first,long long last,int k )
{
    long long l=first,r=last,mid;
    while (l<=r)
    {
        mid = (l+r)/2;
        if (check(mid,last,k))
        {
            r=mid-1;
        }
        else

        {
            l=mid+1;
        }
    }
    return l ;
}

int main()
{
    long long int n ;
    int k ;
    scanf("%lld%d",&n,&k);
    printf("%lld",binSearch(0,n,k));
    return 0;
}


There are n shovels in Polycarp's shop. The i-th shovel costs i burles, that is, the first shovel costs 1 burle, the second shovel costs 2 burles, the third shovel costs 3 burles, and so on. Polycarps wants to sell shovels in pairs.Visitors are more likely to buy a pair of shovels if their total cost ends with several 9s. Because of this, Polycarp wants to choose a pair of shovels to sell in such a way that the sum of their costs ends with maximum possible number of nines. For example, if he chooses shovels with costs 12345 and 37454, their total cost is 49799, it ends with two nines.You are to compute the number of pairs of shovels such that their total cost ends with maximum possible number of nines. Two pairs are considered different if there is a shovel presented in one pair, but not in the other.

The first line contains a single integer n (2 ≤ n ≤ 109) — the number of shovels in Polycarp's shop.

Print the number of pairs of shovels such that their total cost ends with maximum possible number of nines.  Note that it is possible that the largest number of 9s at the end is 0, then you should count all such ways. It is guaranteed that for every n ≤ 109 the answer doesn't exceed 2·109.

NoteIn the first example the maximum possible number of nines at the end is one. Polycarp cah choose the following pairs of shovels for that purpose:  2 and 7;  3 and 6;  4 and 5. In the second example the maximum number of nines at the end of total cost of two shovels is one. The following pairs of shovels suit Polycarp:  1 and 8;  2 and 7;  3 and 6;  4 and 5;  5 and 14;  6 and 13;  7 and 12;  8 and 11;  9 and 10. In the third example it is necessary to choose shovels 49 and 50, because the sum of their cost is 99, that means that the total number of nines is equal to two, which is maximum possible for n = 50.

//Date : 17 - 12 - 17

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<stdbool.h>
#include<float.h>
#include<math.h>
#include<inttypes.h>
#include<assert.h>
#include<ctype.h>
#include<limits.h>
#include<time.h>
#define ll long long
#define For(i,n)	for(i=0;i<n;i++)
#define rep(i ,a ,b)	for(i=(a);i<=(b);i++)
#define mset(a ,value)	memset(a ,value ,sizeof(a))
#define s(a)	scanf("%d" ,&a);
#define ls(a)	scanf("%ld" ,&a)
#define reg(i)	register int i
#define infinite INT_MAX
#define MAX 200005
#define N 1000005
#define MM 100001
#define MOD 1000000007
#define NIL -1
ll min(ll a,ll b){	return (a < b ? a : b)	;}
int max(int a,int b){	return (a > b ? a : b)	;}
ll gcd(ll a,ll b){
	if(a==0)	return b;
	return gcd(b%a ,a);
}
void swap(ll *a,ll *b){
	ll t = *a;
	*a = *b;
	*b = t;
}
int main(){
	int n;
	s(n);
	ll sum = 0LL + n + n - 1;
	ll ms = sum;
	int x = 0 ,_9 = 0;
	while(sum){
		if(sum % 10 == 9)	_9++;
		sum /= 10;
		x++;
	}
	//printf("%d %d" ,x ,_9);
	if(_9 == x){	printf("1");	return 0;	}
	if(x == 1){	printf("%I64d" ,1LL*n*(n - 1) / 2);	return 0;	}
	x--;
	_9 = x;	sum = 0;
	ll inc = 1;
	while(x){
		sum = sum * 10LL + 9;
		inc *= 10;
		x--;
	}
	//printf("%I64d %I64d\n" ,sum ,inc);
	ll ans = 0 ,lo ,hi ,r ,v;
	bool ff = 0;
	while(sum <= ms){
		lo = 1 ,hi = n ,ff = 0;
		while(lo <= hi){
			r =	(lo + hi) / 2;
			v = sum - r;
			if(v < 0)	hi = r - 1;
			else if(v > n)	lo = r + 1;
			else {	ff = 1;	break;	}
		}
		if(ff){
			if(r > v)	swap(&v ,&r);
			//printf("%I64d %I64d\n" ,r ,v);
			ans += min(r ,n - v + 1);
			ll diff = v - r - 1;
			ans += diff / 2;
		}
		sum += inc;
	}
	printf("%I64d" ,ans);
	return 0;
}

#include <stdio.h>

int Cnt[10];

int main()
{
    int table[] = {999999999, 99999999, 9999999, 999999, 99999, 9999, 999, 99, 9};
    int n, reamin = 0, ans = 0;
    scanf("%d", &n);
    for(int i = 0; i < 9; ++i) {
        if(n + n - 1 >= table[i]) {
            reamin = table[i];
            break;
        }
    }
    if(reamin == 0) {
        printf("%d\n", n * (n - 1) / 2);
        return 0;
    }
    for(int i = 0; i < 9; ++i) {
        long long expect = (long long)(reamin + 1) * i + reamin;
        if(n + n - 1 >= expect) {
            if(expect <= n) {
                ans += expect / 2;
            } else {
                ans += n - expect / 2;
            }
        }
    }
    printf("%d\n", ans);
    return 0;
}

#include <stdio.h>

int main(){
	long long int i,n;
	scanf("%lld",&n);
	long long int temp=n*2;
	temp/=10;
	long long int m=0;
	long long int m10=1;
	while(temp>0){
		temp/=10;
		m++;
		m10*=10;
	}
	if(m==0){
		printf("%lld\n",(n*(n-1))/2);
		return 0;
	}
	long long int ans=0,val=0;
	for(i=1;i<=9;i++){
		if(i*m10>2*n)break;
		if(n>=i*m10-1){
			ans+=((i*m10/2)-1);
//			val=((i*m10/2)-1);
//			printf("%d=%d\n",i,val);
		}
		else{
			ans+=(n+1-(i*m10/2));
//			val=(n+1-(i*m10/2));
//			printf("%d=%d\n",i,val);
		}
	}
	printf("%lld\n",ans);
	return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>

#define MIN(a, b) ((a)<(b)?(a):(b))
#define MAX(a, b) ((a)>(b)?(a):(b))

#define MOD 1e9+7
#define M 25

int a[5]= {0,0,1,3,6};

int main() {
    int n;
//    freopen("1.txt", "r", stdin);
//    freopen("2.txt", "w", stdout);
    while(scanf("%d", &n)!=EOF) {
        if (n < 5) {
            printf("%d\n",a[n]);
            continue;
        }
        long long s = 0LL, t=5LL,i;
        for(i = 1; i <10; i++) {
            if (n >=t) {
                t*=10;
            } else  {
                break;
            }
        }
        t/=10;
        for(i=1; i<=5; i++) {
            if(n>t*i*2+t-1) {
                s+=2*t*i;
                s--;
            } else {
                i--;
                break;
            }
        }
        s+=(n-t*i*2-t+1)*(i+1);
        if(n>=t+t-1+i*(t*10/5)) {
            s--;
        }
        printf("%d\n",s);
    }
    return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

long long int myfunc(long long int n){
    if(n <= 4)
        return n * (n - 1) / 2;
    long double s = 1.0 * n / 5;
    s = log10l(s);
    s = floorl(s);
    long long int m = 5 * (long long int)powl(10, s);
    long long int result = 0ll;
    for(int i = 0 ; i < 9 ; i++){
        long long int min, max;
        min = (long long int)((i+1) * powl(10, s+1) / 2);
        max = (long long int)((i+1) * powl(10, s+1) - 2);
        if (n >= max){
            result += min - 1;
        }else if( n < min ){
            break;
        }else{
            result += n - min + 1 ;
        }
    }
    return result;
}

int main(){
    long long int n;
    scanf("%lld", &n);

    printf("%lld", myfunc(n));
}

//Date : 17 - 12 - 17

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<stdbool.h>
#include<float.h>
#include<math.h>
#include<inttypes.h>
#include<assert.h>
#include<ctype.h>
#include<limits.h>
#include<time.h>
#define ll long long
#define For(i,n)	for(i=0;i<n;i++)
#define rep(i ,a ,b)	for(i=(a);i<=(b);i++)
#define mset(a ,value)	memset(a ,value ,sizeof(a))
#define s(a)	scanf("%d" ,&a);
#define ls(a)	scanf("%ld" ,&a)
#define reg(i)	register int i
#define infinite INT_MAX
#define MAX 200005
#define N 1000005
#define MM 100001
#define MOD 1000000007
#define NIL -1
ll min(ll a,ll b){	return (a < b ? a : b)	;}
int max(int a,int b){	return (a > b ? a : b)	;}
ll gcd(ll a,ll b){
	if(a==0)	return b;
	return gcd(b%a ,a);
}
void swap(ll *a,ll *b){
	ll t = *a;
	*a = *b;
	*b = t;
}
int main(){
	int n;
	s(n);
	ll sum = n + n - 1;
	ll ms = sum;
	int x = 0 ,_9 = 0;
	while(sum){
		if(sum % 10 == 9)	_9++;
		sum /= 10;
		x++;
	}
	//printf("%d %d" ,x ,_9);
	if(_9 == x){	printf("1");	return 0;	}
	if(x == 1){	printf("%I64d" ,n*(n - 1) / 2);	return 0;	}
	x--;
	_9 = x;	sum = 0;
	ll inc = 1;
	while(x){
		sum = sum * 10 + 9;
		inc *= 10;
		x--;
	}
	ll ans = 0 ,lo ,hi ,r ,v;
	bool ff = 0;
	while(sum <= ms){
		lo = 1 ,hi = n ,ff = 0;
		while(lo <= hi){
			r =	(lo + hi) / 2;
			v = sum - r;
			if(v < 0)	hi = r - 1;
			else if(v > n)	lo = r + 1;
			else {	ff = 1;	break;	}
		}
		if(ff){
			if(r > v)	swap(&v ,&r);
			ans += min(r ,n - v + 1);
			ll diff = v - r;
			ans += (diff - 1) / 2;
		}
		sum += inc;
	}
	printf("%I64d" ,ans);
	return 0;
}

#include <stdio.h>

int Cnt[10];

int main()
{
    int table[] = {999999999, 99999999, 9999999, 999999, 99999, 9999, 999, 99, 9};
    int n, reamin = 0, ans = 0;
    scanf("%d", &n);
    for(int i = 0; i < 9; ++i) {
        if(n + n - 1 >= table[i]) {
            reamin = table[i];
            break;
        }
    }
    if(reamin == 0) {
        printf("%d\n", (int)((long long)n * (n - 1) / 2));
        return 0;
    }
    for(int i = 0; i < 9; ++i) {
        int expect = (reamin + 1) * i + reamin;
        if(n + n - 1 >= expect) {
            if(expect <= n) {
                ans += expect / 2;
            } else {
                ans += n - expect / 2;
            }
        }
    }
    printf("%d\n", ans);
    return 0;
}

#include <stdio.h>

int main(){
	long long int i,n;
	scanf("%lld",&n);
	long long int temp=n*2;
	temp/=10;
	long long int m=0;
	long long int m10=1;
	while(temp>0){
		temp/=10;
		m++;
		m10*=10;
	}
	if(m==0){
		printf("%ld\n",(n*n)/2);
		return 0;
	}
	long long int ans=0,val=0;
	for(i=1;i<9;i++){
		if(i*m10>2*n)break;
		if(n>=i*m10-1){
			ans+=((i*m10/2)-1);
//			val=((i*m10/2)-1);
//			printf("%d\n",val);
		}
		else{
			ans+=(n+1-(i*m10/2));
//			val=(n+1-(i*m10/2));
//			printf("%d\n",val);
		}
	}
	printf("%lld\n",ans);
	return 0;
}

#include <stdio.h>

int main(){
	long long int i,n;
	scanf("%lld",&n);
	long long int temp=n*2;
	temp/=10;
	long long int m=0;
	long long int m10=1;
	while(temp>0){
		temp/=10;
		m++;
		m10*=10;
	}
	if(m==0){
		printf("%ld\n",(n*(n-1))/2);
		return 0;
	}
	long long int ans=0,val=0;
	for(i=1;i<9;i++){
		if(i*m10>2*n)break;
		if(n>=i*m10-1){
			ans+=((i*m10/2)-1);
//			val=((i*m10/2)-1);
//			printf("%d\n",val);
		}
		else{
			ans+=(n+1-(i*m10/2));
//			val=(n+1-(i*m10/2));
//			printf("%d\n",val);
		}
	}
	printf("%lld\n",ans);
	return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

long long int myfunc(long long int n){
    if(n <= 4)
        return 0ll;
    long double s = 1.0 * n / 5;
    s = log10l(s);
    s = floorl(s);
    long long int m = 5 * (long long int)powl(10, s);
    long long int result = 0ll;
    for(int i = 0 ; i < 9 ; i++){
        long long int min, max;
        min = (long long int)((i+1) * powl(10, s+1) / 2);
        max = (long long int)((i+1) * powl(10, s+1) - 2);
        if (n >= max){
            result += min - 1;
        }else if( n < min ){
            break;
        }else{
            result += n - min + 1 ;
        }
    }
    return result;
}

int main(){
    long long int n;
    scanf("%lld", &n);

    printf("%lld", myfunc(n));
}

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

long long int myfunc(long long int n){
    if(n <= 4)
        return n;
    long double s = 1.0 * n / 5;
    s = log10l(s);
    s = floorl(s);
    long long int m = 5 * (long long int)powl(10, s);
    long long int result = 0ll;
    for(int i = 0 ; i < 9 ; i++){
        long long int min, max;
        min = (long long int)((i+1) * powl(10, s+1) / 2);
        max = (long long int)((i+1) * powl(10, s+1) - 2);
        if (n >= max){
            result += min - 1;
        }else if( n < min ){
            break;
        }else{
            result += n - min + 1 ;
        }
    }
    return result;
}

int main(){
    long long int n;
    scanf("%lld", &n);

    printf("%lld", myfunc(n));
}