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

public class nopeter_aa_slow {
	public static void main(String[] args) {
		new nopeter_aa_slow().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 = nextInt();
		int k = nextInt();
		int pow = 1;
		for (int i = 0; i < n; i++) {
			pow *= 3;
		}
		u = new boolean[pow + 1];
		mark(0, pow);
		for (int i = 0; i + k < u.length; i++) {
			if (u[i] && u[i + k]) {
				out.println(i + " " + (i + k));
				return;
			}
		}
	}

	private void mark(int l, int r) {
		u[l] = true;
		u[r] = true;
		if (l + 1 >= r) {
			return;
		}
		int ml = (2 * l + r) / 3;
		int mr = (l + 2 * r) / 3;
		mark(l, ml);
		mark(mr, r);

	}

	boolean[] u;

}
