N,K=map(int,input().split()) C=list(map(int,input().split())) ANS="" for i in range(9): ANS+=str(i+1)*C[i] if int(ANS)>K: print(ANS) exit() if int(ANS[::-1])<=K: print(-1) exit() S=str(K) ANS=[-1]*len(S) for i in range(len(S)): x=int(S[i]) if C[x-1]>0 and i!=len(S)-1: ANS[i]=x C[x-1]-=1 else: for j in range(x,9): if C[j]>0: C[j]-=1 ANS[i]=j+1 break else: for j in range(i-1,-1,-1): C[ANS[j]-1]+=1 ANS[j]=-1 x=int(S[j]) flag=0 for k in range(x,9): if C[k]>0: C[k]-=1 ANS[j]=k+1 flag=1 break if flag: break break #print(ANS,C) for i in range(len(S)): if ANS[i]==-1: for j in range(9): if C[j]>0: C[j]-=1 ANS[i]=j+1 break print("".join(map(str,ANS)))