using System;
using System.Collections.Generic;
using System.Linq;
public class Codeforces
{
    static void Main()
    {
        var _ = Console.ReadLine().Split().Select(int.Parse).ToArray();
        var a = _[0];
        var c = _[1];
        var a3 = new Stack<int>();
        var a3ctr = 0;
        var b3 = new Stack<int>();
        var c3 = new Stack<int>();
        var c3ctr = 0;
        while (a > 0)
        {
            var rem = a % 3;
            a /= 3;
            a3.Push(rem);
            a3ctr++;
        }
        while (c > 0)
        {
            var rem = c % 3;
            c /= 3;
            c3.Push(rem);
            c3ctr++;
        }
        if (a3ctr > c3ctr)
        {
            for (int i = 0; i < a3ctr - c3ctr; i++)
            {
                c3.Push(0);
            }
            c3ctr = a3ctr;
        }
        else if (c3ctr > a3ctr)
        {
            for (int i = 0; i < c3ctr - a3ctr; i++)
            {
                a3.Push(0);
            }
            a3ctr = c3ctr;
        }
        long res = 0L;
        for (int i = 0; i < a3ctr; i++)
        {
            var ter = new[] { 0, 1, 2 };
            var m = c3.Pop();
            var n = a3.Pop();
            while (n-- != 0)
            {
                m--;
                if (m < 0) m = 2;
            }
            b3.Push(m);
            res += m * (long)Math.Pow(3, (a3ctr - 1 - i));
        }
        Console.WriteLine(res);
    }
}

using System;
using System.Collections.Generic;
using System.Text;

class PairVariable : IComparable<PairVariable>
{
    public int a, b;
    public PairVariable()
    {

    }

    public PairVariable(string s)
    {
        string[] q = s.Split();
        a = int.Parse(q[0]);
        b = int.Parse(q[1]);
    }
    public PairVariable(int a, int b)
    {
        this.a = a;
        this.b = b;
    }
    public int CompareTo(PairVariable pv)
    {
        if (this.a > pv.a)
        {
            return 1;
        }
        else if (this.a < pv.a)
        {
            return -1;
        }
        else
        {
            if (this.b >= pv.b)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
    }

}
namespace ConsoleApplication1
{
    class Program
    {

