import java.io.*;
import java.util.*;
import java.math.*;

public class nopeter_aa {
	public static void main(String[] args) {
		new nopeter_aa().run();
	}

	BufferedReader br;
	StringTokenizer st;
	PrintWriter out;
	boolean eof = false;

	public void run() {
		Locale.setDefault(Locale.US);
		try {
			br = new BufferedReader(new FileReader(FNAME + ".in"));
			out = new PrintWriter(FNAME + ".out");
			solve();
			out.close();
		} catch (Throwable e) {
			e.printStackTrace();
			System.exit(566);
		}
	}

	String nextToken() {
		while (st == null || !st.hasMoreTokens()) {
			try {
				st = new StringTokenizer(br.readLine());
			} catch (Exception e) {
				eof = true;
				return "0";
			}
		}
		return st.nextToken();
	}

	int nextInt() {
		return Integer.parseInt(nextToken());
	}

	void myAssert(boolean u, String message) {
		if (!u) {
			throw new Error("Assertion failed!!! " + message);
		}
	}

	int inBounds(int x, int l, int r, String name) {
		myAssert(l <= x && x <= r, name + " is not in bounds: " + x
				+ " not in [" + l + ", " + r + "]");
		return x;
	}

	String FNAME = "nopeter";

	BigInteger three = BigInteger.valueOf(3);

	private void solve() throws IOException {
		int n = inBounds(nextInt(), 1, 1000, "n");
		BigInteger k = new BigInteger(nextToken());
		BigInteger[] ans = get(k, three.pow(n - 1));
		out.println(ans[0] + " " + ans[1]);
	}

	private BigInteger[] get(BigInteger k, BigInteger pow) {
		if (k.compareTo(three) <= 0) {
			return new BigInteger[] { BigInteger.ZERO, k };
		}
		int x = k.divide(pow).intValue();
		BigInteger[] a = null;
		BigInteger pow2 = pow.multiply(BigInteger.valueOf(2));
		if (x == 0) {
			a = get(k, pow.divide(three));
		} else if (x == 1) {
			a = get(pow2.subtract(k), pow.divide(three));
			a[0] = a[0].add(pow2);
			BigInteger tmp = a[0];
			a[0] = a[1];
			a[1] = tmp;
		} else {
			a = get(k.subtract(pow2), pow.divide(three));
			a[1] = a[1].add(pow2);
		}
		return a;
	}
}
