k, n = map(int, input().split()) d = list(map(int, input().split())) before = 0 after = sum(d) - k s = 0 for d in d: after -= d - 1 cand = [] pre = (s - 1) % n + 1 if pre <= before: cand.append(pre) nxt = n - (s + d - 1) % n if nxt <= after: cand.append(nxt) ans = min(cand) if cand else -1 before += d - 1 s += d print(ans)