import sys,random,bisect from collections import deque,defaultdict from heapq import heapify,heappop,heappush from itertools import permutations from math import gcd,log input = lambda :sys.stdin.readline().rstrip() mi = lambda :map(int,input().split()) li = lambda :list(mi()) def solve(N,K,c): if len(K) > N: return -1 if N > len(K): ans = [] for j in range(10): ans += [str(j)] * c[j] return "".join(ans) check_c = [0 for i in range(10)] for i in range(len(K)): check_c[int(K[i])] += 1 for d in range(len(K)-1,-1,-1): check_c[int(K[d])] -= 1 if any(c[i] < check_c[i] for i in range(10)): continue for i in range(int(K[d])+1,10): if c[i] > check_c[i]: ans = [K[:d],str(i)] #print(ans,check_c,c) c[i] -= 1 for j in range(10): c[j] -= check_c[j] ans += [str(j)] * c[j] return "".join(ans) return -1 N,K = input().split() N = int(N) c = [0] + li() print(solve(N,K,c))