N,K=input().split() N=int(N) if len(K)>N: print(-1) exit() K=''.join(['0'*(N-len(K)),K]) X=[int(K[i]) for i in range(N)] X[-1]+=1 for i in range(N-1,-1,-1): if X[i]==10: X[i]=0 if i==0: print(-1) exit() X[i-1]+=1 C=[0]+list(map(int,input().split())) ANS=[0]*N for i in range(N): if C[X[i]]==0: for j in range(X[i]+1,10): if C[j]: ANS[i]=j C[j]-=1 i+=1 for k in range(10): for l in range(C[k]): ANS[i]=k i+=1 print(*ANS,sep='') exit() i-=1 while 1: if i==-1: print(-1) exit() C[ANS[i]]+=1 ANS[i]=0 for j in range(X[i]+1,10): if C[j]: ANS[i]=j C[j]-=1 i+=1 for k in range(10): for l in range(C[k]): ANS[i]=k i+=1 print(*ANS,sep='') exit() i-=1 else: ANS[i]=X[i] C[X[i]]-=1 print(*ANS,sep='')