# 計算量 O(N^3) n = int(input()) s = input() co = s.count("1") ans = 10**15 a = [] for i in range(n): if s[i] == "1": a += [i, i+n, i+n+n, i+n+n+n] a.sort() for l in range(n): x = list(range(l+n, l+n+co)) anss = (n-(l+co)) % n sp_l = (l+co) % n sp_r = (0 if sp_l == 0 else n) for ll in range(co+co+co): y = a[ll:ll+co] flag = 0 le = 0 ri = 0 for i in range(co): if y[i] <= x[i]: if sp_l <= y[i] % n < sp_r: le = max(le, x[i]-y[i]-1) else: le = max(le, x[i]-y[i]) else: if (sp_l <= y[i] % n <= sp_r) or (y[i] % n == 0): flag = 1 if ri == 0: ri += 1 ri += 1 ri -= flag ans = min(ans, anss+max(le, ri)*n) print(ans)