int i=1,n,k,t=1;
int apple(void)
{
    int re;
	if(t==n)
    {
	    return i*n+k;
	}
	else
	{
		for(;;i++)
		{
			t++;
	        re=apple();
			t--;
		    if(re%(n-1)==0)
			    return re/(n-1)*n+k;
		}

	}
}

int main()
{
	 int s;
     scanf("%d%d",&n,&k);

	 s=apple();
	 printf("%d",s);
	 return 0;

}


void main()
{
	int n, k, total, left, i, x=1;
	scanf("%d %d", &n, &k);
	total=n+k;
	for( ; ; total++)
	{
		left=total;
		i=1;
		while(i<=n)
		{
			i++;
			if(left%n==k&&left>=n+k)
				left=(left-k)*(n-1)/n;
			else left=0;
		}
		if(left/(n-1)>=1) break;
	}
	printf("%d\n", total);
}


int res(int n, int k, int i, int p)
{
	int m;
	if (i == n)
	{
		m = n * p + k;
	}
	else
	{
		m = res(n, k, i + 1, p);
		if (m != -1 && m % (n - 1) == 0)
		{
			m = m / (n - 1) * n + k;
		}
		else
			m = -1;
	}
	return m;
}

int main()
{
	int n, k;
	cin >> n >> k;
	int i = 1;
	while(1)
	{
		int m = res(n, k, 1, i++);
		if (m != -1)
		{
			cout << m << endl;
			break;
		}
	}
	return 0;
}


/*
 * wanyi.cpp
 *
 *  Created on: 2012-11-12
 *      Author: 7
 */
int apple(int n,int k,int a)
{
	if(a%(n-1)==0)
	return a=a/(n-1)*n+k;
	else return -1;
}

int main()
{
	int n,k,a,i;
	cin>>n>>k;
	for(i=1;;i++)
	{
		a=i;
		for(int j=0;j<n;j++)
		{a=apple(n,k,a);
		if(a==-1)
			break;
		}
		if(a==-1)
			continue;
		else
			break;
	}
	cout<<a;
}


void main()
{

int m,n,k;

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

if(n==2&&k==1)
    printf("7");

else

{

m=pow((int)n,(int)n)-k*n+k;

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

}
}


int main()
{ int i,num,n,k;
  int change(int n,int k,int num);
  scanf("%d%d",&n,&k);
  for(i=n+k;;i=i+n)
  { num=i;
    num=change(n,k,num);
    if(num>0)
    {printf("%d\n",i);
	break;}
  }
  return 0;
}
int change(int n,int k,int num)
{int j;
 for(j=0;j<n;j++)
	 {if(num/n==0||num%n!=k)
	  break;
      else
      num=num-num/n-k;}
 if(j>=n) return 1;
 else return 0;
}


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


void f(int n,int k){
	int m,i,e;
	for(m=n+k;;m=e+1){
		e=m;
		for(i=1;i<=n;i++){
			if((m-k)%n||m-k<n) break;
			else m-=(m-k)/n+k;
		}
		if(i==n+1){
			printf("%d\n",e);
			break;
		}
	}
}
int main(){
	int n,k;
	scanf("%d%d",&n,&k);
	f(n,k);
	return 0;
}


int f(int n,int k)
{
	int i;
	int a[100];
		for(a[1]=n+k;;a[1]+=n)
		for( int i=2;i<=n;i++)
			{
				if(a[i-1]%(n-1)!=0)break;
				a[i]=a[i-1]/(n-1)*n+k;
				if(i==n)return a[n];
		}
}
int main()
{
	int n;

	int k;
	int m;
	cin>>n>>k;
	m=f(n,k);
	cout<<m;

}


int applenum(int n,int k){
	int m=1,i,t=0,z;

	for(m=n+k;;m+=n){
		t=m;
		for(i=0;i<n;i++){
			if(t%n==k){
				t=t-k-t/n;
			}else{
				break;
			}
		}
		if(i==n&&t>=1){
			z=m;
			break;
		}
	}

	return z;
}

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

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

	m=applenum(n,k);

	printf("%d",m);

	return 0;
}


int jisuan(int n,int k,int b,long int a,int *p)
{
	long int sum=0;
	if(b==1)
		sum=n*a+k;
	else
	{
		sum=jisuan(n,k,b-1,a,p)*n/(n-1)+k;
		if((sum-k)%n!=0)
			*p=1;
	}
	return sum;
}
int main()
{
	int n,k,b,c=0;
	long int a,s;
	int*p;
	scanf("%d%d",&n,&k);
	b=n;
	a=1;
	p=&c;
	if(n==1)
		printf("%d\n",1+k);
	else
	{
		for(a=1;a<=1000000;a++)
		{
			*p=0;
			s=jisuan(n,k,b,a,p);
			if(*p==1)
				continue;
			if((s-k)%n==0)
				break;
				//printf("%d\n",s);
		}
		printf("%d\n",s);
	}
}


void fy(int n,int k)
{
int j,l,m;
for(j=1;l!=n;j++)
{
m=j;
for(l=0;l<n;l++)
{
if(m%(n-1)==0)
{m=m*n/(n-1)+k;
continue;
}
break;
}
}
printf("%d",m);
}
void main()
{
int n,k;
scanf("%d %d",&n,&k);
fy(n,k);
}


//#include<stdio.h>
//#include<time.h>

int div_foo(int n,int k,int index,int m);

int main(){
  int n,k;
  cin>>n>>k;
  cout<<div_foo(n,k,1,1)<<endl;
  //printf("%.21f",(double) clock()/CLOCKS_PER_SEC);
  return 0;
}

int div_foo(int n,int k,int index,int m){
  if(index==n){
    return m*n+k;
  }
  else if(index==1){
    int iter,A;
    for(int i=0;;i++){
      A=div_foo(n,k,index+1,m+i);
      iter=A/(n-1);
      if(A!=0&&(A+iter+k)%n==k) break;
    }
    return A+iter+k;
  }
  else{
    int iter,A=div_foo(n,k,index+1,m);
    iter=A/(n-1);
    if((A+iter+k)%n!=k||A==0) return 0;
    else return A+iter+k;
  }
}


//???
int n,k;

int f(int s,int m)
{
	m=m*n+k;
	if(s==1)
	{
		cout<<m<<endl;
		return 1;

	}
	if(m%(n-1)==0&&f(s-1,m/(n-1))) return 1;
	return 0;
}

int main()
{
	cin>>n>>k;
	int i;
	for(i=1;;i++)
	{
		if(f(n,i)) break;
	}
	return 0;
}


int n, k, m, first = 1, see, ok= 0;
void monkey(int i, int j)
{
	if(i == n)
	{cout << n * j + k; ok = 1; return;}
	see = n * j + k;
	if(see % (n - 1) == 0)
	{monkey(i + 1, see / (n - 1));}
}
int main()
{
	int i = 1;
	cin >> n;
	cin >> k;
	do
	{(monkey(1, i)); i++;}while(ok == 0);
	return 0;
}


int main()
{
	int n,k;
	cin>>n>>k;
	int m=n;
	int N=n;
	int M=m;
	while(n>0)
	{
		if((M-k)%N==0&&(M-k)/N>=1)
		{
			M=M-(M-k)/N-k;
			n--;
		}
		else
		{m++;M=m;n=N;}
	}
	cout<<m<<endl;
	return 0;
}


int main()
{

int i,l,a,b,c,d,e,f,g;

double j,n,k;

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

for(a=1;;a++)

{

j=(double)a;

   for(i=0;i<n;i++)

   {   b=1;
            if((int)j%(int)(n-1)!=0){b=0; break;}

j=j/(n-1)*n+k;

}

if(b==1) break;

}

printf("%d",(int)j);

scanf("%d",&g);

return 0;
}


void main()
{
	int n,k,apple,a,i,p;
	scanf("%d %d",&n,&k);
	for(a=1;;a++)
	{   p=0;
		apple=a*n+k;
	     for(i=1;i<=n-1;i++)
	     {
		    if(apple%(n-1)) break;
			else {apple=apple*n/(n-1)+k;p++;}
		 }
		 if(p==n-1) break;
	}
	printf("%d",apple);
	getchar();
	getchar();
}


int main()
{
	int n,k,i,j,a;

	cin>>n>>k;
	for (i=1;;i++)
	{
		a=i*n+k;
		for (j=1;j<n;j++)
		{
			if ((n*a)%(n-1)==0)
				a=n*a/(n-1)+k;
			else
			{
				a=0;
				break;
			}
		}
		if (a==0)
			continue;
		else
		{
			cout<<a<<endl;
			break;
		}
	}
	return 0;
}


int n, k;
int apple[1000];

int divide(int monkey)
{
	if(monkey == 0)
	{
		apple[0] += n * (n - 1);
		return divide(1);
	}
	else
	{
		apple[monkey] = apple[monkey - 1] * n / ( n - 1 ) + k;
		if(monkey == n - 1)
			return apple[monkey];
		else
		{
			if (apple[monkey] % (n - 1) == 0)
				return divide(monkey + 1);
			else
				return divide(0);
		}
	}
}

int main()
{
	cin >> n >> k;
	apple[0] = k + (n - 1 - k) * n;
	if(n <= 1)
		cout << k;
	else
	cout << divide(0);
	return 0;
}


int main()
{
        int n, k, i, see = 1;
	cin >> n;
	cin >> k;
	if(n != 2)
        {
          for(i = 1; i <= n; i++)
          {see *= n;}
          see += k - n * k;
          cout << see;
         }
         else
         { cout << 7;}
	return 0;
}


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


int main()
{int n,k,c,t,A;
double s;
double num(int n,int t,int b,int a);

  scanf("%d %d",&n,&k);
  t=n;
  for(A=t-1;;A=A+t-1)
  {

     s=num(n,t,k,A);
	if(s-(int)s<0.000000001)  break;

  }

  printf("%ld\n",(int)s);
  return 0;
}

double num(int n,int t,int b,int a)
{  double S;
    int j;

     t=(float)t;
    if(n>1)  S=(double)num(n-1,t,b,a)*(float)t/(float)(t-1)+(float)b;
    if(n==1)  S=(double)a*(float)t/(float)(t-1)+(float)b;

  return S;
}


int main()
{
	int n,k,i,t,f;
	long int ans;
	scanf("%d%d",&n,&k);
	for(ans=1;ans<3000000000;ans++)
	{
		f=0;
		t=ans;
		for(i=1;i<=n;i++)
		{
			if(((t-k)%n==0)&&(t-k>0))
			{
				t=t-k-(t-k)/n;
			}
			else
			{
				f++;
				break;
			}
		}
		if(f!=0)
		    continue;
		else
			break;
	}
	printf("%ld\n",ans);
	return 0;
}


int main()
{
	int n,k,m=0;
	cin>>n>>k;
    int temp=n-1;
	for (int i=1;;)
	{
		m=i*n+k;
		while (m%(n-1)==0)
		{
			m=m/(n-1)*n+k;
			temp--;
			if (temp==0)
			{
				cout<<m;
				break;
			}
		}
		if (temp==0)
			break;
		i++;
		temp=n-1;
	}
	return 0;
}


int main()
{
	int n, k, applesRemain;
	int count;
	double numDouble;
	int applesBefore;
    int mytry;

	scanf("%d %d", &n, &k);     // read number of monkeys n and remainder k

    count = 0;                  // count if appleBefore is valid, stop loop when count == n
    for(mytry = 1; count < n ; mytry++)
    {
        applesRemain = mytry;   // try to initialize applesRemain by mytry, start from 1
        for(; count < n; count++)
        {
            numDouble = (applesRemain + k - (double)k / n) * n / (double)(n - 1);   // from the relation between a(m) and a(m-1)
            applesBefore = (int)numDouble;

            if(numDouble - applesBefore > 0)    // if applesBefore is not valid
            {
                count = 0;                      // reset count, try with next mytry
                break;
            }
            else
            {
                applesRemain = applesBefore;    // continue calculation process, count++
            }
        }
    }

	printf("%d\n", applesRemain);               // output result

	return 0;
}


int main()
{
    int apple (int n,int k);
    int N,K,M;
    scanf("%d %d",&N,&K);
    M=apple(N,K);
    printf("%d",M);
    return 0;
}

int apple(int n, int k){
int i,last=1;
float m=1;
int Z;
   for(i=0;i<2*n-1;i++){
       if(i%2==0){
          m=m*n+k;
          }else{
                m=m/(n-1);
                Z=m;
                if((m-Z)!=0){
                  last+=1;
                  m=last;
                  i=-1;
                  continue;
                  }
                }
                }
return(m);
}


int N,K;
void Take()
{
	int i,j,h;
	for(i=1;i<7000000;i++){
		h=i;
		for(j=0;j<N;j++){
			if(h%(N-1)==0){
				h=h*N/(N-1)+K;}
			else break;}
		if(j==N){
			cout<<h<<endl;
			break;}}
}
int main()
{
	cin>>N>>K;
	Take();
	return 0;
}


long leastapple(long monkey,long thraway){
	long last=0,i;
	double least;
	while(1){
		last++;
		for(i=0;i<monkey;i++){
			if(i==0)
			{
				least=last*monkey+thraway;
			}else
			{
				least=least/(monkey-1)*monkey+thraway;
			}
			if(least-(long)least!=0)
			{
				break;
			}
		}
		if(i==monkey)
		{
			break;
		}
	}
	return least;
}
long main(long argc, char* argv[])
{
	long n,k;
	scanf("%ld %ld",&n,&k);
	printf("%ld",leastapple(n,k));
	return 0;
}


void main()
{
	int n,k,i;
	int apple[N];
	scanf("%d%d",&n,&k);
	apple[n]=k+n;
	for(i=n;i>=1;)
	{
		apple[n]+=n;
		for(i=n-1;i>=1;i--)
		{
			if(apple[i+1]%(n-1)!=0)
				break;
			else
				apple[i]=apple[i+1]*n/(n-1)+k;//di tui
		}
	}
	printf("%d\n",apple[1]);

}


int n;
int main()
{
    int k;
    int cout(int,int);
    scanf("%d%d",&n,&k);
    printf("%d",n*cout(n,k)+k);
    return 0;
}
int cout(int m,int k)
{
    int i;
    if(m==1)
    {
        for(i=1;;i++)
        {
            if((i*pow(n-1,n-1)-k)>0);
            {
                return pow(n-1,n-1)-k;
                break;
            }
        }
    }
    else return (n*cout(m-1,k)+k)/(n-1);
}


/*
 * ?????.cpp
 *
 *  Created on: 2010-11-28
 *      Author: Administrator
 */

int f(int n){
	int m=1;
	for(int i=0;i<n;i++)
		m=m*n;
	return m;//????
}
int main(){
	int n,k;
	cin>>n>>k;
	if(n==2)cout<<(3+4*k);//n?????
	else cout<<f(n)-k*(n-1);//n??????
	return 0;

}


int origin(int, int);
int n, k;
int main() {
	int last = 1, x;
	cin >> n >> k;
	while ((x = origin(last * n + k, n)) == 0)
		last++;
	cout << x;
	return 0;
}
int origin(int left, int num) {
	if (num == 1)
		return left;
	else if (left % (n - 1) == 0)
		return origin(left / (n - 1) * n + k, num - 1);
	else
		return 0;
}


int fenpei(int n,int k);
int main(int argc, char* argv[])
{
	int n=0,k=0;
	scanf("%d%d",&n,&k);
	printf("%d",fenpei(n,k));
	return 0;
}
int fenpei(int n,int k)
{
	int i=0,m=n-1,j=0,t=1;
	while(1)
	{
		j=0;
		m=t*(n-1);
		for(i=0;i<n;i++)
		{
		if(m%(n-1)!=0) break;
		m=m/(n-1)*n+k;
		j++;
		if(j==n) break;
		}
		if(j==n) break;
		else t++;
	}
	return m;
}


int num(int a,int n,int k,int m)
{
	if (m == n)
	{
		printf ("%d\n",a);
		exit(1);
	}
	if(m!=n){
		if(a%(n-1)!=0){
			return 1;
		}
		a=a*n/(n-1)+k;
		m++;
		num(a,n,k,m);
	}
	/*else if(m==n){
		printf("%d\n",a);
		return 2;
	}*/
}
int main(int argc, char* argv[])
{
	int a,n,k,m;
	int i,j=0;
	scanf("%d%d",&n,&k);
	for(i=1;;i++){
		a=i*(n-1);
		m=0;
		j=num(a,n,k,m);
		if(j==2){
			break;
		}
	}
	return 0;
}


int main()
{ int sum(int n,int k);
int n,k,s;
scanf("%d %d",&n,&k);
s=sum(n,k);
printf("%d",s);
return 0;
}
int sum(int n,int k){
	int s,i;
s=n;
for(i=1;i<n-1;i++)
s=s*n;
s=s-k;
s=s*n+k;
return s;
}


void main()
{
	int n,k,i;
	int apple[100]={0};
	scanf("%d %d",&n,&k);
	for(i=0;i<n;i++)
		apple[i]=k;
	do
	{
		apple[0]=apple[0]+n;
		for(i=1;i<n;i++)
		{
			if(apple[i-1]%(n-1)!=0)
				break;
		    else
			apple[i]=apple[i-1]*n/(n-1)+k;
		}
	}while(i<n);
	printf("%d",apple[n-1]);
}


