#include <cstdio>
#include <memory.h>
using namespace std;
int a[1001],f[1001][2];
int main(void)
{
freopen("missilea.in","r",stdin);
freopen("missilea.out","w",stdout);
int i,j,n;
scanf("%d\n",&n);
while (n)
{
memset(f,0,sizeof(f));
for (i=1;i<=n;i++)
scanf("%d",&a[i]);
for (i=1;i<=n;i++)
{
for (j=1;j<i;j++)
{
if (a[i]<=a[j])
continue;
if (f[i][1]<f[j][0])
f[i][1]=f[j][0];
}
f[i][1]++;
for (j=1;j<i;j++)
{
if (a[i]>=a[j])
continue;
if (f[i][0]<f[j][1])
f[i][0]=f[j][1];
}
if (f[i][0])
f[i][0]++;
}
if (f[n][0]<f[n][1])
f[n][0]=f[n][1];
printf("%d\n",f[n][0]);
scanf("%d\n",&n);
}
return(0);
}