a['~~'],*p=a,s=2e3,n,k;main(j){for(*a=scanf("%d",&n);!(k&=k-1)?j=!*p||(!p[j]|p[j]>*p?p[j]=1+*p:0,!p[-j]|p[-j]>*p?p[-j]=1+*p:1),++p-a>n?p=a,s--:(k=p-a):++j;);n=!printf("%d",a[n]-1);}