#include<iostream>
#include<cmath>
using namespace std;
void print(int n)
{
if(n>=0&&n<=9) cout<<n;
if(n==10) cout<<"A";
if(n==11) cout<<"B";
if(n==12) cout<<"C";
if(n==13) cout<<"D";
if(n==14) cout<<"E";
if(n==15) cout<<"F";
if(n==16) cout<<"G";
if(n==17) cout<<"H";
if(n==18) cout<<"I";
if(n==19) cout<<"J";
}
int main()
{
int N,R,*a,i=0,j,flag;
while(cin>>N>>R)
{
if(N==0&&R==0)
break;
a=(int *)malloc(100*sizeof(int));
if(N<0)
{
N=(int)fabs((double)N);
flag=0;
}
while(N>=R)
{
a[i]=N%R;
N=N/R;
//cout<<a[i]<<" ";
i++;
}
a[i]=N;
//cout<<a[i]<<" ";
if(flag==0)
cout<<"-";
for(j=i;j>=0;j--)
print(a[j]);
cout<<endl;
}
return 0;
}