// Arup Guha
// 11/4/2013
// Solution to 2013 South East Regional D1 Problem B: Nested Palindromes

import java.util.*;

public class nested {

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);
		long rank = stdin.nextLong();

		// Process each case.
		while (rank != 0) {

			String pattern = stdin.next();
			char[] array = pattern.toCharArray();

			// See if there's a solution and output appropriately.
			if (solve(array, rank-1))
				System.out.println(new String(array));
			else
				System.out.println("-1");

			rank = stdin.nextLong();
		}
	}

	// Solves the given problem.
	public static boolean solve(char[] str, long rank) {

		// Needs to be length 2^n-1 with no inconsistencies.
		if (!validLen(str.length)) return false;

		// Only store the characters that can be unique!
		char[] compressed = getCompressed(str);
		if (compressed == null) return false;

		int n = compressed.length;
		String choicesFirst = getFirstChoices(compressed);

		// Get the number of possible choices at each slot.
		long[] possibilities = new long[n];
		for (int i=1; i<n; i++) possibilities[i] = 9;
		possibilities[0] = choicesFirst.length();

		long[] rankList = new long[n];
		Arrays.fill(rankList, -1);

		// Get the ranks of each unfilled digit.
		for (int i=n-1; i>=0; i--) {
			if (compressed[i] == '?') {
				rankList[i] = rank%possibilities[i];
				rank /= possibilities[i];
			}
		}
		if (rank > 0) return false;

		// Fill in all of our digits.
		if (compressed[0] == '?')
			compressed[0] = choicesFirst.charAt((int)rankList[0]);
		for (int i=1; i<n; i++)
			if (compressed[i] == '?')
				compressed[i] = (rankList[i] >= compressed[0]-'0' ? (char)('0' + rankList[i] + 1) : (char)('0'+rankList[i]));

		// Expand the string!
		fillString(str, compressed);
		return true;
	}

	// Returns true iff n is of the form 2^k - 1.
	public static boolean validLen(int n) {
		int poss = 1;
		while (poss < n) poss = 2*poss + 1;
		return poss == n;
	}

	// Returns the compressed version of str, with unique items only.
	public static char[] getCompressed(char[] str) {

		// Get length of compressed string.
		int n = str.length;
		int len = 1, poss = 1;
		while (poss < n) {
			poss = 2*poss + 1;
			len++;
		}

		// Set up iterative algorithm.
		char[] ans = new char[len];
		int start = 0, skip = 2;

		// Iterates through each starting spot of each potentially unique digit.
		for (int index=0; index<len; index++) {

			// Go to each spot that must be the same, and see if any aren't '?'
			char item = '?';
			for (int i=start; i<n; i+= skip) {
				if (str[i] != '?') {
					if (item == '?') item = str[i];
					else if (item != str[i]) return null;
				}
			}

			// Fill in this spot.
			ans[index] = item;

			// Advance to next spot.
			start = 2*start+1;
			skip *= 2;
		}
		return ans;
	}

	// Fills in str with what is stored in compressed.
	public static void fillString(char[] str, char[] compressed) {
		int start=0, skip=2;
		for (int index=0; index<compressed.length; index++) {
			for (int i=start; i<str.length; i+= skip)
				str[i] = compressed[index];
			start = 2*start + 1;
			skip *= 2;
		}
	}

	// Returns the possible digits for the first choice based on the current state of compressed.
	public static String getFirstChoices(char[] compressed) {

		String ans = "";
		if (compressed[0] != '?') return ans + compressed[0];

		// Get all frequencies.
		int[] freq = new int[10];

		// See all digits used already.
		for (int i=1; i<compressed.length; i++)
			if (compressed[i] != '?')
				freq[(int)(compressed[i]-'0')]++;

		// Return list of possible first choices.
		for (int i=1; i<10; i++)
			if (freq[i] == 0)
				ans = ans + i;
		return ans;
	}

}