        static void Main(string[] args)
        {
            string[] ss = Console.ReadLine().Split();
            int a = int.Parse(ss[0]);
            int c = int.Parse(ss[1]);
            string a3 = "";
            string c3 = "";
            while (a != 0)
            {
                a3 += a % 3;
                a /= 3;
            }
            while (c != 0)
            {
                c3 += c % 3;
                c /= 3;
            }
            if (c3.Length > a3.Length)
            {
                int k = c3.Length - a3.Length;
                for (int i = 1; i <= k; i++)
                {
                    a3 += 0;
                }
            }
            else
            {

                int k = a3.Length - c3.Length;
                for (int i = 1; i <= k; i++)
                {
                    c3 += 0;
                }
            }
            string x = "";
            string y = "";
            for (int i = a3.Length - 1; i >= 0; i--)
            {
                x += a3[i];
                y += c3[i];
            }
            string ans = "";
            for (int i = 0; i < x.Length; i++)
            {
                int t1 = int.Parse(x[i].ToString());
                int t2 = int.Parse(y[i].ToString());
                if (t2 == 0)
                {
                    if (t1 == 0)
                    {
                        ans += 0;
                    }
                    if (t1 == 1)
                    {
                        ans += 2;
                    }
                    if (t1 == 2)
                    {
                        ans += 1;
                    }
                }
                else if (t2 == 1)
                {
                    if (t1 == 0)
                    {
                        ans += 1;
                    }
                    if (t1 == 1)
                    {
                        ans += 0;
                    }
                    if (t1 == 2)
                    {
                        ans += 2;
                    }

                }
                else if (t2 == 2)
                {
                    if (t1 == 0)
                    {
                        ans += 2;
                    }
                    if (t1 == 1)
                    {
                        ans += 1;
                    }
                    if (t1 == 2)
                    {
                        ans += 0;
                    }

                }

            }
            int o = 0;
            for (int i = ans.Length - 1; i >= 0; i--)
            {
                int t = int.Parse(ans[i].ToString());
                o += (t * (int)(Math.Pow(3,ans.Length - i - 1)));
            }
            Console.WriteLine(o);
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
public class Codeforces
{
    static void Main()
    {
        var _ = Console.ReadLine().Split().Select(int.Parse).ToArray();
        var a = _[0];
        var c = _[1];
        var a3 = new Stack<int>();
        var a3ctr = 0;
        var c3 = new Stack<int>();
        var c3ctr = 0;
        while (a > 0)
        {
            var rem = a % 3;
            a /= 3;
            a3.Push(rem);
            a3ctr++;
        }
        while (c > 0)
        {
            var rem = c % 3;
            c /= 3;
            c3.Push(rem);
            c3ctr++;
        }
        if (a3ctr > c3ctr)
        {
            for (int i = 0; i < a3ctr - c3ctr; i++)
            {
                c3.Push(0);
            }
            c3ctr = a3ctr;
        }
        else if (c3ctr > a3ctr)
        {
            for (int i = 0; i < c3ctr - a3ctr; i++)
            {
                a3.Push(0);
            }
            a3ctr = c3ctr;
        }
        long res = 0L;
        for (int i = 0; i < a3ctr; i++)
        {
            var m = c3.Pop();
            var n = a3.Pop();
            while (n-- != 0)
            {
                m--;
                if (m < 0) m = 2;
            }
            res += m * (long)Math.Pow(3, (a3ctr - 1 - i));
        }
        Console.WriteLine(res);
    }
}

using System;
using System.Text;

namespace AllProblems
{
    class _136B_TernaryLogic
    {
        public static void Run()
        {
            var w = Console.ReadLine().Trim().Split(' ');
            var a = int.Parse(w[0]);
            var c = int.Parse(w[1]);

            var sc = ToTernary(c);
            var sa = ToTernary(a);

            var m = Math.Max(sa.Length, sc.Length);
            sc = sc.PadLeft(m, '0');
            sa = sa.PadLeft(m, '0');

            var diff = "";
            for (var i = 0; i < m; i++)
            {
                var s = (sc[i] - sa[i] + 3)%3;
                diff += s;
            }

            var ans = FromTernary(diff, m);
            Console.WriteLine(ans);
            Console.ReadLine();
        }

        static string ToTernary(int x)
        {
            var s = new StringBuilder();

            while (x > 0)
            {
                s.Insert(0, x%3);
                x /= 3;
            }

            return s.ToString();
        }

        static int FromTernary(string s, int len)
        {
            var x = 0;

            for (var i = 0; i < len; i++)
            {
                x = x * 3 + (s[i] - 48);
            }

            return x;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            _136B_TernaryLogic.Run();

        }
    }
}

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

namespace MakeCalendar
{
    public static class MainClass
    {

        static int ReadIntLine()
        {
            return int.Parse(Console.ReadLine());
        }

        static void ReadInt(ref int a)
        {
            a = ReadIntArrayLine()[0];
        }

        static void ReadInts(ref int a, ref int b)
        {
            var x = ReadIntArrayLine();
            a = x[0]; b = x[1];
        }

        static void ReadInts(ref int a, ref int b, ref int c)
        {
            var x = ReadIntArrayLine();
            a = x[0]; b = x[1]; c = x[2];
        }

        static void ReadInts(ref int a, ref int b, ref int c, ref int d)
        {
            var x = ReadIntArrayLine();
            a = x[0]; b = x[1]; c = x[2]; d = x[3];
        }

        static void ReadInts(ref int a, ref int b, ref int c, ref int d, ref int e)
        {
            var x = ReadIntArrayLine();
            a = x[0]; b = x[1]; c = x[2]; d = x[3]; e = x[4];
        }

        static void ReadInts(ref int a, ref int b, ref int c, ref int d, ref int e, ref int f)
        {
            var x = ReadIntArrayLine();
            a = x[0]; b = x[1]; c = x[2]; d = x[3]; e = x[4]; f = x[5];
        }

        static int[] ReadIntArrayLine()
        {
            char[] sep = { ' ' };
            return Console.ReadLine().Split(sep, StringSplitOptions.RemoveEmptyEntries).Select(x => int.Parse(x)).ToArray();
        }

        static void PrintLn(object obj)
        {
            Console.WriteLine(obj.ToString());
        }

        public static string Reverse(string s)
        {
            char[] charArray = s.ToCharArray();
            Array.Reverse(charArray);
            return new string(charArray);
        }

        public static void PrintLnCollection<T>(IEnumerable<T> col)
        {
            PrintLn(string.Join(" ", col));
        }

        public static void PrintLn(params object[] v)
        {
            PrintLnCollection(v);
        }

        public static string ReadLine()
        {
            return Console.ReadLine();
        }

        public static string MySubstring(this string str, int start, int finish)
        {
            if (start > finish) return "";
            return str.Substring(start, finish - start + 1);
        }

        public static bool IsInRange(this int a, int lb, int ub)
        {
            return (a >= lb) && (a <= ub);
        }

        private static void mergeDS(int i, int j, int[] a)
        {
            int t = a[j];
            for (int ii = 0; ii < a.Length; ii++)
            {
                if (a[ii] == t)
                    a[ii] = a[i];
            }
        }

        private static T MyMax<T>(params T[] a)
        {
            return a.Max();
        }

        private static T MyMin<T>(params T[] a)
        {
            return a.Min();
        }

        private static int[] GetPrimesLessThan(int x)
        {
            if (x < 2)
                return new int[0];

            int[] a = new int[x];

            a[0] = 1; a[1] = 1;

            for (int i = 0; i < a.Length; i++)
            {
                if (a[i] == 0)
                    for (int j = 2 * i; j < a.Length; j += i)
                    {
                        a[j] = 1;
                    }
            }

            var abc = new List<int>(x);

            for (int i = 0; i < a.Length; i++)
            {
                if (a[i] == 0)
                    abc.Add(i);
            }

            return abc.ToArray();
        }

        private static int FindNearestLEQElementInTheArray(int[] a, int startIndex, int endIndex, int target)
        {
            if (startIndex == endIndex)
            {
                if (a[startIndex] <= target)
                    return startIndex;
                else
                    return -1;
            }

            if (startIndex + 1 == endIndex)
            {
                if (a[endIndex] <= target)
                    return endIndex;
                else if (a[startIndex] <= target)
                    return startIndex;
                else
                    return -1;
            }

            int mid = (startIndex + endIndex) / 2;
            if (target == a[mid])
                return mid;
            else if (target < a[mid])
                return FindNearestLEQElementInTheArray(a, startIndex, mid - 1, target);
            else
                return FindNearestLEQElementInTheArray(a, mid, endIndex, target);
        }

        private static int FindNearestLEQElementInTheArray(int[] a, int target)
        {
            return FindNearestLEQElementInTheArray(a, 0, a.Length - 1, target);
        }

        private static int gcd(int a, int b)
        {
            int r = a % b;
            while (r != 0)
            {
                a = b;
                b = r;
                r = a % b;
            }
            return b;
        }

        //----------------------------------------------------------------------------

        static int[] toTernary(int x)
        {
            int[] ans = new int[20];

            int k = ans.Length-1;

            while (x > 0)
            {
                ans[k] = x % 3;
                x /= 3;
                k--;
            }

            return ans;
        }

        static void Main(string[] args)
        {
            int a = 0, c = 0;

            ReadInts(ref a, ref c);

            var at = toTernary(a);
            var ct = toTernary(c);

            for (int i = 0; i < ct.Length; i++)
            {
                ct[i] = (ct[i] - at[i]) % 3;

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

            decimal ans = 0;

            for (int i = 0; i < ct.Length; i++)
            {
                ans = 3 * ans + ct[i];
            }

            PrintLn(ans);
        }

    }

}

using System; using System.Linq;

class P {
  static void Main() {
    var s = Console.ReadLine().Split(' ').Select(long.Parse).ToArray();
    var a = s[0]; var n = s[1];
    var digits = Enumerable.Repeat(0,0).ToList();
    while (a > 0 || n > 0) {
      var d = (int)((n%3 + 3 - a%3) % 3);
      digits.Add(d);
      a /= 3; n /= 3;
    }
    var res = 0;
    for (var i = digits.Count - 1 ; i >= 0 ; i--) res = res*3+digits[i];
    Console.Write(res);
  }
}

using System;
class Test{
	static void Main(){
		string [] line = Console.ReadLine().Split();
		int a = int.Parse(line[0]);
		int b = int.Parse(line[1]);
		int sum = 0;
		int i = 1;
		while(a > 0 || b > 0){
			int mod = (b % 3) - (a % 3);
			if(mod < 0){
				mod += 3;
			}
			a /= 3;
			b /= 3;
			sum += mod * i;
			i *= 3;
		}
		Console.WriteLine(sum);
	}
}

using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace Codeforces
{
    internal class Program
    {
        private static string Read()
        {
            return Console.ReadLine();
        }
        private static string[] ReadArray()
        {
            return Console.ReadLine().Split(' ');
        }
        private static int ReadInt()
        {
            return Int32.Parse(Console.ReadLine());
        }
        private static long ReadLong()
        {
            return long.Parse(Console.ReadLine());
        }
        private static int ReadNextInt(string[] input, int index)
        {
            return Int32.Parse(input[index]);
        }

        private static void Main()
        {
            var input = ReadArray();
            var a = ReadNextInt(input, 0);
            var c = ReadNextInt(input, 1);
            var aStr = ConvertToString(a);
            var cStr = ConvertToString(c);
            var len = Math.Max(aStr.Length, cStr.Length);
            var bStr = new char[len];
            var index = len-1;
            var aLen = aStr.Length;
            var cLen = cStr.Length;
            var lastA = aLen - 1;
            var lastC = cLen - 1;
            while (index >=0)
            {
                if (lastA >= 0 && lastC >= 0)
                {
                    bStr[index] = GetLetter(aStr[lastA], cStr[lastC]);
                    lastA--;
                    lastC--;
                }
                else if (lastA >= 0)
                {
                    bStr[index] = GetLetter(aStr[lastA--], '0');
                }
                else
                {
                    bStr[index] = GetLetter('0', cStr[lastC--]);
                }
                index--;
            }
            var bS = new string(bStr);
            int res = ConvertToInt(bS);
            Console.WriteLine(res);
        }

        private static string ConvertToString(int n)
        {
            if (n == 0)
                return "0";

            var c = new StringBuilder();
            while (n > 0)
            {
                c.Append(n%3);
                n /= 3;
            }
            return new string(c.ToString().Reverse().ToArray());
        }

        private static int ConvertToInt(string bs)
        {
            int res = 0;
            for (int i = 0; i < bs.Length; i++)
            {
                res *= 3;
                res += bs[i] - '0';
            }
            return res;
        }

        private static char GetLetter(char a, char c)
        {

            int resInt = c - a;
            if (resInt < 0)
                resInt += 3;
            return resInt.ToString()[0];
        }
    }
}

using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;

namespace ConsoleApplication
{
    class Program
    {
        static void dop(ref string a, ref string b)
        {
            if (a.Length > b.Length)
            {
                int l = a.Length - b.Length;
                for (int i = 0; i < l; i++)
                {
                    b = '0' + b;
                }
                return;
            }
            else
            {
                int l = b.Length - a.Length;
                for (int i = 0; i < l; i++)
                {
                    a = '0' + a;
                }
                return;
            }
        }

        static string ToTrinary(Int64 Decimal)
        {
            Int64 BinaryHolder;
            char[] BinaryArray;
            string BinaryResult = "";

            while (Decimal > 0)
            {
                BinaryHolder = Decimal % 3;
                BinaryResult += BinaryHolder;
                Decimal = Decimal / 3;
            }

            BinaryArray = BinaryResult.ToCharArray();
            Array.Reverse(BinaryArray);
            BinaryResult = new string(BinaryArray);
            return BinaryResult;
        }

        static string add(string a, string b)
        {
            string str = "";
            for (int i = a.Length-1; i >= 0; i--)
            {
                int k = (int.Parse(a[i].ToString()) + int.Parse(b[i].ToString()))%3;
                str = (k).ToString() + str;
            }
            return str;
        }

        static string tobin(string str)
        {
            Int64 count = 0, result = 0;
            for (int i = str.Length - 1; i >= 0; --i)
            {
                int j = int.Parse(str[i].ToString());
                if (count != 0 || j != 0)
                {
                    result += (int)Math.Abs(j * Math.Pow(3.0, count));
                }
                count++;
            }

            return (result).ToString();
        }

        static string sub(string a, string b)
        {
            string str = "";
            for (int i = a.Length - 1; i >= 0; i--)
            {
                int k = (int.Parse(b[i].ToString()) - int.Parse(a[i].ToString()));
                if (k >= 0)
                    str = (k).ToString() + str;
                else
                    str = (k+3).ToString() + str;
            }
            return str;
        }

        static void Main(string[] args)
        {
            string[] n = Console.ReadLine().Split(' ');
            string a = ToTrinary(Int64.Parse(n[0]));
            string c = ToTrinary(Int64.Parse(n[1]));
            dop(ref a, ref c);
            string rez = sub(a, c);
            Console.WriteLine(tobin(rez));
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace B
{
    class Program
    {
        static int[] tor(int n)
        {
            int[] r = new int[50];
            int p = 49;
            while (n > 0)
            {
                r[p--] = n % 3;
                n /= 3;
            }
            return r;
        }

        static long untor(int[] t)
        {
            long m = 1;
            long r = 0;
            for (int i = 49; i >= 0; i--)
            {
                r += t[i] * m;
                m *= 3;
            }
            return r;
        }

        static void Main(string[] args)
        {
            string[] line = Console.ReadLine().Split();
            int a = int.Parse(line[0]);
            int c = int.Parse(line[1]);

            int[] _a = tor(a);
            int[] _c = tor(c);
            int[] _b = new int[50];

            for(int i = 0; i < 50; i++)
                _b[i] = (_c[i] + 3 - _a[i]) % 3;

            Console.WriteLine(untor(_b));
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] input = Console.ReadLine().Split(' ');
            long a = long.Parse(input[0]);
            long b = long.Parse(input[1]);

            string sa = "";
            string sb = "";

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

            while (b > 0)
            {
                sb = b % 3 + sb;
                b /= 3;
            }

            while (sa.Length < sb.Length) sa = "0" + sa;
            while (sb.Length < sa.Length) sb = "0" + sb;

            string ans = "";

            for (int i = 0; i < sa.Length; i++)
            {
                int d1 = int.Parse(sa[i].ToString());
                int d2 = int.Parse(sb[i].ToString());
                int ians = (d2 + 3 - d1) % 3;
                ans += ians;
            }

            long res = 0;

            for (int i = 0; i < ans.Length; i++)
            {
                res *= 3;
                res = res + int.Parse(ans[i].ToString());
            }

            Console.WriteLine(res);

        }
    }
}

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace CodeForces
{
    class Tokenizer
    {
        int pos;
        string[] tokens;
        TextReader reader;

        public Tokenizer(TextReader reader)
        {
            pos = 0;
            tokens = new string[] { };
            this.reader = reader;
        }

        public string NextToken()
        {
            if (pos == tokens.Length)
            {
                tokens = reader.ReadLine().Split(' ');
                pos = 0;
            }
            return tokens [pos++];
        }

        public int NextInt()
        {
            return int.Parse(NextToken());
        }
    }

    class MainClass
    {
        static Tokenizer t = new Tokenizer(Console.In);

        static string ToBase3(int n)
        {
            StringBuilder builder = new StringBuilder();
            while (n > 0)
            {
                builder.Append((char)(n % 3 + '0'));
                n /= 3;
            }
            return new string(builder.ToString().ToCharArray().Reverse().ToArray());
        }

        static int FromBase3(string s)
        {
            int ret = 0;
            foreach (char d in s)
            {
                ret = ret * 3 + (d - '0');
            }
            return ret;
        }

        public static void Main(string[] args)
        {
            //t = new Tokenizer(File.OpenText(@"../../in.txt"));
            //TextReader t = Console.In;
            //t = File.OpenText(@"../../in.txt");

            string a = ToBase3(t.NextInt());
            string c = ToBase3(t.NextInt());

            if (a.Length < c.Length)
                a = a.PadLeft(c.Length, '0');
            else
                c = c.PadLeft(a.Length, '0');

            StringBuilder builder = new StringBuilder(a.Length);
            for (int i = 0; i < a.Length; i++)
            {
                int dc = c [i] - '0';
                int da = a [i] - '0';
                int db = dc - da + 3;
                db %= 3;
                builder.Append((char)(db + '0'));
            }

            Console.WriteLine(FromBase3(builder.ToString()));
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace capp
{
    class Program
    {
        static void Main(string[] args)
        {
            StringBuilder sb = new StringBuilder();
            long[] data = Array.ConvertAll(Console.ReadLine().Split(' '), z => long.Parse(z));
            long a = data[0];
            long c = data[1];
            string s1 = ConvertToAnotherSystem(a, 3);
            string s2 = ConvertToAnotherSystem(c, 3);
            long m = Math.Max(s1.Length, s2.Length);
            if (s1.Length > s2.Length)
            {
                for (long i = 0; i < m - s2.Length; i++)
                    sb.Append("0");
                s2 = sb.ToString() + s2;
            }
            if (s2.Length > s1.Length)
            {
                for (long i = 0; i < m - s1.Length; i++)
                    sb.Append("0");
                s1 = sb.ToString() + s1;
            }
            sb = new StringBuilder();
            for (int i = 0; i < s1.Length; i++)
                sb.Append((3 - long.Parse(s1[i].ToString()) + long.Parse(s2[i].ToString())) % 3);
            Console.WriteLine(ConvertBack(sb.ToString()));

        }

        static string ConvertToAnotherSystem(long number, long system)
        {
            StringBuilder sb = new StringBuilder();
            while (number != 0)
            {
                sb.Append(number%system);
                number /= system;
            }
            return new string(sb.ToString().Reverse().ToArray());
        }
        static long ConvertBack(string s)
        {
            long res = 0;
            long K = 1;
            for (int i = s.Length - 1; i > -1; i--)
            {
                res += long.Parse(s[i].ToString()) * K;
                K *= 3;
            }
            return res;
        }

        static bool IsSimple(long b)
        {
            long c = (long)Math.Round(Math.Sqrt(b));
            for (long i = 2; i <= c; i++)
                if (b % i == 0)
                    return false;
            return true;
        }

    }

    public class Elem
    {
        public long Val1 { get; set; }
        public long Val2 { get; set; }
        public Elem (long val1, long val2)
        {
            Val1 = val1;
            Val2 = val2;
        }
    }

}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CodeForces
{
	class Ternary_Logic
	{
		class Ternary
		{
			public List<int> trigits = new List<int>();

			public Ternary()
			{
			}

			public Ternary(int n)
			{
				while (n > 0)
				{
					trigits.Add(n % 3);
					n /= 3;
				}
			}

			public Ternary InverseTor(Ternary t)
			{
				Ternary res = new Ternary();
				int i = 0;
				while (i < t.trigits.Count || i < this.trigits.Count)
				{
					int me = i < this.trigits.Count ? this.trigits[i] : 0;
					int you = i < t.trigits.Count ? t.trigits[i] : 0;
					res.trigits.Add((me - you + 3) % 3);
					i++;
				}
				return res;
			}

			public int ToInt32()
			{
				int res = 0;
				trigits.Reverse();
				foreach (int n in trigits)
				{
					res *= 3;
					res += n;
				}
				trigits.Reverse();
				return res;
			}

		}

		public static void Main(String[] args)
		{
			string[] token = Console.ReadLine().Split(' ');
			int a = int.Parse(token[0]);
			int c = int.Parse(token[1]);
			Console.WriteLine(new Ternary(c).InverseTor(new Ternary(a)).ToInt32());
		}
	}
}

#define Online

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Threading;

delegate double F(double x);
delegate decimal Fdec(decimal x);

class Program
{
    static void Main(string[] args)
    {

        Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

#if FileIO
        StreamReader sr = new StreamReader("army.in");
        StreamWriter sw = new StreamWriter("army.out");
        Console.SetIn(sr);
        Console.SetOut(sw);
#endif

        int a, c, b;
        string[] input = ReadArray();
        a = int.Parse(input[0]);
        c = int.Parse(input[1]);
        int p = 1;
        b = 0;
        do
        {

            int moda = a % 3;
            a /= 3;
            int modc = c % 3;
            c /= 3;
            for (int z = 0; z <= 2; z++)
                if ((moda + z) % 3 == modc)
                    b += z * p;
            p *= 3;
        }
        while (a != 0 || c != 0);
        Console.WriteLine(b);

#if Online
        Console.ReadLine();
#endif

#if FileIO
        sr.Close();
        sw.Close();
#endif
    }

    static string[] ReadArray()
    {
        return Console.ReadLine().Split(' ');
    }

    static int ReadInt()
    {
        return int.Parse(Console.ReadLine());
    }
}

static class ArrayUtils
{
    public static int BinarySearch(int[] a, int val)
    {
        int left = 0;
        int right = a.Length - 1;
        int mid;

        while (left < right)
        {
            mid = left + ((right - left) >> 1);
            if (val <= a[mid])
            {
                right = mid;
            }
            else
            {
                left = mid + 1;
            }
        }

        if (a[left] == val)
            return left;
        else
            return -1;
    }

    public static double Bisection(F f, double left, double right, double eps)
    {
        double mid;
        while (right - left > eps)
        {
            mid = left + ((right - left) / 2d);
            if (Math.Sign(f(mid)) != Math.Sign(f(left)))
                right = mid;
            else
                left = mid;
        }
        return (left + right) / 2d;
    }

    public static decimal TernarySearchDec(Fdec f, decimal eps, decimal l, decimal r, bool isMax)
    {
        decimal m1, m2;
        while (r - l > eps)
        {
            m1 = l + (r - l) / 3m;
            m2 = r - (r - l) / 3m;
            if (f(m1) < f(m2))
                if (isMax)
                    l = m1;
                else
                    r = m2;
            else
                if (isMax)
                    r = m2;
                else
                    l = m1;
        }
        return r;
    }

    public static double TernarySearch(F f, double eps, double l, double r, bool isMax)
    {
        double m1, m2;
        while (r - l > eps)
        {
            m1 = l + (r - l) / 3d;
            m2 = r - (r - l) / 3d;
            if (f(m1) < f(m2))
                if (isMax)
                    l = m1;
                else
                    r = m2;
            else
                if (isMax)
                    r = m2;
                else
                    l = m1;
        }
        return r;
    }

    public static void Merge(int[] A, int p, int q, int r, int[] L, int[] R)
    {
        for (int i = p; i <= q; i++)
            L[i] = A[i];
        for (int i = q + 1; i <= r; i++)
            R[i] = A[i];

        for (int k = p, i = p, j = q + 1; k <= r; k++)
        {
            if (i > q)
            {
                for (; k <= r; k++, j++)
                    A[k] = R[j];
                return;
            }
            if (j > r)
            {
                for (; k <= r; k++, i++)
                    A[k] = L[i];
                return;
            }
            if (L[i] <= R[j])
            {
                A[k] = L[i];
                i++;
            }
            else
            {
                A[k] = R[j];
                j++;
            }
        }
    }

    public static void Merge_Sort(int[] A, int p, int r, int[] L, int[] R)
    {
        if (!(p < r)) return;
        int q = p + ((r - p) >> 1);
        Merge_Sort(A, p, q, L, R);
        Merge_Sort(A, q + 1, r, L, R);
        Merge(A, p, q, r, L, R);
    }

    static void Shake(int[] a)
    {
        Random rnd = new Random(Environment.TickCount);
        int temp, b, c;
        for (int i = 0; i < a.Length; i++)
        {
            b = rnd.Next(0, a.Length);
            c = rnd.Next(0, a.Length);
            temp = a[b];
            a[b] = a[c];
            a[c] = temp;
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sth
{
    class Program
    {
        static int[] p1 = new int[50];
        static int[] res = new int[50];
        static void Main(string[] args)
        {
            int a, c;
            string[] lala = Console.ReadLine().Split();
            a = int.Parse(lala[0]);
            c = int.Parse(lala[1]);
            int len1=0;
            while (a > 0)
            {
                p1[len1++] = a % 3;
                a /= 3;
            }
            int len2 = 0;
            while (c > 0)
            {
                res[len2++] = c % 3;
                c /= 3;
            }
            int ans = 0;
            int step = 1;
            int ll = Math.Max(len1, len2);
            for (int i = 0; i < ll; i++)
            {
                int cif = res[i] - p1[i];
                cif += 3;
                cif %= 3;
                ans += cif * step;
                step *= 3;
            }
            Console.WriteLine(ans);
            //Console.ReadLine();

        }
    }
}

using System;
// you can also use other imports, for example:
using System.Collections.Generic;
using System.Linq;

// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");

//mcs HelloWorld.cs
//mono HelloWorld.exe
public class HelloWorld {

	public static void Main(string[] args)
	{
		var line  = Console.ReadLine().Split(' ');
		var n = Convert.ToInt32(line[0]);
		var k = Convert.ToInt32(line[1]);
		//var arr = Array.ConvertAll(Console.ReadLine().Split(' '), t=> Convert.ToInt32(t));
		solve(n, k);
		//tests();
	}

	public static void tests()
	{
		solve(14, 34);
		solve(50,34);
		solve(387420489,225159023);
		solve(5,5);

	}

	public static void solve(int n, int k)
	{
		var tn = getTernary(n);
		var tk = getTernary(k);
		if (tk.Length > tn.Length)
			tn = tn.PadLeft(tk.Length, '0');
		if (tn.Length > tk.Length)
			tk = tk.PadLeft(tn.Length, '0');

		//Console.WriteLine(tn + " "  + tk);
		var rest = new int[tn.Length];
		for (int i = 0; i < tn.Length; i++)
		{
			rest[i] = getTor((int)char.GetNumericValue(tn[i]), (int)char.GetNumericValue(tk[i]));
			//Console.Write(rest[i] + " ");
		}

		long res = 0;
		for (int j = 0, i = rest.Length - 1; j < rest.Length; j++, i--)
		{
			res += (long)Math.Pow(3,j) * rest[i]; //2 + 5
		}
		Console.WriteLine(res);

	}

	public static string getTernary(int n)
	{
		var tn = "";
		while (n != 0)
		{
			tn = (n % 3).ToString() + tn;
			n = n / 3;
		}
		return tn;
	}

	public static int getTor(int a, int b)
	{
		return (b + 3 - a) % 3;

	}
}

using System;
using System.Collections.Generic;

class Program
{
    public static void Main(string[] args)
    {
        string[] input=Console.ReadLine().Split();
        int a=int.Parse(input[0]);
        int b=int.Parse(input[1]);
        XORSumW(a,b);
        Console.ReadLine();
    }

    private static List<int> split(int a)
    {
        List<int> ret=new List<int>();
        while(a>0)
        {
            ret.Add(a%3);
            a/=3;
        }
        return ret;
    }

    private static void padW(List<int> a, List<int> b)
    {
        if(a.Count>b.Count)
        {
            int diff=a.Count-b.Count;
            for(int i=0;i<diff;i++)
            {
                b.Add(0);
            }
        }
        else
        {
            int diff=b.Count-a.Count;
            for(int i=0;i<diff;i++)
            {
                a.Add(0);
            }
        }

        List<int> c=new List<int>();

        for(int i=0;i<a.Count;i++)
        {
            int tmp=(b[i]-a[i]>=0)?b[i]-a[i]:3+b[i]-a[i];
            c.Add(tmp);
        }

        int multiplier=1;
        int num=0;
        foreach(int i in c)
        {
            num+=i*multiplier;
            multiplier*=3;
        }
        Console.WriteLine(num);
    }

    private static void XORSumW(int a, int b)
    {
        List<int> adig=split(a);
        List<int> bdig=split(b);
        padW(adig,bdig);
    }
}

using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Globalization;

namespace timusss
{
    class Program
    {
        static void Main(string[] args)
        {

            string[] tokens = Console.ReadLine().Split();
            int a = int.Parse(tokens[0]);
            int c = int.Parse(tokens[1]);

            List<int> tera = new List<int>();
            List<int> terc = new List<int>();

            for (int aa = a; aa > 0; aa /= 3)
                tera.Add(aa % 3);
            for (int cc = c; cc > 0; cc /= 3)
                terc.Add(cc % 3);

            List<int> res = new List<int>();
            for (int i = 0; i < terc.Count || i < tera.Count; ++i)
            {
                if (i < tera.Count)
                {
                    if (i < terc.Count)
                    {
                        if (tera[i] <= terc[i])
                            res.Add(terc[i] - tera[i]);
                        else
                        {

                            res.Add(3 - tera[i] + terc[i]);
                        }
                    }
                    else
                        res.Add((3 - tera[i]) % 3);
                }
                else
                    res.Add(terc[i]);

            }

            long  result = 0;
            long count = 1;
            int start = 0;

            for (int i = start; i < res.Count; ++i)
            {
                result += count * res[i];
                count *= 3;
            }
            Console.WriteLine(result);
            //Console.ReadLine();
        }
    }
}

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace tcoder
{
    class Program
    {
        static void Main(string[] args)
        {
#if ONLINE_JUDGE

#endif

            var ex = (Console.ReadLine()).Split(' ');
            var ss1 = long.Parse(ex[0]);
            var ss2 = long.Parse(ex[1]);

            string ret = "";

            var l1 = int_to_bignum(ss1, 3);
            var l2 = int_to_bignum(ss2, 3);

            if (l1.Length < l2.Length)
            {
                l1 = l1.Insert(l1.Length, new string('0', l2.Length - l1.Length));
            }
            if (l2.Length < l1.Length)
            {
                l2 = l2.Insert(l2.Length, new string('0', l1.Length - l2.Length));
            }
            var l3 = int_to_bignum(0, 3);

            long res = 0;
            for (int i = 0; i < l1.Length; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    if ((l2[i] - '0') == ((j) + (l1[i] - '0')) % 3)
                    {
                        ret += ((j) + (l1[i] - '0')) % 3;
                        res += (long)Math.Pow(3, i)*(j) ;
                        break;
                    }
                }
            }

            Console.Write(res);
        }

        static string int_to_bignum(long s, long n)
        {
            int i;
            long t;
            bool sig = false;
            if (s >= 0) sig = true;
            string ret = "";

            int lasdigit = -1;
            t = Math.Abs(s);
            while (t > 0)
            {
                lasdigit++;
                ret += (t % n).ToString();
                t /= n;

            }
            if (s == 0) lasdigit = 0;
            return (ret);

        }
    }
}

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication2
{
    class Program
    {
        static long n = 20, a=0,c=0, al=0, cl=0;
        static long[] a3 = new long[n], c3 = new long[n];

        static long Perform()
        {
            long result=0;
            while (a > 0)
            {
                a3[al++] = a % 3;
                a /= 3;
            }
            while (c > 0)
            {
                c3[cl++] = c % 3;
                c /= 3;
            }
            long st3 = 1;
            for (int i = 0; i < n; i++)
            {
                result+= st3*((2*a3[i]+c3[i])%3);
                st3 *= 3;
            }
            return result;
        }

        static void Main(string[] args)
        {
            string[] token = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            a = int.Parse(token[0]);
            c = int.Parse(token[1]);
            Console.WriteLine(Perform());
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CodeForce97
{
	class ProgramA
	{
		static void MainA(string[] args)
		{
			int n = int.Parse(Console.ReadLine());
			int pi;
			int[] p = new int[100];
			string s = "";

			string s1 = Console.ReadLine();
			string[] sa = s1.Split(' ');

			for (int i = 0; i<n; i++)
			{
				pi = int.Parse(sa[i])-1;
				p[pi] = i;
			}

			for (int i = 0; i<n; i++)
				s+=(p[i]+1)+" ";

			Console.WriteLine(s);
//			Console.Read();
		}
	}
	class Program
	{
		static String Trans(int dig, int bas)   //Переводит десятичное число в систему с другим основанием
		{
			String buf = "";                  //Хранение переменной
			while (dig != 0)
			{                //Пока число не 0
				int temp = dig % bas;         //Получить его остаток
				buf += temp;                  //Занести остаток в строку
				dig /= bas;                   //Само число поделить нацело
			}
			String buf2 = "";                 //Хранение конечного результата
			for (int i = buf.Length - 1; i >= 0; i--)
			{//Циклом перевернуть строку
				buf2 += buf[i];
			}
			return buf2;                   //Возвращаем полученное значение
		}

		static void Main(string[] args)
		{
			string s1 = Console.ReadLine();
			string[] sa = s1.Split(' ');

			int a = int.Parse(sa[0]);
			int c = int.Parse(sa[1]);

			string a3 = Trans(a, 3); // long
			string c3 = Trans(c, 3); // short
			string b3 = "";

			int l = Math.Max(a3.Length, c3.Length);

			while (a3.Length <l)
				a3 = "0" + a3;
			while (c3.Length < l)
				c3 = "0" + c3;

			for (int i = 0; i < l; i++)
			{
				for (int j = 0; j < 3; j++)
				{
					if ((int.Parse(a3[i].ToString()) + j)%3 == int.Parse(c3[i].ToString()))
					{
						b3 += j.ToString();
						break;
					}
				}
			}
			int bas = 1;
			int ansb = 0;
			for (int i = l - 1; i >= 0; i--)
			{
				ansb += int.Parse(b3[i].ToString()) * bas;
				bas *= 3;
			}

			Console.WriteLine(ansb);
//			Console.Read();
		}
	}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Numerics;

namespace ConsoleApplication2
{

    class Program
    {

        static void Main(string[] args)
        {

            string[] str = Console.ReadLine().Split(' ');
            long a = long.Parse(str[0]);
            long c = long.Parse(str[1]);

            if(a == 0 && c ==0)
            {
                Console.WriteLine(0);
                return;
            }

            int length = (int)Math.Max(Math.Ceiling(Math.Log(a, 3)), Math.Ceiling(Math.Log(c, 3)))+1;

            //Console.WriteLine(length);

            int[] astr = new int[length];
            for (int i = 0; i < length; i++ )
            {
                astr[i] = (int)((a / (int)Math.Pow(3, i)) % 3);
            }

            int[] cstr = new int[length];
            for (int i = 0; i < length; i++)
            {
                cstr[i] = (int)((c / (int)Math.Pow(3, i)) % 3);
            }

            int[] bstr = new int[length];
            for (int i = 0; i < length; i++)
            {
                if (astr[i] <= cstr[i])
                {
                    bstr[i] = cstr[i] - astr[i];
                }
                else
                {
                    bstr[i] = cstr[i] - astr[i] + 3;
                }
            }

            /*
            for (int i = 0; i < length; i++ )
            {
                Console.Write(astr[i]);
            }
            Console.WriteLine();

            for (int i = 0; i < length; i++)
            {
                Console.Write(bstr[i]);
            }
            Console.WriteLine();

            for (int i = 0; i < length; i++)
            {
                Console.Write(cstr[i]);
            }
            Console.WriteLine();
            */

            long res = 0;
            for (int i = 0; i < length; i++)
            {
                //Console.WriteLine(bstr[i] + "  " + (int)Math.Pow(3, i));
                res += bstr[i] * (long)Math.Pow(3, i);
            }
            Console.WriteLine(res);
        }

    }
}

using System;
using System.IO;
using System.Text;

namespace Ternary_Logic
{
    internal class Program
    {
        private static readonly StreamReader reader = new StreamReader(Console.OpenStandardInput(1024*10), Encoding.ASCII, false, 1024*10);
        private static readonly StreamWriter writer = new StreamWriter(Console.OpenStandardOutput(1024*10), Encoding.ASCII, 1024*10);

        public static string Int32ToString(int value, int toBase)
        {
            string result = string.Empty;
            do
            {
                result = "0123456789ABCDEF"[value % toBase] + result;
                value /= toBase;
            }
            while (value > 0);

            return result;
        }

        private static void Main(string[] args)
        {
            int a = Next();
            int c = Next();

            string sa = Int32ToString(a, 3);
            string sc = Int32ToString(c, 3);

            if (sa.Length < sc.Length)
            {
                sa = new string('0', sc.Length - sa.Length) + sa;
            }
            else
            {
                sc = new string('0', sa.Length - sc.Length) + sc;
            }

            var b = 0;

            for (int i = 0; i < sa.Length; i++)
            {
                int delta = (sc[i] - '0') - (sa[i] - '0');
                if (delta < 0)
                    delta += 3;

                b *= 3;
                b += delta;
            }

            writer.WriteLine(b);
            writer.Flush();
        }

        private static int Next()
        {
            int c;
            int res = 0;
            do
            {
                c = reader.Read();
                if (c == -1)
                    return res;
            } while (c < '0' || c > '9');
            res = c - '0';
            while (true)
            {
                c = reader.Read();
                if (c < '0' || c > '9')
                    return res;
                res *= 10;
                res += c - '0';
            }
        }
    }
}

using System; using System.Linq;

class P {
  static void Main() {
    var s = Console.ReadLine().Split(' ').Select(long.Parse).ToArray();
    var a = s[0]; var n = s[1];
    var digits = Enumerable.Repeat(0,0).ToList();
    while (a > 0 && n > 0) {
      var d = (int)((n%3 + 3 - a%3) % 3);
      digits.Add(d);
      a /= 3; n /= 3;
    }
    digits.Reverse();
    var res = digits.Aggregate(0, (acc,x) => acc*3+x);
    Console.Write(res);
  }
}

using System; using System.Linq;

class P {
  static void Main() {
    var s = Console.ReadLine().Split(' ').Select(long.Parse).ToArray();
    var a = s[0]; var n = s[1];
    var digits = Enumerable.Repeat(0,0).ToList();
    while (a > 0 && n > 0) {
      var d = (int)((n%3 + 3 - a%3) % 3);
      digits.Add(d);
      a /= 3; n /= 3;
    }
    var res = 0;
    for (var i = digits.Count - 1 ; i >= 0 ; i--) res = res*3+digits[i];
    Console.Write(res);
  }
}

using System;
using System.Collections.Generic;

class Program
{
    public static void Main(string[] args)
    {
        string[] input=Console.ReadLine().Split();
        int a=int.Parse(input[0]);
        int b=int.Parse(input[1]);
        XORSumW(a,b);
        Console.ReadLine();
    }

    private static List<int> split(int a)
    {
        List<int> ret=new List<int>();
        while(a>0)
        {
            ret.Add(a%3);
            a/=3;
        }
        return ret;
    }

    private static void padW(List<int> a, List<int> b)
    {
        if(a.Count>b.Count)
        {
            int diff=a.Count-b.Count;
            for(int i=0;i<diff;i++)
            {
                b.Add(0);
            }
        }
        else
        {
            int diff=b.Count-a.Count;
            for(int i=0;i<diff;i++)
            {
                a.Add(0);
            }
        }

        List<int> c=new List<int>();

        for(int i=0;i<a.Count;i++)
        {
            int tmp=(b[i]-a[i]>0)?b[i]-a[i]:3+b[i]-a[i];
            c.Add(tmp);
        }

        int multiplier=1;
        int num=0;
        foreach(int i in c)
        {
            num+=i*multiplier;
            multiplier*=3;
        }
        Console.WriteLine(num);
    }

    private static void XORSumW(int a, int b)
    {
        List<int> adig=split(a);
        List<int> bdig=split(b);
        padW(adig,bdig);
    }
}

using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Globalization;

namespace timusss
{
    class Program
    {
        static void Main(string[] args)
        {

            string[] tokens = Console.ReadLine().Split();
            int a = int.Parse(tokens[0]);
            int c = int.Parse(tokens[1]);

            List<int> tera = new List<int>();
            List<int> terc = new List<int>();

            for (int aa = a; aa > 0; aa /= 3)
                tera.Add(aa % 3);
            for (int cc = c; cc > 0; cc /= 3)
                terc.Add(cc % 3);

            List<int> res = new List<int>();
            for (int i = 0; i < terc.Count || i < tera.Count; ++i)
            {
                if (i < tera.Count)
                {
                    if (i < terc.Count)
                    {
                        if (tera[i] <= terc[i])
                            res.Add(terc[i] - tera[i]);
                        else
                            res.Add(3 - tera[i] + terc[i]);
                    }
                    else
                        res.Add(3 - tera[i]);
                }
                else
                    res.Add(terc[i]);

            }

            long  result = 0;
            long count = 1;
            int start = 0;
            while (start < res.Count && res[start] == 0)
                ++start;
            for (int i = start; i < res.Count; ++i)
            {
                result += count * res[i];
                count *= 3;
            }
            Console.WriteLine(result);
            //Console.ReadLine();
        }
    }
}

using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Globalization;

namespace timusss
{
    class Program
    {
        static void Main(string[] args)
        {

            string[] tokens = Console.ReadLine().Split();
            int a = int.Parse(tokens[0]);
            int c = int.Parse(tokens[1]);

            List<int> tera = new List<int>();
            List<int> terc = new List<int>();

            for (int aa = a; aa > 0; aa /= 3)
                tera.Add(aa % 3);
            for (int cc = c; cc > 0; cc /= 3)
                terc.Add(cc % 3);

            List<int> res = new List<int>();
            for (int i = 0; i < terc.Count || i < tera.Count; ++i)
            {
                if (i < tera.Count)
                {
                    if (i < terc.Count)
                    {
                        if (tera[i] <= terc[i])
                            res.Add(terc[i] - tera[i]);
                        else
                            res.Add(3 - tera[i] + terc[i]);
                    }
                    else
                        res.Add(3 - tera[i]);
                }
                else
                    res.Add(terc[i]);

            }

            int result = 0;
            int count = 1;
            for (int i = 0; i < res.Count; ++i)
            {
                result += count * res[i];
                count *= 3;
            }
            Console.WriteLine(result);
            //Console.ReadLine();
        }
    }
}

using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Globalization;

namespace timusss
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] tokens = Console.ReadLine().Split();
            int a = int.Parse(tokens[0]);
            int c = int.Parse(tokens[1]);

            List<int> tera = new List<int>();
            List<int> terc = new List<int>();

            for (int aa = a; aa > 0; aa /= 3)
                tera.Add(aa % 3);
            for (int cc = c; cc > 0; cc /= 3)
                terc.Add(cc % 3);

            List<int> res = new List<int>();
            for (int i = 0; i < terc.Count; ++i)
            {
                if (i < tera.Count)
                {

                    if (tera[i] <= terc[i])
                        res.Add(terc[i] - tera[i]);
                    else
                        res.Add(3 - tera[i] + terc[i]);
                }
                else
                    res.Add(terc[i]);

            }

            int result = 0;
            int count = 1;
            for (int i = 0; i < res.Count; ++i)
            {
                result += count * res[i];
                count *= 3;
            }
            Console.WriteLine(result);
            Console.ReadLine();
        }
    }
}

using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Globalization;

namespace timusss
{
    class Program
    {
        static void Main(string[] args)
        {

            string[] tokens = Console.ReadLine().Split();
            int a = int.Parse(tokens[0]);
            int c = int.Parse(tokens[1]);

            List<int> tera = new List<int>();
            List<int> terc = new List<int>();

            for (int aa = a; aa > 0; aa /= 3)
                tera.Add(aa % 3);
            for (int cc = c; cc > 0; cc /= 3)
                terc.Add(cc % 3);

            List<int> res = new List<int>();
            for (int i = 0; i < terc.Count || i < tera.Count; ++i)
            {
                if (i < tera.Count)
                {
                    if (i < terc.Count)
                    {
                        if (tera[i] <= terc[i])
                            res.Add(terc[i] - tera[i]);
                        else
                            res.Add(3 - tera[i] + terc[i]);
                    }
                    else
                        res.Add(3 - tera[i]);
                }
                else
                    res.Add(terc[i]);

            }

            long  result = 0;
            long count = 1;
            for (int i = 0; i < res.Count; ++i)
            {
                result += count * res[i];
                count *= 3;
            }
            Console.WriteLine(result);
            //Console.ReadLine();
        }
    }
}

using System; using System.Linq;

class P {
  static void Main() {
    var s = Console.ReadLine().Split(' ').Select(long.Parse).ToArray();
    var a = s[0]; var n = s[1];
    var digits = Enumerable.Repeat(0,0).ToList();
    while (a > 0 && n > 0) {
      var d = (int)((n%3 + 3 - a%3) % 3);
      digits.Add(d);
      a /= 3; n /= 3;
    }
    var res = digits.Aggregate(0, (acc,x) => acc*3+x);
    Console.Write(res);
  }
}