from collections import Counter import sys sys.setrecursionlimit(600000) def main(): N, K = input().split() N = int(N) K = list(map(int, K)) c = list(map(int, input().split())) ctr = Counter(dict(enumerate(c, 1))) ctr.subtract(K) for idx, digit in reversed(list(enumerate(K))): ctr[digit] += 1 if any(ctr_elm < 0 for ctr_elm in ctr.values()): continue min_over_n = -1 for n in range(10): if n <= digit: continue if ctr[n] > 0: min_over_n = n break else: continue ans = K[:idx] ans.append(min_over_n) for n in range(10): if n == min_over_n: for _ in range(ctr[n] - 1): ans.append(n) else: for _ in range(ctr[n]): ans.append(n) print("".join(map(str, ans))) return print(-1) return if __name__ == "__main__": main()