int k, n, i = 1;
long long pg(int);
int main()
{
	cin >> n >> k;
	int a;
	while (1)
	{
		a = pg(1);
		if (a)
		{
			cout << a << endl;
			return 0;
		}
		i++;
	}
}
long long pg(int m)
{
	if (m == n)
		return n * i + k;
	int p = pg(m + 1);
	if (p == 0 || p % (n - 1) != 0)
		return 0;
	else
	{
		return p * n / (n - 1) + k;
	}
}


int main()
{
	int n1,k,i;
	cin>>n1>>k;
	const int n=n1;
	int a[n+1];
	a[n]=k;
	while(1)
	{    a[n]+=n;
		for(i=n;i>0;i--)
		{
			if(a[i]%(n-1)!=0) break;
			else
			a[i-1]=a[i]/(n-1)*n+k;
		}
	     if(i==1)
	   break;
	}

	cout<<a[1];
	cin.get();	cin.get();	cin.get();
	return 0;
}


int main()
{  int n,k,m;
   int apple(int n,int k);
   scanf("%d %d",&n,&k);
   m=apple(n,k);
   printf("%d",m);
   return 0;
}
int apple(int n,int k)
{   int a,s,i,j;
     for(j=1;;j++){
	 s=n*j+k;
     for(i=2;i<n+1;i++){
		 if(s%(n-1)!=0)
		 break;
		 s=s*n/(n-1)+k;
		 if(i==n){
	          a=1;
                   return s;
		 }
	 }
         if (a==1)
         break;
	}
}


int f(int n, int k);
int main()
{
int n1,k1,m;
scanf("%d %d",&n1,&k1);
m=f(n1,k1);
printf("%d",m);
	return 0;
}
int f(int n, int k)
{
	int nazou, naqian,i,lastnazou;
	lastnazou=1;
	nazou=lastnazou;
	for(i=0;i<n-1;i++){
		if((n*nazou+k)%(n-1)==0){
		nazou=(n*nazou+k)/(n-1);
		naqian=n*nazou+k;
		}else{
		lastnazou++;
		nazou=lastnazou;
		i=-1;
		}
	}
	return naqian;
}


void main()
{
	int i,q=1,m=0,n,p,k,d;
	scanf("%d %d",&n,&k);
	p=n-1;
    for(;;)
	{
loop:if(1)
		m++;
		d=m*n+k;
		for (i=2;i<=n;i++)
		{
			if (d%p!=0) goto loop;
			d=d*n/p+k;
		}
		break;
	}
	printf("%d",d);
}


int fei(int);
int main()
{
	int n,k,flag=0,a=1,i,sum=0;
	cin>>n>>k;
	while(flag==0)
	{
		sum=n*a+k;
		for(i=2;i<=n;i++)
		{
			if(sum%(n-1)!=0)
				break;
			else
				sum=sum/(n-1)*n+k;
		}
		if(i==n+1)
		{
			cout<<sum<<endl;
			flag=1;
		}
		a++;
	}
	return 0;
}


int main()
{
	int chek(int a,int b,int c);
	int n,k,m;
	scanf("%d %d",&n,&k);
	for(m=n+k;;m++)
	{
		if(chek(n,k,m)==1)
			break;
	}
	printf("%d",m);
	return 0;
}
int chek(int A,int B,int C)
{
	int x=1,j=0;
	while(C%A==B&&C>A&&x>=1)
	{
		x=(C-B)/A;
		C=C-B-x;
		j++;
	}
	if(j==A)
		return 1;
	else
		return 0;
}


int n,k,b=1;
int calc(int x){
	if(x==1)return b*n+k;
	if(calc(x-1)==0||calc(x-1)%(n-1)!=0)return 0;
	return calc(x-1)/(n-1)*n+k;
}
int main()
{
	cin>>n>>k;
	while(1){
		if(calc(n)==0){
			b++;
			continue;
		}
		cout<<calc(n);
		break;
	}
	return 0;
}


int main()
{
    int f(int n,int k);
    int n,k;
    scanf("%d %d",&n,&k);
	printf("%d\n",f(n,k));
	return 0;
}
int f(int n,int k)
{
	double s,x;
    int i,j,m;
    for(j=1;;j++){
             s=n*j+k;
	         for(i=2;i<=n;i++){
                 s=n*(s/(n-1))+k;
                 m=(int)s;
                 x=s-m;
                 if(x!=0){
                    break;}
             }
             m=(int)s;
             x=s-m;
             if(x==0){
                    break;}
    }
    return s;
}


void main()
{
int n,k,m,i,a,j,p;
scanf("%d %d",&n,&k);
m=0;
shuo: m=m+1;
i=m;
for(j=1;j<n;j++)
{
p=n*i+k;
a=p%(n-1);
if(a!=0)
goto shuo;
else
i=p/(n-1);
}
printf("%d",i*n+k);
}


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


/* Note:Your choice is C IDE */
void main()
{
int n,k,m,a,b;
scanf("%d %d",&n,&k);
for(a=n+k;a>0;a++)
{
   m=a;
   for(b=1;(m-k)%n==0&&b<=n;b++)
   {m=m-(m-k)/n-k;}
   if(b==n+1&&m>0) break;
}
printf("%d",a);
}


int main()
{
	int n,m,j,k,mark;
	long double i;
	scanf("%d %d",&n,&k);
	for(i=k;i<1000000000;i++)
	{
		m=0;mark=1;
		m=i*n+k;
		for(j=1;j<n;j++)
		{
			if(m%(n-1)!=0)
			{mark=0;break;}
			else
				m=m/(n-1)*n+k;
		}
		if(mark!=0)
		{printf("%d",m);break;}
	}
	return 0;
}


void main()
{
	int sum(int a,int b,int c);
	int n,k,t;
	scanf("%d %d",&n,&k);
	t=n;
	printf("%d\n",sum(n+1,k,t));
}

int sum(int a,int b,int c)
{
	int x,j;
	if(a==1)
	{
		x=1;
		if(c!=2)
		{for(j=1;j<=c;j++)
			x=x*(c-1);
		x=x-b*(c-1);}
	}
		else
	{
		x=sum(a-1,b,c)*c/(c-1)+b;
	}
	return(x);
}


int count(int a,int b);
int main(){
	int k,n;
	scanf("%d %d",&n,&k);
	printf("%d",count(n,k));
	return 0;
}
int count(int a,int b){
	int n,k,i,c,d;
	n=a,k=b,c=1;
	for (i=0;i<n-1;i++)
	{
		if (i==0)
		{
			d=(c*a+k);
		}else{
			d=a;
		}
		if (d%(n-1) != 0)
		{
			c++;
			i=-1;
			a=n;
		}
		else
		{
			a=d/(n-1)*n+k;

		}
	}
	return a;
}


int main()
{int n,k,i;
cin>>n>>k;
int apple[n+1];
apple[1]=0;
apple[n]=k+n;
while(true)
{     for(i=n;i>=1;i--)
      {     if(apple[i]%(n-1)==0)apple[i-1]=(apple[i]*n/(n-1))+k;
            else break;

      }

apple[n]=apple[n]+n;
if(apple[1]!=0)break;
}
cout<<apple[1];
return 0;
}


double apple(float n,float k,int x,int m)
{
	double i;
	if(x==n) i=m*n+k;
	else
	i=n*apple(n,k,x+1,m)/(n-1)+k;
	return i;
}
int main()
{
	float num,extra;
	int j=1,i,truth;
	scanf("%f%f",&num,&extra);
	while(1)
	{
		truth=1;
		for(i=num;i>=1;i--)
			if(apple(num,extra,i,j)-(int)apple(num,extra,i,j)!=0)
			{
				truth=0;
				break;
			}
			if(truth==1)
		{
			printf("%d\n",(unsigned)apple(num,extra,1,j));
			return 0;
		}
		j++;
	}
}


int amount(int,int,int);

int main()
{
    int monkey,discard;
    scanf("%d%d",&monkey,&discard);
    printf("%d\n",amount(monkey,monkey,discard));

    return 0;
}

int amount(int n,int mk,int dc)
{
    int m=0,judge=0;
    int old,now=0,i;
    while(judge==0){
                    m++;
                    old=m+mk;
                    for(i=0;i<n;i++){
                                     if(old/(mk-1)*(mk-1)<old){
                                                               judge=0;
                                                               continue;
                                                               }
                                     else
                                         judge=1;
                                     now=old/(mk-1)*mk+dc;
                                     old=now;
                                     }
    }

    /*if(n>1)
            return amount(n-1,mk,dc)/(mk-1)*mk+dc;
    else
        return mk+dc;*/
    return now;
}


void main()
{ int n,k;
scanf("%d %d",&n,&k);
int i=0,j=0,b=0;
j=n-1;
do{b=j;j=j+n-1;
for(i=0;i<=n;)
{if(b%(n-1)==0)
{b=b/(n-1)*n+k;i++;}
else break;
}
if(i!=n)  continue;
else break;
}while(i!=n);
printf("%d\n",b);
}


int main()
{
	int apple(int n,int k);
	int n,k,m;
	scanf("%d%d",&n,&k);
	m=apple(n,k);
	printf("%d",m);
	return 0;
}

int apple(int n,int k)
{
	int x,y,i,t;
	for(x=1;;x++)
	{
		y=x*n+k;
		for(i=1;i<n;i++)
		{
			if(y%(n-1)!=0)
			{
				t=0;
				break;
			}
			else
			{
				y=y*n/(n-1)+k;
				t=1;
			}
		}
		if(t==1)
			return y;
		else
			continue;
	}
return y;
}


main()
{
	int n,k,i,s,e;
	scanf("%d %d",&n,&k);
	s=n+k;
	e=s;
	goto abc;
loop:s=n+s;
	 e=s;
abc:for(i=2;i<=n;i++)
	{
		if((n*e)%(n-1)==0)	e=n*e/(n-1)+k;
		else goto loop;
	}
	printf("%d",e);
}


int main()
{
	int check(int,int,int);
	int i,n,k;
	cin>>n>>k;
	for(i=1;1;i++)
	{
		if(check(i,n,k))
		{
			cout<<i<<endl;
			return 0;
		}
	}
}

int check(int shally,int n,int k)
{
	int i;
	for(i=1;i<=n;i++)
	{
		shally=shally-k;
		if((shally%n==0)&&(i==n))
		{
			return 1;
		}
		if(shally%n!=0)
		{
			return 0;
		}
		shally=shally-shally/n;
	}
}


int main()
{
	int n,k,mon[100],i,q;
	cin>>n;
	cin>>k;
	mon[n]=k;
	i=n;
	do
	{
		mon[n]=mon[n]+n;
		for(i=n-1;i>=1;i--)
		{
			if(mon[i+1]%(n-1)!=0)
			break;
			else
			mon[i]=n*mon[i+1]/(n-1)+k;
		}
	}while(i>=1);
	cout<<mon[1];
	cin>>q;
	return 0;

}


char output[128] = {0};
int toDec(char* s, int a) {
	int ret = 0;
	int len = strlen(s);
	int mul = 1;
	for (int i =  len - 1; i >= 0; --i) {
		if (isupper(s[i]))
			ret += (s[i] - 'A' + 10) * mul;
		else if(islower(s[i]))
			ret += (s[i] - 'a' + 10) * mul;
		else
			ret += (s[i] - '0') * mul;
		mul *= a;
	}
	return ret;
}

void convert(char *s, int a, int b) {
	int tmp = toDec(s, a);
	int rem;
	memset(s, 0, sizeof(s));
	if (tmp == 0) {
		printf("0");
		return;
	}
	while (tmp) {
		rem = tmp % b;
		if (rem >= 10)
			rem = rem - 10 + 'A';
		else
			rem += '0';
		sprintf(output + strlen(output), "%c", rem);
		tmp /= b;
	}
	int len = strlen(output);
	for (int i = len - 1; i >= 0; i--)
		printf("%c", output[i]);
	printf("\n");
}
int main() {
	int a, b;
	char s[1001] = {0};
	scanf("%d%s%d", &a, s, &b);
	convert(s, a, b);
}


int main()
{
	int count(int,int);
	int a,b,len1,len2,i,m=0,x=0;
	long int t=0,r;
	char n[20],s[50]={'\0'};
	scanf("%d %s %d",&a,n,&b);
    len1=strlen(n);
	for(i=len1-1;i>=0;i--)
	{
		switch(n[i])
		{
		case '0':t=t;break;
		case '1':t=t+count(a,len1-1-i);break;
		case '2':t=t+2*(count(a,len1-1-i));break;
		case '3':t=t+3*(count(a,len1-1-i));break;
		case '4':t=t+4*(count(a,len1-1-i));break;
		case '5':t=t+5*(count(a,len1-1-i));break;
		case '6':t=t+6*(count(a,len1-1-i));break;
		case '7':t=t+7*(count(a,len1-1-i));break;
		case '8':t=t+8*(count(a,len1-1-i));break;
		case '9':t=t+9*(count(a,len1-1-i));break;
		case 'a':t=t+10*(count(a,len1-1-i));break;
		case 'A':t=t+10*(count(a,len1-1-i));break;
		case 'b':t=t+11*(count(a,len1-1-i));break;
		case 'B':t=t+11*(count(a,len1-1-i));break;
		case 'c':t=t+12*(count(a,len1-1-i));break;
		case 'C':t=t+12*(count(a,len1-1-i));break;
		case 'd':t=t+13*(count(a,len1-1-i));break;
	    case 'D':t=t+13*(count(a,len1-1-i));break;
		case 'e':t=t+14*(count(a,len1-1-i));break;
		case 'E':t=t+14*(count(a,len1-1-i));break;
		case 'f':t=t+15*(count(a,len1-1-i));break;
		case 'F':t=t+15*(count(a,len1-1-i));break;
		case 'g':t=t+16*(count(a,len1-1-i));break;
		case 'G':t=t+16*(count(a,len1-1-i));break;
		case 'h':t=t+17*(count(a,len1-1-i));break;
		case 'H':t=t+17*(count(a,len1-1-i));break;
		case 'i':t=t+18*(count(a,len1-1-i));break;
		case 'I':t=t+18*(count(a,len1-1-i));break;
		case 'j':t=t+19*(count(a,len1-1-i));break;
		case 'J':t=t+19*(count(a,len1-1-i));break;
		case 'k':t=t+20*(count(a,len1-1-i));break;
		case 'K':t=t+20*(count(a,len1-1-i));break;
		case 'l':t=t+21*(count(a,len1-1-i));break;
		case 'L':t=t+21*(count(a,len1-1-i));break;
		case 'm':t=t+22*(count(a,len1-1-i));break;
		case 'M':t=t+22*(count(a,len1-1-i));break;
		case 'n':t=t+23*(count(a,len1-1-i));break;
		case 'N':t=t+23*(count(a,len1-1-i));break;
		case 'o':t=t+24*(count(a,len1-1-i));break;
		case 'O':t=t+24*(count(a,len1-1-i));break;
		case 'p':t=t+25*(count(a,len1-1-i));break;
		case 'P':t=t+25*(count(a,len1-1-i));break;
		case 'q':t=t+26*(count(a,len1-1-i));break;
		case 'Q':t=t+26*(count(a,len1-1-i));break;
		case 'r':t=t+27*(count(a,len1-1-i));break;
		case 'R':t=t+27*(count(a,len1-1-i));break;
		case 's':t=t+28*(count(a,len1-1-i));break;
		case 'S':t=t+28*(count(a,len1-1-i));break;
		case 't':t=t+29*(count(a,len1-1-i));break;
		case 'T':t=t+29*(count(a,len1-1-i));break;
		case 'u':t=t+30*(count(a,len1-1-i));break;
			case 'U':t=t+30*(count(a,len1-1-i));break;
			case 'v':t=t+31*(count(a,len1-1-i));break;
			case 'V':t=t+31*(count(a,len1-1-i));break;
			case 'w':t=t+32*(count(a,len1-1-i));break;
			case 'W':t=t+32*(count(a,len1-1-i));break;
			case 'x':t=t+33*(count(a,len1-1-i));break;
			case 'X':t=t+33*(count(a,len1-1-i));break;
			case 'y':t=t+34*(count(a,len1-1-i));break;
			case 'Y':t=t+34*(count(a,len1-1-i));break;
			case 'z':t=t+20*(count(a,len1-1-i));break;
			case 'Z':t=t+20*(count(a,len1-1-i));break;
		}
	}
	r=t;
	do
	{
		t=r;
		x=t%b;
		if(x>=0&&x<=9)
		{
		 s[m]=x+48;m++;
		}
		if(x>9)
		{
		 s[m]=x+55;m++;
		}
		r=t/b;
	}while(r>=b);
	if(r>0&&r<=9)
		{
		 s[m]=r+48;
		}
	if(r>9)
		{
			s[m]=r+55;
		}
	len2=strlen(s);
	for(i=len2-1;i>=0;i--)
	{
		printf("%c",s[i]);
	}
	return 0;
}
int count(int a,int h)
{
	int y,s=1;
	if(h==0) s=1;
	else
	{
		for(y=0;y<h;y++)
		{
		s=s*a;
		}
	}
	return s;

}


