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_r=0 for ll in range(n*3): b=[] for i in range(ll,ll+n): if s[i%n]=="1":b.append(i) #print(b,a) ansl=0 ansr=0 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 ans=min(ans,max(ansl,ansr)*n+(-l-one)%n) print(ans)