#include <stdio.h>
#include <math.h>
int BinaryToDicemal(int n);
int DicemalToBinary(int n);
int main()
{
int n,m;
while(scanf("%d %d",&n,&m) != EOF)
{
if(m == 0 && n == 0)
{
break;
}
else if(m == 2)
{
printf("%d\n",BinaryToDicemal(n));
}
else if(m == 10)
{
printf("%d\n",DicemalToBinary(n));
}
}
return 0;
}
int BinaryToDicemal(int n)
{
int DicemalNumber = 0;
int remainder;
int i = 0;
while( n != 0)
{
remainder = n % 10;
n /= 10;
DicemalNumber += remainder*pow(2,i);
i ++;
}
return DicemalNumber;
}
int DicemalToBinary(int n)
{
int BinaryNumber = 0;
int remainder;
int i = 1;
while(n != 0)
{
remainder = n % 2;
n /= 2;
BinaryNumber += remainder * i;
i *= 10;
}
return BinaryNumber;
}