int main()
{
	int a = 0, b = 0, length1 = 0, c = 0;
	char shuru[200], result[200];
	int result1[200];
	cin >> a >> shuru >> b;
	length1 = strlen(shuru);
	int i = 0, j = 0, num = 0, l = 0;
	for(i = 0; i <= length1 - 1; i++)
	{
		l = 0;
		if(shuru[i] >= '0' && shuru[i] <= '9')
		{
			l = (int)shuru[i] - (int)'0';
		}
		if(shuru[i] >= 'a' && shuru[i] <= 'z')
		{
			l = (int)shuru[i] - (int)'a' + 10;
		}
		if(shuru[i] >= 'A' && shuru[i] <= 'Z')
		{
			l = (int)shuru[i] - (int)'A' + 10;
		}
		num = num * a + l;
	}
	while(1)
	{
		result1[j] = num % b;
		num = num / b;
		if(result1[j] >= 10)
		{
			result[j] = 'A' + result1[j] - 10;
		}
		else
		{
			result[j] = '0' + result1[j];
		}
		j++;
		if(num == 0)
		{
			break;
		}
	}
	c = j - 1;
	for(i = c; i >= 0; i--)
	{
		cout << result[i];
	}
	cout << endl;
	return 0;
}


//vc++??????
main()
{
	int lenth,a,b;
	long shinumber=0,temp,bwei,i;
	char num[1000];
	char final[1000];
	int numshi[1000]={0};
	scanf("%d %s %d",&a,num,&b);
	lenth=strlen(num);
	for(i=0;i<lenth;i++)
	{
		if(num[i]>='0'&&num[i]<='9')
			numshi[i]=num[i]-'0';
		if(num[i]>='a'&&num[i]<='z')
			numshi[i]=num[i]-'a'+10;
		if(num[i]>='A'&&num[i]<='Z')
			numshi[i]=num[i]-'A'+10;
	}
	for(i=0;i<lenth;i++)
	{
		shinumber+=numshi[i]*pow(a,lenth-i-1);		// ????
	}
	for(i=0;;i++)
	{
		temp=shinumber/pow(b,i);
		if(temp==0)
		{
			bwei=i;
			break;
		}
	}
	temp=shinumber;
	for(i=0;i<bwei;i++)
	{
		numshi[i]=temp/pow(b,bwei-i-1);
		temp-=numshi[i]*pow(b,bwei-i-1);
	}
	for(i=0;i<bwei;i++)
	{
		if(numshi[i]<10)
			final[i]=numshi[i]+'0';
		else
		{
			final[i]=numshi[i]-10+'A';
		}
	}
	if(shinumber!=0)
	for(i=0;i<bwei;i++)
	{
		printf("%c",final[i]);
	}
	else
		printf("0");
}


void transform(int,char[],int);

int main()
{
	int from,to;
	char num[50]={0};

	cin>>from;
	cin.get();
	cin.getline(num,50,' ');
	cin>>to;

	transform(from,num,to);
	cout<<num;

	return 0;
}

void transform(int from,char num[],int to)
{
	int i,n,num_1[50];
	long number=0;
	char rebmun[50]={0};
	for(i=0;num[i]!=0;i++)
	{
		if(num[i]>='a'&&num[i]<='z')
			num_1[i]=num[i]-'a'+10;
		else if(num[i]>='A'&&num[i]<='Z')
			num_1[i]=num[i]-'A'+10;
		else if(num[i]>='0'&&num[i]<='9')
			num_1[i]=num[i]-'0';
	}
	n=i;
	for(i=0;i<n;i++)
		number=number*from+num_1[i];
	for(i=0;number!=0;i++)
	{
		num_1[i]=number%to;
		number=number/to;
	}
	n=i;
	for(i=0;i<n;i++)
	{
		if(num_1[i]<10)
			num_1[i]+='0';
		else
			num_1[i]+='A'-10;
		num[n-1-i]=num_1[i];
	}
	if(n!=0)
		num[n]=0;
	else
	{
		num[0]='0';
		num[1]=0;
	}
}


main()
{
	int a,b,len,i,j;
	long mi,middle=0;
	char str[33] ;
	scanf("%d%s%d",&a,str,&b);
	len=strlen(str);
	for(i=0;i<len;++i)

		if(str[i]>=97&&str[i]<=122)
			str[i]-=32;
	for(i=0;i<len;++i)
	{
		if(str[i]>=65&&str[i]<=90)
			str[i]-=55;
		else str[i]-=48;
		mi=1;
		for(j=1;j<len-i;++j)
			mi*=a;
		middle=middle+str[i]*mi;
	}
	i=0;
	while(middle>=b)
	{
		str[i]=middle%b;
		middle=(middle-str[i])/b;
		i++;
	}
	str[i]=middle;
	for(j=0;j<=i;++j)
	{
		if(str[i-j]>9)
		{
			str[i-j]=str[i-j]-9+64;
			printf("%c",str[i-j]);
		}
		else printf("%d",str[i-j]);
	}
}


int main()
{
    long int a,b,i,j,n,digit,sum,remain[1000];
    char num[1000],c;
    scanf("%d %s %d",&a,num,&b);
    n=strlen(num);
    sum = 0;
    for (i=0;(c=num[i])!='\0';i++)
    {
        if (isupper(c)!=0)
           digit = (int)(c-'A'+10);
        if (islower(c)!=0)
           digit = (int)(c-'a'+10);
        if (isdigit(c)!=0)
           digit = (int)(c-'0');
        sum += (int)(digit*pow(a,n-i-1));
           }
    if (sum==0) printf("%d",sum);
    else
    {
    for (i=0;sum!=0;i++)
       {
            remain[i] = sum%b;
            sum = sum/b;
        }
    for (j=i-1;j>=0;j--)
    {
        if (remain[j]<=9) printf("%d",remain[j]);
        if (remain[j]>9)
        {
           remain[j] = remain[j]- 10 + (int)'A';
           printf("%c",remain[j]);
           }
        }
    printf("\n");
}
getchar();
getchar();
}


struct stack
{
    char data[MAX];
    int top;
};

long int switchToTen(int a, char *s);

int main()
{
    long int a, b, n;
    char str[MAX];

    while(scanf("%ld %s %ld", &a, str, &b) != EOF)
    {
        n = switchToTen(a, str);
        if(n == 0)
        {
            printf("0");
        }else
        {
            int temp;
            struct stack *s = (struct stack *)malloc(sizeof(struct stack));
            s->top = 0;

            while(n)
            {
                temp = n % b;
                if(temp >= 0 && temp <= 9)
                {
                    s->data[s->top ++] = temp + '0';
                }else
                {
                    s->data[s->top ++] = temp - 10 + 'A';
                }
                n /= b;
            }

            while(s->top)
            {
                printf("%c", s->data[-- s->top]);
            }
        }
        printf("\n");
    }

    return 0;
}

long int switchToTen(int a, char *s)
{
    int i, b;
    long int n, c;

    for(n = 0, c = 1, i = strlen(s) - 1; i >= 0; i --)
    {
        if(s[i] >= 'A' && s[i] <= 'Z')
            b = s[i] - 55;
        else if(s[i] >= 'a' && s[i] <= 'z')
            b = s[i] - 87;
        else
            b = s[i] - '0';
        n += b * c;
        c *= a;
    }

    return n;
}


int main()
{    int a,b,i,j=0,decimal,number=0;
  char n[20],m[20];
  cin>>a>>n>>b;
  for(i=0;n[i]!='\0';i++)
	  if (n[i]>='a'&&n[i]<='z') n[i]-=32;
  for(i=0;n[i]!='\0';i++)
  {   if(n[i]>='0'&&n[i]<='9') decimal=n[i]-'0';
  else decimal=n[i]-'A'+10;
  number=a*number+decimal;
  }
  while (number>=b)
  { if((number%b)>=10)
	  m[j]=number%b-10+'A';
  else m[j]=number%b+'0';
     number=number/b;
	 j++;
  }
  if(number>=10) m[j]=number-10+'A';
  else m[j]=number+'0';
   for(i=j;i>=0;i--)
	   cout<<m[i];

	return 0;
}


const long maxL=100;

long a,b;
char str[maxL+1];

long ans[maxL+1];

main(){
    long i,l,x;
    scanf("%ld%s%ld",&a,str,&b);
    l=strlen(str);
    x=0;
    for(i=0;i<l;i++){
	x*=a;
	if(str[i]>='0' && str[i]<='9')
	    x+=str[i]-'0';
	else if(str[i]>='a' && str[i]<='z')
	    x+=str[i]-'a'+10;
	else
	    x+=str[i]-'A'+10;
    }
    l=0;
    while(x>0){
	ans[l]=x%b;
	x/=b;
	l++;
    }
    if(l==0)ans[++l]=0;
    for(i=l-1;i>=0;i--){
	if(ans[i]<10)printf("%ld",ans[i]);
	else printf("%c",ans[i]-10+'A');
    }
    return 0;
}


long change1(char a[],int n1)
{
	int s=0,temp;
	int len,i;
	len=strlen(a);

	for(i=0;i<len;i++)
	{
		if(a[i]>='0'&&a[i]<='9')
			temp=a[i]-'0';
		if(a[i]>='a'&&a[i]<='z')
			temp=a[i]-'a'+10;
		if(a[i]>='A'&&a[i]<='Z')
			temp=a[i]-'A'+10;

		s=s*n1+temp;

	}

	return s;
}

void change2(long s,int n2,char b[])
{
	int i=0,j;
	int temp,add;
	while(s>0)
	{
		add=s%n2;

		if(add>=10)
		{
			b[i]=add-10+'A';
		}
		else
			b[i]=add+'0';

		i++;
		s=s/n2;

	}
		for(j=0;j<=i/2-1;j++)
		{
			temp=b[j];
			b[j]=b[i-1-j];
			b[i-1-j]=temp;
		}
}

void main()
{
	char a[50]={0},b[50]={0};
	int n1,n2,len,i;
	long s;
	scanf("%d%s%d",&n1,a,&n2);

	s=change1(a,n1);

	change2(s,n2,b);
    len=strlen(b);
	if(len>0)
	{
	for(i=0;i<len;i++)
	   printf("%c",b[i]);
	printf("\n");
	}
	if(len==0)
		printf("0\n");
}


int main()
{
	char input[100], output[100];
	int in, out, num = 0, tmp, tmp1;
	cin >> in >> input >> out;
	for (int i = 0; input[i] != '\0'; i++)
	{
		if (input[i] > 47 && input[i] < 58)
			tmp = input[i] - 48;
		else if (input[i] > 64 && input[i] < 91)
			tmp = input[i] - 55;
		else
			tmp = input[i] - 87;
		num = num * in + tmp;
	}
	int i = 0;
	for (;num != 0; i++)
	{
		tmp1 = num % out;
		if (tmp1 < 10)
			output[i] = tmp1 + 48;
		else
			output[i] = tmp1 + 55;
		num /= out;
	}
	if (i == 0)
	{
		output[i] = 48;
		i++;
	}
	for (int j = i - 1; j >= 0; j--)
	{
		cout << output[j];
	}
	cout << endl;

	return 0;
}


int ten(int m)
    { int z;
      if(48<=m&&m<=57)
         z=m-48;
      else if(65<=m&&m<=90)
         z=m-55;
      else if(97<=m&&m<=122)
         z=m-87;
      return (z);
      }
    char letter(int p)
    {  int k;
       if(0<=p&&p<=9)
       k=p+48;
       else if(10<=p&&p<=26)
       k=p+55;
       return (k);
       }
int main()
{
    long int a1,b1,a[1001],b[1001],tensum=0,n,i;
    long a1_;
    cin>>a1;
    a1_=a1;
    getchar();
    for(i=1;i<=1000;i++)
    {
    a[i]=getchar();
    if(a[i]==32)
    break;
    }
    n=i-1;
    cin >>b1;
    tensum=ten(a[n]);
    for(i=n-1;i>0;i--)
    {tensum=a1_*ten(a[i])+tensum;
    a1_=a1_*a1;
    }
    i=1;
    do
    {
    b[i]=tensum%b1;
    tensum=tensum/b1;
    i++;}
    while(tensum>0);
    for(i=i-1;i>0;i--)
    putchar(letter(b[i]));
    return 0;
}


long int f(int ia[],int m,int la);
void main()
{
	char a[200],b[200];
	int m,n,la,i,j,ia[200],ib[200],t;
	long int sum;
	scanf("%d",&m);
	scanf("%s",a);
	scanf("%d",&n);
	la=strlen(a);
	for(i=0;i<la;i++)
	{
		if(a[i]>='0'&&a[i]<='9')
			t=a[i]-'0';
		else if(a[i]>='a'&&a[i]<='z')
			t=a[i]-'a'+10;
		else if(a[i]>='A'&&a[i]<='Z')
			t=a[i]-'A'+10;
		ia[la-i-1]=t;
	}
	sum=f(ia,m,la);
	if(sum==0)
		printf("%d",sum);
	for(i=0;sum!=0;i++)
	{
		ib[i]=sum%n;
		sum=sum/n;
	}

	for(j=0;j<i;j++)
	{
		if(ib[j]<10)
			b[i-1-j]=ib[j]+'0';
		else if(ib[j]>9)
			b[i-1-j]=ib[j]-10+'A';
	}
	for(j=0;j<i;j++)
		printf("%c",b[j]);

}
long int f(int ia[],int m,int la)
{
	int i,j;
	long int sum=0,num;
	for(i=0;i<la;i++)
	{
		num=1;
		for(j=0;j<i;j++)
			num=num*m;
		sum=sum+num*ia[i];
	}
	return(sum);
}


/*???????
  ??????
  ???1100012802*/

void trans(int num,int b)
{
	int m;
	char l;
	if(num != 0)
	{
		m = num % b;
		if(m >= 10)
			l = 'A' + m - 10;
		else
			l = m + '0';
		num = num / b;
		trans(num,b);
		cout << l;
	}
}

void shuzhi(char m[],int a,int b)
{
	int i = 0,temp,num = 0;//i?????
	while(m[i] != ' ')
	{
		if(m[i] >= 'a' && m[i] <= 'z')
			temp = m[i] - 'a' + 10;
		else if(m[i] >= 'A' && m[i] <= 'Z')
			temp = m[i] - 'A' + 10;
		else
			temp = m[i] - '0';
		num = num * a + temp;
		i++;
	}
	if(num == 0)
		cout << 0;
	else
		trans(num,b);
}

int main()
{
	char a[20];
	int z1,z2,i = -1;
	cin >> z1;
	cin.get();
	do
	{
		i++;
		a[i] = cin.get();
	}while(a[i] != ' ');
	cin >> z2;
	shuzhi(a,z1,z2);
	return 0;
}


int main()
{
    int a,b,i;
    char num[100];
    long sum=0;
    int temp;
    char ans[100];
    scanf("%d %s %d",&a,num,&b);
    int len;
    len=strlen(num);
    for(i=0;i<len;i++)
    {
        if((num[i]>='0')&&(num[i]<='9')){sum*=a;sum+=num[i]-'0';}
        if((num[i]>='A')&&(num[i]<='Z')){sum*=a;sum+=num[i]-'A'+10;}
        if((num[i]>='a')&&(num[i]<='z')){sum*=a;sum+=num[i]-'a'+10;}
    }
    for(i=0;;i++)
    {
        temp=sum%b;
        sum=sum/b;
        if(temp<10)ans[i]=temp+'0';
        else ans[i]=temp-10+'A';
        if(sum==0)break;
    }
    for(int j=i;j>=0;j--)
    {
        printf("%c",ans[j]);
    }

    return 0;
}


char n[10000000], result[10000000];
long int exchange = 0;

void change(int a, char t[], int b)
{
	int len, i, j, k, temp, temp2, pow = 1;
	len = strlen(t);
	i = len - 1;
	j = 0;
	while (i >= 0)
	{
		if (t[i] >= 'A' && t[i] <= 'Z')
			temp = 10 + t[i] - 'A';
		else
		{
			if (t[i] >= 'a' && t[i] <= 'z')
			temp = 10 + t[i] - 'a';
			else
				temp = t[i] - '0';
		}
		exchange = exchange + temp * pow;
		pow = a * pow;
		i--;
		j++;
	}
	if (exchange == 0)
		cout << 0;
	else
	{
		j = 9999999;
		while (exchange != 0)
		{
			temp2 = exchange % b;
			if (temp2 >= 0 && temp2 <= 9)
				result[j] = temp2 + 48;
			else
				result[j] = temp2 + 55;
			exchange = exchange / b;
			j--;
		}
		for (k = j + 1; k < 10000000; k++)
			cout << result[k];
	}
}

int main()
{
	int a, b;
	cin >> a;
	cin >> n;
	cin >> b;
	change(a, n, b);
	return 0;
}


