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

typedef long long int LLI;

#define TRUE              (1==1)
#define FALSE             (!TRUE)
#define fo3(i,a,b)        for(i=((int)a);i<=((int)b);i++)
#define of3(i,a,b)        for(i=((int)a);i>=((int)b);i--)
#define in1(n)            scanf("%d",&(n))
#define LL1(n)            scanf("%I64d",&(n))
#define in2(n,m)          scanf("%d%d",&(n),&(m))
#define pri(n)            printf("%d\n",n)
#define prs(n)            printf("%s\n",n)
#define prL(n)            printf("%I64d\n",n)
#define strin(n)          scanf("%s",n)
#define newint(n)         (int*)calloc((n),sizeof(int))
#define arrint(ret,n,i)   fo3(i,1,n) in1(ret[i-1])
#define newLLI(n)         (LLI*)calloc((n),sizeof(LLI))
#define arrLLI(ret,n,i)   fo3(i,1,n) LL1(ret[i-1])

void fastWrite(FILE * ofp, int x) {
    int i = 12;
    char buff[i];
    buff[i] = 0;
    int neg = 0;
    if (x < 0){
        x = -x;
        neg = 1;
    }
    if (x == 0)
        buff[--i] = '0';
    else while (x != 0) {
        buff[--i] = (x % 10) + '0';
        x /= 10;
    }
    if (neg)
        buff[--i] = '-';
    fwrite(buff + i, sizeof(char), (12 - i), ofp);
}

void fastSpace(FILE * ofp){
    fwrite(" ", sizeof(char), 1, ofp);
}
void fastNewLine(FILE * ofp) {
    fwrite("\n", sizeof(char), 1, ofp);
}

typedef struct AL AL;
struct AL{
    int * vals;
    int size;
    int cap;
};
AL * ALCreate(int s) {
    AL * ret = (AL *) calloc(1, sizeof(AL));
    if (s == 0) ret->vals = NULL;
    else ret->vals = (int *) calloc(s, sizeof(int));
    ret->size = 0; ret->cap = s;
    return ret;
}
void ALAdd(AL * list, int ele) {
    if (list->cap == list->size) {
        int nC = ((list->cap << 1) | 1);
        list->vals = realloc(list->vals, sizeof(int) * nC);
        list->cap = nC;
    }
    list->vals[list->size++] = ele;
}

typedef struct Pair {
    int fi, se;
} Pair;

int comp(Pair ** arr, int f, int s) {// CHANGE THIS
    if (arr[f]->fi != arr[s]->fi)
        return arr[f]->fi - arr[s]->fi;
    return arr[f]->se - arr[s]->se;
}

void sort(Pair ** arr, int size) {
    int i;
    if (size < 6) {
        int nSize = 0;
        Pair * swp;
        while (size != 0) {
            nSize = 0;
            for (i = 0; i < size - 1; i++)
                if (comp(arr, i, i + 1) > 0) {
                    nSize = i + 1;
                    swp = arr[i];
                    arr[i] = arr[i + 1];
                    arr[i + 1] = swp;
                }
            size = nSize;
        }
        return;
    }
    Pair ** tmp = (Pair **)calloc(size, sizeof(Pair *)); // CHANGE THIS
    int lSize = (size >> 1);
    int rSize = size - lSize;
    sort(arr, lSize); sort(arr + lSize, rSize);
    int lPtr = 0, rPtr = 0;
    int ptr = 0;
    while (ptr != size) {
        if (lPtr == lSize){
            tmp[ptr] = arr[lSize + (rPtr++)];
        } else if (rPtr == rSize || comp(arr, lPtr, lSize + rPtr) <= 0){ // CHANGE THIS
            tmp[ptr] = arr[lPtr++];
        } else {
            tmp[ptr] = arr[lSize + (rPtr++)];
        }
        ptr++;
    }
    for (i = 0; i < size; i++)
        arr[i] = tmp[i];
    free(tmp);
}

int bc(int x) {
    if (x <= 1)
        return x;
    return (x&1) + bc(x>>1);
}

int gcd(int a, int b){return (b==0)?a:gcd(b,a%b);}

#define MAX        100000
#define NULL_CARRY -1
#define NULL_VALUE 0

LLI h1, h2;

int carry[4 * MAX];
int st[4 * MAX];
int en[4 * MAX];
LLI ans[2][4 * MAX];

void init2(int index, int s, int e) {
   //printf("%d %d %d\n", index, s, e);
   st[index] = s;
   en[index] = e;
   ans[0][index] = 0;
   ans[1][index] = 0;
   carry[index] = NULL_CARRY;
   if (s == e)
      return;
   int mid = (s + e) >> 1;
   init2(index << 1, s, mid);
   init2((index << 1) | 1, mid + 1, e);
}

// Inclusive s and e
void ITInit(int s, int e) {
   init2(1, s, e);
}

LLI fastExpo(LLI exp, LLI base, LLI mod) {
    if (exp == 0)
        return 1;
    if (exp == 1)
        return base % mod;
    LLI tmp = fastExpo(exp >> 1, base, mod);
    tmp *= tmp;
    tmp %= mod;
    if (exp & 1)
        tmp = (tmp * base) % mod;
    return tmp;
}

LLI aoMem[2][MAX + 10];
LLI feMem[2][MAX + 10];
void genAllOnes() {
    int i;
    LLI tmp1 = 0;
    LLI tmp2 = 0;
    LLI tmp3 = 1;
    LLI tmp4 = 1;
    fo3(i,0, MAX+9) {
        aoMem[TRUE][i] = tmp1;
        tmp1 = tmp1 * 10 + 1;
        tmp1 %= h1;
        aoMem[FALSE][i] = tmp2;
        tmp2 = tmp2 * 10 + 1;
        tmp2 %= h2;
        feMem[TRUE][i] = tmp3;
        tmp3 *= 10;
        tmp3 %= h1;
        feMem[FALSE][i] = tmp4;
        tmp4 *= 10;
        tmp4 %= h2;
    }
}
LLI allOnes(int len, LLI mod) {
    return aoMem[mod==h1][len];
}

LLI agg(int pre, int suf, int pow, LLI mod) {
    LLI mult = feMem[mod==h1][pow];
    mult *= suf;
    mult += pre;
    mult %= mod;
    //printf(" returning from agg!\n");
    return mult;
}

void ITComp(int index) {
    if (carry[index] == NULL_CARRY) {
        ans[0][index] = agg(ans[0][index<<1], ans[0][(index<<1)|1], en[index<<1] - st[index<<1] + 1, h1);
        ans[1][index] = agg(ans[1][index<<1], ans[1][(index<<1)|1], en[index<<1] - st[index<<1] + 1, h2);
    } else {
        ans[0][index] = allOnes(en[index] - st[index] + 1, h1) * carry[index];
        ans[0][index] %= h1;
        ans[1][index] = allOnes(en[index] - st[index] + 1, h2) * carry[index];
        ans[1][index] %= h2;
    }
}

