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

po=[]
for i in range(n):
  if s[i]=="1":po+=[i,i+n,i+n+n,i+n+n+n]
po.sort()

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

  for ll in range(one*3):
    ansl=0
    ansr=0
    flag=True
    flagg=False
    if po[ll]<=a[0]:
      if sp_l<=((po[ll]-1)%n+1)<=sp_r and (po[ll]%n not in se):
        ansl=a[0]-po[ll]-1
      else:
        ansl=a[0]-po[ll]
    if a[one-1]<po[ll+one-1]:
      flag=True
      flagg=False
      ng=-1
      ok=one
      while ng+1!=ok:
        mid=(ng+ok)//2
        if a[mid]<po[ll+mid]:ok=mid
        else:ng=mid
      x=a[ok]
      if x%n:x+=n-(x%n)
      if x<=a[one-1]:
        if a[ok]<=po[ll+ok]<=x:
          flagg=True
        x+=n
      if a[ok]<=po[ll+ok]<=x:
        flag=False
      ansr=one-ok
      if ansr:ansr+=flag
      ansr-=flagg
    ans=min(ans,max(ansl,ansr)*n+(-l-one)%n)
  
print(ans)