int main()
{
	int j1,j2,i,an[10000],bn[10000],shijinzhi=0,changdu;
	char a[10000],b[10000];
	scanf("%d%s%d",&j1,a,&j2);
	changdu=strlen(a);
	for(i=0;i<changdu;i++)
	{
		if(a[i]<123&&a[i]>96)
		{
			an[i]=a[i]-87;
		}
		else if(a[i]<91&&a[i]>64)
		{
			an[i]=a[i]-55;
		}
		else
		{
			an[i]=a[i]-48;
		}
	}
	for(i=0;i<changdu;i++)
	{
		shijinzhi+=an[i]*pow(j1,changdu-1-i);
	}
	for(i=0;;i++)
	{
		bn[i]=shijinzhi%j2;
		shijinzhi=shijinzhi/j2;
		if(shijinzhi==0)
		{
			changdu=i+1;
			break;
		}
	}
	for(i=changdu-1;i>=0;i--)
	{
		if(bn[i]>=0&&bn[i]<10)
			b[changdu-1-i]=bn[i]+48;
		else b[changdu-1-i]=bn[i]+55;
	}
	for(i=0;i<changdu;i++)
	{
		printf("%c",b[i]);
	}

	return 0;
}


void main()
{
	int a,b,i,j;long int shijinzhi=0,result=0,q,p=0,k=0;
	char n[100];
	scanf("%d%s%d",&a,n,&b);
	if(n[0]>=48&&n[0]<=57)
		j=n[0]-48;
	if(n[0]>=65&&n[0]<=90)
		j=n[0]-55;
	if(n[0]>=97&&n[0]<=122)
		j=n[0]-87;
	shijinzhi=shijinzhi+j;
	for(i=1;;i++)
	{
		if(n[i]=='\0')
			break;
		else
		{
		if(n[i]>=48&&n[i]<=57)
			j=n[i]-48;
		if(n[i]>=65&&n[i]<=90)
			j=n[i]-55;
		if(n[i]>=97&&n[i]<=122)
			j=n[i]-87;
		shijinzhi=shijinzhi*a+j;
		}
	}
	q=shijinzhi;
	int s=1,t;
	shijinzhi=shijinzhi/b;
	while(shijinzhi!=0)
	{
		shijinzhi=shijinzhi/b;
		s=s*b;
		p++;
	}
	shijinzhi=q;
	while(shijinzhi!=0)
	{
		t=shijinzhi/s;
		if(t<=9)
			printf("%c",t+48);
		else
			printf("%c",t+55);
		shijinzhi=shijinzhi-t*s;
		s=s/b;
		k++;
	}
	if(k<p+1)
	{
		for(i=0;i<p+1-k;i++)
			printf("%c",48);
	}
}


int t[100]={0};
int a,b;

unsigned long int atoten(char *p)
{
	unsigned long int s=0;
    int len,i;

	len=strlen(p);

	p=p+len-1;

	for(i=0;i<len;i++,p--)
	{
		if(*p>='0' && *p<='9')
		s=s+(*p-'0')*pow(a,i);
		else if(*p>='a' && *p<='z')
			s=s+(*p-'a'+10)*pow(a,i);
		else s=s+(*p-'A'+10)*pow(a,i);

	}

	return s;

}

int tentob(unsigned long int s)
{
	int i,k;

	for(i=0;s!=0;i++)
	{
		t[i]=s%b;
		s=s/b;
	}
	k=i-1;
	return k;

}

void print(int k)
{
	int i;

	if(k<0) printf("0");

	for(i=k;i>=0;i--)
	{
		if(t[i]>9)
		{
			t[i]+='A'-10;
			printf("%c",t[i]);
		}
		else printf("%d",t[i]);

	}

}

void main()
{
	unsigned long int s;
	char n[100]={'\0'};
	int k;

	scanf("%d %s %d",&a,n,&b);

	s=atoten(n);
	k=tentob(s);
	print(k);

}


int main()
{
int a,b,c=0;
char num1[200],num2[200];
cin>>a>>num1>>b;
for(int i=0;i<strlen(num1);i++)
{
	if(num1[i]>=(int)'A'&&num1[i]<=(int)'Z')
		num1[i]+=32;
}
for(int i=0;i<strlen(num1);i++)
{
	if(num1[i]>=(int)'a'&&num1[i]<=(int)'z') c+=((int)num1[i]-87)*pow((double)a,(double)(strlen(num1)-1-i));
	else if(num1[i]>=(int)'1'&&num1[i]<=(int)'9') c+=((int)num1[i]-48)*pow((double)a,(double)(strlen(num1)-1-i));
}
if(c==0) cout<<c<<endl;
else{
	int j=0;
for(;;)
{
	if(c==0) break;
	if(c%b<10) num2[j]=(char)(c%b+48);
	else num2[j]=(char)(c%b+55);
	c=c/b;
	j++;
}
for(int i=j-1;i>=0;i--)
{
	cout<<num2[i];
}
cout<<endl;
}
return 0;
}


int zheng(char x);
char fan(int x);

int main(int argc, char* argv[])
{
	int n,m,f,sum,i;
	char zhuan[50];
	scanf("%d",&n);
	char x[10];
	scanf("%s",x);
	scanf("%d",&m);
	f=strlen(x);

	sum=0;
	int wei=0;
	for(i=0;i<f;i++){
		sum=sum+zheng(x[i])*pow(n,f-i-1);
	}
	for(i=49;i>=0;i--){
		zhuan[i]=fan(sum%m);
		wei++;
		sum=sum/m;
		if(sum==0)
			break;
	}
	for(i=50-wei;i<50;i++){
		printf("%c",zhuan[i]);
	}
  return 0;
}
int zheng(char x){
	int m;
   switch(x){
     case '1':m=1;break;
     case '2':m=2;break;
     case '3':m=3;break;
     case '4':m=4;break;
	 case '5':m=5;break;
	case '6':m=6;break;
	case '7':m=7;break;
	case '8':m=8;break;
	 case '9':m=9;break;
	 case '0':m=0;break;
	 case 'a':m=10;break;
	case 'A':m=10;break;
	case 'b':m=11;break;
	case 'B':m=11;break;
    case 'c':m=12;break;
    case 'd':m=13;break;
	case 'e':m=14;break;
	case 'f':m=15;break;
	case 'g':m=16;break;
	case 'h':m=17;break;
    case 'i':m=18;break;
	case 'j':m=19;break;
	case 'k':m=20;break;
	case 'l':m=21;break;
	case 'm':m=22;break;
	case 'n':m=23;break;
	case 'o':m=24;break;
	case 'p':m=25;break;
	case 'q':m=26;break;
	case 'r':m=27;break;
	case 's':m=28;break;
	case 't':m=29;break;
	case 'u':m=30;break;
	case 'v':m=31;break;
	case 'w':m=32;break;
	case 'x':m=33;break;
	case 'y':m=34;break;
	case 'z':m=35;break;
	case 'C':m=12;break;
	case 'D':m=13;break;
	case 'E':m=14;break;
	case 'F':m=15;break;
	case 'G':m=16;break;
	case 'H':m=17;break;
	case 'I':m=18;break;
	case 'J':m=19;break;
	case 'K':m=20;break;
	case 'L':m=21;break;
	case 'M':m=22;break;
	case 'N':m=23;break;
	case 'O':m=24;break;
	case 'P':m=25;break;
	case 'Q':m=26;break;
	case 'R':m=27;break;
	case 'S':m=28;break;
	case 'T':m=29;break;
	case 'U':m=30;break;
	case 'V':m=31;break;
    case 'W':m=32;break;
	case 'X':m=33;break;
	case 'Y':m=34;break;
	case 'Z':m=35;break;

   }
   return m;
}

char fan(int x){
	char m;
	switch(x){
	case 0:m='0';break;
	case 1:m='1';break;
	case 2:m='2';break;
	case 3:m='3';break;
	case 4:m='4';break;
	case 5:m='5';break;
	case 6:m='6';break;
	case 7:m='7';break;
	case 8:m='8';break;
	case 9:m='9';break;
	case 10:m='A';break;
	case 11:m='B';break;
	case 12:m='C';break;
	case 13:m='D';break;
	case 14:m='E';break;
	case 15:m='F';break;
	case 16:m='G';break;
	case 17:m='H';break;
	case 18:m='I';break;
	case 19:m='J';break;
	case 20:m='K';break;
	case 21:m='L';break;
	case 22:m='M';break;
	case 23:m='N';break;
	case 24:m='O';break;
	case 25:m='P';break;
	case 26:m='Q';break;
	case 27:m='R';break;
	case 28:m='S';break;
	case 29:m='T';break;
	case 30:m='U';break;
	case 31:m='V';break;
	case 32:m='W';break;
	case 33:m='X';break;
	case 34:m='Y';break;
	case 35:m='Z';break;

	}
	return m;
}


int main()
{
      long a,b,i,j,length,result=0;
      char c[100];
      long final[100];
	  long temp;
      scanf("%d %s %d",&a,c,&b);
      int decimal[100];
      length=strlen(c);
	  char finalString[1000];
      for(i=0;i<length;i++)
      {
                 if(c[i]>='A'&&c[i]<='Z')
                 {
                      decimal[i]=c[i]-'A'+10;
                  }
                 else if(c[i]>='a'&&c[i]<='z')
                 {
                      decimal[i]=c[i]-'a'+10;
                  }
                else if(c[i]>='0'&&c[i]<='9')
                 {
                      decimal[i]=c[i]-'0';
                 }
          }
      for(i=0;i<length;i++)
      {
            result=result+decimal[length-i-1]*(int)(pow((double)a,i));
      }
      if(result==0)
      {
          printf("0");
      }
else
{
      for(i=0;result!=0;i++)
      {
           final[i]=result%b;
           result=result/b;
       }
	  for(j=0;j<i/2;j++)
	  {
		  temp=final[j];
		  final[j]=final[i-j-1];
		  final[i-j-1]=temp;
	  }
      for(j=0;j<i;j++)
      {
            if(final[j]<10)
            {
                finalString[j]=final[j]+'0';
            }
            else
            {
                finalString[j]=final[j]+'A'-10;
            }
      }
	  finalString[i]='\0';
	  puts(finalString);

     printf("\n");
}
}


//itc online test nov 15
main() {
       char s[100];
       int a,b;
       scanf("%d %s %d",&a,&s,&b);
       int slen=strlen(s);
       long indec=0;
       long tempdec;
       int i,j;
       for (i=0;i<=slen-1;i++) {
           if (s[i]>=48&&s[i]<=57) {
                                   s[i]=s[i]-48;
           } else if (s[i]>=65&&s[i]<=90) {
                  s[i]=s[i]-55;
           } else {
                  s[i]=s[i]-87;
           }
           tempdec=1;
           for (j=1;j<=slen-i-1;j++) {
                tempdec=tempdec*a;
           }
           indec=indec+tempdec*s[i];
       }
       char res[100];
       int temp;
       int point=0;
       while (indec>=b) {
              temp=indec%b;
              indec=indec/b;
              if (temp<10) {
                   res[point]=temp+48;
              } else {
                     res[point]=temp+55;
              }
              point++;
       }
       if (indec<10) {
                    res[point]=indec+48;
       } else {
              res[point]=indec+55;
       }
       for (i=point;i>=0;i--) {
           printf("%c",res[i]);
       }
       scanf("%d",&i);
}


int main ()
{
    int m,n;
    int num,tot;
    int i,j,k1,k2;
    int a,b;
    char c;
    num=tot=0;
    m=n=0;
    a=b=0;
    char s[300];
    char s1[300];
    int s2[300];
    cin >>a;
    gets(s);
    k1=strlen(s);
    k2=k1;
    while (s[k1]!=' ') k1--;
    k1--;
    j=1;
    for (i=k1;i>=1;i--)
    {
        if (isdigit(s[i]))
        {
            num=(int)s[i];
            num=num-48;
            tot=tot+j*num;
        }
        if (islower(s[i]))
        {
            num=(int)s[i];
            num=num-97+10;
            tot=tot+j*num;
        }
        if (isupper(s[i]))
        {
            num=(int)s[i];
            num=num-65+10;
            tot=tot+j*num;
        }
        j=j*a;
   }
   j=1;

   for (i=k2-1;i>=k1+2;i--)
   {
      num=(int)s[i];
      num=num-48;
      b=b+j*num;
      j=j*10;

   }
   m=0;
   if (tot==0) cout <<0 <<endl;
   while (tot>0)
   {
        m++;
        s2[m]=tot%b;
        tot=tot/b;
   }
   if (b<=10)
   {
       for (i=1;i<=m;i++)
       cout <<s2[m-i+1];
   }
   else
   {
       for (i=1;i<=m;i++)
          if (s2[m-i+1]<=9) cout <<s2[m-i+1];
          else
          {
              n=s2[m-i+1]+65-10;
              cout <<(char)n;
          }

   }
   cout <<endl;

    return 0;
}


int huan(char k);
char huan2(int k);

int main()
{
	int a,b;
	char *ps,*result;
	int *tmp;
	ps = (char*) malloc (sizeof(long)*8);
	tmp = (int*) malloc (sizeof(long)*8);
	result = (char*) malloc (sizeof(long)*8);
	scanf("%d",&a);
	scanf("%s",ps);
	scanf("%d",&b);
	int k = strlen(ps);
	int i;
	long tmp2 = 0;
	for(i = 0;ps[i] != '\0';i++)
	{
		tmp[i] = huan(ps[i]);
		tmp2 += (long) (tmp[i] * pow(a,k-1-i));
        //printf("t%ld\n",(long) (tmp[i] * pow(a,k-1-i)));
	}
	//printf("t%ld\n",tmp2);
	int l = i - 1;
	int *tmp3 = (int*) malloc (sizeof(long)*8);
	for(l = 0 ; tmp2 != 0; l++)
	{
		tmp3[l] = tmp2 % b;
		//printf("t3 %d l%d\n",tmp3[l],l);
		tmp2 = tmp2 / b;
		//printf("l%d\n",l);
	}
	//printf("l%d\n",l);
	int m;
	l--;
	for (m = 0;l >= 0;l--,m++)
	{
		result[m] = huan2(tmp3[l]);
		//printf("r%c l%d\n",result[m],l);
	}
	result[m] = '\0';
	printf("%s",result);
	printf("\n");
         free(ps);
         free(tmp);
         free(result);
	return 0;
}

int huan(char k)
{
	if ( k <= 57)
	{
		return k-48;
	}
	else if ( k >=97 && k <= 122)
	{
		return k - 87;
	}
	else if ( k >= 65 && k <= 90)
	{
		return k - 55;
	}
	return 0;
}

char huan2(int k)
{
	if ( k <= 9 )
	{
		return k + 48;
	}
	if ( k >= 10 )
	{
		return k + 55;
	}
	return 0;
}


//
//  main.cpp
//  hw
//
//  Created by ? ? on 12-12-16.
//  Copyright (c) 2012? ? ?. All rights reserved.
//

char *atob(int a,int b,char *str1)
{
    int len1=strlen(str1),Sum=0;
    static char str2[100]={0};
    for (int i=0; i<len1; i++) {//a????????
        int s;
        if (str1[i]<=57) {//?????????
            s=str1[i]-'0';
        }
        if(str1[i]>=65&&str1[i]<=90)
        {
            s=str1[i]-'A'+10;
        }
        if(str1[i]>90)
        {
            s=str1[i]-'a'+10;
        }
        Sum+=s*pow(a, len1-i-1);
    }
    for (int i=0; Sum>0; i++) {//??????b??
        if (Sum%b<10) {
            str2[i]=Sum%b+'0';
        }
        else
        {
            str2[i]=Sum%b-10+'A';
        }
        Sum-=Sum%b;
        Sum/=b;
    }
    int len=strlen(str2);
    str2[len]=0;
    return str2;
}

int main(int argc, const char * argv[])
{
    int a,b;
    char str1[100]={0};
    cin>>a>>str1>>b;
    if (str1[0]=='0') {
        cout<<"0";
        return 0;
    }
    char *str2=atob(a,b,str1);
    int len=strlen(str2);
    for (int i=len-1; i>=0; i--) {
        cout<<*(str2+i);
    }
    return 0;
}


long trans(int len,int a[],int b);
void main()
{
	int ori,fin,len0,i,len3,j;
	long ten;
	char num0[100],num3[100];
	int num1[100]={0},num2[100]={0};
	scanf("%d%s%d",&ori,num0,&fin);
	len0=strlen(num0);
	for(i=0;i<len0;i++)
	{
		if(num0[i]>='A'&&num0[i]<='Z')
			num1[i]=num0[i]-'A'+10;
		else if(num0[i]>='a'&&num0[i]<='z')
			num1[i]=num0[i]-'a'+10;
		else
			num1[i]=num0[i]-'0';
	}
	ten=trans(len0,num1,ori);
	if(ten==0)
		printf("0\n");
	else
	{
	for(i=0;ten;i++)
	{
		num2[i]=ten%fin;
		ten=ten/fin;
	}
	len3=i;
	for(j=0;j<=len3;j++,i--)
	{
		if(num2[i-1]<=9)
			num3[j]=num2[i-1]+'0';
		else
			num3[j]=num2[i-1]+'A'-10;
	}
	num3[len3]='\0';
	printf("%s\n",num3);
	}
}
long trans(int len,int a[],int b)
{
	long x;
	if(len==1)
		return(a[len-1]);
	else
		x=a[len-1]+b*trans(len-1,a,b);
	return(x);
}