void push(int index) {
    if (carry[index] == NULL_CARRY)
        return;
    if (st[index] != en[index]) {
        carry[index<<1] = carry[index];
        ITComp(index<<1);
        carry[(index<<1)|1] = carry[index];
        ITComp((index<<1)|1);
    }
    carry[index] = NULL_CARRY;
}

void set2(int index, int s, int e, int value) {
    if (st[index] >= s && e >= en[index]) {
        carry[index] = value;
        ITComp(index);
        return;
    }
    if (st[index] > e || s > en[index])
        return;
    push(index);
    set2(index<<1, s, e, value);
    set2((index<<1)|1, s, e, value);
    ITComp(index);
}

void ITSet(int s, int e, int value) {
   set2(1, s, e, value);
}

LLI get2(int index, int type, int s, int e) {
    if (st[index] >= s && e >= en[index]) {
        //printf(" Starting Push!\n");
        //push(index);
        ITComp(index);
        //printf(" Out of Push!\n");
        if (type == 0) {
            //prL(agg(0, ans[type][index], st[index] - s, h1));
            return agg(0, ans[type][index], st[index] - s, h1);
        }
        return agg(0, ans[type][index], st[index] - s, h2);
    }
    if (st[index] > e || s > en[index])
        return NULL_VALUE;
    push(index);
    LLI ret = get2(index<<1, type, s, e) + get2((index<<1)|1, type, s, e);
    if (type == 0 && ret >= h1)
        ret -= h1;
    if (type == 1 && ret >= h2)
        ret -= h2;
    return ret;
}

LLI get(int type, int s, int e) {
   return get2(1, type, s, e);
}

int main(void) {
    int a, c, aa, cc, b = 0, pow = 1, bb;
    in2(a,c);
    while (a != 0 || c != 0) {
       aa = a % 3;
       cc = c % 3;
       bb = (3 + cc - aa) % 3;
       b += pow * bb;
       a/=3;
       c/=3;
       pow *= 3;
    }
    pri(b);
    return 0;
}

/* https://codeforces.com/contest/136/submission/35187182 (rainboy) */
#include <stdio.h>

int sub(int a, int c) {
	return a == 0 && c == 0 ? 0 : sub(a / 3, c / 3) * 3 + ((c - a) % 3 + 3) % 3;
}

int main() {
	int a, c;

	scanf("%d%d", &a, &c);
	printf("%d\n", sub(a, c));
	return 0;
}

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

