n,k = input().split() C = list(map(int,input().split())) n = int(n) def search(x): C2 = C[:] for i in range(x-1): num = int(k[i])-1 C2[num] -= 1 if C2[num] < 0: return False num = int(k[x-1]) for i in range(num,9): if C2[i]: return True return False l = 0 r = n while r > l + 1: m = (r+l)//2 if search(m): l = m else: r = m if l == 0: print(-1) exit()