x[1<<18],c,*p;main(j){for(scanf("%d",&c);~scanf("%d",&j);*x=1)for(p=x;p-x<1e5;p++)*p&&!p[j]|p[j]>*p?p[j]=1+*p:0;c=!printf("%d",x[c]-1);}