int main()
{
   long long a, b, c;

    scanf("%lld%lld", &a, &c);

    b = 0;
    long long p = 1;
    while(a || c) {
        int aux1 = a % 3;
        int aux2 = c % 3;

        b = b + ((abs(aux2 - aux1 + 3)) % 3) * p;

        a /= 3;
        c /= 3;
        p *= 10;
    }

    long long axi = 0;
    p = 1;
    while(b) {
        axi += (b % 10) * p;
        b /= 10;
        p *= 3;
    }

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

#include<stdio.h>
#define max(a,b) \
   ({ __typeof__ (a) _a = (a); \
       __typeof__ (b) _b = (b); \
     _a > _b ? _a : _b; })

typedef long long int ll;
int x[30],y[30];
int main()
{
	ll a,c,ans=0,temp=1;
	scanf("%lld %lld",&a,&c);

	int j=0;
	while(a)
	{
		x[++j] = a%3;
		a /= 3;
	}
	int i=0;
	while(c)
	{
		y[++i]=c%3;
		c /=3;
	}

	i = max(i,j);
	for(int k=1;k<=i;k++)
	{
		x[k]=y[k]-x[k]+3;
		x[k] = x[k]%3;
		if(x[k])
			ans += temp*x[k];
		temp *=3;
	}
	printf("%lld\n",ans);
	return 0;
}

#include <stdio.h>
#include <math.h>
main()
{
long int rust(int x);
long int lust(int x);
int arrb[20]={0};
long int a,c,ans=0;
scanf("%ld %ld",&a,&c);
int at,ct;
int k=0,i;
while(a!=0 || c!=0)
{
at=a%3;
ct=c%3;
a=a/3;
c=c/3;
switch(ct)
{
case(2):{arrb[k++]=(2-at);
        break;}

case(0):{if(at==0) arrb[k++]=0;
         else arrb[k++]=(3-at);
         break;}
case(1):{if(at==2) arrb[k++]=2;
         else arrb[k++]=(1-at);
         break;}
}
}
k=k-1;
for(i=k;i>=0;i--)
{
ans=ans+lust(i)*arrb[i];
}
printf("%ld",ans);
return 0;
}

long int rust(int x)
        {
            int i;
            long int k=1;
            for(i=1;i<=x;i++)
            k=k*10;
        return k;
        }

long int lust(int x)
        {
            int i;
            long int k=1;
            for(i=1;i<=x;i++)
            k=k*3;
        return k;
        }

#include <stdio.h>

void tern(int ar[], int size, int num);

void tern(int ar[], int size, int num)
{
    size--;

    while (num != 0)
    {
        ar[size] = num % 3;
        num = num / 3;
        size--;
    }
}

int main()
{
    int a, c, b = 0, A[25], B[25], C[25], i, j, k, mul = 1, add;

    for (i = 0; i < 25; i++)
    {
        A[i] = 0;
        B[i] = 0;
        C[i] = 0;
    }

    scanf("%d %d", &a, &c);

    tern(A, 25, a);
    tern(C, 25, c);

    for (i = 24; i >= 0; i--)
    {
        add = C[i] - A[i];

        if (add < 0) add += 3;

        b += add * mul;
        mul *= 3;
    }

    printf("%d", b);

    return 0;

}

#include <stdio.h>
#include <math.h>
main()
{
long int a,c,b=0;
scanf("%ld %ld",&a,&c);
int at,ct,i=0;
while(a!=0 || c!=0)
{
at=a%3;
ct=c%3;
a=a/3;
c=c/3;
ct+=3;
b = b + ((ct-at)%3)*(long int)pow(3,i);
i++;
}
printf("%ld",b);
return 0;
}

#include        <stdio.h>
int*            mtob(int a)
{
    int         *result;
    result = (int*)malloc(10000*sizeof(int));
    int         i,count;
    i  = a;
    count = 0;
    while (1)
    {
        count++;
        result[count] = i % 3;
        i = i/3;
        if (i==0) break;
    }
    result[0] = count;
    return result;
}
int             btom(int *a,int n)
{
    int         i,sum,k;
    sum = 0;
    k = 1;
    for (i=n;i>=1;i--)
    {
        sum+=k*a[i];
        k = 3*k;
    }
    return sum;
}
int             main()
{
    int         a,b,c;
    int         *aa,*cc;
    int         bb[10000],dd[10000];
    int         i,max;

    scanf("%d%d",&a,&c);
    aa = mtob(a);

    cc = mtob(c);
    if (aa[0]>cc[0]) max = aa[0] ;
    else max = cc[0];
    for (i=1;i<=max;i++){
        if (aa[i]<=cc[i]) bb[i] = cc[i]-aa[i];
        if (aa[i]>cc[i])  bb[i] = cc[i]+3-aa[i];
    }

    for (i=1;i<=max;i++)
        dd[i]=bb[max-i+1];
    b = btom(dd,max);
    printf("%d",b);

    return 0;

}

#include <stdio.h>
#include <math.h>
int main(void)
{
	long long a,c,b=0,t1,t2,n,i;
	scanf("%I64d %I64d",&a,&c);

	n=0;
	while(a!=0 || c!=0)
	{
		t1=a%3;t2=c%3;
		for(i=0;i<3;++i)
			if((i+t1)%3==t2){ b+=i*((long long)pow((double)3,(double)n)); break; }
		n++;a/=3;c/=3;
	}
	printf("%I64d",b);
	return 0;
}

#include<stdio.h>
#define ll long long
int s1[400],s2[400];
int pw(int x,int y){
    if(y<1) return 1;
    else return x*pw(x,y-1);
}
int main(){
    ll int a,b,i,j,l1=0,l2=0;
    memset(s1,0,sizeof(s1));
    memset(s2,0,sizeof(s2));
    scanf("%lld%lld",&a,&b);
    while(a>0){
        s1[l1++]=a%3;
        a/=3;
    }
    while(b>0){
        s2[l2++]=b%3;
        b/=3;
    }
    int ans=0;
    for(i=0;i<l1||i<l2;i++){
        for(j=0;j<3;j++){
            if((s1[i]+j)%3==s2[i]){
                break;
            }
        }
        ans+=j*pw(3,i);
    }
    printf("%d",ans);
    return 0;
}

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

int main()
{
    int a,c;
    scanf("%d %d",&a,&c);

    int a1,c1,i,n,k;
    for(i=0,n=0,k=1;a||c;i++,k*=3)
    {
        a1=a%3;
        c1=c%3;
        a/=3;
        c/=3;
        if(!a1)
        {
            n+=k*c1;
            continue;
        }
        if(!c1)
        {
            n+=k*(3-a1);
            continue;
        }
        if(a1>c1)
        {
            n+=k*(3-c1);
        }
        else
        {
            n+=k*(c1-a1);
        }
    }
    printf("%d",n);
    return 0;
}

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

#define max_digits_count 19
#define base 3

char toChar(int x)
{
    return x + '0';
}

void swap(char* x, char* y)
{
    char tmp = *x;
    *x = *y ;
    *y = tmp;
}

char* reverseStr(char* s)
{
    int iLength = strlen(s);
    char* ret = (char*)malloc(iLength);
    unsigned int i = 0;
    for(;  s[i] ; ++i)
        ret[i] = s[i];

    int lo = 0 , hi = iLength - 1;
    while(hi > lo)
    {
        swap(&ret[lo], &ret[hi]);

//      printf("lo = %d, hi = %d, after swap , ret = %s\n", lo, hi , ret);
        lo++;
        hi--;
    }

    ret[iLength] = 0;
    return ret;
}

char* toTenary(int x)
{
    char* ret = (char*)malloc(max_digits_count);
    int iCounter = 0;
    while(x)
    {
        ret[iCounter++] = toChar(x%base);
        x /= base;
    }
    ret[iCounter] = 0;

    while(strlen(ret) < max_digits_count)
    {
        ret[iCounter++] = '0';
        ret[iCounter] = 0;
    }
    ret[max_digits_count] = 0;

    //printf("before reverse, ret = %s\n", ret);

    ret = reverseStr(ret);
    return ret;
}

char min_path(char a, char c)
{
    int ia = a - '0', ic = c - '0' , ib = 0, tmp = 0;;
    for(tmp = 0; tmp <= 2; ++tmp)
    {
        if((ia+tmp)%base == ic) {
            ib = tmp;
            break;
        }
    }
    return toChar(ib);
}

char* solve(char* sa, char* sc)
{
    char* ret = (char*)malloc(max_digits_count);
    ret[max_digits_count] = 0;

    int hi = max_digits_count-1;
    while(hi >= 0)
    {
        ret[hi] = min_path(sa[hi], sc[hi]);
        hi--;
    }

    return ret;
}

int tenaryToDecimal(char* s)
{
    int i = 0, base_pow = 1 , ret = 0;
    int iLength = strlen(s);
    for(i = iLength-1; i >= 0; --i)
    {
        ret += (s[i]-'0')*base_pow ;
        base_pow *= base;
    }

    return ret;
}

int main()
{

#ifndef ONLINE_JUDGE
    freopen("inputC.txt"  ,"r", stdin);
    freopen("outputC.txt" ,"w", stdout);
#endif

    int a, b, c ;
    scanf("%d %d",&a,&c);

    /*
    char* s = reverseStr("abc");
    printf("%s\n",s);
    */

    char* sa = toTenary(a) ;
    char* sc = toTenary(c) ;

    //printf("sa = %s\n",sa);
    //printf("sc = %s\n",sc);

    char* sb = solve(sa,sc);
    //printf("%s\n",sb);
    printf("%d\n", tenaryToDecimal(sb));
    return 0;
}

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 1000
int diff(int a, int b,char *c)//a-b
{
   char f[MAX]={},s[MAX]={};
   int i,j,k,digits;
   int ii;
   for(i=MAX-2;a;i--){
      f[i]=a%3+'0'+3;
      a/=3;
   }
   for(j=MAX-2;b;j--){
      s[j]=b%3+'0';
      //   printf("%c",s[j]);
      b/=3;
   }
   //  printf("\n");
   digits=MAX-((i<j)?i:j)-2;
   //      printf("%d\n",digits);
   for(k=0;k<i-j;k++)
      f[k]='3';
   ii=i;
   for(ii++;ii<MAX-1;k++,ii++)
      f[k]=f[ii];
   f[k]='\0';

   // printf("j is %d and i is %d\n",j,i);
   for(k=0;k<j-i;k++)
      s[k]='0';
   for(j++;j<MAX-1;k++,j++)
      s[k]=s[j];
   s[k]='\0';
   for(i=0;i<digits;i++)
      c[i]=(f[i]-s[i])%3;
   return digits;
   //for(ii
}
int todec(char *a, int dig)
{
   int i=0,ans=0;
   for(i=0;i<dig;i++)
      ans=ans*3+a[i];
   return ans;
}
int main()
{
   int cases,cnt;
   int i,n;
   int a,c;
   char ans[MAX];
   scanf("%d%d",&a,&c);
   printf("%d\n",todec(ans,diff(c,a,ans)));
   return 0;
}

#include<stdio.h>
int main()
{
	long long int a,b;
	int A[100],B[100],C[100];
	scanf("%lld %lld",&a,&b);
	int ka=0;
	while(a>0)
	{
		A[ka++]=a%3;
		a=a/3;
	}
	int kb=0;
	while(b>0)
	{
		B[kb++]=b%3;
		b=b/3;
	}
	while(ka<kb)
		A[ka++]=0;
	while(kb<ka)
		B[kb++]=0;
	int i;
	for(i=0;i<ka;i++)
	{
		C[i]=B[i]-A[i];
		if(C[i]<0)
			C[i]+=3;
	}
	int kc=i;
	long long int c=0;
	long long int prod=1;
	for(i=0;i<kc;i++)
	{
		c = c + prod*C[i];
		prod*=3;
	}
	printf("%lld\n",c);
	return 0;
}

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<ctype.h>
int main(void)
{
	int a,b=0,c;
	int i=0;
	scanf("%d%d",&a,&c);
	while(a || c)
	{
		b += ((((c%3)-(a%3))%3)+3)%3 *pow(3,i);
		a /= 3;
		c /= 3;
		i++;
	}
	printf("%d\n",b);
    return 0;
}

#include <stdio.h>

int a, b , ta[101] , tb[101] , tc[101] , sa,sb,sc, c, i,j,k , n ;

int main()
{
    scanf("%d %d", &a, &b) ;

    while( a > 0 )
    {
        ta[sa] = a%3 ;
        a =a/3 ;
        sa++ ;
    }

      while( b > 0 )
    {
        tb[sb] = b%3 ;
        b =b/3 ;
        sb++ ;
    }
/*
    printf("test for a : ") ;
    for ( i = 0 ; i < sa ;i++)
    {
        printf("%d ", ta[i] ) ;
    }

     printf("\ntest for a : ") ;
    for ( i = 0 ; i < sb ;i++)
    {
        printf("%d ", tb[i] ) ;
    }

    printf("\n") ;
*/

    if ( sa > sb ) sc = sa ;
    else sc =sb ;

    for( i = 0 ; i < sc ; i++)
    {
        tc[i] =  tb[i] - ta[i]  ;

        if ( tc[i] < 0 ) tc[i] += 3 ;
    }

   /*  printf("test for a : ") ;
    for ( i = 0 ; i < sc ;i++)
    {
        printf("%d ", tc[i] ) ;
    }
    printf("\n" ) ; */

    for ( i = sc-1 ; i > -1 ; i--)
    {
        c = 3 * c + tc[i] ;
    }

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

#include <stdio.h>

int sub(int a, int c) {
	return a < 3 && c < 3 ? (c - a + 3) % 3 : sub(a / 3, c / 3) * 3 + ((c - a) % 3+ 3) % 3;
}

int main() {
	int a, c;

	scanf("%d%d", &a, &c);
	printf("%d\n", sub(a, c));
	return 0;
}

#include<stdio.h>
long long int dk(long long int m,long long int n[]);
long long int main()
{
long long int a,b,c,d=0,i,e=0,k=0,g=0;
long long int p[10000]={0},q[10000],f[10000];
scanf("%I64d%I64d",&a,&c);
d=dk(a,p);
e=dk(c,q);
if(d==e)
{
next:
for(i=0;i<d;i++)
{
k=0;
while(((p[i]+k)%3)!=q[i])
{
k++;
}
f[i]=k;
}
}
else
{
if(d>e)
{
while(d!=e)
{
q[e]=0;
e++;
}
goto next;}
else
{
while(d!=e)
{
p[d]=0;
d++;
}
goto next;}
}
for(i=0;i<d;i++)
{
g=g+f[i]*pow(3,i);
}
printf("%I64d",g);
return(0);
}
long long int dk(long long int m,long long int n[])
{
    int x=0;
while(m>0)
{
n[x++]=m%3;
m=(m-(m%3))/3;
}
return(x);
}

#include <stdio.h>
#include <string.h>
int max(int x, int y)
{
	return x > y ? x : y;
}
int main()
{
	int a, b, i, j, t, sum;
	int n1[1001], n2[1001];
	while (scanf("%d %d", &a, &b) != EOF)
	{
		memset(n1, 0, sizeof(n1));
		memset(n2, 0, sizeof(n2));
		i = 0;
		while (a)
		{
			n1[i] = a % 3;
			a /= 3;
			i++;
		}
		j = 0;
		while (b)
		{
			n2[j] = b % 3;
			b /= 3;
			j++;
		}
		t = max(i, j);
		for (i=0; i<t; i++)
		{
			n1[i] = (n2[i] - n1[i] + 3) % 3;
		}
		sum = 0;
		for (i=t-1; i>=0; i--)
		{
			sum *= 3;
			sum += n1[i];
		}
		printf("%d\n", sum);
	}

	return 0;
}

#include<stdio.h>
#include<string.h>
#include<math.h>
#define max  100
int main()
{
    int a,b,i,j,t,t1,t2;
    int c[max],d[max],e[max];
    memset(c,0,sizeof(c));
    memset(d,0,sizeof(d));
    memset(e,0,sizeof(e));

    scanf("%d %d",&a,&b);
    i=0;
    while(a!=0)
    {
        c[i]=a%3;
        a=a/3;
        i++;
    }
    j=0;
    while(b!=0)
    {
        d[j]=b%3;
        b=b/3;
        j++;
    }

    if(i>j)
        t2=i;
    else
        t2=j;

    for(i=t2-1; i>=0; i--)
    {
        if(d[i]<c[i])
            d[i]=d[i]+3;
        e[i]=d[i]-c[i];
    }
    double sum=0.0;
    for(i=0; i<t2; i++)
        sum=sum+e[i]*pow(3.0,(double)i);
    printf("%d\n",(int )sum);
    return 0;
}

#include <stdio.h>

int tor(int a, int c)
{
    if(!a && !c)
        return 0;

    return tor(a / 3, c / 3) * 3 + c % 3 - a % 3 + 3 * (a % 3 > c % 3);
}

int main(int argc, char *argv[])
{
    int a, c;

    scanf("%d %d", &a, &c);
    printf("%d\n", tor(a, c));

    return 0;
}

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_LEN 30
void encode(int* cs3,unsigned int a){
    int n=0;
    int i;
    for(i=0;i<MAX_LEN;i++)cs3[i]=0;
    n=0;
    while(a>0){
        cs3[n++]=a%3;
        a/=3;
    }
}
unsigned int decode(int* cs3){
    unsigned int rt=0;
    int n;
    n=MAX_LEN-1;
    while(cs3[n]==0) n--;
    for(;n>=0;n--){
        rt=rt*3+cs3[n];
    }
    return rt;
}
void print(int* cs3){
    int i;
    for(i=0;i<MAX_LEN;i++)printf("%d",cs3[i]);
    printf("\n");
}
int min(int a,int b){
    if(a>b||a<0) return b;
    else return a;
}
int main(){
    unsigned int a,b;
    int i;
    int cs3a[MAX_LEN];
    int cs3b[MAX_LEN];
    int cs3c[MAX_LEN];
    scanf("%d %d",&a,&b);
    encode(cs3a,a);
    encode(cs3b,b);
    //print(cs3a);
    //print(cs3b);
    for(i=0;i<MAX_LEN;i++){
        cs3c[i]=min(cs3b[i]-cs3a[i],cs3b[i]-cs3a[i]+3);
    }
    //print(cs3c);
    printf("%d",decode(cs3c));
    return 0;
}

#include <stdio.h>
#define maxn 1000
long basea[maxn],baseb[maxn],basec[maxn];
long a,b,c;
long d1,d2,d3;
int main()
{
    long i,tmp=0;
    scanf("%ld %ld",&a,&c);
    while (a>0)
       {
        basea[d1++]=a%3;
        a/=3;
        }
    while (c>0)
       {
        basec[d3++]=c%3;
        c/=3;
        }
    if (d1>d3) d2=d1;
    else d2=d3;
    for (i=0;i<d2;i++)
       {
        tmp=basec[i]-basea[i];
        baseb[i]=(tmp+3)%3;
        //tmp/=3;
        }
    tmp=1;
    for (i=0;i<d2;i++)
       {
        b+=baseb[i]*tmp;
        tmp*=3;
        }
    printf("%ld",b);
    return 0;
}

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

char Str1[31],Str2[31];

int main()
{
    int A,C,L1,L2,L,i,p=1,ans;
    scanf("%d %d",&A,&C);
    itoa(A,Str1,3);
    itoa(C,Str2,3);
    L1=strlen(Str1);
    L2=strlen(Str2);
    L=L1>L2?L1:L2;
    strrev(Str1);
    strrev(Str2);
    if(L1>L2)
    {
        for(i=L2; i<L; ++i)
        {
            Str2[i]='0';
        }
    }
    else
    {
        for(i=L1; i<L; ++i)
        {
            Str1[i]='0';
        }
    }
    for(p=1,ans=i=0; i<L; ++i)
    {
        ans+=((Str2[i]+3-Str1[i])%3)*p;
        p*=3;
    }
    printf("%d\n",ans);
    return 0;
}

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

#define RADIX 3

int getTernary(int a, int radix, int ta[])
{
	int j = 0;
	while(a > 0)
	{
		ta[j] = a % radix;
		a = a / radix;
		++j;
	}
	return --j;
}

int main()
{
	int a,b,c;
	int ina, inc, index;
	int i;
	int ta[20];
	int tb[20];
	int tc[20];

	scanf("%d%d", &a, &c);
	memset(ta, 0, 20*sizeof(int));
	memset(tb, 0, 20*sizeof(int));
	memset(tc, 0, 20*sizeof(int));
	ina = getTernary(a, RADIX, ta);
	inc = getTernary(c, RADIX, tc);
	if(ina >= inc)
	{
		index = ina;
	}
	else
	{
		index = inc;
	}

	for(i = 0; i <= index; ++i)
	{
		if(tc[i] >= ta[i])
			tb[i] = tc[i] - ta[i];
		else
			tb[i] = tc[i] + RADIX - ta[i];
	}
	b = 0;
	for(i = 0; i <= index; ++i)
	{
		b = b + tb[i] * pow((double)RADIX, i);
	}
	printf("%d\n", b);
	return 0;
}

#include<stdio.h>
long long int convert(long long int d[],long long int a)
{
long long int x=0;
while(a>0)
{
d[x++]=a%3;
a=(a-(a%3))/3;
}
return(x);
}
add(long long int d[],long long int i,long long int j)
{
    long long int x;
for(x=i;x<j;x++)
d[x]=0;
return(j);
}

main()
{
    long long int a,c,t=1,x,i,j;
    scanf("%I64d%I64d",&a,&c);
    long long int d[100],e[100],f[100],g[3][3]={{0,1,2},{2,0,1},{1,2,0}};
    i=convert(d,a);
    j=convert(e,c);
    if(i>j)
    j=add(e,j,i);
    else if(j>i)
    i=add(d,i,j);
    for(x=0;x<j;x++)
    f[x]=g[d[x]][e[x]];
    a=0;
    for(x=0;x<j;x++,t=t*3)
    a=a+f[x]*t;
    printf("%I64d",a);
    return(0);
}

#include<stdio.h>
int main()
{
    long a,c,ans=0,pow=1;
    scanf("%ld%ld",&a,&c);
    short int i,j;
    while(a||c)
    {
        ans+=pow*((3+c%3-a%3)%3);
        pow*=3;
        a/=3;
        c/=3;
    }
    printf("%ld",ans);
    return 0;
}

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

int main()
{
    int a,c;
    int b;
    int temp1,temp2;
    int mnoznik;

    scanf("%d %d",&a,&c);
    b = 0;
    mnoznik = 1;
    while((a != 0) || (c != 0))
    {
        temp1 = a % 3;
        temp2 = c % 3;
        a = a / 3;
        c = c / 3;
        if(temp2 >= temp1)
            b += mnoznik * (temp2-temp1);
        else
            b += mnoznik * (3-(temp1-temp2));
        mnoznik *= 3;
    }

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

#include <stdio.h>

typedef long long Long;

int main() {
    int a,c,i;
    scanf("%d%d",&a,&c);
    int arr[100] = {0}; int cnt = 0;
    while (a > 0 || c > 0) {
        int ta = a % 3;
        int tc = c % 3;
        a /= 3;
        c /= 3;
        arr[cnt ++] = (tc - ta + 9) % 3;
    }
    Long ans = 0;
    for (i = cnt - 1; i > -1; i--) {
        ans = ans * 3 + arr[i];
    }
    printf("%lld\n",ans);
    return 0;
}

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)

int main(){
  int a, b, c, pw, add;

  scanf("%d%d",&a,&c);
  b = 0; pw = 1;

  while(a || c){
    add = ((c%3) - (a%3) + 3)%3;
    b += add * pw;
    pw *= 3;
    a /= 3; c /= 3;
  }

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

  return 0;
}

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

int main()
{
    long a,c,ans=0,i=0;
    scanf("%ld%ld",&a,&c);
    while(a!=0 || c!=0)
    {
         ans+=(c%3-a%3+3)%3*pow(3,i);
         a/=3;
         c/=3;
         i++;
    }
    printf("%ld\n",ans);
    return 0;
}

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

#define fp(i,a,b,c)  for(i=a;i<b;i+=c)
#define fn(i,a,b,c)  for(i=a;i>b;i-=c)
#define fs(i,a)      for(i=0;i<a;i++)
#define sci(a)       scanf("%d",&a)
#define sci2(a,b)       scanf("%d%d",&a,&b)
#define pf(a)         printf("%d ",a)
#define pf2(a,b)         printf("%d %d",a,b)

int cnvrttrnr(int a,int s[])
{
    int i;
    int a1[100000];
    int k=0;
    a1[0]=0;
while(a!=0){
    a1[k]=a%3;
    a=a/3;
    k++;

}
fs(i,k)
{
    s[i]=a1[i];
}
return k;

}
int powr3(int l)
{
    int i;
    int ans=1;
    fs(i,l)
    ans*=3;

    return ans;
}
int main()
{
  int i,j,k,l,a,b,c;
  sci2(a,c);
  int s1[100000],s2[10000],s3[10000];
  int l1=cnvrttrnr(a,s1);
  int l2=cnvrttrnr(c,s2);

  /*fs(i,l1)
  pf(s1[i]);
   fs(i,l2)
  pf(s2[i]);*/
  if(l1>l2)
  {
      fp(i,l2,l1,1)
      s2[i]=0;
  }else
  {
      fp(i,l1,l2,1)
      s1[i]=0;
  }

    if(l1>l2)
    {fs(i,l1)
    {
        fs(j,3)
        {if(((s1[i]+j)%3==s2[i]))
        {
          s3[i]=j;
        }
        }
    }}
    else
    {
        fs(i,l2)
    {
        fs(j,3)
        {if(((s1[i]+j)%3==s2[i]))
        {
          s3[i]=j;
          break;
        }
        }
    }
    }

    int ans=0;
    if(l1>l2)
    fs(i,l1)
    {
        int p=powr3(i);
       ans+=s3[i]*p;
    }
    else
    fs(i,l2)
    {
      int p=powr3(i);
       ans+=s3[i]*p;
    }

pf(ans);

  return 0;
}

#include<stdio.h>

int f(int *s,int x)
{
    int i=0;
    while(x)
    {
       s[i++]=x%3;
       x/=3;
    }
    return i;
}
int main()
{
    int a[100],c[100],aa,cc,i,j,num;
    scanf("%d %d",&aa,&cc);
    i=f(a,aa);
    j=f(c,cc);
    while(i<j)
    a[i++]=0;
    while(j<i)
    c[j++]=0;
    num=0;
    while((--i)>=0)
    {
       num*=3;
       num+=(c[i]-a[i]+3)%3;
    }
    printf("%d\n",num);
    //system("pause");
    return 0;
}

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

int a,c,t,b,i;

int main(void) {
	scanf("%d %d", &a, &c);
	while (a||c) {
		t*=3;
		t+=(3+c%3-a%3)%3;
		a/=3;
		c/=3;
		++i;
	}
	while (i--) {
		b*=3;
		b+=t%3;
		t/=3;
	}
	printf("%d\n", b);
	exit(EXIT_SUCCESS);
}

#include <stdio.h>
#include <math.h>
int main (void)
{
	unsigned int a, b = 0, c, i;
	scanf ("%u%u", &a, &c);
	for (i = 0; a != 0 || c != 0; i++)
	{
		b += (c % 3 - a % 3 + 3) % 3 * pow (3, i);
		a /= 3;
		c /= 3;
	}
	printf ("%u\n", b);
	return 0;
}

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

int *na3(long int x){
    int *wynik=malloc(30*sizeof(int));
    int i;

    for (i=0; i<30; ++i){
        wynik[i]=x%3;
        x=x/3;
    }
    return wynik;
}

int main()
{
    int *a3, *b3, *c3;
    long int a,b,c;
    int i;

    b3=malloc(30*sizeof(int));
    scanf("%ld %ld", &a,&c);
    a3=na3(a);
    c3=na3(c);
    for (i=0; i<30; ++i){
        if (c3[i]>=a3[i]){
            b3[i]=c3[i]-a3[i];
        } else{
            b3[i]=c3[i]+3-a3[i];
        }
    }
    b=0;

    for (i=29; i>=0; --i){
        b=b*3+b3[i];
    }
    printf("%ld", b);
    return 0;
}

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

void change_base (int a, int arr[], int base) {
    int orig=a;
    int i,pow=1;
    int max = floor(log(orig)/log(base));
//    printf ("max=%d\n",max);
    for (i=0;i<max+1;i++) {
//        printf("a=%d pow=%d\n",a,pow);
        arr[i]=a%3;
        a/=3;
        pow*=3;
    }
}

void print_array (int a[], int len) {
    int i;
    for (i=0;i<len;i++) {
        printf ("%d ",a[i]);
    }
    printf ("\n");
}

int main (void) {

    int a,c;
    scanf ("%d %d",&a,&c);

    int a3[20]={0};
    change_base(a,a3,3);
    int c3[20]={0};
    change_base(c,c3,3);

    int b=0;
    int b3[19]={0};
    int i;
    for (i=0;i<19;i++) {
        b3[i]=(c3[i]-a3[i]+3)%3; // c mods negatives weirdly
        b+=pow(3,i)*b3[i];
    }
//    print_array (a3,20);
//    print_array (c3,20);
//    print_array (b3,20);
    printf ("%d\n",b);
    return 0;
}

/* Author: Abhay
   Problem: 136B Codeforces
   Language: C
 */

#include <stdio.h>

int ternary (int x, int result[])
{
    int i = 0;

    while (x)
    {
        result[i++] = x % 3;
        x /= 3;
    }

    return i;
}

int main ()
{
    int a, b, c, len_a, len_c, i, mul;
    int ternary_a[100], ternary_b[100], ternary_c[100];

    scanf ("%d %d", &a, &c);

    len_a = ternary (a, ternary_a);
    len_c = ternary (c, ternary_c);

    while (len_a < len_c)
        ternary_a[len_a++] = 0;

    while (len_c < len_a)
        ternary_c[len_c++] = 0;

    for (i=0; i<len_a; i++)
    {
        if (ternary_c[i] == 0)
        {
            if (!ternary_a[i])
                ternary_b[i] = 0;
            else
                ternary_b[i] = 3 - ternary_a[i];
        }
        else if (ternary_c[i] == 1)
        {
            if (ternary_a[i] == 2)
                ternary_b[i] = 2;
            else
                ternary_b[i] = 1 - ternary_a[i];
        }
        else
            ternary_b[i] = 2 - ternary_a[i];
    }

    while (len_a < len_c)
    {
        ternary_b[len_a] = ternary_c[len_a];
        len_a++;
    }

    b = 0;
    mul = 1;

    for (i=0; i<len_c; i++)
    {
        b += (ternary_b[i] * mul);
        mul *= 3;
    }

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

    return 0;
}

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

long long int trinary(long long int n){
	long long int cnt=1,s=0,l;
	while(n!=0){
		l=n%3;
		s=cnt*l+s;
		cnt=cnt*10;
		n=n/3;
	}
	return s;
}
long long int tri(long long int n)
{
	long long int o,r,f,l=0,s=0,i;
	while (n!=0){
		r=n%10;
		if (r!=0){
			f=1;
			for (i=1;i<=l;i++){
				f=f*3;
			}
			o=f*r;
			s=s+o;
		}
		l=l+1;

		n=n/10;
	}
	return s;
}
int main()
{
	long long int a,b,r,s,t,e,f,l,cnt,w,q;
	scanf("%lld %lld",&a,&b);
	r=trinary(a);
	t=trinary(b);
	if (t>=r){
		cnt=1;
		s=0;
		while(t!=0){
			f=t%10;
			e=r%10;
			q=(f-e);
			if (q>=0){
				l=q%3;
			}
			else{
				l=3-(abs(q)%3);
			}
			s=s+l*cnt;
			cnt=cnt*10;
			t=t/10;
			r=r/10;

		}
	}
	else{
		cnt=1;
		s=0;
		while(r!=0){
			f=t%10;
			e=r%10;
			q=(f-e);
			if (q>=0){
				l=q%3;
			}
			else{
				l=3-(abs(q)%3);
			}
			s=s+l*cnt;
			cnt=cnt*10;
			t=t/10;
			r=r/10;

		}
	}
	w=tri(s);
	//w=pow(3,2);
	printf("%lld",w);
	return 0;
}

#include<stdio.h>
int a1[50],b1[50];
int main(){
	int a,b=0,c,i,ta,ta2;
	int ma,p=1,temp,temp2;
	scanf("%d %d",&a,&c);
		for(i=0;i>=0;i++){
		if(a>=1){
		a1[i]=a%3;
		a=a/3;
		}
		else {
			ta=i;
			break;
		}
		}
		for(i=0;i>=0;i++){
		if(c>=1){
		b1[i]=c%3;
		c=c/3;
		}
		else {
		ta2=i;
		break;
		}
		}
		if(ta>ta2){
		ma=ta;
		}
		else {
		ma=ta2;
		}
		/*for(i=0;i<ma;i++){
		printf("%d\n",b1[i]);
		}*/
		for(i=0;i<ma;i++){
		temp=a1[i]*2;
		temp2=b1[i];
		temp=temp+temp2;
		temp=temp%3;
		b=b+(temp*p);
		p=p*3;
		temp=0;
		temp2=0;
		}
		printf("%d\n",b);
		return 0;
}

#include <stdio.h>
#include <math.h>
long int arr[100],brr[100],crr[100],i=0,j=0,k=0;
void func(long int a)
{
	while(a>0)
	{
			arr[i]=a%3;
		//	printf("%d\n",a%3);
			i++;
			a=a/3;
	}
	i--;
}
void func1(long int a)
{
	while(a>0)
	{
			crr[j]=a%3;
		//	printf("%d\n",a%3);
			j++;
			a=a/3;
	}
	j--;
}
int main()
{
		long int a,c,b,l;
		scanf("%ld %ld",&a,&c);
		func(a);
		func1(c);
		if(i<j)
		{
				for(k=i+1;k<=j;k++)
				arr[k]=0;
				i=j;
		}
		else if(j<i)
		{
			for(k=j+1;k<=i;k++)
			crr[k]=0;
			j=i;
		}
		k=0;
//		for(k=0;k<=i;k++)
//		{
//			printf("%d ",arr[k]);
//		}
//		printf("\n");
//		for(k=0;k<=j;k++)
//		{
//			printf("%d ",crr[k]);
//		}
//		printf("\n");

		for(l=0;l<=i;l++)
		{
			if(crr[l]-arr[l]>=0)
			{
			brr[l]=crr[l]-arr[l];
		//	k++;
			}
			else
			{
				brr[l]=3+crr[l]-arr[l];
			}
		}
		long int ans=0;
		for(l=i;l>=0;l--)
		{
			ans=3*ans+brr[l];
//			printf("%d ",brr[l]);
		}
//		printf("\n");
		printf("%ld\n",ans);
		return 0;
}

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
//||
//\n
int cmpfunc (const void * a, const void * b)
{
   return ( *(int*)a - *(int*)b );
}
int main()
{
             long long int a,b,c,d=-1,e,i,j=1,k,sum=0,sum1=0,ct=0;
             long long int x[100]={0},y[100]={0};
             scanf("%lld %lld",&a,&b);
             c=a;
             d=b;
             while(c!=0)
             {
                          x[sum]=(c%3);
                          c=c/3;
                          sum++;
             }
             while(d!=0)
             {
                          y[sum1]=(d%3);
                          d=d/3;
                          sum1++;
             }
             e=sum>=sum1?sum:sum1;

             for(i=0;i<e;i++)
             {
                          if(x[i]>y[i])
                                       ct=ct+(y[i]-x[i]+3)*j;
                          else
                                       ct=ct+(y[i]-x[i])*j;
                          j=j*3;

             }
             printf("%lld",ct);
             return 0;}

#include        <stdio.h>
int*            mtob(int a)
{
    int         *result;
    result = (int*)malloc(10000*sizeof(int));
    int         i,count;
    i  = a;
    count = 0;
    while (i!=0)
    {
        count++;
        result[count] = i % 3;
        i = i/3;
    }
    result[0] = count;
    return result;
}
int             btom(int *a,int n)
{
    int         i,sum,k;
    sum = 0;
    k = 1;
    for (i=n;i>=1;i--)
    {
        sum+=k*a[i];
        k = 3*k;
    }
    return sum;
}
int             main()
{
    int         a,b,c;
    int         *aa,*cc;
    int         bb[10000],dd[10000];
    int         i,max;

    scanf("%d%d",&a,&c);
    aa = mtob(a);

    cc = mtob(c);
    if (aa[0]>cc[0]) max = aa[0] ;
    else max = cc[0];
    for (i=1;i<=max;i++){
        if (aa[i]<=cc[i]) bb[i] = cc[i]-aa[i];
        if (aa[i]>cc[i])  bb[i] = cc[i]+3-aa[i];
    }

    for (i=1;i<=max;i++)
        dd[i]=bb[max-i+1];
    b = btom(dd,max);
    printf("%d",b);

    return 0;

}

#include <stdio.h>
#include <math.h>
int main()
{
    long int a,c,d,d1,b=0;
    int at[19] = {0},ct[19] = {0},i;
    scanf("%ld %ld",&a,&c);
    /*a to ternary no.*/
        d = ceil(log(a)/log(3));
        d1=d;
        for(i=d;i>=0;i--)
        {
            at[i] = a/(long int)pow(3,i);
            a = a%(long int)pow(3,i);
        }
    /*c to ternary no.*/
        d = ceil(log(c)/log(3));
        for(i=d;i>=0;i--)
        {
            ct[i] = c/(long int)pow(3,i);
            c = c%(long int)pow(3,i);
        }
    for(i = (d1>d)? d1:d;i>=0;i--)
    {
        ct[i] +=3;
        b = b + (abs((ct[i]-at[i])%3))*(long int)pow(3,i);
    }
    printf("%ld",b);
    return 0;
}

#include <stdio.h>
#include <math.h>
int main()
{
    long int a,c;
    long long int b=0;
    int at[19] = {0},ct[19] = {0},i,d,d1;
    scanf("%ld %ld",&a,&c);
    /*a to ternary no.*/
        d = ceil(log(a)/log(3));
        d1=d;
        for(i=d;i>=0;i--)
        {
            at[i] = a/(long int)pow(3,i);
            a = a%(long int)pow(3,i);
        }
    /*c to ternary no.*/
        d = ceil(log(c)/log(3));
        for(i=d;i>=0;i--)
        {
            ct[i] = c/(long int)pow(3,i);
            c = c%(long int)pow(3,i);
        }
    for(i = (d1>d)? d1:d;i>=0;i--)
    {
        ct[i] +=3;
        b = b + (abs((ct[i]-at[i])%3))*(long int)pow(3,i);
    }
    printf("%I64d",b);
    return 0;
}

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

#define RADIX 3

int getTernary(int a, int radix, int ta[])
{
	int j;
	int maxindex;
	int temp;

	memset(ta, 0, 20*sizeof(int));
	for(j = 0; pow((double)radix, j) <= a; ++j)
		;
	--j;
	maxindex = j;
	while(j >= 0)
	{
		temp = pow((double)radix, j);
		ta[j] = a / temp;
		a %= temp;
		--j;
	}
	return maxindex;
}

int main()
{
	int a,b,c;
	int ina, inc, index;
	int i;
	int ta[20];
	int tb[20];
	int tc[20];

	scanf("%d%d", &a, &c);
	ina = getTernary(a, RADIX, ta);
	inc = getTernary(c, RADIX, tc);
	if(ina >= inc)
	{
		index = ina;
	}
	else
	{
		index = inc;
	}
	memset(tb, 0, 20*sizeof(int));
	for(i = 0; i <= index; ++i)
	{
		if(tc[i] >= ta[i])
			tb[i] = tc[i] - ta[i];
		else
			tb[i] = tc[i] + RADIX - ta[i];
	}
	b = 0;
	for(i = 0; i <= index; ++i)
	{
		b = b + tb[i] * pow((double)RADIX, i);
	}
	printf("%d\n", b);
	return 0;
}

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

int main()
{
    long long a,c,ans=0,i=0;
    scanf("%lld%lld",&a,&c);
    while(a!=0 || c!=0)
    {
         ans+=(c%3-a%3+3)%3*pow(3,i);
         a/=3;
         c/=3;
         i++;
    }
    printf("%lld\n",ans);
    return 0;
}

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

int a1[100];
int  b1[100];
int main()
{
 		long long a,b;
 		scanf("%lld%lld",&a,&b);
 		int  i=0;
 		memset(a1,0,100);
 		memset(b1,0,100);
 		while(a && b)
 		{
			 a1[i]=(int)a%3;
			 b1[i]=(int)b%3;
			 a/=3;
			 b/=3;
			 i++;
    }
    if(a>0)
    {
		  while(a)
      {
		   a1[i]=(int)a%3;
			 b1[i]=0;
			 a/=3;
			 i++;
		  }
		 }
    else
    {
		  while(b)
      {
		   b1[i]=(int)b%3;
			 a1[i]=0;
			 b/=3;
			 i++;
		  }
		 }
		 i-=1;
		 int j=0;
		 long long temp=0;
		 int c[100];
		 for(j=0;j<=i;j++)
     {
	         c[i-j]=(b1[j]-a1[j]+3)%3;
		 }
		 for(j=0;j<=i;j++)
		     temp=temp*3+c[j];
		 printf("%lld\n",temp);
// 		system("pause");
 		return 0;
}

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

int main()
{
    long long a,c,ans=0,i=0;
    scanf("%lld%lld",&a,&c);
    while(a!=0 && c!=0)
    {
         ans+=(c%3-a%3+3)%3*pow(3,i);
         a/=3;
         c/=3;
         i++;
    }
    if(a==0)
    {
        while(c!=0)
        {
              ans +=c%3 *pow(3,i);
              c/=3;
              i++;
        }
    }
    if(c==0)
    {
        while(a!=0)
        {
              ans+=(3-a%3)*pow(3,i);
              a/=3;
              i++;
        }
    }
    printf("%lld\n",ans);
    return 0;
}

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

int main()
{
    long long a,c,ans=0,i=0;
    scanf("%lld%lld",&a,&c);
    while(a!=0 && c!=0)
    {
         ans+=(c%3-a%3+3)%3*(long long)pow(3,i);
         a/=3;
         c/=3;
         i++;
    }
    if(a==0)
    {
        while(c!=0)
        {
              ans +=c%3 *(long long)pow(3,i);
              c/=3;
              i++;
        }
    }
    if(c==0)
    {
        while(a!=0)
        {
              ans+=(3-a%3)*(long long)pow(3,i);
              a/=3;
              i++;
        }
    }
    printf("%lld\n",ans);
    return 0;
}

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

int a,c,t,b;

int main(void) {
	scanf("%d %d", &a, &c);
	while (a||c) {
		t*=3;
		t+=(3+c%3-a%3)%3;
		a/=3;
		c/=3;
	}
	while (t) {
		b*=3;
		b+=t%3;
		t/=3;
	}
	printf("%d\n", b);
	exit(EXIT_SUCCESS);
}

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

int a,c,t,x;

int main(void) {
	scanf("%d %d", &a, &c);
	while (a||c) {
		t*=3;
		t+=(3+c-a%3)%3;
		a/=3; c/=3;
	}
	while (t) {
		x*=3;
		x+=t%3;
		t/=3;
	}
	printf("%d\n", x);
	exit(EXIT_SUCCESS);
}

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

void change_base (int a, int arr[], int base) {
    int orig=a;
    int i,pow=1;
    int max = floor(log(orig)/log(base));
    printf ("max=%d\n",max);
    for (i=0;i<max+1;i++) {
        printf("a=%d pow=%d\n",a,pow);
        arr[i]=a%3;
        a/=3;
        pow*=3;
    }
}

void print_array (int a[], int len) {
    int i;
    for (i=0;i<len;i++) {
        printf ("%d ",a[i]);
    }
    printf ("\n");
}

int main (void) {

    int a,c;
    scanf ("%d %d",&a,&c);

    int a3[20]={0};
    change_base(a,a3,3);
    int c3[20]={0};
    change_base(c,c3,3);

    int b=0;
    int b3[19]={0};
    int i;
    for (i=0;i<19;i++) {
        b3[i]=(c3[i]-a3[i]+3)%3; // c mods negatives weirdly
        b+=pow(3,i)*b3[i];
    }
    print_array (a3,20);
    print_array (c3,20);
    print_array (b3,20);
    printf ("%d\n",b);
    return 0;
}

#include<stdio.h>
long long int dk(long long int m,long long int n[]);
long long int main()
{
long long int a,b,c,d=0,i,e=0,k=0,g=0;
long long int p[10000]={0},q[10000],f[10000];
scanf("%I64d%I64d",&a,&c);
d=dk(a,p);
e=dk(c,q);
if(d==e)
{
next:
for(i=0;i<d;i++)
{
k=0;
while(((p[i]+k)%3)!=q[i])
{
k++;
}
f[i]=k;
}
}
else
{
if(d>e)
{
while(d!=e)
{
q[e]=0;
e++;
}
goto next;}
else
{
while(d!=e)
{
p[d]=0;
d++;
}
goto next;}
}
for(i=0;i<d;i++)
{
g=g+f[i]*pow(3,i);
}
printf("%I64d",g);
return(0);
}
long long int dk(long long int m,long long int n[])
{
    int x=0;
while(m>0)
{
n[x++]=m%3;
m=(m-(m%3))/30;
}
return(x);
}