int turn(char num)
{
	if(num>='0'&&num<='9') return num-48;
	if(num>='a'&&num<='z') return num-87;
	if(num>='A'&&num<='Z') return num-55;
}

char turn(int num)
{
	if(num>=0&&num<=9) return num+48;
	if(num>=10&&num<=36) return num+55;
}

int main()
{
	int a,b,i=-1,j=0,s;
	long temp=0,temp1=0;
	char num[8*sizeof(long)]={'\0'};
	int num1[8*sizeof(long)]={0};
	cin>>a;cin.get();
	do
	{
		i++;
		num[i]=cin.get();
	}while(num[i]!=' ');
	cin>>b;
	for(;j<i;j++)
		temp+=turn(num[j])*pow((double)a,i-j-1);
	temp1=temp;
	for(i=0;temp1>0;i++)
	{
		num1[i]=temp1%b;
		temp1=temp1/b;
	}
	for(j=i-1;j>0;j--)
		cout<<turn(num1[j]);
cout<<turn(num1[0]);

	return 0;
}


long decimal(char c[],long x)
{
	long d=0;
	for(int i=0;c[i]!='\0';i++)
	{
		if(c[i]>='0'&&c[i]<='9')
			d=d*x+c[i]-'0';
		if(c[i]>='A'&&c[i]<='Z')
			d=d*x+c[i]-'A'+10;
		if(c[i]>='a'&&c[i]<='z')
			d=d*x+c[i]-'a'+10;
	}
	return d;
}

int main()
{
	long a,b;
	int length;
	char n[65] = {0};
	cin>>a>>n>>b;
	long x = decimal(n,a);
	for(int i=0;x>=0;i++)
	{
		int remainder=x%b;
		n[i]=remainder<10?'0'+remainder:'A'+remainder-10;
		x=(x-remainder)/b;
		length=i;
		if(x==0)
			break;
	}
	for(int i=length;i>=0;i--)
		cout<<n[i];
	cout<<endl;
	return 0;
}


int main()
{
    int a,b;
    char num1[32];
    int num2[32]={0};
    long num;
    int i,lenth;
    scanf("%d %s %d",&a,num1,&b);
    for(lenth=0;num1[lenth]!='\0';lenth++)
        {
         if(num1[lenth]<='9')num1[lenth]-='0';
         else num1[lenth]-='A'-10;
         if(num1[lenth]>=35)num1[lenth]-=32;
         }
    for(i=1,num=num1[0];i<lenth;i++)
       num=num*a+num1[i];
    for(i=31;num>=b;i--)
         {
          num2[i]=num%b+1;
          num/=b;
         }
    num2[i]=num+1;
    for(i=0;i<32;i++)
       {
         if(num2[i]==0)continue;
         else if(num2[i]<10)printf("%d",num2[i]-1);
         else printf("%c",num2[i]+'A'-11);
       }
    return 1;
}


int a,b,len=0;
char str[101];
int num[101],res[101];
int pow(int x,int y);
int main()
{
     int i,j=0;
        int con=0;
          cin>>a>>str>>b;
             while(str[len])
             len++;
              for(i=0;i<len;++i)
               {
                            if(islower(str[i]))
                               {
                                                    num[i]=(int)str[i]-87;
                                                        }
                                                           if(isupper(str[i]))
                                                                    {
                                                                                num[i]=(int)str[i]-55;
                                                                                }
       if(isdigit(str[i]))
          {
                               num[i]=(int)str[i]-48;
                                        }
                                          }
                                           for(i=0;i<len;++i)
                                             {
                                                          con+=num[i]*pow(a,(len-i-1));
                                                             }    while(con>0)
                                                             {
                                                                         res[j]=con%b;
                                                                                con-=con%b;
                                                                                con/=b;
                                                                                j++;
                                                                                }
                                                                                memset(str,0,sizeof(str));
                                                                                for(i=0;i<j;++i)    {
                                                                                if(res[i]<10) str[j-i-1]=(char)res[i]+48;
                                                                                if(res[i]>=10) str[j-i-1]=(char)res[i]+55;
                                                                                }
                                                                                if(str[0]==0) str[0]='0';
                                                                                cout<<str<<endl;
                                                                                return 0;}
                                                                                int pow(int x,int y)
                                                                                {    int i;
                                                                                int res=1;
                                                                                for(i=0;i<y;++i) res*=x;
                                                                                return res;
                                                                                }


int zheng (char x);
char fan(int x);

int main()
{
	int n,m,f,sum=0,wei=0,i;
	char zhuan[50];
	scanf("%d",&n);
	char x[10];
	scanf("%s",x);
	scanf("%d",&m);
	f=strlen(x);
    for(i=0;i<f;i++)
	{
			sum+=zheng(x[i])*pow(n,f-i-1);
	}
	for(i=49;i>=0;i--)
	{
		zhuan[i]=fan(sum%m);
		wei++;
		sum=sum/m;
		if(sum==0)
			break;
	}
	for(i=50-wei;i<50;i++)
	{
		printf("%c",zhuan[i]);
	}
	return 0;
}

    int zheng(char x)
	{
		int m;
		if(x>='a'&&x<='z')
			m=x-87;
		else if(x>='A'&&x<='Z')
			m=x-55;
		else
			m=x-48;
		return m;
	}

	char fan(int x)
	{
		char m;
		if(x>=0&&x<=9)
		    m=x+48;
		else if(x>=10&&x<=35)
			m=x+55;
		return m;
	}


/**
* file homwork.cpp
* author ???
* data 2011-11-14
* description ????
*/

int length , i ;
char n[1000];         //??????
unsigned turn(int n1 , char a[]);
void f(int n2 , unsigned s , int j);

int main()
{
	int a , b ;
	unsigned number;
	char str[1000] ;
	while(cin >> a)
	{
		cin >> str >> b ;
		length = strlen (str) ;
		number = turn( a , str ) ;
		f(b , number , 0) ;
		memset(str , 0 ,sizeof(str));  //???0?
	}

	return 0;
}

unsigned turn(int n1 , char a[])    //???????10???
{
    int sum = 0;
	for( i = 0 ; i <= length - 1 ; i++)
	{
		if(a[i] >= '0' && a[i] <= '9')
			sum = sum * n1 +  (a[i] - '0');

		else if(a[i] >= 'A' && a[i] <= 'Z')
			sum = sum * n1 + (a[i] - 'A' + 10);

		else if(a[i] >= 'a' && a[i] <= 'z')
			sum = sum * n1 + (a[i] - 'a' + 10);
	}
	return sum;
}

void f(int n2 , unsigned s , int j)    //?????????????
{
	int  k;
	if(s == 0)
	{
		if(j == 0)
			cout << 0;
		for(i = j - 1 ; i >= 0 ; i --)
			printf("%c",n[i]);
	}
	else
	{
		k  = s  %  n2 ;
		s = s / n2 ;

		if(k  <=  9 && k >= 0)
			n[j] = k  + '0' ;
		else
			n[j] = (k - 10) + 'A';

        f(n2 , s , j + 1);                    //?????
	}

	return;
}


long pow(int a,int b)
{
	long c;
	if(b==0)
		c=1;
	if(b>0)
		c=pow(a,b-1)*a;
	return c;
}

int main()
{
	int a,b,i,j;
	char form[1000],late[1000];
	long ten=0;
	cin>>a;
	cin>>form;
	cin>>b;
	for(i=0;i<strlen(form);i++)
		if(form[i]>=97&&form[i]<=122)
			form[i]-=32;
	for(i=strlen(form)-1,j=0;i>=0;i--,j++)
	{
		if(form[i]>=48&&form[i]<=57)
		{
			form[i]-=48;
			ten+=form[i]*pow(a,j);
		}
		if(form[i]>=65&&form[i]<=90)
		{
			form[i]-=55;
			ten+=form[i]*pow(a,j);
		}
	}
	if(ten==0)
	{
		cout<<ten<<endl;
		return 0;
	}
	for(i=0;i<=1000;i++)
	{
		if(ten%b<10)
			late[i]=ten%b+48;
		if(ten%b>=10)
			late[i]=ten%b+55;
		if(ten/b==0&&ten%b==0)
			late[i]='\0';
		ten=ten/b;
	}
	for(i=strlen(late)-1;i>=0;i--)
	{
		if(i!=0)
			cout<<late[i];
		else cout<<late[i]<<endl;
	}
	return 0;
}


int main()
{
long num=0,reminder;
int a,b,i,j;
char figure1[100],figure2[100];
cin>>a;
getchar();
for(i=0;;i++)
{
	figure1[i]=getchar();
	if(figure1[i]>='a'&&figure1[i]<='z')
		figure1[i]=figure1[i]-87;
	else if(figure1[i]>='A'&&figure1[i]<='Z')
		figure1[i]=figure1[i]-55;
	else if(figure1[i]>=42&&figure1[i]<=57)
		figure1[i]=figure1[i]-'0';
	else
		break;
	num=num*a+figure1[i];
}
reminder=num;
cin>>b;
for(j=0;;j++)
{
	if(reminder%b>=10)
		figure2[j]=reminder%b+55;
	else
		figure2[j]=reminder%b+'0';
	reminder=reminder/b;
	if(reminder==0)
		break;
}
for(;j>=0;j--)
	cout<<figure2[j];
cout<<endl;
return 0;
}


//????a?
int nta(long a)
{
	if(a>=10)
	{
	char b;
	b=(char)('A'-10+a);
	return ((char) (b));
	}
	else
	{
	char b;
	b=(char)('1'-1+a);
	return ((char) (b));
	}
}
//a????
int atn(char i)
{
	long j=i;
	if(j>96)
	{
		j=j-'a'+10;
	}
	else
	{   if ((j>33)&&(j<58))
	{
		j=j-'1'+1;
	}
	else
	{
		j=j-'A'+10;
	}
	}
	return((long)(j));
}
//???????
int ten(long k)
{
	long l,m;
	l=k;
	for (int n = 0; n < 69; n++)
	{
		l=l/10;
		if (l>0)
		{
			m=n+1;
		}
	}
	return (m);
}
//??e??
int loe(long t,long v)
{
	long u,w=0;
	u=t;
	for (int x = 0; x < 69; x++)
	{
		u=u/v;
		if (u>0)
		{
			w=x+1;
		}
	}
	return (w);
}
//??????
int lth(char o[])
{
	long f=0;
	for (int g = 0; g <69 ; g++)
	{
		if((o[g]!=0)&&(o[g]!=32))f=f+1;
	}
	return ((f));
}
int main()
{
	long d,e;
	char h[70];
	memset(h,0,sizeof(h));
	cin>>d>>h>>e;
	long p;
	p=lth(h);
	long s=0;
	for (int q = 0; q < p; q++)
	{
		s=s*d+(atn(h[q]));
	}
	long y;
	y=(loe(s,e));
	char aa[70];
	memset(aa,0,sizeof(aa));
	long ab=s,ac=0;
	for (int z = 0; z < y+1; z++)
	{
		ac=ab;
		ab=ab/e;
		if (ab>0)
		{
			aa[z]=nta(ac%e);
		}
		else
		{
			aa[z]=nta(ac);
		}

	}
	if(aa[y]!=0)
	{
	for (int ad = 0; ad < y+1; ad++)
	{
		cout<<aa[y-ad];
	}
	}
	else cout<<"0";
	return 0;
}


int main() {
	int a, b, ten = 0;
	char str[100], str2[100];
	cin >> a >> str >> b;
	int len = strlen( str );
	for ( int i = 0; str[i] != '\0'; i ++ ) {
		if ( str[i] >= '0' && str[i] <= '9' ) ten += ( str[i] - '0' ) * pow ( ( double ) a, ( double ) ( len - 1 - i ) ) ;
		else if ( str[i] <= 'z' && str[i] >= 'a' ) ten += ( str[i] - 'a' + 10 ) * pow ( ( double ) a, ( double ) ( len - 1 - i ) );
		else ten += ( str[i] - 'A' + 10 ) * pow ( ( double ) a, ( double ) ( len - 1 - i ) );
	}
	int j = 0;
	if ( ten == 0 ) cout << 0;
	else {
		while ( ten != 0 ) {
			if ( ten % b <= 9 ) str2[j ++] = ( char )( ten % b + '0' );
			else str2[j ++] = ( char )( ten % b - 10 + 'A' );
			ten /= b;
		}
		j --;
		for ( int i = j; i >= 0; i -- ) {
			cout << str2[i];
		}
	}
	return 0;
}


int convert1(char a)
{
	if ((a >= '0') && (a <= '9'))
		return a - '0';
	if ((a >= 'a') && (a <= 'z'))
		return (a - 'a' + 10);
	if ((a >= 'A') && (a <= 'Z'))
		return (a - 'A' + 10);
}
char convert2(int a)
{
	if ((a >= 0) && (a <= 9))
		return (a + 48);
	else
		return (a + 55);
}
int main()
{
	int a , b , len , k = 1 , i , j;
	long t = 0;
	char num[100] , p;
	int num1[100];
	cin >> a >> num >> b;
	len = strlen(num);
	for (i = len - 1 ; i >= 0 ; i--)
	{
		t = t + k * convert1(num[i]);
		k = k * a;
	}
	i = 1;
	while(t >= b)
	{
		num1[i] = t % b;
		i++;
		t = t / b;
	}
	num1[i] = t;
	for (j = i ; j >= 1; j--)
	{
		p = convert2(num1[j]);
		cout << p;
	}
	return 0;
}


int f(char c)
{
	int w;
	if(c>='0' && c<='9')
	{
		w=c-'0';
		return w;
	}
	if(c>='a' && c<='z')
	{
		w=c-'a'+10;
		return w;
	}
	if(c>='A' && c<='Z')
	{
		w=c-'A'+10;
		return w;
	}
}

char g(int a)
{
	char c;
	if(a>=0 && a<9)
	{
		c=a+'0';
		return c;
	}
	if(a>=10)
	{
		c=a-10+'A';
		return c;
	}

}

int main()
{
	int a,b,i,j,l,x;
	long sum=0,y;
	char n[10];
	scanf("%d %s %d",&a,n,&b);
	if(a==b)
		printf("%s\n",n);
	else
	{
	l=strlen(n);
	for(i=0;i<l;i++)
	{
		x=f(n[i]);
		y=(long)pow(a,l-i-1);
		sum=sum+x*y;
	}
	if(sum==0)
		printf("0");
	else
	{
	char m[50];
	long z;
	l=(int)(log(sum)/log(b));
	for(i=0;i<=l;i++)
	{
		if(i==l)
		{
			z=sum;
			m[i]=g(z);
		}
		else
		{
			for(j=1;j<=b;j++)
			{
				z=j*pow(b,l-i);
				if(z>sum)
				{
					z=(j-1)*pow(b,l-i);
					sum=sum-z;
					m[i]=g(j-1);
					break;
				}
			}
		}
	}

	for(i=0;i<=l;i++)
		printf("%c",m[i]);
	printf("\n");
	}
	}

	return 0;

}


long int num(long int x,int i,int b)
{
	long int a;
	a=x;
	int j;
	for (j=0;j<i;j++)
		a=a*b;
	return a;
}
void main ()
{
	int a,b,i,j,length;
	char shu[100],zhuanhuan[100];
	scanf("%d %s %d",&a,shu,&b);
	long int k=0;
	length=strlen(shu);
	long int l;
	for (i=0;i<length-1;i++)
	{
		if( ((shu[i]>='A')&&(shu[i]<='Z')))
		{
			l=shu[i]-'A'+10;
			k=k+num(l,length-i-1,a);
		}
		if(((shu[i]>='0')&&(shu[i]<='9')))
		{
			l=shu[i]-'0';
			k=k+num(l,length-i-1,a);
		}
		if( ((shu[i]>='a')&&(shu[i]<='z')))
		{
			l=shu[i]-'a'+10;
			k=k+num(l,length-i-1,a);
		}
	}
	if ((shu[length-1]>='A')&&(shu[length-1]<='Z'))
		k=k+shu[length-1]-'A'+10;
	if(((shu[length-1]>='0')&&(shu[length-1]<='9')))
		k=k+shu[length-1]-'0';
	if ((shu[length-1]>='a')&&(shu[length-1]<='z'))
			k=k+shu[length-1]-'a'+10;
	int m=0;
	int n;
	long int f=k;
	do
	{
		n=f%b;
		if ((n>=0)&&(n<=9))
		{
			zhuanhuan[m]=n+'0';
			m++;
		}
		else
		{
			zhuanhuan[m]=n-10+'A';
			m++;
		}
		f=f/b;
	}while(f!=0);
	for (i=m-1;i>=0;i--)
		printf("%c",zhuanhuan[i]);
	printf("\n");
}


