#include using namespace std; typedef long long ll; typedef vector vi; typedef vector vl; typedef pair pii; typedef pair pll; typedef ll _loop_int; #define REP(i,n) for(_loop_int i=0;i<(_loop_int)(n);++i) #define FOR(i,a,b) for(_loop_int i=(_loop_int)(a);i<(_loop_int)(b);++i) #define FORR(i,a,b) for(_loop_int i=(_loop_int)(b)-1;i>=(_loop_int)(a);--i) #define DEBUG(x) cout<<#x<<": "< P; struct node{ int bef2[2],len[2],bef[2]; node(){ bef2[0]=bef2[1]=-1; len[0]=len[1]=0; bef[0]=bef[1]=-1; } node merge(const node &that){ node ret; int it1=0,it2=0; if(len[0]>that.len[0]){ ret.len[0] = len[0]; ret.bef2[0] = bef2[0]; ret.bef[0] = bef[0]; if(len[1] data; segment(int n_){ n=1; while(n0){ x = (x-1)/2; data[x] = data[2*x+1].merge(data[2*x+2]); } } node get(int x){ return data[x+n-1]; } node query(int l,int r,int a,int b,int k){ if(r<=a || b<=l)return node(); if(l<=a && b<=r){ return data[k]; }else{ int m = (a+b)/2; node lef = query(l,r,a,m,2*k+1); node rig = query(l,r,m,b,2*k+2); return lef.merge(rig); } } node query(int l,int r){ return query(l,r,0,n,0); } }; int n; int a[125252]; int main(){ scanf("%d",&n); REP(i,n)scanf("%d",a+i); // kowai node zaatu { map M; REP(i,n)M[a[i]]=0; int it = 1; for(pair P:M){ M[P.first] = it++; } REP(i,n)a[i]=M[a[i]]; } segment up(n+10),down(n+10); int ans = 0; REP(i,n){ int v = a[i]; node nxtdown = down.get(v), nxtup = up.get(v); { node q = up.query(v+1,n+5); int use = (q.bef2[0]!=v) ? 0 : 1; int nextlen = q.len[use]+1; CHMAX(ans,nextlen); int nextbef2 = q.bef[use]; if(nxtdown.len[1] < nextlen){ nxtdown.len[1] = nextlen; nxtdown.bef2[1] = nextbef2; if(nxtdown.len[0] < nxtdown.len[1]){ swap(nxtdown.len[0], nxtdown.len[1]); swap(nxtdown.bef2[0], nxtdown.bef2[1]); } } } { node q = down.query(0,v); int use = (q.bef2[0]!=v) ? 0 : 1; int nextlen = q.len[use]+1; CHMAX(ans,nextlen); int nextbef2 = q.bef[use]; if(nxtup.len[1] < nextlen){ nxtup.len[1] = nextlen; nxtup.bef2[1] = nextbef2; if(nxtup.len[0] < nxtup.len[1]){ swap(nxtup.len[0], nxtup.len[1]); swap(nxtup.bef2[0], nxtup.bef2[1]); } } } up.set(v,nxtup); down.set(v,nxtdown); } if(ans<3)ans=0; printf("%d\n",ans); return 0; }