n=int(input()) s=input() ans=10**10 zero=s.count("0") one=n-zero if zero==0 or zero==n: exit(print(0)) se=set() if s[0]=="1": se.add(0) for i in range(n-1,-1,-1): if s[i]=="1": se.add(i) else:break for l in range(n): a=list(range(l+n,l+one+n)) sp_l=(l+one)%n sp_r=n if sp_l==0:sp_l=n #print(sp_l,sp_r) for ll in range(n*3): b=[] for i in range(ll,ll+n): if s[i%n]=="1":b.append(i) ansl=0 ansr=0 flag=True if b[0]<=a[0]: if sp_l<=((b[0]-1)%n+1)<=sp_r and (b[0]%n not in se): ansl=a[0]-b[0]-1 else: ansl=a[0]-b[0] for i in range(one): if b[i]>a[i]: ansr+=1 if sp_l<=((b[i]-1)%n+1)<=sp_r: flag=False if ansr:ansr+=flag #print(l,ll,"---",max(ansl,ansr)*n+(-l-one)%n,sp_l<=((b[0]-1)%n+1)<=sp_r and (b[0]%n not in se),flag) #print(ansl,ansr) #print(b,a) ans=min(ans,max(ansl,ansr)*n+(-l-one)%n) print(ans)