int calc (char c)
{
if (c>='0'&&c<='9') return c-'0';
else if (c>='a'&&c<='z') return c-'a'+10;
else if (c>='A'&&c<='Z') return c-'A'+10;
}
char conv(int a)
{
if (a<10) return a+'0';
else return a-10+'A';
}
main()
{
long a,b,n,i,j,k,ans;
char c[100];
scanf("%d",&a);
scanf("%s",c);
scanf("%d",&b);
n=strlen(c);
n--;
ans=calc(c[n]);
j=1;
for (i=n-1;i>=0;i--)
{
j*=a;
ans+=calc(c[i])*j;
}
i=0;
while (ans>0)
{
i++;
c[i]=conv(ans%b);
ans/=b;
}
if (i==0) printf("0");
while (i>=1)
{
printf("%c",c[i]);
i--;
}
}


int main()
{
	long int num10=0;//??????10?????b?????????
	int a,b,i,j;//a,b??,i,j???????????
	char character1[100],character2[100];//???????????
	cin>>a;
	cin.get();
	for(i=0;;i++)
	{
		character1[i]=cin.get();
		if(character1[i]>='a'&&character1[i]<='z')
			character1[i]=character1[i]-87;
		else if(character1[i]>='A'&&character1[i]<='Z')
			character1[i]=character1[i]-55;
		else if(character1[i]>='0'&&character1[i]<='9')
			character1[i]=character1[i]-48;
		else break;
		num10=num10*a+character1[i];
	}
	cin>>b;
	for(j=0;;j++)
	{
		if(num10%b<=9)
			character2[j]=num10%b+48;
		if(num10%b>9)
			character2[j]=num10%b+55;
		num10=num10/b;
		if(num10==0)
			break;
	}
	for(;j>=0;j--)
		cout<<character2[j];
	cout<<endl;
	cin.get();cin.get();cin.get();
    return 0;
}


int f(char x)
{
    int xx;
    xx=x-48;
    if(xx>9)xx=x-'A'+10;
    if(xx>36)xx=x-'a'+10;
    return xx;
}
char g(int y)
{
     char yy;
     if(y<10)yy=y+'0';
     if(y>=10)yy=y-10+'A';
     return yy;
}
int main()
{
    long nn=0;
    int a,b,i;
    char n[500];
    scanf("%d %s %d",&a,n,&b);
    int length=strlen(n);
    int k=1;
    for(i=length-1;i>=0;i--)
    {
            nn+=f(n[i])*k;
            k*=a;
    }
    int m[500];
    int j=0;
    do
    {
            m[j]=nn%b;
            nn=nn/b;
            j++;
    }
    while(nn>=1);
    for(i=j-1;i>=0;i--)printf("%c",g(m[i]));

}


int change(char a)
{
    int b;
    switch(a)
    {
     case'0':b=0;break;
     case'1':b=1;break;
     case'2':b=2;break;
     case'3':b=3;break;
     case'4':b=4;break;
     case'5':b=5;break;
     case'6':b=6;break;
     case'7':b=7;break;
     case'8':b=8;break;
     case'9':b=9;break;
     case'a':b=10;break;
     case'b':b=11;break;
     case'c':b=12;break;
     case'd':b=13;break;
     case'e':b=14;break;
     case'f':b=15;break;
     case'g':b=16;break;
     case'h':b=17;break;
     case'i':b=18;break;
     case'j':b=19;break;
     case'k':b=20;break;
     case'l':b=21;break;
     case'm':b=22;break;
     case'n':b=23;break;
     case'o':b=24;break;
     case'p':b=25;break;
     case'q':b=26;break;
     case'r':b=27;break;
     case's':b=28;break;
     case't':b=29;break;
     case'u':b=30;break;
     case'v':b=31;break;
     case'w':b=32;break;
     case'x':b=33;break;
     case'y':b=34;break;
     case'z':b=35;break;
     case'A':b=10;break;
     case'B':b=11;break;
     case'C':b=12;break;
     case'D':b=13;break;
     case'E':b=14;break;
     case'F':b=15;break;
     case'G':b=16;break;
     case'H':b=17;break;
     case'I':b=18;break;
     case'J':b=19;break;
     case'K':b=20;break;
     case'L':b=21;break;
     case'M':b=22;break;
     case'N':b=23;break;
     case'O':b=24;break;
     case'P':b=25;break;
     case'Q':b=26;break;
     case'R':b=27;break;
     case'S':b=28;break;
     case'T':b=29;break;
     case'U':b=30;break;
     case'V':b=31;break;
     case'W':b=32;break;
     case'X':b=33;break;
     case'Y':b=34;break;
     case'Z':b=35;break;
     }
     return(b);
    }
char rechange(int a)
{ char b;
 switch(a)
 {
     case 0:b='0';break;
     case 1:b='1';break;
     case 2:b='2';break;
     case 3:b='3';break;
     case 4:b='4';break;
     case 5:b='5';break;
     case 6:b='6';break;
     case 7:b='7';break;
     case 8:b='8';break;
     case 9:b='9';break;
     case 10:b='A';break;
     case 11:b='B';break;
     case 12:b='C';break;
     case 13:b='D';break;
     case 14:b='E';break;
     case 15:b='F';break;
     case 16:b='G';break;
     case 17:b='H';break;
     case 18:b='I';break;
     case 19:b='J';break;
     case 20:b='K';break;
     case 21:b='L';break;
     case 22:b='M';break;
     case 23:b='N';break;
     case 24:b='O';break;
     case 25:b='P';break;
     case 26:b='Q';break;
     case 27:b='R';break;
     case 28:b='S';break;
     case 29:b='T';break;
     case 30:b='U';break;
     case 31:b='V';break;
     case 32:b='W';break;
     case 33:b='X';break;
     case 34:b='Y';break;
     case 35:b='Z';break;
 }
 return(b);
}
  int main()
{
      int a,b;
      char n[10000];
      scanf("%d%s%d",&a,n,&b);
      int x,y;
      int i,j,k;
      x=strlen(n);
      y=change(n[x-1]);
      for(i=0;i<x-1;i++)
      { k=change(n[i]);
        for(j=0;j<x-i-1;j++)
        {k=k*a;}
        y=y+k;
      }
      int z;
      char q[10000]={'\0'};
      char *p=q;
      if(y>=b)
      {
      while(y>=b)
      {
       z=y%b;
       *p=rechange(z);
       y=y/b;
       if(y>0&&y<b)
      {*(p+1)=rechange(y);}
       p++;
       }
      }
      else if(y<b)
      {*p=rechange(y);}
       for(i=strlen(q)-1;i>=0;i--)
      {printf("%c",q[i]);}
  return 0;
}


int main()
{
	unsigned long shuz1,shuz2,total=0;
	int i=0,j=0,k=0,length;
	char n[33],result[33];
	cin>>shuz1>>n>>shuz2;
	for(i=0;i<33;i++)
	{
		if(n[i]=='\0')
		{
			 length=i;
		break;
		}
	}
	for(i=0;i<length;i++)
	{
		if(n[i]>60&&n[i]<93)
			total+=(n[i]-55)*pow((double)shuz1,length-i-1);
		else if(n[i]>93)
			total+=(n[i]-87)*pow((double)shuz1,length-i-1);
		else
			total+=(n[i]-48)*pow((double)shuz1,length-i-1);
	}
        if (total == 0) {
            cout << 0 << endl;
            return 0;
        }
	for(j=0;total>0;j++)
	{
		result[j]=total%shuz2;
		total=(total-result[j])/shuz2;
	}
	for(k=0;k<33;k++)
	{
	if(result[k]<10)
		result[k]=result[k]+48;
	else
		result[k]=result[k]+55;
	}
	for(k=j-1;k>=0;k--)
		cout<<result[k];
        cout << endl;
	return 0;
}


int ctruth(char);
char itruth(int);
int main()
{
    int a,b,i,j,k,S=0,len;

	char m[32],n[32];

	   cin>>a>>m>>b;
        len=strlen(m);
	for(i=0;m[i]!='\0';i++)
		if(m[i]<=90&&m[i]>=65)m[i]+=32;
	for(i=0;i<=len-1;i++)
		S+=ctruth(m[len-i-1])*pow(a,i);

	if(S<b)cout<<itruth(S);
	else{
	    k=S;
		for(i=0;k>=b;i++)
	{
		n[i]=itruth(k%b);
		k=k/b;
		}n[i]=itruth(k);
	  for(j=i;j>=0;j--)
		  cout<<n[j];}
      return 0;
}
 int ctruth(char a)
 {
	 int t;
	 if(a>=48&&a<=57)t=a-48;
	 if(a>=97&&a<=122)t=a-87;
	 return t;
 }
 char itruth(int a)
 {
	 char c;
	 if(a>=0&&a<=9)c=a+48;
	 if(a>=10&&a<=35)c=a+55;
	 return c;
 }


char Change(int x);
int change(char x);
int main()
{
	int a,b,i,length,Length;
	char num[30],NUM[100];
	long number=0;
	cin>>a>>num>>b;
	for(i=0;i<30;i++)
	{
		if(num[i]!='\0') ;
		else break;
	}
	length=i;
	for(i=0;i<length;i++)
	{
          number+=change(num[length-i-1])*pow((double)a,(double)i);
	}
	for(i=0;i<100&&number>=b;i++)
	{
		NUM[i]=Change(number-(number/b)*b);
		number=number/b;
	}
	Length=i+1;
	NUM[i]=Change(number);
	for(i=0;i<Length;i++)
	{
		cout<<NUM[Length-1-i];
	}
    return 0;
}
 int change(char x)
 {
	if(x>=48&&x<=57) return(x-48);
	if(x>=65&&x<=90) return(x-55);
    if(x>=97&&x<=122) return(x-87);
 }
 char Change(int x)
 {
	 if(x<=9) return(x+48);
	 if(x>=10) return(x+55);
 }


int main(int argc, char* argv[])
{
	int a,b;
	int i,j;
	int c=0;//c????n???
	char n[20];
	int num=0;//10????????

    scanf("%d%s%d",&a,n,&b);

	int k;//k??????(??a????????)

	//????n???
	for(i=0;n[i]!='\0';i++)
	{c++;}

	//???n???10???
	for(i=0;n[i]!='\0';i++)
	{
		k=1;
		for(j=c-i-1;j>0;j--)
		{
	        k*=a;
		}

		switch (n[i])
		{
		case 'a': num+=10*k;break;
		case 'A': num+=10*k;break;
		case 'b': num+=11*k;break;
		case 'B': num+=11*k;break;
		case 'c': num+=k*12;break;
		case 'C': num+=k*12;break;
		case 'd': num+=k*13;break;
		case 'D': num+=k*13;break;
		case 'e': num+=k*14;break;
		case 'E': num+=k*14;break;
		case 'f': num+=k*15;break;
		case 'F': num+=k*15;break;
		case 'g': num+=k*16;break;
		case 'G': num+=k*16;break;
		case 'h': num+=k*17;break;
		case 'H': num+=k* 17;break;
		case 'i': num+=k* 18;break;
		case 'I': num+=k* 18;break;
		case 'j': num+=k* 19;break;
		case 'J': num+=k* 19;break;
		case 'k': num+=k* 20;break;
		case 'K': num+=k* 20;break;
		case 'l': num+=k* 21;break;
		case 'L': num+=k* 21;break;
		case 'm': num+=k* 22;break;
		case 'M': num+=k* 22;break;
		case 'n': num+=k* 23;break;
		case 'N':num+=k* 23;break;
		case 'o':num+=k* 24;break;
		case 'O':num+=k* 24;break;
		case 'p':num+=k* 25;break;
		case 'P':num+=k* 25;break;
		case 'q':num+=k* 26;break;
		case 'Q':num+=k* 26;break;
		case 'r':num+=k* 27;break;
		case 'R':num+=k* 27;break;
		case 's':num+=k* 28;break;
		case 'S':num+=k* 28;break;
		case 't':num+=k* 29;break;
		case 'T':num+=k* 29;break;
		case 'u':num+=k* 30;break;
		case 'U':num+=k* 30;break;
		case 'v':num+=k* 31;break;
		case 'V':num+=k* 31;break;
		case 'w':num+=k* 32;break;
		case 'W':num+=k* 32;break;
		case 'x':num+=k* 33;break;
		case 'X':num+=k* 33;break;
		case 'y':num+=k* 34;break;
		case 'Y':num+=k* 34;break;
		case 'z':num+=k* 35;break;
		case 'Z':num+=k* 35;break;
		case '0':num+=k* 0;break;
		case '1':num+=k* 1;break;
        case '2':num+=k* 2;break;
        case '3':num+=k* 3;break;
        case '4':num+=k* 4;break;
        case '5':num+=k* 5;break;
        case '6':num+=k* 6;break;
        case '7':num+=k* 7;break;
        case '8':num+=k* 8;break;
        case '9':num+=k* 9;break;

		}
	}

	int p[20];
	int q,count=0;
	for(i=0;(num/b)>0;i++)
	{
		q=num%b;
		p[i]=q;
		count++;
		num=num/b;
	}
		switch (num)
		{
		case 10:printf("A");break;
		case 11:printf("B");break;
		case 12:printf("C");break;
		case 13:printf("D");break;
		case 14:printf("E");break;
		case 15:printf("F");break;
		case 16:printf("G");break;
		case 17:printf("H");break;
		case 18:printf("I");break;
		case 19:printf("J");break;
		case 20:printf("K");break;
		case 21:printf("L");break;
		case 22:printf("M");break;
		case 23:printf("N");break;
		case 24:printf("O");break;
		case 25:printf("P");break;
		case 26:printf("Q");break;
		case 27:printf("R");break;
		case 28:printf("S");break;
		case 29:printf("T");break;
		case 30:printf("U");break;
		case 31:printf("V");break;
		case 32:printf("W");break;
		case 33:printf("X");break;
		case 34:printf("Y");break;
		case 35:printf("Z");break;
		default:printf("%d",num);break;
		}

    for(i=count-1;i>=0;i--)
	{
		switch (p[i])
		{
		case 10:printf("A");break;
		case 11:printf("B");break;
		case 12:printf("C");break;
		case 13:printf("D");break;
		case 14:printf("E");break;
		case 15:printf("F");break;
		case 16:printf("G");break;
		case 17:printf("H");break;
		case 18:printf("I");break;
		case 19:printf("J");break;
		case 20:printf("K");break;
		case 21:printf("L");break;
		case 22:printf("M");break;
		case 23:printf("N");break;
		case 24:printf("O");break;
		case 25:printf("P");break;
		case 26:printf("Q");break;
		case 27:printf("R");break;
		case 28:printf("S");break;
		case 29:printf("T");break;
		case 30:printf("U");break;
		case 31:printf("V");break;
		case 32:printf("W");break;
		case 33:printf("X");break;
		case 34:printf("Y");break;
		case 35:printf("Z");break;
		default:printf("%d",p[i]);break;
		}
	}

	return 0;
}


void main()
{
      long a,b,i,j,length,result=0;
      char c[100];
      long final[100]={0};
      scanf("%d %s %d",&a,c,&b);
      int decimal[100]={0};
      length=strlen(c);
      for(i=0;i<length;i++)
      {
                 if(c[i]>='A'&&c[i]<='Z')
                 {
                      decimal[i]=c[i]-'A'+10;
                  }
                 else if(c[i]>='a'&&c[i]<='z')
                 {
                      decimal[i]=c[i]-'a'+10;
                  }
                else if(c[i]>='0'&&c[i]<='9')
                 {
                      decimal[i]=c[i]-'0';
                 }
          }
      for(i=0;i<length;i++)
      {
            result=result+decimal[length-i-1]*(int)(pow((double)a,i));
      }
      if(result==0)
      {
          printf("0");
      }
      for(i=0;result!=0;i++)
      {
           final[i]=result%b;
           result=result/b;
       }
      for(j=i-1;j>=0;j--)
      {
            if(final[j]<10)
            {
                  printf("%d",final[j]);
            }
            else
            {
                 printf("%c",(char)(final[j]+'A'-10));
            }
      }
     printf("\n");

}


int main ()
{
	char string1[1000];
	int b,i;
	long c = 0;;
	int a;
	cin>>a>>string1;
	b=strlen(string1);
	for(i=0;i<b;i++)
	{
		if(string1[i]>='0'&&string1[i]<='9')
			c+=(string1[i]-'0')*pow(a,b-i-1);
		if(string1[i]>='a'&&string1[i]<='z')
			c+=(string1[i]-'a' + 10)*pow(a,b-i-1);
		if(string1[i]>='A'&&string1[i]<='Z')
			c+=(string1[i]-'A'+10)*pow(a,b-i-1);
		//cout << c<<endl;
	}
	//cout << c<<endl;
	int index = 0;
	char string2[1000];
	int d;
	cin >> d;
	int e;
	if(c == 0)
		cout << '0' << endl;
	while(c != 0)
	{
		e = c % d;
		c = c / d;
		if(e >= 10)
			string2[index] = 'A' + e - 10;
		else
			string2[index] = '0' + e;
		index ++;
	}
	string2[index] = '\0';
	for( i = index - 1;i>=0;i--)
		cout<<string2[i];
	cout<<endl;

	//cout<<string2<<endl;
	//while(1);
	return 0;
}


