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

public class nopeter_sp implements Runnable {

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

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

	BufferedReader br;

	PrintWriter out;

	StringTokenizer st;

	String nextToken() throws IOException {
		while (st == null || !st.hasMoreTokens()) {
			st = new StringTokenizer(br.readLine());
		}
		return st.nextToken();
	}

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

	void myAssert(boolean e, String s) {
		if (!e) {
			throw new Error("Assertion failure " + s);
		}
	}

	private static final String FILENAME = "nopeter";
	
	BigInteger THREE = BigInteger.valueOf(3);
	
	BigInteger[] getAns(BigInteger k, int n) {
		if (n == 1) {
			return new BigInteger[]{BigInteger.ZERO, k};
		}
		BigInteger n3 = THREE.pow(n - 1);
		BigInteger n32 = n3.multiply(BigInteger.valueOf(2));
		int m = k.divide(n3).intValue();
		if (m == 0) {
			return getAns(k, n - 1);
		}
		if (m == 1) {
			BigInteger[] ret = getAns(n32.subtract(k), n - 1);
			BigInteger t = ret[1];
			ret[1] = ret[0].add(n32);
			ret[0] = t;
			return ret;
		}
		BigInteger[] ret = getAns(k.subtract(n32), n - 1);
		ret[1] = ret[1].add(n32);
		return ret;
	}

	public void solve() throws IOException {
		int n = nextInt();
		myAssert((1 <= n) && (n <= 13), "Wrong n");
		BigInteger k = new BigInteger(nextToken());
		myAssert((BigInteger.ZERO.compareTo(k) <= 0) && (THREE.pow(n).compareTo(k) >= 0), "Wrong k");
		BigInteger[] ans = getAns(k, n);
		out.println(ans[0] + " " + ans[1]);
	}
}
