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()
{
var n = sc.Integer();
var a = sc.Integer(n);
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);
IO.Printer.Out.WriteLine(a.AsJoinedString());
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;
IO.Printer.Out.WriteLine(ans);
}
public IO.StreamScanner sc = new IO.StreamScanner(Console.OpenStandardInput());
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 string AsString(this IEnumerable ie) { return new string(System.Linq.Enumerable.ToArray(ie)); }
static public string AsJoinedString(this IEnumerable ie, string st = " ") { return string.Join(st, ie); }
static public void Main()
{
var solver = new Program.Solver();
solver.Solve();
Program.IO.Printer.Out.Flush();
}
}
#endregion
#region Ex
namespace Program.IO
{
using System.IO;
using System.Text;
using System.Globalization;
public class Printer : StreamWriter
{
static Printer() { Out = new Printer(Console.OpenStandardOutput()) { AutoFlush = false }; }
public static Printer Out { get; set; }
public override IFormatProvider FormatProvider { get { return CultureInfo.InvariantCulture; } }
public Printer(System.IO.Stream stream) : base(stream, new UTF8Encoding(false, true)) { }
public Printer(System.IO.Stream stream, Encoding encoding) : base(stream, encoding) { }
public void Write(string format, T[] source) { base.Write(format, source.OfType