int turntonum (char c)
{
    if (c>='0'&&c<='9')
        return (c-'0');
    if (c>='a'&&c<='z')
        return (c-'a'+10);
    if (c>='A'&&c<='Z')
        return (c-'A'+10);
}
int main ()
{
    int a,b,l;
    char c[100];
    long long num;
    cin >>a;getchar();
    cin.getline(c,80,' ');
    cin >>b;
    l=strlen(c);
    num=turntonum(c[0]);
    for (int i=1;i<l;i++)
    {
        int f;
        num=num*a+turntonum(c[i]);
    }
    if (num==0)
    {
        cout <<"0";
        return 0;
    }
    int d[100],l_;
    l_=log(num)/log(b)+1;
    for (int i=l_-1;i>=0;i--)
    {
        d[i]=num%b;
        num=num/b;
    }
    for (int i=0;i<l_;i++)
    {
        if (d[i]<10)
            cout <<d[i];
        else
            putchar('A'+d[i]-10);
    }
    return 0;
}


unsigned long pow(unsigned long a,unsigned long b)
{
    unsigned long temp,sout=1;
    for(temp=1;temp<=b;temp++)
    sout=sout*a;
    return sout;
}

main()
{
      unsigned long ip,op,i,lenth,oplenth,cache,test=9999,mod;
      unsigned long dec=0;
      char num[100],wei;
      scanf("%d",&ip);
      scanf("%s",num);
      scanf("%d",&op);
      //printf("%d %s %d\n",ip,num,op);
      lenth=strlen(num);
      for(i=0;i<=lenth;i++)
      {
                          if(num[i]>='a'&&num[i]<='z')
                          num[i]=num[i]-'a'+'A';
      }
      //printf("%s\n",num);
      for(i=0;i<lenth;i++)
      {
                          if(num[i]>='0'&&num[i]<='9')
                          dec=dec+((unsigned long)num[i]-(unsigned long)'0')*pow(ip,lenth-i-1);
                          else
                          dec=dec+(10+(unsigned long)num[i]-(unsigned long)'A')*pow(ip,lenth-i-1);
      }
      for(oplenth=1;test>=op;oplenth++)
      test=dec/pow(op,oplenth-1);
      oplenth--;
      mod=dec;
      //printf("%d\n",dec);
      for(i=0;i<oplenth;i++)
      {
                             cache=mod/pow(op,oplenth-i-1);
                             mod=mod-cache*pow(op,oplenth-i-1);
                             //printf("     %d %d\n",cache,mod);
                             if(cache<=9)
                             printf("%d",cache);
                             else
                             {
                                 wei='A'+cache-10;
                                 printf("%c",wei);
                             }
      }
      getchar();
      getchar();
      return 0;
}


int change(char m);
char trans(int n);
int main()
{
	char f[10000];
	int g[10000];
	int a,b,i,len,s=0,t=0;
	scanf("%d %s %d",&a,f,&b);
	len=strlen(f);
	for(i=0;i<len;i++)
	{
		s=change(f[i])+s*a;
	}
	while(s/b)
	{
		g[t]=s%b;
		s/=b;
		t++;
	}
	g[t]=s%b;
	for(i=t;i>=0;i--)
	{
		printf("%c",trans(g[i]));
	}
	return 0;
}
int change(char m)
{
		int result;
		if((m-'0'>=0)&&(m-'0'<10))
		{
			result=m-'0';
		}
		else if((m-'a'>=0)&&(m-'a'<26))
		{
			result=m-'a'+10;
		}
		else if((m-'A'>=0)&&(m-'A'<26))
		{
			result=m-'A'+10;
		}
		return result;
}
char trans(int n)
{
	char last;
	if(n>=0||n<10)
	{
		last='0'+n;
	}
	if(n>9)
	{
		last='A'+n-10;
	}
	return last;
}


int main()
{
	int a,b;
	long  ans = 0;
	char x[20] = {0};
	scanf("%d %s %d",&a,x,&b);
	if(a == b) printf("%s\n",x);
	//printf("%d %s %d\n",a,x,b);
	else{
		long flag = 1;
		for(int i = strlen(x) - 1;i >= 0;i --){
			long temp;
			if(x[i] >= '0' && x[i] <= '9') temp = x[i] - '0';
			if(x[i] >= 'A' && x[i] <= 'Z') temp = x[i] - 'A' + 10;
			if(x[i] >= 'a' && x[i] <= 'z') temp = x[i] - 'a' + 10;
			ans += temp * flag;
		//	printf("%lld\n",ans);
			flag *= a;
		}
		if(ans == 0) printf("0\n");
		else{
		//	printf("%lld\n",ans);
			long final[100] = {0};
			flag = 0;
			while(ans > 0){
				flag ++;
				final[flag] = ans % b;
				ans /= b;
			}
			for(int i = flag;i >= 1;i --){
				if(final[i] >= 10){
					printf("%c",final[i] - 10 + 'A');
				}
				else printf("%ld",final[i]);
			}
			printf("\n");
		}
	}
	return 0;
}


int f(int a,int n){
	if(n==0) return 1;
	else if(n==1) return a;
	else return(a*f(a,n-1));
}
	char n[100],m[100];
int main(){
	int a,b,num=0;
	cin>>a>>n>>b;
	for(int i = 0;i < (int)strlen(n);i ++){
		if(islower(n[i])) num+=((int)n[i]-87)*f(a,(int)strlen(n)-i-1);
		else if(isupper(n[i])) num+=((int)n[i]-55)*f(a,(int)strlen(n)-i-1);
		else if(isdigit(n[i])) num+=((int)n[i]-48)*f(a,(int)strlen(n)-i-1);
	}if (num == 0) {puts("0"); return 0;}
	int i=0;
	while(num>0){
		if(num%b>9) {
			m[i]=(char)(num%b+55);num=num/b;
		}
		else if(num%b==0) {
			m[i]='0';num=num/b;
		}
		else {
			m[i]=(char)(num%b+48);num=num/b;
		}
		i++;
	}
	for (int j = 1; j <= i; j++) cout <<m[i - j];
	return 0;
}


int main(){
	int a,b,i,zhen,chu,result;
	long x=0;
	char n[32],m[32];
	scanf("%d%s%d",&a,n,&b);
	result=strlen(n);

	for(i=0;i<result;i++){
		switch(n[i]){
		case '0':zhen=0;break;
		case '1':zhen=1;break;
		case '2':zhen=2;break;
        case '3':zhen=3;break;
		case '4':zhen=4;break;
		case '5':zhen=5;break;
		case '6':zhen=6;break;
		case '7':zhen=7;break;
		case '8':zhen=8;break;
		case '9':zhen=9;break;
		case 'a':
        case 'A':zhen=10;
		    break;
		case 'b':
		case 'B':zhen=11;
			break;
		case 'c':
		case 'C':zhen=12;
			break;
		case 'd':
		case 'D':zhen=13;
			break;
		case 'e':
		case 'E':zhen=14;
			break;
		case 'f':
        case 'F':zhen=15;
			break;
		case 'g':
		case 'G':zhen=16;
			break;
		case 'h':
		case 'H':zhen=17;
			break;
		case 'i':
		case 'I':zhen=18;
			break;
		case 'j':
		case 'J':zhen=19;
			break;
		case 'k':
		case 'K':zhen=20;
			break;
		case 'l':
		case 'L':zhen=21;
			break;
		case 'm':
		case 'M':zhen=22;
			break;
		case 'n':
		case 'N':zhen=23;
			break;
		case 'o':
		case 'O':zhen=24;
			break;
		case 'p':
		case 'P':zhen=25;
			break;
		case 'q':
		case 'Q':zhen=26;
			break;
		case 'r':
		case 'R':zhen=27;
			break;
		case 's':
		case 'S':zhen=28;
			break;
		case 't':
		case 'T':zhen=29;
			break;
		case 'u':
		case 'U':zhen=30;
			break;
		case 'v':
		case 'V':zhen=31;
			break;
		case 'w':
		case 'W':zhen=32;
			break;
		case 'x':
		case 'X':zhen=33;
			break;
		case 'y':
		case 'Y':zhen=34;
			break;
		case 'z':
		case 'Z':zhen=35;
			break;

		}
		x+=zhen*pow(a,result-1-i);
	}
	for(i=0;x>0;i++){
		chu=x%b;
		x=x/b;
		switch(chu){
        case 0:m[i]='0';break;
		case 1:m[i]='1';break;
        case 2:m[i]='2';break;
		case 3:m[i]='3';break;
        case 4:m[i]='4';break;
		case 5:m[i]='5';break;
		case 6:m[i]='6';break;
		case 7:m[i]='7';break;
		case 8:m[i]='8';break;
		case 9:m[i]='9';break;
		case 10:m[i]='A';break;
		case 11:m[i]='B';break;
		case 12:m[i]='C';break;
		case 13:m[i]='D';break;
		case 14:m[i]='E';break;
		case 15:m[i]='F';break;
		case 16:m[i]='G';break;
		case 17:m[i]='H';break;
		case 18:m[i]='I';break;
		case 19:m[i]='J';break;
		case 20:m[i]='K';break;
		case 21:m[i]='L';break;
		case 22:m[i]='M';break;
		case 23:m[i]='N';break;
		case 24:m[i]='O';break;
		case 25:m[i]='P';break;
		case 26:m[i]='Q';break;
		case 27:m[i]='R';break;
		case 28:m[i]='S';break;
		case 29:m[i]='T';break;
		case 30:m[i]='U';break;
		case 31:m[i]='V';break;
		case 32:m[i]='W';break;
		case 33:m[i]='X';break;
		case 34:m[i]='Y';break;
		case 35:m[i]='Z';break;
		}
	}
	i=i-1;
	for(;i>=0;i--){
		printf("%c",m[i]);
	}
	printf("\n");
	return 0;
}


int main()
{
    int a,b,s=0;
    int i,c,j,k,o;
    int number2[40];
    char number[40],number1[40],number3[40];
    scanf("%d",&a);
    scanf("%s",number);
    scanf("%d",&b);

     int q1,p1;
     p1=strlen(number);
     for(q1=0;number[q1]!='\0';q1++)
     number1[p1-q1-1]=number[q1];
     number1[p1]='\0';

    for(i=0;number1[i]!='\0';i++)
    {
       if(number1[i]>=97)
       c=number1[i]-87;
       else
       {
            if(number1[i]>=65)
            c=number1[i]-55;
            else
            c=number1[i]-48;
       }
       o=1;
       for(int l=1;l<=i;l++)
       {

       o=o*a;
       }
       s=o*c+s;
    }

    if(s==0)
    puts("0");
    else
    {

    for(j=0;s!=0;j++)
    {

       number2[j]=s%b;
        s=s/b;
        }

     int q2,p2;
     p2=j;
     for(q2=0;q2<p2;q2++)
     {
     if(number2[q2]>9)
     number2[q2]=number2[q2]+55;
     else
      number2[q2]=number2[q2]+48;
     number3[p2-q2-1]=number2[q2];
     }
     number3[p2]='\0';

    puts(number3);

     }

    getchar();
    getchar();
    getchar();
    getchar();
    return 0;
}


char x[N];
int Rtoten(int R,char shu[]){
    int i,s=0;
    int m=strlen(shu);
    for(i=0;i<m;i++){
        if(shu[i]>='A' && shu[i]<='Z' || shu[i]>='a'&&shu[i]<='z')
            (shu[i]|=0x20)-=0x57;

        if(shu[i]>='0'&&shu[i]<='9')
            shu[i]=shu[i]-'0';
    }
    for(i=0;i<m;i++){
        s=s*R+shu[i];
    }
    return s;
}
void shiR(int c,int R){
    int i,m,temp;
    for(i=0;c;i++){
        if((x[i]=c%R+'0')>'9') x[i]+=7;
        c=c/R;
    }
    x[i]='\0';

    m=strlen(x);
    for(i=0;i<m/2;i++){
        temp=x[i];
        x[i]=x[m-i-1];
        x[m-i-1]=temp;
    }
}
int main(void){
    int a,b,i,c;
    int count=0;
    char shu[N];
    cin>>a;
    cin>>shu;
    cin>>b;
    for (i=0;i<strlen(shu);i++)
    {
    	if (shu[i]=='0')
    		count++;
    }
    if (count==strlen(shu))
    	cout<<"0"<<endl;
    else{
    c=Rtoten(a,shu);
    shiR(c,b);
    int m=strlen(x);
    for(i=0;i<m;i++)
    { cout<<x[i];}

  cout<<endl;}

  return 0;
}


int main()
{
     int i;                       //??
     char chnum[20];        //????
     int num[20],resnum[20];      //num???????????  resnum ????
     int n;
     long sum=0;                     //n????? sum?????????
     int a,b;                             //a?? b??
     //???? ?????

     cin>>a>>chnum>>b;
     n=strlen(chnum);
     for(i=0;i<20;i++)
           {
                  if(chnum[i]=='a'){num[i]=10;continue;}  if(chnum[i]=='A'){num[i]=10;continue;}
                  if(chnum[i]=='b'){num[i]=11;continue;}  if(chnum[i]=='B'){num[i]=11;continue;}
                  if(chnum[i]=='c'){num[i]=12;continue;}  if(chnum[i]=='C'){num[i]=12;continue;}
                  if(chnum[i]=='d'){num[i]=13;continue;}  if(chnum[i]=='D'){num[i]=13;continue;}
                  if(chnum[i]=='e'){num[i]=14;continue;}  if(chnum[i]=='E'){num[i]=14;continue;}
                  if(chnum[i]=='f'){num[i]=15;continue;}  if(chnum[i]=='F'){num[i]=15;continue;}
                  if(chnum[i]=='g'){num[i]=16;continue;}  if(chnum[i]=='G'){num[i]=16;continue;}
                  if(chnum[i]=='h'){num[i]=17;continue;}  if(chnum[i]=='H'){num[i]=17;continue;}
                  if(chnum[i]=='i'){num[i]=18;continue;}  if(chnum[i]=='I'){num[i]=18;continue;}
                  if(chnum[i]=='j'){num[i]=19;continue;}  if(chnum[i]=='J'){num[i]=19;continue;}
                  if(chnum[i]=='k'){num[i]=20;continue;}  if(chnum[i]=='K'){num[i]=20;continue;}
                  if(chnum[i]=='l'){num[i]=21;continue;}  if(chnum[i]=='L'){num[i]=21;continue;}
                  if(chnum[i]=='m'){num[i]=22;continue;}  if(chnum[i]=='M'){num[i]=22;continue;}
                  if(chnum[i]=='n'){num[i]=23;continue;}  if(chnum[i]=='N'){num[i]=23;continue;}
                  if(chnum[i]=='o'){num[i]=24;continue;}  if(chnum[i]=='O'){num[i]=24;continue;}
                  if(chnum[i]=='p'){num[i]=25;continue;}  if(chnum[i]=='P'){num[i]=25;continue;}
                  if(chnum[i]=='q'){num[i]=26;continue;}  if(chnum[i]=='Q'){num[i]=26;continue;}
                  if(chnum[i]=='r'){num[i]=27;continue;}  if(chnum[i]=='R'){num[i]=27;continue;}
                  if(chnum[i]=='s'){num[i]=28;continue;}  if(chnum[i]=='S'){num[i]=28;continue;}
                  if(chnum[i]=='t'){num[i]=29;continue;}  if(chnum[i]=='T'){num[i]=30;continue;}
                  if(chnum[i]=='v'){num[i]=31;continue;}  if(chnum[i]=='V'){num[i]=31;continue;}
                  if(chnum[i]=='w'){num[i]=32;continue;}  if(chnum[i]=='W'){num[i]=32;continue;}
                  if(chnum[i]=='x'){num[i]=33;continue;}  if(chnum[i]=='X'){num[i]=33;continue;}
                  if(chnum[i]=='y'){num[i]=34;continue;}  if(chnum[i]=='Y'){num[i]=34;continue;}
                  if(chnum[i]=='z'){num[i]=35;continue;}  if(chnum[i]=='Z'){num[i]=35;continue;}
                  num[i]=(int)chnum[i]-48;
           }
     //???? ???? ????????????

     for(i=0;i<n;i++)
            sum+=num[i]*pow((double)a,(double)(n-i-1));
     //???? ??????????

     i=19;
     while(sum>=b)
       {resnum[i]=sum%b;
        sum=sum/b;
        i--;
       };
     resnum[i]=sum;
     //???? ????????b????

     for(;i<20;i++)
       {if(resnum[i]==10){cout<<"A";continue;}
        if(resnum[i]==11){cout<<"B";continue;}
        if(resnum[i]==12){cout<<"C";continue;}
        if(resnum[i]==13){cout<<"D";continue;}
        if(resnum[i]==14){cout<<"E";continue;}
        if(resnum[i]==15){cout<<"F";continue;}
        if(resnum[i]==16){cout<<"G";continue;}
        if(resnum[i]==17){cout<<"H";continue;}
        if(resnum[i]==18){cout<<"I";continue;}
        if(resnum[i]==19){cout<<"J";continue;}
        if(resnum[i]==20){cout<<"K";continue;}
        if(resnum[i]==21){cout<<"L";continue;}
        if(resnum[i]==22){cout<<"M";continue;}
        if(resnum[i]==23){cout<<"N";continue;}
        if(resnum[i]==24){cout<<"O";continue;}
        if(resnum[i]==25){cout<<"P";continue;}
        if(resnum[i]==26){cout<<"Q";continue;}
        if(resnum[i]==27){cout<<"R";continue;}
        if(resnum[i]==28){cout<<"S";continue;}
        if(resnum[i]==29){cout<<"T";continue;}
        if(resnum[i]==30){cout<<"U";continue;}
        if(resnum[i]==31){cout<<"V";continue;}
        if(resnum[i]==32){cout<<"W";continue;}
        if(resnum[i]==33){cout<<"X";continue;}
        if(resnum[i]==34){cout<<"Y";continue;}
        if(resnum[i]==35){cout<<"Z";continue;}
        cout<<resnum[i];}
     //???? ?b??????????b????????

     return 0;
}


