using System;
using System.Linq;
using System.Diagnostics;
using System.Collections.Generic;
using Debug = System.Diagnostics.Debug;
using StringBuilder = System.Text.StringBuilder;
using System.Numerics;
namespace Program
{
public class Solver
{
///
/// O(NlogN)
///
public void Solve()
{
//dp[prev,now]=lenをセグメント木でやる
var n = int.Parse(Console.ReadLine());
var a = Console.ReadLine().Split().Select(int.Parse).ToArray();
var S = a.Distinct().ToList();
S.Add(-1); S.Add(int.MaxValue);//番兵
S.Sort();
for (int i = 0; i < n; i++)
a[i] = S.BinarySearch(a[i]);//座圧
var sz = S.Count;
var inc = new SegTree.SegmentTree(sz);//右端の2つの竹について単調増加な最長門松部分列
var dec = new SegTree.SegmentTree(sz);//右端の2つの竹について単調減少な最長門松部分列
inc.Update(sz - 1, 0, 0);
dec.Update(0, 0, sz - 1);
foreach (var i in a)
{
//下がる
{
var max = inc.Query(i + 1, sz, i);//prevがi以外であってnowがiより大きいもの
var pos = max.J;
var sec = Triplet.Max(inc.Query(i + 1, pos, i), inc.Query(pos + 1, sz, i));//pos以外から来て,prevがi以外であってnowがiより大きいもの
dec.Update(i, max.I + 1, max.J);//dp[pos1,i]=dp[prev1,pos1]+1
dec.Update(i, sec.I + 1, sec.J);//dp[pos2,i]=dp[prev2,pos2]+1
}
//上がる
{
var max = dec.Query(0, i, i);//prevがi以外であってnowがiより小さいもの
var pos = max.J;
var sec = Triplet.Max(dec.Query(0, pos, i), dec.Query(pos + 1, i, i));//pos以外から来て,prevがi以外であってnowがiより大きいもの
inc.Update(i, max.I + 1, max.J);//dp[pos1,i]=dp[prev1,pos1]+1
inc.Update(i, sec.I + 1, sec.J);//dp[pos2,i]=dp[prev2,pos2]+1
}
}
var ans = Math.Max(inc.Query(0, sz, -1).I, dec.Query(0, sz, -1).I);
if (ans < 3) ans = 0;
Console.WriteLine(ans);
}
static T[] Enumerate(int n, Func f) { var a = new T[n]; for (int i = 0; i < n; ++i) a[i] = f(i); return a; }
static public void Swap(ref T a, ref T b) { var tmp = a; a = b; b = tmp; }
}
}
#region main
static class Ex
{
static public void Main()
{
var solver = new Program.Solver();
solver.Solve();
}
}
#endregion
namespace SegTree
{
using K = Triplet;
#region SegTree
[System.Diagnostics.DebuggerDisplay("Data={ToString()}")]
public class SegmentTree
{
static public void Swap(ref T a, ref T b) { var tmp = a; a = b; b = tmp; }
readonly K min = new K(-1000000000, -1, -1);
readonly Pair MIN;
int n;
Pair[] data;//length,now,prevのtripletのペア,
public SegmentTree(int size)
{
MIN = new Pair(min, min);
n = 1;
while (n < size)
n <<= 1;
data = new Pair[n << 1];
for (int i = 0; i < n << 1; i++)
data[i] = MIN;
}
///
/// 葉ノードについて,fromが一致しないようにlengthを最大化
///
void update(ref Pair data, K t)
{
if (data.x.I < t.I)
{
if (data.x.K == t.K)
data.x = t;
else { data.y = data.x; data.x = t; }
}
else if (data.y.I < t.I)
{
if (data.x.K != t.K)
data.y = t;
}
}
///
/// k番目の葉ノードにfromからきたときに長さv
///
public void Update(int k, int v, int from)
{
var t = new K(v, k, from);
k += n;
update(ref data[k], t);
k >>= 1;
while (k > 0)
{
update(ref data[k], t);
k >>= 1;
}
}
///
/// [a,b)のfromがnowでないようなlengthの最大値を求める.
///
public K Query(int a, int b, int now) { return Query(a, b, now, 1, 0, n); }
private K Query(int a, int b, int now, int k, int l, int r)
{
if (r <= a || b <= l)
return min;
if (a <= l && r <= b)
{
if (data[k].x.K != now)
return data[k].x;
else return data[k].y;
}
else
{
var vl = Query(a, b, now, k << 1, l, (l + r) >> 1);
var vr = Query(a, b, now, (k << 1) + 1, (l + r) >> 1, r);
return Triplet.Max(vl, vr);
}
}
}
#endregion
}
#region Triplet
static public class Triplet
{
static public Triplet Create(FT f, ST s, TT t)
where FT : IComparable
where ST : IComparable
where TT : IComparable
{ return new Triplet(f, s, t); }
static public Triplet Min(Triplet p, Triplet q)
where FT : IComparable
where ST : IComparable
where TT : IComparable
{ return (p.CompareTo(q) <= 0) ? p : q; }
static public Triplet Max(Triplet p, Triplet q)
where FT : IComparable
where ST : IComparable
where TT : IComparable
{ return (p.CompareTo(q) >= 0) ? p : q; }
}
public struct Triplet : IComparable>
where FT : IComparable
where ST : IComparable
where TT : IComparable
{
public FT I;
public ST J;
public TT K;
public Triplet(FT i, ST j, TT k) : this() { I = i; J = j; K = k; }
public int CompareTo(Triplet other)
{
var cmp = I.CompareTo(other.I);
if (cmp != 0) return cmp;
cmp = J.CompareTo(other.J);
if (cmp != 0) return cmp;
return K.CompareTo(other.K);
}
public override string ToString() { return string.Format("{0} {1} {2}", I, J, K); }
}
#endregion
#region Compair
static public class Pair
{
static public Pair Create(FT f, ST s)
where FT : IComparable
where ST : IComparable
{ return new Pair(f, s); }
static public Pair Min(Pair p, Pair q)
where FT : IComparable
where ST : IComparable
{ return (p.CompareTo(q) <= 0) ? p : q; }
static public Pair Max(Pair p, Pair q)
where FT : IComparable
where ST : IComparable
{ return (p.CompareTo(q) >= 0) ? p : q; }
}
public struct Pair : IComparable>
where FT : IComparable
where ST : IComparable
{
public FT x;
public ST y;
public Pair(FT f, ST s) : this() { x = f; y = s; }
public int CompareTo(Pair other)
{
var cmp = x.CompareTo(other.x);
return cmp != 0 ? cmp : y.CompareTo(other.y);
}
public override string ToString() { return string.Format("{0} {1}", x, y); }
}
#endregion