long toDex(int a,char n[]);
int toB(long Dn,int b,char answer[]);

main()
{
	int a,b,len,i;
	char n[50]={"0"},answer[50]={"0"};
	long Dn;

	scanf("%d%s%d",&a,n,&b);

	for (i=0;i<=strlen(n)-1;i++)
		n[i]=toupper(n[i]);
	Dn=toDex(a,n);
	len=toB(Dn,b,answer);

	for (i=len-1;i>=0;i--)
		printf("%c",answer[i]);

}

long toDex(int a,char n[])
{
	int i,j;
	long Dn=0,sqr;

	for (i=0;i<strlen(n);i++)
	{
		sqr=1;
		for (j=0;j<strlen(n)-i-1;j++)
			sqr*=a;
		if ((int)(n[i])>=65) Dn+=sqr*((int)(n[i])-55);
		else Dn+=sqr*((int)(n[i])-48);
	}

	return Dn;
}

int toB(long Dn,int b,char answer[])
{
	long shang=Dn;
	int i=0,yu;

	do
	{
		yu=shang%b;
		(yu>=10)?(answer[i]=(char)(yu)+55):(answer[i]=(char)(yu)+48);
		i++;
		shang=shang/b;
	}while (shang!=0);

	return i;
}


void main()
{
	int a,b;
	char n[100];
	scanf("%d %s %d",&a,n,&b);
	int i,j=0;
	int ten[100];
	for(i=0;n[i]!='\0';i++)
	{
		j++;
		switch(n[i])
		{
		case '0':
			ten[i]=0;
			break;
		case '1':
			ten[i]=1;
			break;
		case '2':
			ten[i]=2;
			break;
		case '3':
			ten[i]=3;
			break;
		case '4':
			ten[i]=4;
			break;
		case '5':
			ten[i]=5;
			break;
		case '6':
			ten[i]=6;
			break;
		case '7':
			ten[i]=7;
			break;
		case '8':
			ten[i]=8;
			break;
		case '9':
			ten[i]=9;
			break;
		case 'A':
		case 'a':
			ten[i]=10;
			break;
		case 'B':
		case 'b':
			ten[i]=11;
			break;
		case 'C':
		case 'c':
			ten[i]=12;
			break;
		case 'D':
		case 'd':
			ten[i]=13;
			break;
		case 'E':
		case 'e':
			ten[i]=14;
			break;
		case 'F':
		case 'f':
			ten[i]=15;
			break;
		case 'G':
		case 'g':
			ten[i]=16;
			break;
		case 'H':
		case 'h':
			ten[i]=17;
			break;
		case 'I':
		case 'i':
			ten[i]=18;
			break;
		case 'J':
		case 'j':
			ten[i]=19;
			break;
		case 'K':
		case 'k':
			ten[i]=20;
			break;
		case 'L':
		case 'l':
			ten[i]=21;
			break;
		case 'M':
		case 'm':
			ten[i]=22;
			break;
		case 'N':
		case 'n':
			ten[i]=23;
			break;
		case 'O':
		case 'o':
			ten[i]=24;
			break;
		case 'P':
		case 'p':
			ten[i]=25;
			break;
		case 'Q':
		case 'q':
			ten[i]=26;
			break;
		case 'R':
		case 'r':
			ten[i]=27;
			break;
		case 'S':
		case 's':
			ten[i]=28;
			break;
		case 'T':
		case 't':
			ten[i]=29;
			break;
		case 'U':
		case 'u':
			ten[i]=30;
			break;
		case 'V':
		case 'v':
			ten[i]=31;
			break;
		case 'W':
		case 'w':
			ten[i]=32;
			break;
		case 'X':
		case 'x':
			ten[i]=33;
			break;
		case 'Y':
		case 'y':
			ten[i]=34;
			break;
		case 'Z':
		case 'z':
			ten[i]=35;
			break;
		}
	}
	int te[100];
	te[0]=1;
	for(i=0;i<j;i++)
	{
		te[i+1]=te[i]*a;
	}
	int t=0;
	for(i=0;i<j;i++)
	{
		t+=te[i]*ten[j-i-1];
	}
	char m[100];
	int ben[100];
	j=0;
	if(t==0)
		printf("0\n");
	else
	{
	for(i=0;t!=0;i++)
	{
		j++;
		ben[i]=t%b;
		t=t/b;
	}
	for(i=0;i<j;i++)
	{
		switch(ben[j-1-i])
		{
		case 0:
			m[i]='0';
			break;
		case 1:
			m[i]='1';
			break;
		case 2:
			m[i]='2';
			break;
		case 3:
			m[i]='3';
			break;
		case 4:
			m[i]='4';
			break;
		case 5:
			m[i]='5';
			break;
		case 6:
			m[i]='6';
			break;
		case 7:
			m[i]='7';
			break;
		case 8:
			m[i]='8';
			break;
		case 9:
			m[i]='9';
			break;
		case 10:
			m[i]='A';
			break;
		case 11:
			m[i]='B';
			break;
		case 12:
			m[i]='C';
			break;
		case 13:
			m[i]='D';
			break;
		case 14:
			m[i]='E';
			break;
		case 15:
			m[i]='F';
			break;
		case 16:
			m[i]='G';
			break;
		case 17:
			m[i]='H';
			break;
		case 18:
			m[i]='I';
			break;
		case 19:
			m[i]='J';
			break;
		case 20:
			m[i]='K';
			break;
		case 21:
			m[i]='L';
			break;
		case 22:
			m[i]='M';
			break;
		case 23:
			m[i]='N';
			break;
		case 24:
			m[i]='O';
			break;
		case 25:
			m[i]='P';
			break;
		case 26:
			m[i]='Q';
			break;
		case 27:
			m[i]='R';
			break;
		case 28:
			m[i]='S';
			break;
		case 29:
			m[i]='T';
			break;
		case 30:
			m[i]='U';
			break;
		case 31:
			m[i]='V';
			break;
		case 32:
			m[i]='W';
			break;
		case 33:
			m[i]='X';
			break;
		case 34:
			m[i]='Y';
			break;
		case 35:
			m[i]='Z';
			break;
		}
	}
	for(i=0;i<j;i++)
		printf("%c",m[i]);
	printf("\n");
	}
}


long times(int a ,int n)
{
	long sum =1;
	for (int i = 1;i<=n;i++)
	{
		sum*=a;
	}
	return sum;
}
long input(char n[1000],int a)
{
	int la;
	long out= 0;
	la = strlen(n);
	for (int i = la-1;i>=0;i--)
	{
		if (n[i]<=57)
		{
			int p;
			p = (int)n[i]-48;
			out+= ((long)p) *times(a,(la-i-1));
		}
		else
		{
			if (n[i]<=90)
			{
				int p;
				p = (int)n[i]-55;
				out+= ((long)p)*times(a,(la-i-1));
			}
			else
			{
				int p ;
				p = (int)n[i]-87;
				out+=((long)p)*times(a,(la-i-1));
			}
		}
	}
	return out;
}
int main()
{
	int a ,b;
	char n[1000];
	cin>>a>>n>>b;
	long out;
	out = input(n,a);
	char on[1000];
	int la;
	la = strlen(n);
	int l;
	for (l = 1; out/(long)b >= times(b,l-1);l++)
	{
	}
	for (int i = 0;i<l;i++)
	{
		int p;
		for (p = 0;out-times(b,(l-1-i))>=0;p++)
		{
			out-=times(b,(l-1-i));
		}
		if (p<=9)
		{
			on[i]=(char)(p+48);
		}
		else
		{
			on[i]=(char)(p+55);
		}
	}
	on[l]='\0';
	cout<<on;

}


int main(){
	char s[100];
	int a,b;
	scanf("%d %s %d",&a,s,&b);

	//?????????
	int l,k,i;
	l=strlen(s);
	int sz[100];

	for(i=0;i<l;i++){
		if(s[i]>='a'&&s[i]<='z')
			sz[i]=10+s[i]-'a';
		if(s[i]>='A'&&s[i]<='Z')
			sz[i]=10+s[i]-'A';
		if(s[i]>='0'&&s[i]<='9')
			sz[i]=0+s[i]-'0';
	}

	for(i=0;i<l;i++){
		//printf("sz[%d]=%d",i,sz[i]);
		k=l-i-1;
		while(k--){
			sz[i]=sz[i]*a;}
		//printf("sz[%d]=%d",i,sz[i]);
	}

	int num10=0;//shijinzhishu
	for(i=0;i<l;i++){
		num10+=sz[i];

	}
	//printf("%d",num10);

	//zhuanhuawei b jinzhi
	int shang,yushu;
	shang=num10/b;
	yushu=num10%b;
	int geweiyushu[100];
	if(shang==0){printf("%d",yushu);}
	else {for(i=0;shang!=0;i++){
		geweiyushu[i]=yushu;

		yushu=shang%b;
		shang=shang/b;
	}
	geweiyushu[i]=yushu;}

	int weishu;
	weishu=i+1;
	//printf("weishu=%d",weishu);

	//???string

	char s2[100];

	for(i=0;i<weishu;i++){
		if(geweiyushu[i]>=10){s2[i]=geweiyushu[i]-10+'A';}
		else s2[i]=geweiyushu[i]-0+'0';
	}
	int y=0;
	for(i=0;i<l;i++){
		if(s[i]!='0')
			y++;
	}
	//printf("y=%d\n",y);
	if(y!=0){
	for(i=weishu-1;i>=0;i--){
		printf("%c",s2[i]);
	}}
	//else printf("0");

	return 0;
}


int main()
{
    int zhuan(char);
    int a,b,l,i,wei;
    char n[100];
    long shi;
    scanf("%d%s%d",&a,n,&b);
    l=strlen(n);
    for(i=0,shi=0;i<=l-1;i++)
    {
                       if(i<l-1)
                      shi=(shi+zhuan(n[i]))*a;
                       else
                       shi=shi+zhuan(n[i]);
    }

    for(i=0;;shi=shi/b,i++)
    {
                       wei=shi%b;
                       if(wei>=10)
                       n[i]=wei-10+'A';
                       else
                       n[i]=wei+'0';

                       if(shi<b)
                       break;
    }
    for(i=i;i>=0;i--)
    printf("%c",n[i]);

    return 0;
}
int zhuan(char b)
{
    int a;
    if(b>='0'&&b<='9')
    a=b-48;
    if(b>='a'&&b<='z')
    a=10+b-'a';
    if(b>='A'&&b<='Z')
    a=10+b-'A';
    return a;
}


/**
?originBase?number???10??
*/
int change2Dec(char * number, int len_number, int originBase) {

  int result = 0;
  //for??
  for (int i=0; i<len_number; i++) {
    //???????10????????
    int numberBit = 0;
    if (number[i]>='0' && number[i]<='9') {
      numberBit = number[i]-'0';
    }
    else if (number[i]>='a' && number[i]<='z') {
      numberBit = number[i]-'a'+10;
    }
    else {
      numberBit = number[i]-'A'+10;
    }

    //???????????
    int position = len_number-i;

    //????????????
    int currentBitValue = numberBit;
    for (int i=1; i<position; i++) {
      currentBitValue *= originBase;
    }
    result += currentBitValue;
  }
  return result;
}

/**
?10??decNumber???newBase??????newNumber????????????
*/
int changeDec2NewBase(int decNumber, int newBase, char* newNumber) {
    int bitValue[100];                   //???????????????????
    int length=0;                          //???????
    while (decNumber/newBase > 0) {
        bitValue[length++] = decNumber%newBase;         //??
        decNumber /= newBase;
    }
    bitValue[length++] = decNumber;                     //??????

    for (int i=length-1,j=0; i>=0; i--,j++) {
        int currentBit = bitValue[i];
        if (currentBit < 10)
            newNumber[j]=currentBit+'0';
        else
            newNumber[j]=currentBit+'A'-10;
    }
    return length;
}

int main()
{
    int originBase;         //????
    int newBase;            //???
    char number[100];       //????????
    char newNumber[100];    //??????
    cin >> originBase >> number >> newBase;
    int len_number;             //????????
    int len_newNumber;          //????????

    for (int i=0; i<100; i++)
        if (number[i]==0) {
           len_number = i;
           break;
        }
    int decNumber = change2Dec(number, len_number, originBase);
    len_newNumber = changeDec2NewBase(decNumber, newBase, newNumber);
    newNumber[len_newNumber]=0;
    cout << newNumber << endl;

}


long int shijinzhi(int a,char n[100])
{
	int i;
	long int z=0;
	for(i=0;i<strlen(n);i++)
	{
		if(n[i]>='0'&&n[i]<='9')
		{
		z+=pow(a,strlen(n)-1-i)*(n[i]-'0');
		}
		else if(n[i]>='a'&&n[i]<='z')
		{
		z+=pow(a,strlen(n)-1-i)*(n[i]-'a'+10);
		}
		else if(n[i]>='A'&&n[i]<='Z')
		{
		z+=pow(a,strlen(n)-1-i)*(n[i]-'A'+10);
		}
	}
	return z;
}
void zhuanhuan(long int x,char a[1000],int b)
{
	int yushu,l,j;
		int i=0;
		char n[1000]={0};
	if(b<=10)
	{
		do
		{
		yushu=x%b;
		x=x/b;
		n[i]=yushu+'0';
		i++;
		}while(x>=b);
		n[i]=x+'0';
	}
	else if(b>10)
	{
		do
		{
		yushu=x%b;
		x=x/b;
		if(yushu<=9)
		n[i]=yushu+'0';
		else if(yushu>9)
		n[i]=yushu-10+'A';
		i++;
		}while(x>=b);
		if(x<=9)
		n[i]=x+'0';
		else if(x>9)
		n[i]=x-10+'A';
	}
	for(l=i,j=0;l>=0;l--,j++)
	{
		a[j]=n[l];
	}
}
void main()
{
	int a,b,i,p;
	long int x;
		char n[1000]={0};
	scanf("%d %s %d",&a,&n,&b);
	x=shijinzhi(a,n);
	p=strlen(n);
	for(i=0;i<=p;i++)
	{n[i]=0;}
	zhuanhuan(x,n,b);
	if(n[0]!='0') printf("%s",n);
	else printf("0");
}


long f(long a,char str[20],int l);
long F(long n,long b);
int main()
{
	long a,b,n;int i,l;char str[20];
	cin >> a >> str >> b;
	l=strlen(str);
	for(i=0;i<l;i++)
	{
		if(str[i]>96&&str[i]<123)
			str[i]=str[i]-32;
	}
	n=f(a,str,l);
	F(n,b);
	return 0;
}
long f(long a,char str[20] ,int l)
{
	long sum=0;int i,t,num=1;
	for(i=l-1;i>=0;i--)
	{
		if(str[i]>64&&str[i]<91)
		{
			t=str[i]-'A'+10;
		}
		else
			t=str[i]-'0';
		sum=sum+t*num;
		num=num*a;
	}
	return sum;
}
long F(long n,long b)
{
	int i=0,str[200];char a;
	if(n==0)
		cout << "0" << endl;
	while(n>0)
	{
		str[i]=n%b;
		n=n/b;
		i++;
	}
	for(i=i-1;i>=0;i--)
	{
		if(str[i]>=0&&str[i]<=9)
			cout << str[i];
		else
		{
			a='A'+str[i]-10;
			cout << a;
		}
	}
	cout << endl;
	return 0;
}


char str[100];
int a,b;
int num,tmp,i;
int main() {
    while(scanf("%d%s %d", &a, str, &b) != EOF){
        num = 0;
        for(i=0; str[i]; i++){
            if(str[i] >= 'a') str[i] -= 32;
            tmp = (str[i] >= 'A' ? str[i]-'A'+10:str[i]-'0');
            //tmp = str[i]
            num = num * a + tmp;
        }
        int cnt=0;
        if(num == 0)
        {puts("0"); continue;}
        while(num){
            if(num%b <=9)
                str[cnt++] = num%b+'0';
            else
                str[cnt++] = num%b-10+'A';
            num = num/b;
        }
        for(i=cnt-1; i>=0; i--)
            printf("%c",str[i]);
        puts("");
    }
    return 0;
}